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

Rails と Rack と HTTP と通信の話 / Rails Rack HTTP, communication

Rails と Rack と HTTP と通信の話 / Rails Rack HTTP, communication

Yusuke SUGAMIYA

November 22, 2019
Tweet

More Decks by Yusuke SUGAMIYA

Other Decks in Programming

Transcript

  1. $ curl http://example.com <!doctype html> <html> <head> <title>Example Domain</title> [

    ... தུ ... ] </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html>
  2. 3'$

  3. $ curl --verbose http://example.com * Trying 93.184.216.34... * TCP_NODELAY set

    * Connected to example.com (93.184.216.34) port 80 (#0)
  4. $ curl --verbose http://example.com * Trying 93.184.216.34... * TCP_NODELAY set

    * Connected to example.com (93.184.216.34) port 80 (#0) > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/7.64.1 > Accept: */* > ϦΫΤετϔομ ۭߦ
  5. $ curl --verbose http://example.com [...] < HTTP/1.1 200 OK <

    Cache-Control: max-age=604800 < Content-Type: text/html; charset=UTF-8 < Date: Thu, 21 Nov 2019 10:07:23 GMT < Etag: "3147526947+gzip+ident" < Expires: Thu, 28 Nov 2019 10:07:23 GMT < Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT < Server: ECS (sjc/4E68) < Vary: Accept-Encoding < X-Cache: HIT < Content-Length: 1256 < Ϩεϙϯεϔομ ۭߦ
  6. $ curl --verbose http://example.com <!doctype html> <html> <head> <title>Example Domain</title>

    [...] </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> ϨεϙϯεϘσΟ
  7. $ curl --verbose http://example.com <!doctype html> <html> <head> <title>Example Domain</title>

    [...] </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> * Connection #0 to host example.com left intact * Closing connection 0
  8. $ curl --verbose http://example.com * Trying 93.184.216.34... * TCP_NODELAY set

    * Connected to example.com (93.184.216.34) port 80 (#0) > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/7.64.1 > Accept: */* > ϦΫΤετϔομ
  9. $ curl --verbose http://example.com * Trying 93.184.216.34... * TCP_NODELAY set

    * Connected to example.com (93.184.216.34) port 80 (#0) > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/7.64.1 > Accept: */* > ͜Ε
  10. $ curl --verbose http://example.com [...] < HTTP/1.1 200 OK <

    Cache-Control: max-age=604800 < Content-Type: text/html; charset=UTF-8 < Date: Thu, 21 Nov 2019 10:07:23 GMT < Etag: "3147526947+gzip+ident" < Expires: Thu, 28 Nov 2019 10:07:23 GMT < Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT < Server: ECS (sjc/4E68) < Vary: Accept-Encoding < X-Cache: HIT < Content-Length: 1256 < Ϩεϙϯεϔομ ۭߦ
  11. $ curl --verbose http://example.com [...] < HTTP/1.1 200 OK <

    Cache-Control: max-age=604800 < Content-Type: text/html; charset=UTF-8 < Date: Thu, 21 Nov 2019 10:07:23 GMT < Etag: "3147526947+gzip+ident" < Expires: Thu, 28 Nov 2019 10:07:23 GMT < Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT < Server: ECS (sjc/4E68) < Vary: Accept-Encoding < X-Cache: HIT < Content-Length: 1256 <
  12. $ curl --verbose http://example.com [...] < HTTP/1.1 200 OK <

    Cache-Control: max-age=604800 < Content-Type: text/html; charset=UTF-8 < Date: Thu, 21 Nov 2019 10:07:23 GMT < Etag: "3147526947+gzip+ident" < Expires: Thu, 28 Nov 2019 10:07:23 GMT < Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT < Server: ECS (sjc/4E68) < Vary: Accept-Encoding < X-Cache: HIT < Content-Length: 1256 < ੒ޭͨ͠Α
  13. run Proc.new do |env| [ 200, {'Content-Type' => 'text/plain'}, ['Hello']

    ] end envʹ͸ΫϥΠΞϯτ͔Βͷ ϦΫΤετͷ৘ใ͕ೖͬͯΔΑɻ Ϩεϙϯεͷεςʔλείʔυ Ϩεϙϯεϔομ ϨεϙϯεϘσΟ 3BDLͷΤϯυϙΠϯτͱͯ࣍͠ͷ Proc Λ࢖͏Αɻ
  14. class Hello def call(env) [200, {'Content-Type' => 'text/plain'}, ["Hello Rack\n"]]

    end end run Hello.new $ curl http://localhost:9292/ Hello Rack
  15. class Secret def initialize(app) @app = app end def call(env)

    if env["PATH_INFO"] == '/secret' [200, {'Content-Type' => 'text/plain'}, ["This is Secret\n"]] else @app.call(env) end end end class Hello def call(env) [200, {'Content-Type' => 'text/plain'}, ["Hello Rack\n"]] end end run Secret.new(Hello.new)
  16. class Secret def initialize(app) @app = app end def call(env)

    if env["PATH_INFO"] == '/secret' [200, {'Content-Type' => 'text/plain'}, ["This is Secret\n"]] else @app.call(env) end end end class Hello def call(env) [200, {'Content-Type' => 'text/plain'}, ["Hello Rack\n"]] end end run Secret.new(Hello.new) NJEEMFXBSFͱͯ͠࢖͏DMBTTΛ3BDLͷ ΠϯλʔϑΣʔεʹैͬͨܗͰ࡞ΔΑɻ TFDSFUʹΞΫηεͨ͠ͱ͖͚ͩ5IJTJT4FDSFUͱ ฦͯ͠ɺͦΕҎ֎ͷͱ͖͸ॲཧΛଞʹؙ౤͛͢ΔΑɻ 4FDSFUΛΤϯυϙΠϯτʹͯ͠ɺؙ౤͛ઌΛ)FMMPʹ͢ΔΑɻ
  17. $ ./bin/rails middleware use Rack::Sendfile use ActionDispatch::Static use ActionDispatch::Executor use

    ActiveSupport::Cache::Strategy::LocalCache::Middleware use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use ActionDispatch::RemoteIp use Sprockets::Rails::QuietAssets use Rails::Rack::Logger use ActionDispatch::ShowExceptions use WebConsole::Middleware use ActionDispatch::DebugExceptions use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ContentSecurityPolicy::Middleware use Rack::Head use Rack::ConditionalGet use Rack::ETag use Rack::TempfileReaper run MyApp::Application.routes