内容长度被剥离

发布于 10-30 15:53 字数 601 浏览 3 评论 0 原文

我正在使用 webpy 0.34,python 2.6.6。我也在使用 mimerender。我试图在我的 http 响应中包含内容长度,但由于某种原因,标头被删除。我说删除是因为我可以很好地创建自定义标头,并且我可以在客户端上看到这些标头。但是当我尝试设置内容长度时,标头永远不会到达客户端。我尝试将标头包含在 web.created 对象中(如图所示),并且我还尝试使用

web.header('Content-Length', len(data))

我做错了什么和/或不了解此代码的工作原理?

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)})

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)})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-11-06 15:53:25

如果数据以分块形式发送(Transfer-Encoding: chunked),则必须省略 Content-Length 标头,根据 RFC 2616

  1. [剪断]
  1. 如果存在传输编码头字段(第 14.41 节)并且
    具有除“identity”之外的任何值,则传输长度为
    通过使用“分块”传输编码来定义(第 3.6 节),
    除非消息通过关闭连接而终止。
  1. 如果存在 Content-Length 标头字段(第 14.13 节),则其
    OCTET 中的十进制值同时表示实体长度和实体长度
    传输长度。不得发送 Content-Length 标头字段
    如果这两个长度不同(即,如果传输编码
    存在标头字段)。如果收到一条消息
    Transfer-Encoding 头字段和 Content-Length 头字段,
    后者必须被忽略。

If the data is sent as chunked (Transfer-Encoding: chunked), then the Content-Length header must be omitted, as per RFC 2616:

  1. [snip]
  1. 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.
  1. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文