CFNetwork读取http标头Transfer-Encoding Identity,但wireshark显示chunked

时间:2012-10-16 18:38:35

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

我正在使用CFNetwork读取标题,我正在尝试阅读我的请求是什么类型的“Transfer-Encoding”。应该是“分块”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);
        }
    }

提前感谢您的帮助。

1 个答案:

答案 0 :(得分: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”。