tmm1 / http_parser.rb

simple callback-based HTTP request/response parser
MIT License
200 stars 43 forks source link

POST request with no length information breaks (JRUBY) #53

Open HoneyryderChuck opened 6 years ago

HoneyryderChuck commented 6 years ago

Usually, when a POST request is parsed, one either expects a Content-Length or a Transfer-Encoding: chunked in the headers. When none of these present themselves, the server can respond with "411 Length Required". The C-based parser parses the request (sometimes it breaks with "not parse data entirely" error) enough for me to identify the error and respond (it also considers the message complete before parsing the incoming body). The Java parser just breaks with an "invalid method" exception:

HTTP::Parser::Error: invalid method
      << at org/ruby_http_parser/RubyHttpParser.java:372

Below you'll find a script which demonstrates the issue. I don't know if you consider one or the other more valid (I stand for the first one), but they both need to have the same behaviour.

 require "http_parser"

 $payload = "POST / HTTP/1.1\r\nAccept-Encoding: identity;q=1.0\r\nAccept: */*\r\nUser-Agent: Ruby\r\nHost: 127.0.0.1:58229\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nonetwothree".b

 $parser = HTTP::Parser.new

 $parser.on_message_begin = -> { puts "started!!!" }
 $parser.on_headers_complete = -> (h) { puts "headers! #{h}" }
 $parser.on_body = -> (ch) { puts "chunk! #{ch}" }
 $parser.on_message_complete = -> { puts "we're done!!" }

 $parser << $payload