内容长度正在被剥离 [英] Content-Length is being stripped

查看:36
本文介绍了内容长度正在被剥离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 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)类 MyHandler:@mimerender(默认 = 'json',json = render_json,)def POST(自我):数据 = "abcd"提高 web.created(data, headers={'Content-Length': len(data)})

解决方案

如果数据是分块发送的(Transfer-Encoding: chunked),那么Content-Length 标头必须省略,根据 RFC 2616:

<块引用>

  1. [截图]

  2. 如果存在 Transfer-Encoding 标头字段(第 14.41 节)并且具有除身份"以外的任何值,则传输长度为通过使用分块"传输编码(第 3.6 节)定义,除非通过关闭连接来终止消息.

  3. 如果存在 Content-Length 头字段(第 14.13 节),则其OCTET 中的十进制值表示实体长度和传输长度.不得发送 Content-Length 头字段如果这两个长度不同(即,如果传输编码头字段存在).如果收到的消息同时包含Transfer-Encoding 头域和 Content-Length 头域,后者必须被忽略.

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

解决方案

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.

这篇关于内容长度正在被剥离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆