Share
Embed
Transcript
45 slides
-
Experimenting with HTTP Headers and Rails Joseph Paul Silvashy -
@jpsilvashy - jpsilvashy.com -
Overview a basic GET request: Sends request headers Sends response
headers and content body Client (your browser) Server (web server, reverse proxy, application...) -
Accept: text/html, [...] Accept-Encoding: gzip, deflate Accept-Language: en-US,en; q=0.5 User-Agent:
[...] GET http://example.com -
Cache-Control: must-revalidate, private, max-age=0 Connection: keep-alive Content-Length: 3146 Content-Type: text/html;
charset=utf-8 Date: Thu, 24 Jan 2013 21:16:51 GMT <html> ... HTTP/1.1 200 OK -
Accept Accept-Charset Accept-Encoding Accept-Language Accept-Datetime Authorization Cache-Control Connection Cookie Content-Length
Content-MD5 Content-Type Date Expect From Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified- Since Max-Forwards Pragma Proxy- Authorization Range Referer TE Upgrade User-Agent Via Warning Typical Response Headers -
Accept Accept-Charset Accept-Encoding Accept-Language Accept-Datetime Authorization Cache-Control Connection Cookie Content-Length
Content-MD5 Content-Type Date Expect From Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified- Since Max-Forwards Pragma Proxy- Authorization Range Referer TE Upgrade User-Agent Via Warning Typical Response Headers -
Accept Accept-Charset Accept-Encoding Accept-Language Accept-Datetime Authorization Cache-Control Connection Cookie Content-Length
Content-MD5 Content-Type Date Expect From Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified- Since Max-Forwards Pragma Proxy- Authorization Range Referer TE Upgrade User-Agent Via Warning Typical Response Headers Warning -
The Warning general-header field is used to carry additional information
about the status or transformation of a message which might not be reflected in the message. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Warning (From rfc2616 See §14.46): -
299: Miscellaneous persistent warning. The warning text MAY include arbitrary
information to be presented to a human user, or logged. A system receiving this warning MUST NOT take any automated action. Warning (From rfc2616 See §14.46): http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html -
Requests: X-CSRF-Token X-Requested-With Some non-standard headers Response: X-Frame-Options X-Runtime X-Rack-Cache
X-Request-Id X-Ua-Compatible -
Typical Rails Response: HTTP/1.1 200 OK Cache-Control: max-age=0, private, must-revalidate
Connection: keep-alive Content-Length: 3146 Content-Type: text/html; charset=utf-8 Date: Thu, 24 Jan 2013 20:15:48 GMT Etag: "9cc373612927384b4a3997397061ba60" Set-Cookie: _myapp_session=abcd; path=/; HttpOnly (cont...) -
Typical Rails Response (cont.): X-Rack-Cache: miss X-Request-Id: 52f48735303febd137f014fb4db5f767 X-Runtime: 0.010621
X-Ua-Compatible: IE=Edge,chrome=1 -
Creators of new parameters to be used in the context
of application protocols SHOULD NOT prefix their parameter names with "X-" or similar constructs. IETF Says: (From rfc6648 §3, June 2012 ) http://www.ietf.org/rfc/rfc6648.txt -
Alternatively, we can send the flash messages in a header
for easy consumption by the client. -
after_filter :x_messages after_filter { flash.discard if request.xhr? } def x_messages
response.headers['X-Messages'] = flash.to_hash.to_json flash.to_hash end application_controller.rb https://github.com/jpsilvashy/x-messages/blob/master/app/controllers/ application_controller.rb -
application_helper.rb https://github.com/jpsilvashy/x-messages/blob/master/app/controllers/ application_controller.rb def render_x_messages content_tag :ul, :class => 'x-messages'
do x_messages.collect do |type, message| concat content_tag(:li, message, :class => "message-#{type}") end end end -
application.html.erb https://github.com/jpsilvashy/x-messages/blob/master/app/views/layouts/ application.html.erb <!-- Replace your flash messages with this
--> <%= render_x_messages %> <!-- Place this near the end of the body --> <script> var x_messages = <%= x_messages.to_json.html_safe %> </script> -
x-messages.js https://github.com/jpsilvashy/x-messages/blob/master/app/assets/ javascripts/x-messages.js var getXMessages = function(response) { var xMessagesHeader
= response.getResponseHeader('X-Messages'); if ((typeof xMessagesHeader !== "undefined") && (xMessagesHeader !== null)) { return $.parseJSON(xMessagesHeader) } } -
https://github.com/jpsilvashy/x-messages/blob/master/app/assets/ javascripts/x-messages.js var handleMessages = function(messages) { $.each(messages, function(type, message)
{ alertify.log(message, type); }); } x-messages.js (cont.) -
https://github.com/jpsilvashy/x-messages/blob/master/app/assets/ javascripts/x-messages.js $(document).ajaxComplete(function(event, response) { handleMessages(getXMessages(response)) }); $(function() { $('.x-messages').hide();
if ((typeof xMessages !== "undefined") && (xMessages !== null)) { handleMessages(xMessages); } }) x-messages.js (cont.) -
Issues with implementation • Error messages still have to be
rendered to the views since we cannot retrieve the response headers with javascript in our views unless we render the flash as an array. So this is only valuable for XHR requests at the moment (I think this can be fixed). • We still has “X-” name for header. https://github.com/jpsilvashy/x-messages/issues