lib/rack/lint.rb in rack-1.4.7 vs lib/rack/lint.rb in rack-1.5.0.beta.1
- old
+ new
@@ -1,6 +1,7 @@
require 'rack/utils'
+require 'forwardable'
module Rack
# Rack::Lint validates your application and the requests and
# responses according to the Rack spec.
@@ -48,10 +49,13 @@
status, headers, @body = @app.call(env)
## The *status*,
check_status status
## the *headers*,
check_headers headers
+
+ check_hijack_response headers, env
+
## and the *body*.
check_content_type status, headers
check_content_length status, headers
@head_request = env["REQUEST_METHOD"] == "HEAD"
[status, headers, self]
@@ -106,11 +110,12 @@
## names begin with <tt>HTTP_</tt>). The
## presence or absence of these
## variables should correspond with
## the presence or absence of the
## appropriate HTTP header in the
- ## request.
+ ## request. See <a href="https://tools.ietf.org/html/rfc3875#section-4.1.18">
+ ## RFC3875 section 4.1.18</a> for specific behavior.
## In addition to this, the Rack environment must include these
## Rack-specific variables:
## <tt>rack.version</tt>:: The Array [1,1], representing this version of Rack.
@@ -118,10 +123,13 @@
## <tt>rack.input</tt>:: See below, the input stream.
## <tt>rack.errors</tt>:: See below, the error stream.
## <tt>rack.multithread</tt>:: true if the application object may be simultaneously invoked by another thread in the same process, false otherwise.
## <tt>rack.multiprocess</tt>:: true if an equivalent application object may be simultaneously invoked by another process, false otherwise.
## <tt>rack.run_once</tt>:: true if the server expects (but does not guarantee!) that the application will only be invoked this one time during the life of its containing process. Normally, this will only be true for a server based on CGI (or something similar).
+ ## <tt>rack.hijack?</tt>:: present and true if the server supports connection hijacking. See below, hijacking.
+ ## <tt>rack.hijack</tt>:: an object responding to #call that must be called at least once before using rack.hijack_io. It is recommended #call return rack.hijack_io as well as setting it in env if necessary.
+ ## <tt>rack.hijack_io</tt>:: if rack.hijack? is true, and rack.hijack has received #call, this will contain an object resembling an IO. See hijacking.
##
## Additional environment specifications have approved to
## standardized middleware APIs. None of these are required to
## be implemented by the server.
@@ -224,10 +232,12 @@
## * There must be a valid input stream in <tt>rack.input</tt>.
check_input env["rack.input"]
## * There must be a valid error stream in <tt>rack.errors</tt>.
check_error env["rack.errors"]
+ ## * There may be a valid hijack stream in <tt>rack.hijack_io</tt>
+ check_hijack env
## * The <tt>REQUEST_METHOD</tt> must be a valid token.
assert("REQUEST_METHOD unknown: #{env["REQUEST_METHOD"]}") {
env["REQUEST_METHOD"] =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
}
@@ -414,10 +424,125 @@
def close(*args)
assert("rack.errors#close must not be called") { false }
end
end
+ class HijackWrapper
+ include Assertion
+ extend Forwardable
+
+ REQUIRED_METHODS = [
+ :read, :write, :read_nonblock, :write_nonblock, :flush, :close,
+ :close_read, :close_write, :closed?
+ ]
+
+ def_delegators :@io, *REQUIRED_METHODS
+
+ def initialize(io)
+ @io = io
+ REQUIRED_METHODS.each do |meth|
+ assert("rack.hijack_io must respond to #{meth}") { io.respond_to? meth }
+ end
+ end
+ end
+
+ ## === Hijacking
+ #
+ # AUTHORS: n.b. The trailing whitespace between paragraphs is important and
+ # should not be removed. The whitespace creates paragraphs in the RDoc
+ # output.
+ #
+ ## ==== Request (before status)
+ def check_hijack(env)
+ if env['rack.hijack?']
+ ## If rack.hijack? is true then rack.hijack must respond to #call.
+ original_hijack = env['rack.hijack']
+ assert("rack.hijack must respond to call") { original_hijack.respond_to?(:call) }
+ env['rack.hijack'] = proc do
+ ## rack.hijack must return the io that will also be assigned (or is
+ ## already present, in rack.hijack_io.
+ io = original_hijack.call
+ HijackWrapper.new(io)
+ ##
+ ## rack.hijack_io must respond to:
+ ## <tt>read, write, read_nonblock, write_nonblock, flush, close,
+ ## close_read, close_write, closed?</tt>
+ ##
+ ## The semantics of these IO methods must be a best effort match to
+ ## those of a normal ruby IO or Socket object, using standard
+ ## arguments and raising standard exceptions. Servers are encouraged
+ ## to simply pass on real IO objects, although it is recognized that
+ ## this approach is not directly compatible with SPDY and HTTP 2.0.
+ ##
+ ## IO provided in rack.hijack_io should preference the
+ ## IO::WaitReadable and IO::WaitWritable APIs wherever supported.
+ ##
+ ## There is a deliberate lack of full specification around
+ ## rack.hijack_io, as semantics will change from server to server.
+ ## Users are encouraged to utilize this API with a knowledge of their
+ ## server choice, and servers may extend the functionality of
+ ## hijack_io to provide additional features to users. The purpose of
+ ## rack.hijack is for Rack to "get out of the way", as such, Rack only
+ ## provides the minimum of specification and support.
+ env['rack.hijack_io'] = HijackWrapper.new(env['rack.hijack_io'])
+ io
+ end
+ else
+ ##
+ ## If rack.hijack? is false, then rack.hijack should not be set.
+ assert("rack.hijack? is false, but rack.hijack is present") { env['rack.hijack'].nil? }
+ ##
+ ## If rack.hijack? is false, then rack.hijack_io should not be set.
+ assert("rack.hijack? is false, but rack.hijack_io is present") { env['rack.hijack_io'].nil? }
+ end
+ end
+
+ ## ==== Response (after headers)
+ ## It is also possible to hijack a response after the status and headers
+ ## have been sent.
+ def check_hijack_response(headers, env)
+ ## In order to do this, an application may set the special header
+ ## <tt>rack.hijack</tt> to an object that responds to <tt>call</tt>
+ ## accepting an argument that conforms to the <tt>rack.hijack_io</tt>
+ ## protocol.
+ ##
+ ## After the headers have been sent, and this hijack callback has been
+ ## called, the application is now responsible for the remaining lifecycle
+ ## of the IO. The application is also responsible for maintaining HTTP
+ ## semantics. Of specific note, in almost all cases in the current SPEC,
+ ## applications will have wanted to specify the header Connection:close in
+ ## HTTP/1.1, and not Connection:keep-alive, as there is no protocol for
+ ## returning hijacked sockets to the web server. For that purpose, use the
+ ## body streaming API instead (progressively yielding strings via each).
+ ##
+ ## Servers must ignore the <tt>body</tt> part of the response tuple when
+ ## the <tt>rack.hijack</tt> response API is in use.
+
+ if env['rack.hijack?'] && headers['rack.hijack']
+ assert('rack.hijack header must respond to #call') {
+ headers['rack.hijack'].respond_to? :call
+ }
+ original_hijack = headers['rack.hijack']
+ headers['rack.hijack'] = proc do |io|
+ original_hijack.call HijackWrapper.new(io)
+ end
+ else
+ ##
+ ## The special response header <tt>rack.hijack</tt> must only be set
+ ## if the request env has <tt>rack.hijack?</tt> <tt>true</tt>.
+ assert('rack.hijack header must not be present if server does not support hijacking') {
+ headers['rack.hijack'].nil?
+ }
+ end
+ end
+ ## ==== Conventions
+ ## * Middleware should not use hijack unless it is handling the whole
+ ## response.
+ ## * Middleware may wrap the IO object for the response pattern.
+ ## * Middleware should not wrap the IO object for the request pattern. The
+ ## request pattern is intended to provide the hijacker with "raw tcp".
+
## == The Response
## === The Status
def check_status(status)
## This is an HTTP status. When parsed as integer (+to_i+), it must be
@@ -430,10 +555,14 @@
## The header must respond to +each+, and yield values of key and value.
assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
header.respond_to? :each
}
header.each { |key, value|
+ ## Special headers starting "rack." are for communicating with the
+ ## server, and must not be sent back to the client.
+ next if key =~ /^rack\..+$/
+
## The header keys must be Strings.
assert("header key must be a string, was #{key.class}") {
key.kind_of? String
}
## The header must not contain a +Status+ key,
@@ -461,21 +590,17 @@
end
## === The Content-Type
def check_content_type(status, headers)
headers.each { |key, value|
- ## There must be a <tt>Content-Type</tt>, except when the
- ## +Status+ is 1xx, 204, 205 or 304, in which case there must be none
- ## given.
+ ## There must not be a <tt>Content-Type</tt>, when the +Status+ is 1xx,
+ ## 204, 205 or 304.
if key.downcase == "content-type"
assert("Content-Type header found in #{status} response, not allowed") {
not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
}
return
end
- }
- assert("No Content-Type header found") {
- Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
}
end
## === The Content-Length
def check_content_length(status, headers)