• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            理解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;
            }

             

            posted on 2011-06-26 03:09 肥仔 閱讀(7169) 評論(0)  編輯 收藏 引用 所屬分類: Web-后臺

            色综合久久综合网观看| 99久久精品免费看国产一区二区三区 | 亚洲午夜无码久久久久| 97久久久精品综合88久久| 国产成人精品久久| 久久中文字幕人妻熟av女| 久久本道伊人久久| 99精品国产免费久久久久久下载 | 久久久精品国产免大香伊 | 亚洲精品无码久久一线| 色综合久久综精品| 日日躁夜夜躁狠狠久久AV| 久久精品国产亚洲5555| 日日噜噜夜夜狠狠久久丁香五月 | 久久er99热精品一区二区| 国产精品综合久久第一页| 久久精品国产亚洲av水果派| 思思久久99热免费精品6| 久久―日本道色综合久久| 午夜精品久久久久久久久| 亚洲性久久久影院| 久久久久人妻一区精品| 91精品国产高清久久久久久91| 亚洲va国产va天堂va久久| 亚洲人成电影网站久久| 九九热久久免费视频| 久久93精品国产91久久综合| 91久久精品视频| 色综合久久综合网观看| 久久久精品免费国产四虎| 精品久久一区二区| 美女写真久久影院| 亚洲国产成人久久精品影视| 久久久久国产一级毛片高清版| 国产婷婷成人久久Av免费高清| 久久综合给合久久国产免费| 人妻少妇久久中文字幕| 久久久久久国产精品免费无码| 精品久久久久久久久午夜福利| 久久亚洲美女精品国产精品| 久久成人国产精品|