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/

Nick Quaranto

January 12, 2012
Tweet

More Decks by Nick Quaranto

Other Decks in Programming

Transcript

  1. 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
  2. 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.
  3. headers Date: Wed, 09 Dec 2009 15:25:13 GMT Content-Type: text/html

    X-Served-By: Apache Content-Encoding: gzip
  4. history servers and webapps can't talk to each other CGI

    (C, Perl, PHP, etc) WSGI (Python) Ruby....?
  5. rackup comes with the rack gem starts a rack app

    looks at config.ru needs an endpoint
  6. ,.---._ ,,,, / `, \\\\ / '\_ ; |||| /\/``-.__\;'

    ::::/\/_ {{`-.__.-'(`(^^(^^^(^ 9 `.=========' {{{{{{ { ( ( ( ( (-----:= {{.-'~~'-.(,(,,(,,,(__6_.'=========. ::::\/\ |||| \/\ ,-'/, //// \ `` _/ ; '''' \ ` .' `---'
  7. I::Rack def call(env) if env["PATH_INFO"] == "/WMD" [ 404, {"Content-Type"

    => "text/plain"}, "Not found" ] else @app.call(env) end end
  8. 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
  9. 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
  10. 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 @@@
  11. maintenance mode problem: serve gems when running long migrations rack

    promotes decoupled architectures enable when tmp/maintenanance.txt exists
  12. 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