/* john pfeiffer january 2007 my c version of an http client */
#include <stdio.h>
#include <stdlib.h> /* for file functions */
#include <time.h> /* for inserting the date in the output file */
#include <winsock.h> /*important to load object files C:\devcpp\Lib\libwsock32.a*/
#define BUFFERMAX 2048 /* what is the right buffer size? */
/* currently ignoring the possibility that the </head> could fall exactly on
a 1024 byte border -> e = 1023 and then a = 0 of the next buffer */
void set_output_filename(char filename[20])
{ char tmp[20];
long n = time(NULL);
strcpy(tmp,ctime(&n));
filename[0] = tmp[22];
filename[1] = tmp[23];
filename[2] = tmp[4];
filename[3] = tmp[5];
filename[4] = tmp[6];
filename[5] = tmp[8];
filename[6] = tmp[9];
filename[7] = '\0'; /* strcat needs the target to already have a \0 */
strcat(filename,"-slashdot.htm");
/* strcat(filename,"-newsgoogle.htm"); */
}/* end of func set_filename */
void setkeyword(char keyword[10])
{ keyword[0] = '<';
keyword[1] = '/';
keyword[2] = 'h';
keyword[3] = 't';
keyword[4] = 'm';
keyword[5] = 'l';
keyword[6] = '>';
keyword[7] = '\0'; /* needed for strlen to work properly */
}/* end of func setkeyword */
void write_buffer_tofile( char buffer[BUFFERMAX], FILE* ofp )
{ char c;
int i=0;
c=buffer[i];
while( (c != -1) && (i<BUFFERMAX+1)) /* extra test to not write too much */
{
putc(c,ofp);
i++;
c=buffer[i];
};
}/* end of func write_buffer_tofile */
void zerobuffer(char buffer[BUFFERMAX])
{ for( int i =0; i< BUFFERMAX; i++)
{ buffer[i] = -1; }
}/* end of func zerobuffer */
void exit_all( SOCKET s )
{ shutdown(s,1); /*better than close() */
closesocket(s);
WSACleanup();
}/* end func exit_all() Cleans up the WSA & closes the socket */
int main(void)
{
int bytes_rcvd=0;
int buffersused=0;
char buffer[BUFFERMAX];
char server_response_code[4]; /* 250- */
struct sockaddr_in a; /* sockaddr_in(ternet) */
char outputfilename[20];
FILE* ofp;
WSADATA ws;
int d = WSAStartup(0x0202,&ws);
SOCKET s = socket(AF_INET,SOCK_STREAM,0); /* get handle to socket (should be > 0) */
a.sin_family = AF_INET; /*internet protocol addressing */
a.sin_port = htons(80); /*connect to port 80 */
a.sin_addr.s_addr = inet_addr("66.35.250.151"); /* slashdot.org */
/* a.sin_addr.s_addr = inet_addr("72.14.203.99"); /* news.google.com */
printf("WSAStartup = %d, ",d);
d = connect( s, (struct sockaddr *) &a, sizeof(a) ); /* must cast */
printf(" SOCKET = %d, Connected? %d (should be: 0)\n",s,d);
if( d < 0 ){ printf("\nERROR GETTING SOCKET\n"); exit(1); }
/* set_output_filename(outputfilename);
// printf("\n%s\n",outputfilename);
ofp = fopen(outputfilename, "w"); */
ofp = fopen("testrange.htm", "w");
if( ofp == NULL ){ printf("file error\n"); getchar(); exit(1); }
strcpy(buffer,"GET / HTTP/1.0\r\n");
strcat(buffer,"host: slashdot.org\r\n");
strcat(buffer,"Range: bytes=-2048\r\n");
strcat(buffer,"User-Agent: Random\r\n");
/* strcat(buffer,"host: news.google.com\r\n"); */
puts(buffer); /*displays commands for console window*/
strcat(buffer,"\r\n"); /* denotes end of msg */
send(s,buffer,sizeof(buffer),0); /* the zero at the end? */
char keyword[10];
int keywordsize;
int iBuf=0;
int iKey=0;
bool match = false;
bool exit_loop = false;
bool endofbuffer = false;
setkeyword(keyword);
keywordsize = strlen(keyword);
buffersused = 0;
do{
iBuf=0;
zerobuffer(buffer);
buffersused = buffersused+1;
bytes_rcvd = recv(s,buffer,sizeof(buffer),0);
/* printf("\n%d bytes recvd, xfer %d chars to buffer, %d buffers used,
press any key\n",bytes_rcvd,bytes_rcvd,buffersused);
getchar();
puts(buffer); "." represents one buffer = 2048 bytes
fputs(buffer,ofp);
*/
printf(".");
write_buffer_tofile(buffer,ofp);
while( ( iBuf < bytes_rcvd) && (match != true) )
{
if( buffer[iBuf]==keyword[0] ) // </head>
{ iBuf=iBuf+1;
if( buffer[iBuf]==keyword[1] )
{ iBuf=iBuf+1;
if( buffer[iBuf]==keyword[2] )
{ iBuf=iBuf+1;
if( buffer[iBuf]==keyword[3] )
{ iBuf=iBuf+1;
if( buffer[iBuf]==keyword[4] )
{ iBuf=iBuf+1;
if( buffer[iBuf]==keyword[5] )
{ iBuf=iBuf+1;
if( buffer[iBuf]==keyword[6] )
{ iBuf=iBuf+1;
match=true;
}
}
}
}
}
}
}
iBuf=iBuf+1;
}; /* end of while */
}while( (buffersused < 80) && (match!=true) ); /* keep getting data until 8 buffers */
fclose(ofp);
exit_all(s);
printf("\ndownload of site done, </html> match = %d",match);
printf("\napprox buffers used %d with %d buffersize",buffersused, BUFFERMAX);
getchar();
return 0;
} /*end main*/