int http_parser::collapse_chunk_headers(char* buffer, int size) const { if (!chunked_encoding()) return size; // go through all chunks and compact them // since we're bottled, and the buffer is our after all // it's OK to mutate it char* write_ptr = (char*)buffer; // the offsets in the array are from the start of the // buffer, not start of the body, so subtract the size // of the HTTP header from them int offset = body_start(); std::vector<std::pair<size_type, size_type> > const& c = chunks(); for (std::vector<std::pair<size_type, size_type> >::const_iterator i = c.begin() , end(c.end()); i != end; ++i) { TORRENT_ASSERT(i->second - i->first < INT_MAX); TORRENT_ASSERT(i->second - offset <= size); int len = int(i->second - i->first); if (i->first - offset + len > size) len = size - int(i->first) + offset; memmove(write_ptr, buffer + i->first - offset, len); write_ptr += len; } size = write_ptr - buffer; return size; }
int main(int argc, char **argv) { char *streamfile=NULL; FILE *stream=NULL; FILE *content=NULL; size_t len=1024; char data[len]; long offset=0; int bytes=0; int c=0; if(argc != 2) usage(); streamfile = argv[1]; if((stream = fopen(streamfile,"r")) == NULL) usage(); if(!chunked_encoding(stream)){ printf("Transfer-Encoding: chunked - not found in "); printf("\"%s\"\n", streamfile); exit(0); } while(!feof(stream)){ if((bytes=fread(data,1,len,stream)) == 0) break; for(c=0;c<(bytes-1);c++){ if(data[c] == 0x0D && data[c+1] == 0x0A) { data[c]='\0'; offset++; break; } offset++; } content = disposition(data); if(content != NULL) break; fseek(stream,offset,SEEK_SET); } offset--; fseek(stream,offset,SEEK_SET); while(!feof(stream)){ if((bytes=fread(data,1,len,stream)) == 0) break; for(c=0;c<(bytes-4);c++){ if(data[c] == 0x0D && data[c+1] == 0x0A && data[c+2] == 0x0D && data[c+3] == 0x0A ){ offset+=2; fseek(stream,offset,SEEK_SET); extract(stream,content); fclose(stream); break; } offset++; } } return 0; }