Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1070896
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: C/C++

2011-07-21 12:56:03

通常,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
#include

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;
}
阅读(816) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

@sky2011-07-21 12:57:24

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#define err(msg) perror(msg)
#define SA struct sockaddr

static int tcp_listen(const char *ip, int port)
{
        struct sockaddr_in addr;
        int fd;

  &nbs