Rack: A Case Study in Design

Rack: A Case Study in Design

Explores the nature of HTTP1 requests and how it relates the design and implementation of Rack in ruby. Concludes with a discussion on the future of, and successors to, rack.

1633e2784d925ea41167b45bccddcd19?s=128

Michael Cordell

October 01, 2015
Tweet

Transcript

  1. rack A case study in design Michael Cordell

  2. Why? Rack underpins the lion-share of successful ruby web frameworks

    Understanding the implementation and API is crucial to better framework utilization Excellent case study of design
  3. Overview History and Web Requests Design Implementation and usage Future

  4. Web request Overview ɂ Ȑ

  5. ɂ Ȑ

  6. ɂ Ȑ http://www.geocities.com

  7. None
  8. ɂ Ȑ http://www.geocities.com

  9. Anatomy of a HTTP request Ȑ ɂ

  10. Common Gateway Interface (CGI) Spec written in 1993 by National

    Center for Supercomputing Applications Allowed web servers to interact with executables and dynamically generate webpages Along with passed query params server also sets up a set of environmental variables
  11. SERVER_SOFTWARE SERVER_NAME GATEWAY_INTERFACE SERVER_PROTOCOL SERVER_PORT REQUEST_METHOD PATH_INFO PATH_TRANSLATED SCRIPT_NAME QUERY_STRING

    REMOTE_HOST REMOTE_ADDR AUTH_TYPE SERVER_PORT REMOTE_USER REMOTE_IDENT CONTENT_TYPE CONTENT_LENGTH HTTP_ACCEPT HTTP_ACCEPT_LANGUAGE HTTP_USER_AGENT HTTP_COOKIE
  12. ɂ Ȑ http://www.amazon.com?author=douglas%20adams

  13. Anatomy of a Web Request ɂ Ȑ

  14. The Server Ȑ

  15. The Client ɂ curl -X POST -H "Content-Type: application/json” \

    -d '{ "name": "michael" }' \ http://localhost:9292/hello
  16. Ȑ Request & Response ɂ

  17. tcpdump tcpdump -qns 0 -A -i lo0 port 9292

  18. None
  19. Source

  20. Destination

  21. Hexadecimal view

  22. ASCII view

  23. None
  24. None
  25. E...V.@.@.............$L..[...>...1.........S...S... POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".} Request

  26. None
  27. None
  28. E...1'@.@...........$L....>...\}..1.........S...S... HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/2.2.2/2015-04-13).. Date:.Wed,.23.Sep.2015.23:32:19.GMT.. Connection:.Keep-Alive.... Response: Header Ȑ

  29. None
  30. None
  31. E..O.1@.@...........$L....>...\}..1..C......S...S... {"hello":"Welcome.michael"} Response: Body Ȑ

  32. None
  33. None
  34. ɂ Ȑ

  35. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

  36. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

    HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep. 2015.23:32:19.GMT.. Connection:.Keep-Alive....
  37. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

    HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep. 2015.23:32:19.GMT.. Connection:.Keep-Alive.... {"hello":"Welcome.michael"}
  38. rack

  39. rack, finally.

  40. rack history created in 2007 by Christian Neukirchen many frameworks,

    many web servers
  41. ɂ Ȑ POST./hello. HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}

    HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep. 2015.23:32:19.GMT.. Connection:.Keep-Alive.... {"hello":"Welcome.michael"}
  42. Ȑ HTTP/1.1.201.Created... Content-Type:.application/json.. Content-Length:.27.. Server:.WEBrick/1.3.1.(Ruby/ 2.2.2/2015-04-13).. Date:.Wed,.23.Sep.2015.23:32:19.GMT.. Connection:.Keep-Alive.... {"hello":"Welcome.michael"} POST./hello.

    HTTP/1.1.. Host:.127.0.0.1:9292.. User-Agent:.curl/7.43.0.. Accept:.*/*.. Content-Type:.application/json.. Content-Length:.21.. ..{."name":."michael".}
  43. Ȑ receive request respond with: • status code • headers

    • body
  44. None
  45. None
  46. rack rules A rack app: implements a call method that

    accepts a hash on CGI-like environment call returns an array containing: a status code a hash of response headers an object that represents the body that responds to .each and contains strings for the body
  47. None
  48. None
  49. None
  50. adapter pattern App Rack handler Rack handler Rack handler Rack

    handler
  51. adapter pattern Without this we would have f * s

    implementations where f is the # of frameworks and s is the number of servers rails-puma rails-unicorn sinatra-puma sinatra-unicorn
  52. adapter pattern Instead we have f + s number of

    implementations Each framework speaks rack and each sever speaks rack
  53. pipeline pattern higher-level architecture pattern break a larger task in

    to discrete steps each step’s output is passed as the input to the next step also known as pipes and filters (step)
  54. None
  55. None
  56. None
  57. None
  58. middleware

  59. middleware “filters” in the pipeline that modify the request and/or

    response Can be composed into a stack
  60. An example: warden warden is a “general rack authentication framework”

    used by devise (the rails auth gem) redirects the user to a failure app with a 401 status code when not authenticated app can trigger auth failure by throwing a warden error: throw(:warden)
  61. warden Client app [200, {}, [“ok”]] [401, {}, [“Unauthenicated!”]] failure

    app
  62. None
  63. None
  64. None
  65. None
  66. None
  67. None
  68. None
  69. None
  70. None
  71. None
  72. None
  73. Ȑpuma Middleware A Middleware B HelloWorld Middleware B Middleware A

    Rack handler Rack handler
  74. None
  75. Rails Normal Rails API

  76. None
  77. None
  78. None
  79. criticism

  80. ENV Despite the simple API, a lot of complexity hides

    behind that env/request hash The env hash is setup like a CGI environment but we are getting farther and farther away from CGI environment NoMethodError on an Object vs. nil from a hash
  81. None
  82. Ȑpuma Middleware A Middleware B HelloWorld Middleware B Middleware A

    Rack handler Rack handler
  83. what’s next?

  84. Rack 2? Unlikely that there will ever be a re-write

    of rack by the core team Decided on a “hold” the torch approach to maintenance in Aug. 2014
  85. Rack 2? Unlikely that there will ever be a re-write

    of rack by the core team Decided on a “hold” the torch approach to maintenance in Aug. 2014
  86. Websockets with Faye faye server and associated utilities established solution/gem

    sits alongside the rest of rack stack
  87. Web sockets with Action Cable Will be released in Rails

    5, announced at rails conf 2015, code open-sourced in early July Offers a JS and backend integration
  88. “Action Cable seamlessly integrates websockets with the rest of your

    Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable.”
  89. None
  90. None
  91. None
  92. Javascript & NPM module Works with node and express

  93. None
  94. None
  95. None
  96. None
  97. Thank you

  98. twitter: @mike_cordell speaker_deck: Request and Response by tenderlove: https://www.youtube.com/1EeWXojdqvU José

    Valim: Why your web framework should not adopt Rack API: http://blog.plataformatec.com.br/2012/06/ why-your-web-framework-should-not-adopt- rack-api/