This is Data Receiving functionality Code from http server, Perfectly Working.. But i want to add pause and Resume to this function..
can anybody suggest me how to do that ??
Note: Ignore variable Declaration.
int rest=0,size=0,count=0;
memset(buffer,'\0',sizeof(buffer));
do {
rc=read(sockfd,buffer,1500); //Reading from socket connection
if(rest==0)
{ /*Extracting Content length*/
data=strstr((char*)buffer,"Content-Length");
if(data!=NULL)
{
value=atoi((char*)data+15);data_rest=value;
rest=1;
}
}
if(count==0)
{ /*Extracting Header*/
content=strstr(buffer,"\r\n\r\n");
if(content!=NULL)
{
printf("FOUND Header END\n");
content=content+4;count=1;
/*copying DATA to the file which has same Extension*/
data_wrote=fwrite(content,sizeof(char),rc-(content-buffer),fp);
}
}
else
{
content=buffer;
/*copying DATA to the file which has same Extension*/
data_wrote=fwrite(content,sizeof(char),rc,fp);
}
size=size+rc;
total_wrote=total_wrote+data_wrote;
printf("Total size = %d\n",size);
memset(buffer,'\0',sizeof(buffer));
} while(rc>0); /*until end of file*/
Here is the process I would use:
Send the initial request with
Range: bytes=0-HTTP/1.1 request header.If the server responds with a
HTTP/1.1 206 Partial content, you know you can resume the connection later.Save the
ETagresponse header, as it should stay unmodified unless the content on the server changes, and/or theContent-Type, andLast- Modifiedheaders.Note that the
Content-Rangeresponse header specifies the offsets of the bytes in this response, with the final number being the total length of the full content, andContent-Lengthonly specifies the length of the current response (not the length of the original content).Interrupt the transmission by simply closing the connection.
Send the continuation request with
Range: bytes=N-HTTP/1.1 request header, whereNis the offset to the first byte still missing from the local copy.If the server does not respond with a
HTTP/1.1 206 Partial content, you have no recourse but have to download the entire content again (using normal HTTP request processing).If the
ETag,Content-Type, orLast-Modifiedresponse headers mismatch, then it is possible the content was modified on the server, and you should drop this connection and download the full request instead.The
Content-Rangeresponse header specifies the offsets of the bytes in this response, with the final number being the total length of the full content, andContent-Lengthonly specifies the length of the current response (not the length of the original content).In all cases it is possible the server uses the
chunkedcontent transfer encoding, so you must be prepared to handle that, toom since the request is HTTP 1.1. See the RFC2616 for details.When you test this, I recommend you use a simple Bash script to connect to some URL using your custom headers, and to output the response headers to standard error and message content to standard output:
The above is not a HTTP client, and does not do chunked transfers, for example; it is just a debugging tool you can use to see exactly the data that flows from the server to the client side.