Thread: downloading a file

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    downloading a file

    i have a problem that it alawys says that the server couldnt understand i dont rlly understand http protocol i quite got some of the picture by packet sniffing but i didnt acctually read the RFC
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    #pragma comment( lib, "ws2_32" )
    char *Http="http://www.google.com.com";
    int Port = 80;//default port for http
    void Send_Packet(SOCKET sock,char *packets, ... )//by a59 with lil touch by me
    {
    	char buffersize[400];
    
    	va_list varagums;
    
    	va_start( varagums, packets );
    
    	vsprintf( buffersize, packets, varagums );
    	
    	va_end(varagums);
    
    	send(sock,buffersize,strlen(buffersize),0);
    }
    
    int Setup_it()
    {
    	SOCKET http;
    	SOCKADDR_IN Info;
    	LPHOSTENT hostEntry;
    	int error;
    
    	/*Resolve it*/
    	hostEntry=gethostbyname(Http);
    
    	if(!hostEntry)
    		return FALSE;
    	//cr8 the socket
    	http=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(http==INVALID_SOCKET)
    		return FALSE;
    	Info.sin_family = AF_INET;
    	Info.sin_addr=*((LPIN_ADDR)*hostEntry->h_addr_list);
    	Info.sin_port=htons(Port);
    	
    	error=connect(http,(SOCKADDR*)&Info,sizeof Info);
    	if(error==INVALID_SOCKET)
    		return FALSE;
    	return http;
    }
    BOOL download_it()
    {
    	char path[100];
    	char buffer[100]={0};
    	FILE *fp;
    	SOCKET http;
    	http=Setup_it();
    	if(!http) return FALSE;
    	GetTempPath(sizeof path,path);
    	fp=fopen(path,"w");
    	Send_Packet(http,"GET /kosomack1234/exe.exe HTTP/1.1\r\n");
    	Send_Packet(http,"Host: www.google.com\r\n");
    	Send_Packet(http,"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5\r\n");
    	Send_Packet(http,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
    	Send_Packet(http,"Accept-Language: en-us,en;q=0.5\r\n");
    	Send_Packet(http,"Accept-Encoding: gzip,deflate\r\n");
    	Send_Packet(http,"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
    	Send_Packet(http,"Keep-Alive: 300\r\n");
    	Send_Packet(http,"Connection: keep-alive\r\n");
    	recv(http,buffer,sizeof buffer,0);
    	puts(buffer);
    
    }
    int main(void)
    {
    	WSADATA Client;
    	WSAStartup(MAKEWORD(2,2),&Client);
    	download_it();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to send a double "\r\n" to indicate you are done sending the header. So you should have:
    Code:
    Send_Packet(http,"Connection: keep-alive\r\n\r\n");

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh thanks it says found now so what should i do to copy the file to my folder i fprintf buffer or what?

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    also shouldnt it put OK not Found in packets since when i download something my packet sniff says OK not Found ?

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by elwad View Post
    also shouldnt it put OK not Found in packets since when i download something my packet sniff says OK not Found ?
    English. Clear, comprehensible English. What exactly is your packet sniff showing?

    On a successful request, the server's response will be much like a request. Several lines (separated with CRLF) of header information, a blank line, and then the actual body (which may be encoded). Something like:
    Code:
    HTTP/1.1 200 OK
    Content-Type: image/png
    Server: gws
    Content-Length: 10000
    (other lines)
    (blank line)
    (content)
    Where each of those lines is terminated with a CRLF (\r\n).
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    HTTP/1.1 302 Found
    Date: Wed, 03 Jun 2009 08:20:48 GMT
    Server: Apache
    Content-Length: 0
    Connection: close
    Content-Type: text/html; charset=UTF-8

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i dont Get OK

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by elwad View Post
    i dont Get OK
    Then read the RFC. Don't expect other people to "tell you along the way". HTTP 302 is often used for redirections. I doubt that "/kosomack1234/exe.exe" actually exists at google.com.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i thought that would get the html of it

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't do this that much, but the only line I send is this one:
    Code:
    Send_Packet(http,"GET /kosomack1234/exe.exe HTTP/1.1\r\n\r\n");
    with two \r\n's on the end*, none of the rest of the optional stuff ("user-agent", etc) and it always seems to work (except when you need to pose as a specific user agent).

    Have you tried with a different server and file? Try to grab just a normal html page or an image somewhere...

    *and I say HTTP/1.0, if that matters
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i did with one manga it worked but without double \r\n in the GET when i added them it said bad request
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    #pragma comment( lib, "ws2_32" )
    char *Http="http://www.onemanga.com/";
    int Port = 80;//default port for http
    void Send_Packet(SOCKET sock,char *packets, ... )//by a59 with lil touch by me
    {
    	char buffersize[400];
    
    	va_list varagums;
    
    	va_start( varagums, packets );
    
    	vsprintf( buffersize, packets, varagums );
    	
    	va_end(varagums);
    
    	send(sock,buffersize,strlen(buffersize),0);
    }
    
    int Setup_it()
    {
    	SOCKET http;
    	SOCKADDR_IN Info;
    	LPHOSTENT hostEntry;
    	int error;
    
    	/*Resolve it*/
    	hostEntry=gethostbyname(Http);
    
    	if(!hostEntry)
    		return FALSE;
    	//cr8 the socket
    	http=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(http==INVALID_SOCKET)
    		return FALSE;
    	Info.sin_family = AF_INET;
    	Info.sin_addr=*((LPIN_ADDR)*hostEntry->h_addr_list);
    	Info.sin_port=htons(Port);
    	
    	error=connect(http,(SOCKADDR*)&Info,sizeof Info);
    	if(error==INVALID_SOCKET)
    		return FALSE;
    	return http;
    }
    BOOL download_it()
    {
    	char path[100];
    	char buffer[100]={0};
    	FILE *fp;
    	SOCKET http;
    	http=Setup_it();
    	if(!http) return FALSE;
    	GetTempPath(sizeof path,path);
    	strcat(path,"\\exe.exe");
    	fp=fopen(path,"w");
    
    	Send_Packet(http,"GET /html HTTP/1.1\r\n"
    	                 "Host: www.onemanga.com\r\n"
    					 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5\r\n"
    					 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    					 "Accept-Language: en-us,en;q=0.5\r\n"
    					 "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
    					 "Keep-Alive: 300\r\n"
    					 "Connection: keep-alive\r\n\r\n"/*needed to send double one to indicate that i m finished sending the header*/
    					 );
    	
    	recv(http,buffer,sizeof buffer,0);
    
    	puts(buffer);
    
    
    	return TRUE;
    }
    int main(void)
    {
    	WSADATA Client;
    	WSAStartup(MAKEWORD(2,2),&Client);
    	download_it();
    	return 0;
    }

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elwad View Post
    yah i did with one manga it worked but without double \r\n in the GET when i added them it said bad request
    Okay. I saw your note about putting the double /r/n at the end, I had forgotten the reason for that. My point is that in my (limited) experience, you do not have to send more than just the one line with GET in it. Getting rid of the unnecessary stuff will eliminate the possibility that it is that which is causing the problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i m srry to ask this i know i should read RFC and all but wanted to ask whats most important information i need to send to download a File instead of just sending random stuff i saw in my packet sniffer...

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elwad View Post
    i m srry to ask this i know i should read RFC and all but wanted to ask whats most important information i need to send to download a File instead of just sending random stuff i saw in my packet sniffer...
    Like I said in post #10, AFAIK you only have to send one line (the one with GET in it). Some servers may want more (eg, some of them will only accept certain user-agents) but *most* of them do not.

    If you are getting a response like you said earlier, even if it is an error (file not found) your request has been correctly received and processed.

    I just downloaded this (using just a one line GET), so the address works, try it:

    http://www.cearta.ie/wp-content/uplo...ello_world.png
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by MK27
    Like I said in post #10, AFAIK you only have to send one line (the one with GET in it). Some servers may want more (eg, some of them will only accept certain user-agents) but *most* of them do not.
    If any server is letting you get away with 1 line requests, then shame on it - such a request is invalid. At the very minimum, you must specify a Host header. Ie:
    Code:
    GET / HTTP/1.1
    Host: whoever.com
    Further to your example, you did? What exactly did you send:
    Code:
    GET /wp-content/uploads/2006/hello_world.png HTTP/1.1
    
    HTTP/1.1 400 Bad Request
    ...

    You're getting a 302 - it's essentially a "it's over here" response. You'll have to send another request with a different URI. Reading the RFC:
    Quote Originally Posted by HTTP
    10.3.3 302 Found

    The requested resource resides temporarily under a different URI.
    Since the redirection might be altered on occasion, the client SHOULD
    continue to use the Request-URI for future requests. This response
    is only cacheable if indicated by a Cache-Control or Expires header
    field.

    The temporary URI SHOULD be given by the Location field in the
    response. Unless the request method was HEAD, the entity of the
    response SHOULD contain a short hypertext note with a hyperlink to
    the new URI(s).

    If the 302 status code is received in response to a request other
    than GET or HEAD, the user agent MUST NOT automatically redirect the
    request unless it can be confirmed by the user, since this might
    change the conditions under which the request was issued.

    Note: RFC 1945 and RFC 2068 specify that the client is not allowed
    to change the method on the redirected request. However, most
    existing user agent implementations treat 302 as if it were a 303
    response, performing a GET on the Location field-value regardless
    of the original request method. The status codes 303 and 307 have
    been added for servers that wish to make unambiguously clear which
    kind of reaction is expected of the client.
    Additionally, your network code is poor. You need to loop that recv() until you have _all_ of the data. Have I mentioned yet that there are spiffy libraries that do all of this... like libcurl?
    Last edited by Cactus_Hugger; 06-03-2009 at 04:36 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM