62. $ 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>
89. $ 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>
レスポンスボディ
90. $ 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
126. class Secret
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] == '/secret'
[200, {'Content-Type' => 'text/plain'}, ["This is Secretn"]]
else
@app.call(env)
end
end
end
class Hello
def call(env)
[200, {'Content-Type' => 'text/plain'}, ["Hello Rackn"]]
end
end
run Secret.new(Hello.new)
127. class Secret
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] == '/secret'
[200, {'Content-Type' => 'text/plain'}, ["This is Secretn"]]
else
@app.call(env)
end
end
end
class Hello
def call(env)
[200, {'Content-Type' => 'text/plain'}, ["Hello Rackn"]]
end
end
run Secret.new(Hello.new)
middleware として使う class を Rack の
インターフェースに従った形で作るよ。
/secret にアクセスしたときだけ This is Secret と
返して、それ以外のときは処理を他に丸投げするよ。
Secret をエンドポイントにして、丸投げ先を Hello にするよ。
144. $ ./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