lib/http/protocol/http1/connection.rb in http-protocol-0.8.0 vs lib/http/protocol/http1/connection.rb in http-protocol-0.8.1

- old
+ new

@@ -145,19 +145,21 @@ end def read_response(method) version, status, reason = read_line.split(/\s+/, 3) + status = Integer(status) + headers = read_headers @persistent = persistent?(version, headers) body = read_response_body(method, status, headers) @count += 1 - return version, Integer(status), reason, headers, body + return version, status, reason, headers, body end def read_headers fields = [] @@ -313,11 +315,11 @@ # 1. Any response to a HEAD request and any response with a 1xx # (Informational), 204 (No Content), or 304 (Not Modified) status # code is always terminated by the first empty line after the # header fields, regardless of the header fields present in the # message, and thus cannot contain a message body. - if method == "HEAD" or status == 204 or status == 304 + if method == "HEAD" or (status >= 100 and status < 200) or status == 204 or status == 304 return nil end # 2. Any 2xx (Successful) response to a CONNECT request implies that # the connection will become a tunnel immediately after the empty @@ -326,30 +328,20 @@ # such a message. if method == "CONNECT" and status == 200 return read_tunnel_body end - if body = read_body(headers) - return body - else - # 7. Otherwise, this is a response message without a declared message - # body length, so the message body length is determined by the - # number of octets received prior to the server closing the - # connection. - return read_remainder_body - end + return read_body(headers, true) end def read_request_body(headers) # 6. If this is a request message and none of the above are true, then # the message body length is zero (no message body is present). - if body = read_body(headers) - return body - end + return read_body(headers) end - def read_body(headers) + def read_body(headers, remainder = false) # 3. If a Transfer-Encoding header field is present and the chunked # transfer coding (Section 4.1) is the final encoding, the message # body length is determined by reading and decoding the chunked # data until the transfer coding indicates the data is complete. if transfer_encoding = headers.delete(TRANSFER_ENCODING) @@ -385,14 +377,24 @@ # the recipient times out before the indicated number of octets are # received, the recipient MUST consider the message to be # incomplete and close the connection. if content_length = headers.delete(CONTENT_LENGTH) length = Integer(content_length) - if length >= 0 + if length > 0 return read_fixed_body(length) + elsif length == 0 + return nil else raise BadRequest, "Invalid content length: #{content_length}" end + end + + if remainder + # 7. Otherwise, this is a response message without a declared message + # body length, so the message body length is determined by the + # number of octets received prior to the server closing the + # connection. + return read_remainder_body end end end end end