Upgrade to Pro — share decks privately, control downloads, hide ads and more …

x-messages

 x-messages

Experimenting with HTTP Headers and Rails

Avatar for Joseph Silvashy

Joseph Silvashy

January 25, 2013
Tweet

Other Decks in Programming

Transcript

  1. Overview a basic GET request: Sends request headers Sends response

    headers and content body Client (your browser) Server (web server, reverse proxy, application...)
  2. 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
  3. 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
  4. 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
  5. 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):
  6. 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
  7. ?

  8. 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...)
  9. 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
  10. Alternatively, we can send the flash messages in a header

    for easy consumption by the client.
  11. 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
  12. 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>
  13. 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) } }
  14. 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