Content-Length is being stripped

南楼画角 提交于 2019-12-19 04:13:14

问题


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:

  1. [snip]

  2. 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.

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!