7 #if defined(__MINGW32__)
8 typedef unsigned int uint
;
11 #include <sys/socket.h>
13 #include <sys/socket.h>
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
25 void parse_url( char *input
, char *host
, int *port
, char *pathname
, char* outfile
)
29 p
= strrchr(input
,'/')+1; /* find start of pathname */
33 *port
= 80; /* default port number */
34 if (!strncmp( input
, "http://", 7 ))
37 if (!strncmp( input
, "http:", 5 )) input
+= 5;
39 p
= strchr(input
,'/'); /* find start of pathname */
40 if (p
) { /* pathname found */
41 strcpy( pathname
, p
); /* rest is pathname */
42 *p
= 0; /* terminator for host name */
43 } else { /* else: no path */
44 strcpy( pathname
, "/" ); /* path name defaults to '/' */
47 p
= strchr(input
,':'); /* port number specified ? */
48 if (p
) { /* port found */
49 *p
= 0; /* terminator for host name */
50 p
++; /* move over '\0' */
51 *port
= atoi( p
); /* convert port to int */
54 strcpy( host
, input
);
55 } /* end of parse_url */
58 int main(int argc
, char ** argv
){
70 struct sockaddr_in address
;
79 outfile
= malloc(256);
80 parse_url (url
, host
, &port
, pathname
, outfile
);
82 if(argc
> 2) outfile
= argv
[2];
86 printf( "host = %s\n", host
);
88 printf( "port = %d\n", port
);
89 printf( "path = %s\n", pathname
);
90 printf( "outfile = %s\n", outfile
);
94 f
= open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0644);
96 ha
= inet_addr ( host
);
98 hp
= gethostbyname ( host
);
101 printf( "Host '%s' not found\n", host
);
104 ha
= *(long *)*hp
->h_addr_list
;
107 address
.sin_addr
.s_addr
= ha
;
108 address
.sin_family
= AF_INET
;
109 address
.sin_port
= htons(port
);
111 if ( ( fd
= socket(AF_INET
, SOCK_STREAM
, 0 ) ) <= 0 )
113 perror( "Socket failed" );
118 printf( "Connecting to %s\n", inet_ntoa(address
.sin_addr
) );
120 if ( connect(fd
, (struct sockaddr
*)&address
, (int)sizeof(address
)) != 0 )
122 perror( "Connect failed" );
127 printf( "Connected\n") ;
128 sprintf( buf
, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", pathname
, host
);
130 write(fd
, buf
, strlen(buf
));
132 while((nread
= read(fd
, buf
, 1)) > 0){
133 if(buf
[0] == '\n' && (c
== '\n')) break;
134 if(buf
[0] == '\r') continue;
138 while ((nread
= read(fd
, buf
, sizeof(buf
)))>0){
139 write(f
, buf
, nread
);
141 printf( "Downloaded\n") ;
183 int main(int argc
, char ** argv
)
201 fprintf( stderr
, "Usage: wwwget [-proxy proxyhost] url [outfile]\n" );
205 if (argv
[1][0] == '-')
207 if (!strcmp(argv
[1], "-proxy"))
209 parse_url( argv
[2], host
, &port
, pathname
, &outfile
);
210 strcpy( pathname
, argv
[3] );
214 printf("Unknown option: %s\n", argv
[1] );
221 parse_url( url
, host
, &port
, pathname
, &outfile
);
225 printf( "host = %s\n", host
);
227 printf( "port = %d\n", port
);
228 printf( "path = %s\n", pathname
);
229 printf( "outfile = %s\n", outfile
);
233 FILE *f
= fopen("wwwini","r");
237 if (!feof(f
)) strcat( buf
, buf2
);
241 strcat(buf
,"\r\n\r\n");
243 rlen
= (int)strlen(buf
);
244 if ( send(fd
, buf
, rlen
, 0) <= 0 )
246 perror("send failed");
256 starttime
= gettimeofday();
257 while ( (rlen
= recv(fd
, buf
, sizeof(buf
), 0 )) > 0) {
261 secs
= (long)((gettimeofday() - starttime
)/1000000);
262 if (secs
== 0) secs
= 1;
264 left
= (size
-count
)/(count
/secs
);
267 if (!header
&& outfile
)
268 printf( "Received %ld bytes in %ld seconds (%ld bytes/sec;"
269 "%ld seconds left)\r",
270 count
, secs
, (count
/secs
), left
);
272 while (header
&& rlen
) {
277 if (lf
) { /* second lf in a row */
282 strncpy( cmpbuf
, p
+1, 30 );
283 for (i
=0;i
<30;i
++) cmpbuf
[i
] = tolower(cmpbuf
[i
]);
284 if (!strncmp( cmpbuf
, "content-length:", 15 ))
286 q
= strchr( p
+1, ':');
290 if (*p
!= '\r') lf
= 0;
307 if (wrcount
> 4096) wrcount
= 4096;
308 write( f
, p
, (short)wrcount
, &countwritten
);
309 if (countwritten
< wrcount
) exit (2);
310 rlen
-= countwritten
;
311 if (rlen
< 0) exit(2);
318 if (rlen
<0) perror( "recv failed: ");
319 if (outfile
) close(f
);