问题
I'm using webpy 0.34, python 2.6.6. I'm also using mimerender. I am trying to include the content-length in my http response, but for some reason the header is being removed. I say removed because I can create custom headers just fine, and I can see those headers on the client. But when I try to set content-length, the header never makes it to the client. I've tried including the header in the web.created object (as shown) and I've also tried using
web.header('Content-Length', len(data))
What am I doing wrong and/or not understanding about how this code works?
render_json = lambda **args: json.JSONEncoder().encode(args)
class MyHandler:
@mimerender(
default = 'json',
json = render_json,
)
def POST(self):
data = "abcd"
raise web.created(data, headers={'Content-Length': len(data)})
回答1:
If the data is sent as chunked (Transfer-Encoding: chunked
), then the Content-Length
header must be omitted, as per RFC 2616:
[snip]
If a Transfer-Encoding header field (section 14.41) is present and has any value other than "identity", then the transfer-length is defined by use of the "chunked" transfer-coding (section 3.6), unless the message is terminated by closing the connection.
If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored.
来源:https://stackoverflow.com/questions/5553151/content-length-is-being-stripped