Skip to content

Commit 1b6b8ad

Browse files
authored
Do not raise error raised on CONNECT, fixes #1441 (#2932)
1 parent 5dbb75c commit 1b6b8ad

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

lib/puma/const.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ module Const
148148

149149
REQUEST_METHOD = "REQUEST_METHOD".freeze
150150
HEAD = "HEAD".freeze
151+
GET = "GET".freeze
152+
POST = "POST".freeze
153+
PUT = "PUT".freeze
154+
DELETE = "DELETE".freeze
155+
OPTIONS = "OPTIONS".freeze
156+
TRACE = "TRACE".freeze
157+
PATCH = "PATCH".freeze
158+
SUPPORTED_HTTP_METHODS = [HEAD, GET, POST, PUT, DELETE, OPTIONS, TRACE, PATCH].freeze
151159
# ETag is based on the apache standard of hex mtime-size-inode (inode is 0 on win32)
152160
LINE_END = "\r\n".freeze
153161
REMOTE_ADDR = "REMOTE_ADDR".freeze

lib/puma/request.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ def handle_request(client, lines, requests)
7373

7474
begin
7575
begin
76-
status, headers, res_body = @thread_pool.with_force_shutdown do
77-
@app.call(env)
76+
if SUPPORTED_HTTP_METHODS.include?(env[REQUEST_METHOD])
77+
status, headers, res_body = @thread_pool.with_force_shutdown do
78+
@app.call(env)
79+
end
80+
else
81+
@log_writer.log "Unsupported HTTP method used: #{env[REQUEST_METHOD]}"
82+
status, headers, res_body = [501, {}, ["#{env[REQUEST_METHOD]} method is not supported"]]
7883
end
7984

8085
return :async if client.hijacked
@@ -271,14 +276,12 @@ def normalize_env(env, client)
271276
uri = URI.parse(env[REQUEST_URI])
272277
env[REQUEST_PATH] = uri.path
273278

274-
raise "No REQUEST PATH" unless env[REQUEST_PATH]
275-
276279
# A nil env value will cause a LintError (and fatal errors elsewhere),
277280
# so only set the env value if there actually is a value.
278281
env[QUERY_STRING] = uri.query if uri.query
279282
end
280283

281-
env[PATH_INFO] = env[REQUEST_PATH]
284+
env[PATH_INFO] = env[REQUEST_PATH].to_s # #to_s in case it's nil
282285

283286
# From https://www.ietf.org/rfc/rfc3875 :
284287
# "Script authors should be aware that the REMOTE_ADDR and

test/test_web_server.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ def test_file_streamed_request
7979
socket.close
8080
end
8181

82+
def test_unsupported_method
83+
socket = do_test("CONNECT www.zedshaw.com:443 HTTP/1.1\r\nConnection: close\r\n\r\n", 100)
84+
response = socket.read
85+
assert_match "Not Implemented", response
86+
socket.close
87+
end
88+
89+
def test_nonexistent_method
90+
socket = do_test("FOOBARBAZ www.zedshaw.com:443 HTTP/1.1\r\nConnection: close\r\n\r\n", 100)
91+
response = socket.read
92+
assert_match "Not Implemented", response
93+
socket.close
94+
end
95+
8296
private
8397

8498
def do_test(string, chunk)

0 commit comments

Comments
 (0)