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

How a Request Becomes a Response

How a Request Becomes a Response

A RailsConf 2013 intro talk by Aimee Simone & Christopher Green

F6ec4c44c43a0277355756ef431a29de?s=128

aimeesimone

April 29, 2013
Tweet

Transcript

  1. HOW A REQUEST BECOMES A RESPONSE BY AIMEE SIMONE &

    CHRISTOPHER GREEN 1 Tuesday, May 7, 13
  2. railsconftutorials.com/ sessions/ request_response.html 2 Tuesday, May 7, 13

  3. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    3 Tuesday, May 7, 13
  4. 4 Tuesday, May 7, 13

  5. ::WHAT IS A REQUEST?:: 4 Tuesday, May 7, 13

  6. A REQUEST 5 Tuesday, May 7, 13

  7. is a set of instructions that tells a server what

    kind of response we want A REQUEST 5 Tuesday, May 7, 13
  8. 6 Tuesday, May 7, 13

  9. CODESCHOOL.COM/COURSES 6 Tuesday, May 7, 13

  10. REQUEST PATH 7 Tuesday, May 7, 13

  11. REQUEST PATH 8 Tuesday, May 7, 13

  12. REQUEST PATH /courses 8 Tuesday, May 7, 13

  13. REQUEST VERB 9 Tuesday, May 7, 13

  14. REQUEST VERB 10 Tuesday, May 7, 13

  15. REQUEST VERB GET 10 Tuesday, May 7, 13

  16. REQUEST VERB GET 11 Tuesday, May 7, 13

  17. REQUEST VERB GET POST PUT DELETE 11 Tuesday, May 7,

    13
  18. QUERY DATA 12 Tuesday, May 7, 13

  19. QUERY DATA 13 Tuesday, May 7, 13

  20. QUERY DATA /courses?page=2 13 Tuesday, May 7, 13

  21. 14 Tuesday, May 7, 13

  22. HEADER DATA 14 Tuesday, May 7, 13

  23. HEADER DATA 15 Tuesday, May 7, 13

  24. 16 Tuesday, May 7, 13

  25. ::BROWSER TO WEB SERVER:: 16 Tuesday, May 7, 13

  26. 17 Tuesday, May 7, 13

  27. CODESCHOOL.COM/COURSES 17 Tuesday, May 7, 13

  28. CODESCHOOL.COM/COURSES 18 Tuesday, May 7, 13

  29. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    19 Tuesday, May 7, 13
  30. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 19 Tuesday, May 7, 13
  31. 20 Tuesday, May 7, 13

  32. WHAT IS A WEB SERVER? 20 Tuesday, May 7, 13

  33. A WEB SERVER 21 Tuesday, May 7, 13

  34. delivers web pages on the request of clients using HTTP

    A WEB SERVER 21 Tuesday, May 7, 13
  35. A WEB SERVER 21 Tuesday, May 7, 13

  36. A WEB SERVER 22 Tuesday, May 7, 13

  37. nginx A WEB SERVER 22 Tuesday, May 7, 13

  38. A WEB SERVER 22 Tuesday, May 7, 13

  39. A WEB SERVER 23 Tuesday, May 7, 13

  40. apache A WEB SERVER 23 Tuesday, May 7, 13

  41. A WEB SERVER 23 Tuesday, May 7, 13

  42. A WEB SERVER 24 Tuesday, May 7, 13

  43. webrick A WEB SERVER 24 Tuesday, May 7, 13

  44. A WEB SERVER 24 Tuesday, May 7, 13

  45. 25 Tuesday, May 7, 13

  46. ::WEB SERVER TO APP SERVER:: 25 Tuesday, May 7, 13

  47. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 26 Tuesday, May 7, 13
  48. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER 26 Tuesday, May 7, 13
  49. 27 Tuesday, May 7, 13

  50. WHAT IS AN APP SERVER? 27 Tuesday, May 7, 13

  51. AN APP SERVER 28 Tuesday, May 7, 13

  52. handles execution of procedures (programs, routines, scripts) for an application

    AN APP SERVER 28 Tuesday, May 7, 13
  53. AN APP SERVER 28 Tuesday, May 7, 13

  54. AN APP SERVER 29 Tuesday, May 7, 13

  55. passenger AN APP SERVER 29 Tuesday, May 7, 13

  56. AN APP SERVER 29 Tuesday, May 7, 13

  57. AN APP SERVER 30 Tuesday, May 7, 13

  58. thin AN APP SERVER 30 Tuesday, May 7, 13

  59. AN APP SERVER 30 Tuesday, May 7, 13

  60. AN APP SERVER 31 Tuesday, May 7, 13

  61. mongrel AN APP SERVER 31 Tuesday, May 7, 13

  62. AN APP SERVER 31 Tuesday, May 7, 13

  63. 32 Tuesday, May 7, 13

  64. ::RAILS:: 32 Tuesday, May 7, 13

  65. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 33 Tuesday, May 7, 13
  66. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER 33 Tuesday, May 7, 13
  67. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER REQUEST IS PASSED TO RAILS 33 Tuesday, May 7, 13
  68. 34 Tuesday, May 7, 13

  69. ROUTER CONTROLLER MODEL VIEW RAILS 34 Tuesday, May 7, 13

  70. ROUTER GET http://codeschool.com/courses OUR REQUEST: 35 Tuesday, May 7, 13

  71. ROUTER GET http://codeschool.com/courses OUR REQUEST: config/routes.rb resources :courses, only: [:index,

    :show] do get :goto, on: :member get :enroll, on: :member post :remind_me, on: :member put :notify, on: :member get :suggest, on: :collection resources :reviews, only: [:index, :new, :create, :edit, :update] end 35 Tuesday, May 7, 13
  72. ROUTER GET http://codeschool.com/courses OUR REQUEST: config/routes.rb resources :courses, only: [:index,

    :show] do get :goto, on: :member get :enroll, on: :member post :remind_me, on: :member put :notify, on: :member get :suggest, on: :collection resources :reviews, only: [:index, :new, :create, :edit, :update] end goto_course GET /courses/:id/goto(.:format) {:action=>"goto", :controller=>"courses"} enroll_course GET /courses/:id/enroll(.:format) {:action=>"enroll", :controller=>"courses"} remind_me_course POST /courses/:id/remind_me(.:format) {:action=>"remind_me", :controller=>"courses"} notify_course PUT /courses/:id/notify(.:format) {:action=>"notify", :controller=>"courses"} suggest_courses GET /courses/suggest(.:format) {:action=>"suggest", :controller=>"courses"} course_reviews GET /courses/:course_id/reviews(.:format) {:action=>"index", :controller=>"reviews"} POST /courses/:course_id/reviews(.:format) {:action=>"create", :controller=>"reviews"} new_course_review GET /courses/:course_id/reviews/new(.:format) {:action=>"new", :controller=>"reviews"} edit_course_review GET /courses/:course_id/reviews/:id/edit(.:format) {:action=>"edit", :controller=>"reviews"} course_review PUT /courses/:course_id/reviews/:id(.:format) {:action=>"update", :controller=>"reviews"} courses GET /courses(.:format) {:action=>"index", :controller=>"courses"} course GET /courses/:id(.:format) {:action=>"show", :controller=>"courses"} $ rake routes 35 Tuesday, May 7, 13
  73. ROUTER GET http://codeschool.com/courses OUR REQUEST: 36 Tuesday, May 7, 13

  74. ROUTER GET http://codeschool.com/courses OUR REQUEST: goto_course GET /courses/:id/goto(.:format) {:action=>"goto", :controller=>"courses"}

    enroll_course GET /courses/:id/enroll(.:format) {:action=>"enroll", :controller=>"courses"} remind_me_course POST /courses/:id/remind_me(.:format) {:action=>"remind_me", :controller=>"courses"} notify_course PUT /courses/:id/notify(.:format) {:action=>"notify", :controller=>"courses"} suggest_courses GET /courses/suggest(.:format) {:action=>"suggest", :controller=>"courses"} course_reviews GET /courses/:course_id/reviews(.:format) {:action=>"index", :controller=>"reviews"} POST /courses/:course_id/reviews(.:format) {:action=>"create", :controller=>"reviews"} new_course_review GET /courses/:course_id/reviews/new(.:format) {:action=>"new", :controller=>"reviews"} course_review PUT /courses/:course_id/reviews/:id(.:format) {:action=>"update", :controller=>"reviews"} courses GET /courses(.:format) {:action=>"index", :controller=>"courses"} course GET /courses/:id(.:format) {:action=>"show", :controller=>"courses"} 36 Tuesday, May 7, 13
  75. ROUTER GET http://codeschool.com/courses OUR REQUEST: 36 Tuesday, May 7, 13

  76. ROUTER GET http://codeschool.com/courses OUR REQUEST: GET /courses CONTROLLER: “courses” ACTION:

    “index” CONTROLLER AND ACTION CHOSEN 36 Tuesday, May 7, 13
  77. ROUTER GET http://codeschool.com/courses OUR REQUEST: 37 Tuesday, May 7, 13

  78. ROUTER GET http://codeschool.com/courses OUR REQUEST: course_review PUT /courses/:course_id/reviews/:id(.:format) {:action=>"update", :controller=>"reviews"}

    courses GET /courses(.:format) {:action=>"index", :controller=>"courses"} course GET /courses/:id(.:format) {:action=>"show", :controller=>"courses"} 37 Tuesday, May 7, 13
  79. ROUTER GET http://codeschool.com/courses OUR REQUEST: GET /courses CONTROLLER: “courses” ACTION:

    “index” course_review PUT /courses/:course_id/reviews/:id(.:format) {:action=>"update", :controller=>"reviews"} courses GET /courses(.:format) {:action=>"index", :controller=>"courses"} course GET /courses/:id(.:format) {:action=>"show", :controller=>"courses"} CONTROLLER AND ACTION CHOSEN 37 Tuesday, May 7, 13
  80. create show index update Action Action Action Action MODEL VIEW

    CONTROLLER: “courses” GET /courses ACTION: “index” CONTROLLER 38 Tuesday, May 7, 13
  81. create show index update Action Action Action MODEL VIEW CONTROLLER:

    “courses” GET /courses ACTION: “index” CONTROLLER 38 Tuesday, May 7, 13
  82. create show index update Action Action MODEL VIEW CONTROLLER: “courses”

    GET /courses ACTION: “index” CONTROLLER 38 Tuesday, May 7, 13
  83. create show index update Action MODEL VIEW CONTROLLER: “courses” GET

    /courses ACTION: “index” CONTROLLER 38 Tuesday, May 7, 13
  84. create show index update MODEL VIEW CONTROLLER: “courses” GET /courses

    ACTION: “index” CONTROLLER CoursesController 38 Tuesday, May 7, 13
  85. create show update CoursesController index MODEL VIEW CONTROLLER: “courses” GET

    /courses ACTION: “index” CONTROLLER 39 Tuesday, May 7, 13
  86. create show update CoursesController index MODEL VIEW CONTROLLER: “courses” GET

    /courses ACTION: “index” CONTROLLER 39 Tuesday, May 7, 13
  87. class CoursesController < ApplicationController respond_to :html, :json def index @courses

    = Course.order(:position).all @paths = Path.all @elective = @paths.pop respond_with end end CONTROLLER: “courses” GET /courses ACTION: “index” CONTROLLER 40 Tuesday, May 7, 13
  88. class CoursesController < ApplicationController respond_to :html, :json def index @courses

    = Course.order(:position).all @paths = Path.all @elective = @paths.pop respond_with end end app/controllers/courses_controller.rb CONTROLLER: “courses” GET /courses ACTION: “index” CONTROLLER 40 Tuesday, May 7, 13
  89. CoursesController MODEL VIEW index @courses = Course.order(:position).all MODEL 41 Tuesday,

    May 7, 13
  90. CoursesController MODEL VIEW index @courses = Course.order(:position).all MODEL 41 Tuesday,

    May 7, 13
  91. MODEL @courses = Course.order(:position).all @paths = Path.all MODEL CoursesController index

    Course MODEL Path MODEL MODEL 42 Tuesday, May 7, 13
  92. MODEL @courses = Course.order(:position).all @paths = Path.all MODEL CoursesController index

    Course MODEL Path MODEL ActiveRecord ActiveRecord MODEL 42 Tuesday, May 7, 13
  93. MODEL @courses = Course.order(:position).all @paths = Path.all MODEL CoursesController index

    Course MODEL Path MODEL ActiveRecord ActiveRecord DATABASE courses Table paths Table MODEL 42 Tuesday, May 7, 13
  94. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    43 Tuesday, May 7, 13
  95. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 43 Tuesday, May 7, 13
  96. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER 43 Tuesday, May 7, 13
  97. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER APPLICATION SERVER GIVES REQUEST TO MIDDLEWARE, RAILS IS STARTED, GETS REQUEST. 43 Tuesday, May 7, 13
  98. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER APPLICATION SERVER GIVES REQUEST TO MIDDLEWARE, RAILS IS STARTED, GETS REQUEST. REQUEST PATH AND METHOD MAPPED TO CONTROLLER AND ACTION 43 Tuesday, May 7, 13
  99. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses REQUEST ACCEPTED, PASSED TO APPLICATION SERVER APPLICATION SERVER GIVES REQUEST TO MIDDLEWARE, RAILS IS STARTED, GETS REQUEST. REQUEST PATH AND METHOD MAPPED TO CONTROLLER AND ACTION CONTROLLER ACTION GATHERS INFORMATION FROM MODELS TO SUPPLY TO VIEW 43 Tuesday, May 7, 13
  100. CoursesController index VIEW VIEW 44 Tuesday, May 7, 13

  101. CoursesController index VIEW VIEW 44 Tuesday, May 7, 13

  102. CoursesController index VIEW 45 Tuesday, May 7, 13

  103. CoursesController index VIEW app/views/courses/index.html.haml VIEW 45 Tuesday, May 7, 13

  104. CoursesController index VIEW app/views/courses/index.html.haml View template creates HTML for response.

    VIEW 45 Tuesday, May 7, 13
  105. HTTP RESPONSE 46 Tuesday, May 7, 13

  106. HTTP RESPONSE HEADERS Content-Type:text/html; charset=utf-8 46 Tuesday, May 7, 13

  107. HTTP RESPONSE HEADERS Content-Type:text/html; charset=utf-8 Accept:text/html REQUEST HEADER 46 Tuesday,

    May 7, 13
  108. HTTP RESPONSE HEADERS Content-Type:text/html; charset=utf-8 Accept:text/html REQUEST HEADER Content-Encoding:gzip Accept-Encoding:gzip,deflate

    46 Tuesday, May 7, 13
  109. HTTP RESPONSE HEADERS Content-Type:text/html; charset=utf-8 Accept:text/html REQUEST HEADER Content-Encoding:gzip Accept-Encoding:gzip,deflate

    STATUS 200 OK === GOOD! 1xx: Informational - Request received, continuing process 2xx: Success - The action was successfully received, understood, and accepted 3xx: Redirection - Further action must be taken in order to complete the request 4xx: Client Error - The request contains bad syntax or cannot be fulfilled 5xx: Server Error - The server failed to fulfill an apparently valid request 46 Tuesday, May 7, 13
  110. HTTP RESPONSE HEADERS Content-Type:text/html; charset=utf-8 Accept:text/html REQUEST HEADER Content-Encoding:gzip Accept-Encoding:gzip,deflate

    STATUS 200 OK === GOOD! 1xx: Informational - Request received, continuing process 2xx: Success - The action was successfully received, understood, and accepted 3xx: Redirection - Further action must be taken in order to complete the request 4xx: Client Error - The request contains bad syntax or cannot be fulfilled 5xx: Server Error - The server failed to fulfill an apparently valid request BODY HTML JSON XML FILE CONTENTS - Image, Video, Compressed file. Set-Cookie: key=value; path=/; 46 Tuesday, May 7, 13
  111. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses APPLICATION SERVER GIVES REQUEST TO MIDDLEWARE, RAILS IS STARTED, GETS REQUEST. REQUEST PATH AND METHOD MAPPED TO CONTROLLER AND ACTION CONTROLLER ACTION GATHERS INFORMATION FROM MODELS, SUPPLIES INFORMATION TO VIEW CREATING HTML REQUEST ACCEPTED, PASSED TO APPLICATION SERVER 47 Tuesday, May 7, 13
  112. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    47 Tuesday, May 7, 13
  113. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    48 Tuesday, May 7, 13
  114. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    VIEW WRITES HTML TO RESPONSE BODY 48 Tuesday, May 7, 13
  115. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    49 Tuesday, May 7, 13
  116. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    VIEW WRITES HTML TO RESPONSE BODY 49 Tuesday, May 7, 13
  117. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    VIEW WRITES HTML TO RESPONSE BODY RESPONSE RETURNED TO BROWSER 49 Tuesday, May 7, 13
  118. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    VIEW WRITES HTML TO RESPONSE BODY RESPONSE RETURNED TO BROWSER RESPONSE RECEIVED! 49 Tuesday, May 7, 13
  119. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    VIEW WRITES HTML TO RESPONSE BODY RESPONSE RETURNED TO BROWSER RESPONSE RECEIVED! RESPONSE TRAVELS BACK THROUGH MIDDLEWARE STACK 49 Tuesday, May 7, 13
  120. RESPONSE 50 Tuesday, May 7, 13

  121. RESPONSE 50 Tuesday, May 7, 13

  122. 51 Tuesday, May 7, 13

  123. QUESTIONS? 51 Tuesday, May 7, 13

  124. THANK YOU! Christopher Green @tastyfishes https://github.com/chrisgre Aimee Simone @aimee_simone github.com/aimeesimone

    52 Tuesday, May 7, 13