$30 off During Our Annual Pro Sale. View Details »

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. rack: HTTP 200 OK
    nick quaranto

    View Slide

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

    View Slide

  3. this talk is about
    internets
    rubies
    unicorns

    View Slide

  4. HTTP

    View Slide

  5. history

    View Slide

  6. tim berners-lee

    View Slide

  7. the first web server

    View Slide

  8. View Slide

  9. http 1.0: rfc1945
    http 1.1: rfc2616

    View Slide

  10. View Slide

  11. the protocol
    request => response
    stateless

    View Slide

  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

    View Slide

  13. verbs
    HEAD
    GET
    POST
    PUT
    DELETE
    etc...

    View Slide

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

    View Slide

  15. codes

    View Slide

  16. 200s

    View Slide

  17. 200s
    200 OK
    201 Created
    202 Accepted

    View Slide

  18. 300s

    View Slide

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

    View Slide

  20. 400s

    View Slide

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

    View Slide

  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.

    View Slide

  23. 500s

    View Slide

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

    View Slide

  25. headers
    Date: Wed, 09 Dec 2009 15:25:13 GMT
    Content-Type: text/html
    X-Served-By: Apache
    Content-Encoding: gzip

    View Slide

  26. let's do this

    View Slide

  27. tip of the iceberg
    sessions
    security
    caching
    proxies

    View Slide

  28. rack

    View Slide

  29. history
    servers and webapps can't talk to each other
    CGI (C, Perl, PHP, etc)
    WSGI (Python)
    Ruby....?

    View Slide

  30. servers
    apache (passenger)
    mongrel
    thin
    unicorn
    and more...

    View Slide

  31. frameworks
    rails
    merb
    sinatra
    and more...

    View Slide

  32. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  36. let's make a rack app

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  40. rackup
    comes with the rack gem
    starts a rack app
    looks at config.ru
    needs an endpoint

    View Slide

  41. require 'rack/lobster'
    run Rack::Lobster.new

    View Slide

  42. ,.---._
    ,,,, / `,
    \\\\ / '\_ ;
    |||| /\/``-.__\;'
    ::::/\/_
    {{`-.__.-'(`(^^(^^^(^ 9 `.========='
    {{{{{{ { ( ( ( ( (-----:=
    {{.-'~~'-.(,(,,(,,,(__6_.'=========.
    ::::\/\
    |||| \/\ ,-'/,
    //// \ `` _/ ;
    '''' \ ` .'
    `---'

    View Slide

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

    View Slide

  44. middleware
    think of pancakes
    stack them up!
    instead of run, use
    mmm, pancakes

    View Slide

  45. View Slide

  46. class PassThrough
    def initialize(app)
    @app = app
    end
    def call(env)
    @app.call(env)
    end
    end

    View Slide

  47. I::Rack
    def call(env)
    if env["PATH_INFO"] == "/WMD"
    [
    404,
    {"Content-Type" => "text/plain"},
    "Not found"
    ]
    else
    @app.call(env)
    end
    end

    View Slide

  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

    View Slide

  49. Rack::Kanye

    View Slide

  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

    View Slide

  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, #
    use
    ActionController::ParamsParser use
    Rack::MethodOverride use Rack::Head use
    ActiveRecord::ConnectionAdapters::ConnectionM
    use ActiveRecord::QueryCache run
    ActionController::Dispatcher.new @@@

    View Slide

  52. real world use:

    View Slide

  53. maintenance mode
    problem: serve gems when running long
    migrations
    rack promotes decoupled architectures
    enable when tmp/maintenanance.txt exists

    View Slide

  54. normally
    # config.ru
    run Rack::Adapter::Rails.new(:environment => ENV['RAILS_ENV'])

    View Slide

  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

    View Slide

  56. the point(s)

    View Slide

  57. http is everywhere

    View Slide

  58. use Rack

    View Slide

  59. thanks

    View Slide