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

Rack: HTTP 200 OK

Rack: HTTP 200 OK

An introduction to HTTP and Ruby's Rack library.

Given to the RIT Society of Software Engineers in 2010. http://sse.se.rit.edu/

Eb8975af8e49e19e3dd6b6b84a542e26?s=128

Nick Quaranto

January 12, 2012
Tweet

Transcript

  1. rack: HTTP 200 OK nick quaranto

  2. me 5th year SE/CS bills fan ruby fanatic gemcutter lead

  3. this talk is about internets rubies unicorns

  4. HTTP

  5. history

  6. tim berners-lee

  7. the first web server

  8. None
  9. http 1.0: rfc1945 http 1.1: rfc2616

  10. None
  11. the protocol request => response stateless

  12. requests: Request line, such as GET /images/logo.gif HTTP/1.1 Headers, such

    as Accept-Language: en An empty line An optional message body
  13. verbs HEAD GET POST PUT DELETE etc...

  14. response code (200, 404, 500) more headers body

  15. codes

  16. 200s

  17. 200s 200 OK 201 Created 202 Accepted

  18. 300s

  19. 300s 301 Moved Permanently 302 Found 304 Not Modified

  20. 400s

  21. 400s 401 Unauthorized 403 Forbidden 404 Not Found 418 I'm

    a teapot
  22. RFC 2324 HTCPCP/1.0 1 April 1998 In practice, most automated

    coffee pots cannot currently provide additions. 2.3.2 418 I'm a teapot Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout.
  23. 500s

  24. 500s 500 Internal Server Error 502 Bad Gateway 503 Service

    Unavailable
  25. headers Date: Wed, 09 Dec 2009 15:25:13 GMT Content-Type: text/html

    X-Served-By: Apache Content-Encoding: gzip
  26. let's do this

  27. tip of the iceberg sessions security caching proxies

  28. rack

  29. history servers and webapps can't talk to each other CGI

    (C, Perl, PHP, etc) WSGI (Python) Ruby....?
  30. servers apache (passenger) mongrel thin unicorn and more...

  31. frameworks rails merb sinatra and more...

  32. None
  33. what's in a response? code headers body

  34. what's in a response? code => Integer headers => Hash

    body => String
  35. [200, {'Content-Type' => 'text/plain'}, 'Hello!']

  36. let's make a rack app

  37. class Awesome def call(env) [200, {'Content-Type' => 'text/plain'}, 'AWESOME.'] end

    end
  38. lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'AWESOME.'] }

  39. env what? a Hash of environment variables PATH_INFO QUERY_STRING REMOTE_ADDR

    REQUEST_URI
  40. rackup comes with the rack gem starts a rack app

    looks at config.ru needs an endpoint
  41. require 'rack/lobster' run Rack::Lobster.new

  42. ,.---._ ,,,, / `, \\\\ / '\_ ; |||| /\/``-.__\;'

    ::::/\/_ {{`-.__.-'(`(^^(^^^(^ 9 `.=========' {{{{{{ { ( ( ( ( (-----:= {{.-'~~'-.(,(,,(,,,(__6_.'=========. ::::\/\ |||| \/\ ,-'/, //// \ `` _/ ; '''' \ ` .' `---'
  43. # config.ru class Awesome def call(env) [200, {'Content-Type' => 'text/plain'},

    'AWESOME.'] end end run Awesome
  44. middleware think of pancakes stack them up! instead of run,

    use mmm, pancakes
  45. None
  46. class PassThrough def initialize(app) @app = app end def call(env)

    @app.call(env) end end
  47. I::Rack def call(env) if env["PATH_INFO"] == "/WMD" [ 404, {"Content-Type"

    => "text/plain"}, "Not found" ] else @app.call(env) end end
  48. Rack::Obama def call(env) if env["CONTENT_TYPE"] == "nobel-prize/peace" [ 202, {"Content-Type"

    => "nobel-prize/peace"}, "Accepted" ] else @app.call(env) end end
  49. Rack::Kanye

  50. def call(env) request_uri = env['REQUEST_URI'] if request_uri =~ /kanye=true/ env['REQUEST_URI'].gsub!("&kanye=true","")

    @app.call(env) else req = "http://#{env['HTTP_HOST']}#{request_uri}&kanye=true" [ 302, {'Location' => "http://kanyelicious.appspot.com/" + req }, 'Redirecting...' ] end end
  51. useful middlewares Rack::Cache Rack::Bug Rack::Maintenance too many to list !SLIDE

    @@@ text $ rake middleware use Rack::Lock use ActionController::Failsafe use ActionController::Session::CookieStore, # <Proc:0xb6f807d0@(eval):8> use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::ConnectionAdapters::ConnectionM use ActiveRecord::QueryCache run ActionController::Dispatcher.new @@@
  52. real world use:

  53. maintenance mode problem: serve gems when running long migrations rack

    promotes decoupled architectures enable when tmp/maintenanance.txt exists
  54. normally # config.ru run Rack::Adapter::Rails.new(:environment => ENV['RAILS_ENV'])

  55. require File.join('vendor', 'bundler_gems', 'environment') require File.join('config', 'environment') use Rack::Static, :urls

    => ["/index.html", "/favicon.ico", "/images", "/stylesheets"], :root => "public/maintenance" use Hostess use Rack::Maintenance, :file => File.join('public', 'maintenance', 'index.html') run Sinatra::Application
  56. the point(s)

  57. http is everywhere

  58. use Rack

  59. thanks