diff --git a/lib/net/http.rb b/lib/net/http.rb index 150e2277..38752b33 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -2516,10 +2516,12 @@ def send_entity(path, data, initheader, dest, type, &block) def transport_request(req) count = 0 begin + body_sent = false begin_transport req res = catch(:response) { begin req.exec @socket, @curr_http_version, edit_path(req.path) + body_sent = true rescue Errno::EPIPE # Failure when writing full request, but we can probably # still read the received response. @@ -2566,6 +2568,7 @@ def transport_request(req) raise end + @socket.close unless body_sent end_transport req, res res rescue => exception diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 96396b0a..d62d8252 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -411,12 +411,24 @@ def flush_buffer(out, buf, chunked_p) def wait_for_continue(sock, ver) if ver >= '1.1' and @header['expect'] and @header['expect'].include?('100-continue') - if sock.io.to_io.wait_readable(sock.continue_timeout) + timeout = sock.continue_timeout + deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout if timeout + informational_count = 0 + while sock.io.to_io.wait_readable(timeout) res = Net::HTTPResponse.read_new(sock) - unless res.kind_of?(Net::HTTPContinue) - res.decode_content = @decode_content - throw :response, res + break if res.kind_of?(Net::HTTPContinue) + if res.kind_of?(Net::HTTPInformation) && !res.kind_of?(Net::HTTPSwitchProtocol) + informational_count += 1 + raise Net::HTTPBadResponse, 'too many informational responses' if + informational_count > Net::HTTPResponse::MAX_INFORMATIONAL_RESPONSES + if deadline + timeout = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) + break if timeout <= 0 + end + next end + res.decode_content = @decode_content + throw :response, res end end end diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 8a306920..97e2f57d 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1142,6 +1142,80 @@ def test_expect_continue_error_while_waiting assert_match(/Expect: 100-continue/, @debug.string) assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) end + + def test_expect_continue_after_early_hints + server = TCPServer.new(config('host'), 0) + th = Thread.new do + sock = server.accept + begin + while (reqline = sock.gets) + clen = 0 + while (line = sock.gets) && line != "\r\n" + clen = line.split(':', 2)[1].to_i if line.start_with?('Content-Length') + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 100 Continue\r\n\r\n" + body = sock.read(clen) + sock.write "HTTP/1.1 201 Created\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}" + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 1 + http.start { + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('201', res.code) + assert_equal('data!', res.body) + res = http.get('/second') + assert_equal("GET /second HTTP/1.1\r\n", res.body) + } + ensure + th&.kill + th&.join + server&.close + end + + def test_expect_continue_early_response + server = TCPServer.new(config('host'), 0) + th = Thread.new do + 2.times do + sock = server.accept + begin + while (reqline = sock.gets) + clen = 0 + while (line = sock.gets) && line != "\r\n" + clen = line.split(':', 2)[1].to_i if line.start_with?('Content-Length') + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 429 Too Many Requests\r\nContent-Length: 0\r\n\r\n" + sock.read(clen) + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 1 + http.start { + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('429', res.code) + res = http.get('/second') + assert_equal("GET /second HTTP/1.1\r\n", res.body) + } + ensure + th&.kill + th&.join + server&.close + end end class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase