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

aimeesimone

April 29, 2013
Tweet

More Decks by aimeesimone

Other Decks in Programming

Transcript

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

    CHRISTOPHER GREEN 1 Tuesday, May 7, 13
  2. is a set of instructions that tells a server what

    kind of response we want A REQUEST 5 Tuesday, May 7, 13
  3. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 19 Tuesday, May 7, 13
  4. delivers web pages on the request of clients using HTTP

    A WEB SERVER 21 Tuesday, May 7, 13
  5. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 26 Tuesday, May 7, 13
  6. 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
  7. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 33 Tuesday, May 7, 13
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. create show index update Action Action Action Action MODEL VIEW

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

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

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

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

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

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

    /courses ACTION: “index” CONTROLLER 39 Tuesday, May 7, 13
  22. 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
  23. 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
  24. MODEL @courses = Course.order(:position).all @paths = Path.all MODEL CoursesController index

    Course MODEL Path MODEL ActiveRecord ActiveRecord MODEL 42 Tuesday, May 7, 13
  25. 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
  26. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

    REQUEST MADE: GET http://codeschool.com/courses 43 Tuesday, May 7, 13
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. BROWSER WEB SERVER APP SERVER ROUTER CONTROLLER MODEL VIEW RAILS

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

    VIEW WRITES HTML TO RESPONSE BODY 49 Tuesday, May 7, 13
  36. 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
  37. 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
  38. 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