Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SERVER_PROTOCOL to SPEC #1883

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file. For info on
- Middleware must no longer call `#each` on the body, but they can call `#to_ary` on the body if it responds to `#to_ary`.
- `rack.input` is no longer required to be rewindable.
- `rack.multithread/rack.multiprocess/rack.run_once` are no longer required environment keys.
- `SERVER_PROTOCOL` is now a required key, matching the HTTP protocol used in the request.

### Removed

Expand Down
4 changes: 4 additions & 0 deletions SPEC.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ below.
<tt>SERVER_PORT</tt>:: An optional +Integer+ which is the port the
server is running on. Should be specified if
the server is running on a non-standard port.
<tt>SERVER_PROTOCOL</tt>:: A string representing the HTTP version used
for the request.
<tt>HTTP_</tt> Variables:: Variables corresponding to the
client-supplied HTTP request
headers (i.e., variables whose
Expand Down Expand Up @@ -117,6 +119,8 @@ accepted specifications and must not be used otherwise.
The <tt>SERVER_PORT</tt> must be an Integer if set.
The <tt>SERVER_NAME</tt> must be a valid authority as defined by RFC7540.
The <tt>HTTP_HOST</tt> must be a valid authority as defined by RFC7540.
The <tt>SERVER_PROTOCOL</tt> must match the regexp <tt>HTTP/\d(\.\d)?</tt>.
If the <tt>HTTP_VERSION</tt> is present, it must equal the <tt>SERVER_PROTOCOL</tt>.
The environment must not contain the keys
<tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
(use the versions without <tt>HTTP_</tt>).
Expand Down
16 changes: 15 additions & 1 deletion lib/rack/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def check_environment(env)
## server is running on. Should be specified if
## the server is running on a non-standard port.

## <tt>SERVER_PROTOCOL</tt>:: A string representing the HTTP version used
## for the request.

## <tt>HTTP_</tt> Variables:: Variables corresponding to the
## client-supplied HTTP request
## headers (i.e., variables whose
Expand Down Expand Up @@ -269,7 +272,7 @@ def check_environment(env)
## accepted specifications and must not be used otherwise.
##

%w[REQUEST_METHOD SERVER_NAME QUERY_STRING
%w[REQUEST_METHOD SERVER_NAME QUERY_STRING SERVER_PROTOCOL
rack.version rack.input rack.errors].each { |header|
raise LintError, "env missing required key #{header}" unless env.include? header
}
Expand All @@ -290,6 +293,17 @@ def check_environment(env)
raise LintError, "#{env[HTTP_HOST]} must be a valid authority"
end

## The <tt>SERVER_PROTOCOL</tt> must match the regexp <tt>HTTP/\d(\.\d)?</tt>.
server_protocol = env['SERVER_PROTOCOL']
unless %r{HTTP/\d(\.\d)?}.match?(server_protocol)
raise LintError, "env[SERVER_PROTOCOL] does not match HTTP/\\d(\\.\\d)?"
end

## If the <tt>HTTP_VERSION</tt> is present, it must equal the <tt>SERVER_PROTOCOL</tt>.
if env['HTTP_VERSION'] && env['HTTP_VERSION'] != server_protocol
raise LintError, "env[HTTP_VERSION] does not equal env[SERVER_PROTOCOL]"
end

## The environment must not contain the keys
## <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
## (use the versions without <tt>HTTP_</tt>).
Expand Down
2 changes: 2 additions & 0 deletions lib/rack/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def self.parse_uri_rfc2396(uri)
# Options:
# :fatal :: Whether to raise an exception if request outputs to rack.errors
# :input :: The rack.input to set
# :http_version :: The SERVER_PROTOCOL to set
# :method :: The HTTP request method to use
# :params :: The params to use
# :script_name :: The SCRIPT_NAME to set
Expand All @@ -113,6 +114,7 @@ def self.env_for(uri = "", opts = {})
env[REQUEST_METHOD] = (opts[:method] ? opts[:method].to_s.upcase : GET).b
env[SERVER_NAME] = (uri.host || "example.org").b
env[SERVER_PORT] = (uri.port ? uri.port.to_s : "80").b
env[SERVER_PROTOCOL] = opts[:http_version] || 'HTTP/1.1'
env[QUERY_STRING] = (uri.query.to_s).b
env[PATH_INFO] = ((!uri.path || uri.path.empty?) ? "/" : uri.path).b
env[RACK_URL_SCHEME] = (uri.scheme || "http").b
Expand Down
3 changes: 0 additions & 3 deletions test/spec_chunked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ def obj.each; yield 's' end
body.join.must_equal 'Hello World!'
end

@env.delete('SERVER_PROTOCOL') # unicorn will do this on pre-HTTP/1.0 requests
check.call

@env['SERVER_PROTOCOL'] = 'HTTP/0.9' # not sure if this happens in practice
check.call
end
Expand Down
24 changes: 16 additions & 8 deletions test/spec_common_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@
res = Rack::MockRequest.new(Rack::CommonLogger.new(app)).get("/")

res.errors.wont_be :empty?
res.errors.must_match(/"GET \/ " 200 #{length} /)
res.errors.must_match(/"GET \/ HTTP\/1\.1" 200 #{length} /)
end

it "log to anything with +write+" do
log = StringIO.new
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")

log.string.must_match(/"GET \/ " 200 #{length} /)
log.string.must_match(/"GET \/ HTTP\/1\.1" 200 #{length} /)
end

it "work with standard library logger" do
logdev = StringIO.new
log = Logger.new(logdev)
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")

logdev.string.must_match(/"GET \/ " 200 #{length} /)
logdev.string.must_match(/"GET \/ HTTP\/1\.1" 200 #{length} /)
end

it "log - content length if header is missing" do
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_without_length)).get("/")

res.errors.wont_be :empty?
res.errors.must_match(/"GET \/ " 200 - /)
res.errors.must_match(/"GET \/ HTTP\/1\.1" 200 - /)
end

it "log - content length if header is zero" do
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_with_zero_length)).get("/")

res.errors.wont_be :empty?
res.errors.must_match(/"GET \/ " 200 - /)
res.errors.must_match(/"GET \/ HTTP\/1\.1" 200 - /)
end

it "log - records host from X-Forwarded-For header" do
Expand Down Expand Up @@ -94,7 +94,7 @@ def with_mock_time(t = 0)
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
end

md = /- - - \[([^\]]+)\] "(\w+) \/ " (\d{3}) \d+ ([\d\.]+)/.match(log.string)
md = /- - - \[([^\]]+)\] "(\w+) \/ HTTP\/1\.1" (\d{3}) \d+ ([\d\.]+)/.match(log.string)
md.wont_equal nil
time, method, status, duration = *md.captures
time.must_equal Time.at(0).strftime("%d/%b/%Y:%H:%M:%S %z")
Expand All @@ -108,15 +108,23 @@ def with_mock_time(t = 0)
log = Logger.new(logdev)
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/hello")

logdev.string.must_match(/"GET \/hello " 200 #{length} /)
logdev.string.must_match(/"GET \/hello HTTP\/1\.1" 200 #{length} /)
end

it "log path with SCRIPT_NAME" do
logdev = StringIO.new
log = Logger.new(logdev)
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/path", script_name: "/script")

logdev.string.must_match(/"GET \/script\/path " 200 #{length} /)
logdev.string.must_match(/"GET \/script\/path HTTP\/1\.1" 200 #{length} /)
end

it "log path with SERVER_PROTOCOL" do
logdev = StringIO.new
log = Logger.new(logdev)
Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/path", http_version: "HTTP/1.0")

logdev.string.must_match(/"GET \/path HTTP\/1\.0" 200 #{length} /)
end

def length
Expand Down
20 changes: 20 additions & 0 deletions test/spec_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ def env(*args)
}.must_raise(Rack::Lint::LintError).
message.must_match(/missing required key SERVER_NAME/)

lambda {
e = env
e.delete("SERVER_PROTOCOL")
Rack::Lint.new(nil).call(e)
}.must_raise(Rack::Lint::LintError).
message.must_match(/missing required key SERVER_PROTOCOL/)

lambda {
e = env
e["SERVER_PROTOCOL"] = 'Foo'
Rack::Lint.new(nil).call(e)
}.must_raise(Rack::Lint::LintError).
message.must_match(/env\[SERVER_PROTOCOL\] does not match HTTP/)

lambda {
e = env
e["HTTP_VERSION"] = 'HTTP/1.0'
Rack::Lint.new(nil).call(e)
}.must_raise(Rack::Lint::LintError).
message.must_match(/env\[HTTP_VERSION\] does not equal env\[SERVER_PROTOCOL\]/)

lambda {
Rack::Lint.new(nil).call(env("HTTP_CONTENT_TYPE" => "text/plain"))
Expand Down
13 changes: 13 additions & 0 deletions test/spec_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
env["REQUEST_METHOD"].must_equal "GET"
env["SERVER_NAME"].must_equal "example.org"
env["SERVER_PORT"].must_equal "80"
env["SERVER_PROTOCOL"].must_equal "HTTP/1.1"
env["QUERY_STRING"].must_equal ""
env["PATH_INFO"].must_equal "/"
env["SCRIPT_NAME"].must_equal ""
Expand Down Expand Up @@ -172,6 +173,18 @@
env["REQUEST_METHOD"].must_equal "GET"
end

it "accept :script_name option to set SCRIPT_NAME" do
res = Rack::MockRequest.new(app).get("/", script_name: '/foo')
env = YAML.unsafe_load(res.body)
env["SCRIPT_NAME"].must_equal "/foo"
end

it "accept :http_version option to set SERVER_PROTOCOL" do
res = Rack::MockRequest.new(app).get("/", http_version: 'HTTP/1.0')
env = YAML.unsafe_load(res.body)
env["SERVER_PROTOCOL"].must_equal "HTTP/1.0"
end

it "accept params and build query string for GET requests" do
res = Rack::MockRequest.new(app).get("/foo?baz=2", params: { foo: { bar: "1" } })
env = YAML.unsafe_load(res.body)
Expand Down