finagle
finagle copied to clipboard
HTTP client adds Content-Length header to response
When the HTTP client receives a 204 response with no content-length header, it appears to add a content-length header to the response object.
Expected behavior
Content-length header should not be added to the response.
Actual behavior
Content-length header is added to response.
Steps to reproduce the behavior
Consider the following snippet:
val server = Http.serve(s":$port", Service.mk { req: Request =>
val rsp = req.response
rsp.contentString = ""
rsp.statusCode = 204
println("Response Headers Sent")
println(rsp.headerMap)
Future.value(rsp)
})
val service = Http.client.newService(Name.bound(Address(host, port)), "client")
val req = Request()
val rsp = Await.result(service(req))
println("Response Headers Received")
println(rsp.headerMap)
This prints:
INFO: Finagle version 18.9.1 (rev=63182f815cb20549ced80c99465879609b05d94b) built at 20180926-144723
Response Headers Sent
Map()
Response Headers Received
Map(content-length -> 0)
Using Wireshark, I confirmed that no content-length header is being sent over the wire.
Furthermore, if we then try to re-transmit that response, we got this log message:
Response with a status code of 204 must not have a Content-Length header field thus the field has been removed. Content-Length: 0
@adleong, seems like a bug. Are you interested in digging into it?
I dug into this a little bit more and it appears that this is a bug in Netty's HTTPObjectAggregrator
here. It looks like when netty parses a full http message it sets the Content-Length header for that message even when the http message is of type 204 No Content. Based on RFC 7230 3.3.2
A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational) or 204 (No Content). A server MUST NOT send a Content-Length header field in any 2xx (Successful) response to a CONNECT request (Section 4.3.6 of [RFC7231]).
@dadjeibaah, it looks like you've run into https://github.com/netty/netty/pull/6862.