update default blocklist url (Closes #4)
[mldonkey.git] / config / wget.c
blob12ec2f40efc082195d6808b24362d3fd595324a6
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
7 #if defined(__MINGW32__)
8 typedef unsigned int uint;
9 #include <winsock.h>
10 #include <netdb.h>
11 #include <sys/socket.h>
12 #else
13 #include <sys/socket.h>
14 #include <netdb.h>
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
17 #endif
21 #include <fcntl.h>
22 #include <errno.h>
25 void parse_url( char *input, char *host, int *port, char *pathname, char* outfile )
27 char *p;
29 p = strrchr(input,'/')+1; /* find start of pathname */
31 strcpy(outfile, p);
33 *port = 80; /* default port number */
34 if (!strncmp( input, "http://", 7 ))
35 input += 7;
36 else
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){
60 if(argc > 1)
64 char *url = argv[1];
65 char host[256];
66 char pathname[4096];
67 int port = 80;
68 char *outfile;
69 int f;
70 struct sockaddr_in address ;
71 struct hostent *hp;
72 unsigned long ha;
73 int fd ;
74 char buf[2000];
75 int nread;
76 char c = 0;
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 );
97 if (ha == -1) {
98 hp = gethostbyname ( host );
99 if (!hp)
101 printf( "Host '%s' not found\n", host );
102 exit(0);
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" );
114 close(f);
115 exit(-1) ;
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" );
123 close(f);
124 exit(-1) ;
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;
135 c = buf[0];
138 while ((nread = read(fd, buf, sizeof(buf)))>0){
139 write(f, buf, nread);
141 printf( "Downloaded\n") ;
143 close(f);
144 close(fd);
145 return 0;
175 #if 0
176 int f;
177 char *outfile;
178 int rlen;
179 long count;
180 int port = 80;
183 int main(int argc, char ** argv)
185 char *url;
186 char buf[30000] ;
187 short f;
188 short error;
189 short len;
191 int header;
192 int lf;
193 long long starttime;
194 long secs;
195 long left;
196 long size = 0;
198 outfile = NULL;
200 if (argc < 2) {
201 fprintf( stderr, "Usage: wwwget [-proxy proxyhost] url [outfile]\n" );
202 return 0;
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] );
211 outfile = argv[4];
212 } else
214 printf("Unknown option: %s\n", argv[1] );
215 exit(1);
217 } else
219 url = argv[1];
220 outfile = argv[2];
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");
234 char buf2[200];
235 while (!feof(f)) {
236 fgets(buf2,200,f);
237 if (!feof(f)) strcat( buf, buf2 );
239 fclose(f);
241 strcat(buf,"\r\n\r\n");
243 rlen = (int)strlen(buf);
244 if ( send(fd, buf, rlen, 0) <= 0 )
246 perror("send failed");
247 if (outfile) {
248 close(f);
249 exit(1) ;
253 count = 0;
254 header = 1;
255 lf = 0;
256 starttime = gettimeofday();
257 while ( (rlen = recv(fd, buf, sizeof(buf), 0 )) > 0) {
258 char *p;
259 buf[rlen] = 0;
260 count += rlen;
261 secs = (long)((gettimeofday() - starttime)/1000000);
262 if (secs == 0) secs = 1;
263 if (size > 0)
264 left = (size-count)/(count/secs);
265 else
266 left = 0;
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 );
271 p = buf;
272 while (header && rlen) {
273 if (*p == '\n') {
274 char *q;
275 char cmpbuf[31];
276 int i;
277 if (lf) { /* second lf in a row */
278 header = 0;
279 count = rlen-1;
281 lf = 1;
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, ':');
287 size = atol( q+1 );
289 } else {
290 if (*p != '\r') lf = 0;
292 putchar(*p);
293 p++;
294 rlen--;
296 if (!header) {
297 if (!outfile) {
298 printf("%s", p );
300 else
301 while (rlen) {
302 int wrcount;
303 short countwritten;
304 short error;
306 wrcount = rlen;
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);
312 p += countwritten;
315 } /* end while */
317 puts("");
318 if (rlen<0) perror( "recv failed: ");
319 if (outfile) close(f);
321 close(fd) ;
322 return 0;
324 } /* end main */
326 #endif