ruby / webrick

HTTP server toolkit
BSD 2-Clause "Simplified" License
286 stars 97 forks source link

POST/PUT with an empty body hangs forever #67

Closed davidtaylorhq closed 3 years ago

davidtaylorhq commented 3 years ago

When making a PUT/POST request with an empty body, we're seeing all requests hang forever. This seems to have started since https://github.com/ruby/webrick/commit/069e9b1908aad3a30a0dcf67b6d3bb13c3216d2c. Reverting to version 1.6.1 takes us back to the previous behaviour (which is an instant 411 response).

@OsamaSayegh shared some details in #30:

It seems like WEBrick still doesn't allow POST/PUT requests with empty body, but the difference now is that the server doesn't respond with a 411, instead the request is blocked forever because the server gets stuck at the eof call here:

https://github.com/ruby/webrick/blob/cda6d407c4a985a458a78c43b6b598520d88fb39/lib/webrick/httprequest.rb#L525

I can repro with this script:

require 'webrick'

class Simple < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
    puts "Hello world!"
    res.status = 200
    res.body = "Hello world!"
  end

  alias do_POST do_GET
end
server = WEBrick::HTTPServer.new(Port: 9988)                                                                                                                                                                                                                                    
server.logger.level = 5
server.mount '/', Simple
server.start

And:

~ » curl -X POST --verbose localhost:9988
*   Trying 127.0.0.1:9988...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9988 (#0)
> POST / HTTP/1.1
> Host: localhost:9988
> User-Agent: curl/7.68.0
> Accept: */*
>
# blocks forever; I have to Ctrl+C it
jeremyevans commented 3 years ago

I've reverted that commit. I'll see if I can work on an approach that doesn't hang.

davidtaylorhq commented 3 years ago

Thanks for the quick revert @jeremyevans. Would it be possible to cut a new release which includes the revert? At the moment we are pinning our projects to v1.6.1.

jeremyevans commented 3 years ago

That would be up to @hsbt .