【问题标题】:CFNetwork reads http header Transfer-Encoding Identity but wireshark shows chunkedCFNetwork 读取 http 标头 Transfer-Encoding Identity 但wireshark 显示分块
【发布时间】:2012-10-06 22:37:18
【问题描述】:

我正在使用 CFNetwork 读取标头,并尝试读取我的请求是什么类型的“传输编码”。应该是“分块” Wireshark 显示正确的请求“Transfer-Encoding: chunked”,但实际使用 CFNetwork 的代码将请求头重写为“Transfer-Encoding: Identity”

有人知道为什么会这样吗?

这是我读取标题的代码:

if (r->_headers) {
        CFStringRef header_return = CFStringCreateWithFormat (kCFAllocatorDefault, NULL, CFSTR("%@: %@\r\n"), key, value);
        if (header_return) {            
            char temp[256];
            CFStringGetCString(header_return, temp, sizeof(temp), kCFStringEncodingUTF8);

            char *trans_enc = NULL;
            if (pico_http_internal_native_header_get(temp, "Transfer-Encoding:", &trans_enc)) {
                if (strcmp(trans_enc, "chunked") == 0) {  // <-- This always says "Identity"
                    r->_chunked = true; // Never hit, but wireshark shows it would  be correct
                }
            }

            r->_headers(r->_context, temp, strlen(temp));
            pico_cfrelease(header_return);
        }
    }

提前感谢您的帮助。

【问题讨论】:

  • 我刚刚遇到了同样的问题。 Chrome 显示 chunked,而 Mac 上的 Safari 显示 identity。还不太明白。如果您发现有用的东西,请告诉我。
  • 嗨@Eye 我添加了我最终在我的代码中使用的答案。希望它有所帮助。

标签: iphone http http-headers chunked-encoding cfnetwork


【解决方案1】:

我使用以下逻辑来确定它是否被分块:

// response header info
    if(CFHTTPMessageIsHeaderComplete(cf_response)) {
        CFDictionaryRef headers = CFHTTPMessageCopyAllHeaderFields(cf_response);
        if (headers) {
            CFDictionaryApplyFunction(headers, pico_http_internal_cfnetwork_header_apply_callback, r);
            if((CFDictionaryContainsKey(headers, CFSTR("Content-Length")) == false) && (CFDictionaryContainsKey(headers, CFSTR("Transfer-Encoding")) == true)
               && (CFDictionaryContainsValue(headers, CFSTR("Keep-Alive")) == true)) {
                r->_chunked = true;
                }
     }

基本上,如果标头中没有“Content-Length”,但标头报告“Transfer-Encoding”,并且报告“Keep-Alive”,则它是一个分块响应。这些是分块响应和非分块响应之间的主要区别。分块响应不使用“Content-Length”,但设置了“Transfer-Encoding”和“Keep-Alive”。

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 2014-03-30
    • 2017-10-03
    • 2020-04-17
    • 2015-09-04
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多