HTTP協議中的"Transfer-Encoding: chunked"

   通常,HTTP協議中使用Content-Length這個頭來告知數據的長度。然後,在數據下行的過程中,Content-Length的方式要預先在服務器中緩存所有數據,然後所有數據再一股腦兒地發給客戶端。
    如果要一邊產生數據,一邊發給客戶端,WEB 服務器就需要使用"Transfer-Encoding: chunked"這樣的方式來代替Content-Length。

    "Transfer-Encoding: chunked"是這樣編碼的:
HTTP頭
/r/n
/r/n      --連續的兩個/r/n之後就是HTTP體了
16進制值代表的數據長度
/r/n
上面所指的數據長度
/r/n    --每段數據結束後,以/r/n標識

16進制代表的第二段數據
/r/n
XX長度的數據
/r/n

………… (反覆通過這樣的方式表示每次傳輸的數據長度)

0      --數據結束部分用0表示,然後是連續的兩個/r/n
/r/n
/r/n

      下面的代碼演示和如何解析"Transfer-Encoding: chunked"的數據:
//test_chunked.cpp
#include <stdio.h>
#include <string.h>

int Hex2Int(const char* str)
{
    int nResult = 0;
    while (*str!='/0')
    {
        switch (*str)
        {
        case '0'...'9':
            nResult = nResult*16 + *str-'0';
            break;
        case 'a'...'f':
            nResult = nResult*16 + *str-'a'+10;
            break;
        case 'A'...'F':
            nResult = nResult*16 + *str-'A'+10;
            break;
        default:
            return -1;
            break;
        }
        str++;
    }
    return nResult;
}

#define COPY_STRING(dst, src, src_len) do{memcpy((dst), (src), (src_len)); dst[(src_len)]='/0';}while(0);

void test(const char* file)
{
    //
    const int BUFFER_SIZE = 1024*10;
    char* buf = new char[BUFFER_SIZE];
    FILE* fp = fopen(file, "rb");
    if (NULL==fp)
    {
        printf("open file error/n");
        return;
    }
    int nLen = fread(buf, 1, BUFFER_SIZE, fp);
    fclose(fp);
    fp = NULL;
    buf[nLen] = '/0';
    //
    char* pBody = strstr(buf, "/r/n/r/n");
    if (NULL==pBody)
    {
        return;
    }
    pBody += 4;
    FILE* fDst = fopen("result.txt.gz", "ab");
    //下面開始解析
    int nBytes;
    char* pStart = pBody;
    char* pTemp;
    char temp[10];
    do
    {
        pTemp = strchr(pStart, '/r');
        if (NULL==pTemp)
        {
            printf("格式錯誤!/n");
            break;
        }
        nLen = pTemp-pStart;
        COPY_STRING(temp, pStart, nLen);
        nBytes = Hex2Int(temp);
        pStart = pTemp + 2;
        //下面寫入到另一個文件
        if (nBytes>0)
        {
            if (nBytes!=fwrite(pStart, 1, nBytes, fDst))
            {
                printf("write error!/n");
                break;
            }
            pStart += nBytes + 2;
        }
    } while(nBytes>0);
    fclose(fDst);
    fDst = NULL;
    delete[] buf;
    buf = NULL;
}

int main()
{
    test("chunked.txt");
    return 1;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章