Changeset 24286 for titan/titan/sock.h


Ignore:
Timestamp:
10/13/13 22:02:15 (10 years ago)
Author:
nit
Message:

[titan] add chunk support to gethttp

File:
1 edited

Legend:

Unmodified
Added
Removed
  • titan/titan/sock.h

    r24270 r24286  
    585585}
    586586
     587int getchunkedlen(int sock, int timeout)
     588{
     589        int ret = 0, end = 0;;
     590        char chunked[16] = {'\0'};
     591        int chunkedid = 0;
     592
     593        while(chunkedid < 15)
     594        {
     595                unsigned char c = '\0';
     596
     597                ret = sockreceive(&sock, &c, 1, timeout * 1000);
     598                if(ret != 0)
     599                {
     600                        printf("no client data in buffer");
     601                        break;
     602                }
     603
     604                if(c == ';') break;
     605
     606                chunked[chunkedid] = c;
     607                chunkedid++;
     608                if(chunkedid > 2 && c == '\n')
     609                        break;
     610        }
     611
     612        return(strtol(chunked, NULL, 16));
     613}
     614
    587615//flag 0: output without header
    588616//flag 1: output with header
     
    591619{
    592620        int sock = -1, ret = 0, count = 0, hret = 0, freeheader = 0, gzip = 0;
    593         int headerlen = 0;
     621        int headerlen = 0, chunkedlen = 0, chunked = 0;
    594622        unsigned int len = 0, maxret = 0;
    595623        char *ip = NULL;
     
    680708        while(pbuf - tmpbuf < MINMALLOC)
    681709        {
    682                 unsigned char c;
     710                unsigned char c = '\0';
    683711
    684712                ret = sockreceive(&sock, &c, 1, timeout * 1000);
     
    736764                else if(flag == 1) gzip = headerlen;
    737765        }
     766        if(strstr(tmpbuf, "Transfer-Encoding: chunked") != NULL)
     767        {
     768                chunked = 1;
     769                chunkedlen = getchunkedlen(sock, timeout);
     770                if(chunkedlen > MINMALLOC) chunked = 0;
     771        }
     772               
    738773
    739774        if(flag == 0) headerlen = 0;
    740         while((ret = sockread(sock, (unsigned char*)tmpbuf + headerlen, 0, MINMALLOC - headerlen, timeout * 1000, 0)) > 0)
     775        if(chunked == 0) chunkedlen = MINMALLOC - headerlen;
     776        while((ret = sockread(sock, (unsigned char*)tmpbuf + headerlen, 0, chunkedlen, timeout * 1000, 0)) > 0)
    741777        {
    742778                maxret += ret;
     
    762798                }
    763799                memset(tmpbuf, 0, ret);
     800               
     801                if(chunked == 1)
     802                {
     803                        chunkedlen = getchunkedlen(sock, timeout);
     804                        if(chunkedlen > MINMALLOC) chunked = 0;
     805                }
     806                if(chunked == 0) chunkedlen = MINMALLOC - headerlen;
    764807        }
    765808        if(ret < 0)
Note: See TracChangeset for help on using the changeset viewer.