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..7d6eb54f 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -411,16 +411,35 @@ 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 response_available?(sock, 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 + def response_available?(sock, timeout) + return sock.wait_readable(timeout) if sock.respond_to?(:wait_readable) + sock.__send__(:rbuf_size) > 0 || + (sock.io.respond_to?(:pending) && sock.io.pending > 0) || + sock.io.to_io.wait_readable(timeout) + end + def write_header(sock, ver, path) reqline = "#{@method} #{path} HTTP/#{ver}" if /[\r\n]/ =~ reqline diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 8a306920..417c42c1 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1142,6 +1142,146 @@ 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 = 10 + http.start { + t = Process.clock_gettime(Process::CLOCK_MONOTONIC) + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_operator(Process.clock_gettime(Process::CLOCK_MONOTONIC) - t, :<, 5) + 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_hints_with_final_response + server = TCPServer.new(config('host'), 0) + th = Thread.new do + sock = server.accept + begin + while (reqline = sock.gets) + while (line = sock.gets) && line != "\r\n" + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 429 Too Many Requests\r\nContent-Length: 0\r\n\r\n" + 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 = 10 + http.start { + t = Process.clock_gettime(Process::CLOCK_MONOTONIC) + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('429', res.code) + assert_operator(Process.clock_gettime(Process::CLOCK_MONOTONIC) - t, :<, 5) + } + ensure + th&.kill + th&.join + server&.close + end + + def test_expect_continue_pending_ssl_data + io = Object.new + def io.pending + 1 + end + def io.to_io + raise "must not wait on the socket" + end + sock = Object.new + sock.define_singleton_method(:io) { io } + def sock.rbuf_size + 0 + end + sock.singleton_class.send(:private, :rbuf_size) + req = Net::HTTP::Put.new('/') + assert_equal(true, req.__send__(:response_available?, sock, 10)) + end + + def test_expect_continue_buffered_io_wait_readable + sock = Object.new + def sock.wait_readable(timeout) + @timeout = timeout + :ready + end + def sock.io + raise "must not look at the socket directly" + end + req = Net::HTTP::Put.new('/') + assert_equal(:ready, req.__send__(:response_available?, sock, 10)) + assert_equal(10, sock.instance_variable_get(:@timeout)) + 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