1 /* vi: set sw=4 ts=4: */
3 * wget - retrieve a file using HTTP
17 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path);
26 FILE *open_socket(char *host, int port);
27 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
30 int wget_main(int argc, char **argv)
32 FILE *sfp; /* socket to web server */
33 char *uri_host, *uri_path; /* parsed from command line url */
38 char *fname_out = NULL; /* where to direct output (-O) */
39 int do_continue = 0; /* continue a prev transfer (-c) */
40 long beg_range = 0L; /* range at which continue begins */
41 int got_clen = 0; /* got content-length: from server */
42 long clen = 1L; /* the content length */
47 while ((n = getopt(argc, argv, "cO:")) != EOF) {
53 fname_out = (strcmp(optarg, "-") == 0 ? NULL : optarg);
59 if (do_continue && !fname_out)
60 fatalError("wget: cannot specify continue (-c) without a filename (-O)\n");
61 if (argc - optind != 1)
64 * Parse url into components.
66 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
69 * Open socket to server.
71 sfp = open_socket(uri_host, uri_port);
74 * Open the output stream.
76 if (fname_out != NULL) {
77 if (freopen(fname_out, (do_continue ? "a" : "w"), stdout) == NULL)
78 fatalError("wget: freopen(%s): %s\n", fname_out, strerror(errno));
82 * Determine where to start transfer.
86 if (fstat(fileno(stdout), &sbuf) < 0)
87 fatalError("wget: fstat(): %s\n", strerror(errno));
89 beg_range = sbuf.st_size;
97 fprintf(sfp, "GET %s HTTP/1.1\r\nHost: %s\r\n", uri_path, uri_host);
99 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
100 fputs("Connection: close\r\n\r\n", sfp);
103 * Retrieve HTTP response line and check for "200" status code.
105 if (fgets(buf, sizeof(buf), sfp) == NULL)
106 fatalError("wget: no response from server\n");
107 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
109 for ( ; isspace(*s) ; ++s)
115 fatalError("wget: cannot continue - server does not properly support ranges\n");
121 fatalError("wget: server returned error: %s", buf);
125 * Retrieve HTTP headers.
127 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
128 if (strcmp(buf, "content-length") == 0) {
133 if (strcmp(buf, "transfer-encoding") == 0) {
134 fatalError("wget: i do not do transfer encodings, server wants to do: %s\n", s);
140 * Retrieve HTTP body.
142 while (clen > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) {
143 fwrite(buf, 1, n, stdout);
147 if (n == 0 && ferror(sfp))
148 fatalError("wget: network read error: %s", strerror(errno));
154 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
160 if (strncmp(url, "http://", 7) != 0)
161 fatalError("wget: not an http url: %s\n", url);
163 /* pull the host portion to the front of the buffer */
164 for (s = url, h = url+7 ; *h != '/' ; ++h) {
166 fatalError("wget: cannot parse url: %s\n", url);
168 *uri_port = atoi(h+1);
179 FILE *open_socket(char *host, int port)
181 struct sockaddr_in sin;
186 memzero(&sin, sizeof(sin));
187 sin.sin_family = AF_INET;
188 if ((hp = (struct hostent *) gethostbyname(host)) == NULL)
189 fatalError("wget: cannot resolve %s\n", host);
190 memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
191 sin.sin_port = htons(port);
194 * Get the server onto a stdio stream.
196 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
197 fatalError("wget: socket(): %s\n", strerror(errno));
198 if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0)
199 fatalError("wget: connect(%s): %s\n", host, strerror(errno));
200 if ((fp = fdopen(fd, "r+")) == NULL)
201 fatalError("wget: fdopen(): %s\n", strerror(errno));
207 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
214 /* retrieve header line */
215 if (fgets(buf, bufsiz, fp) == NULL)
218 /* see if we are at the end of the headers */
219 for (s = buf ; *s == '\r' ; ++s)
224 /* convert the header name to lower case */
225 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
228 /* verify we are at the end of the header name */
230 fatalError("wget: bad header line: %s\n", buf);
232 /* locate the start of the header value */
233 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
237 /* locate the end of header */
238 while (*s != '\0' && *s != '\r' && *s != '\n')
241 /* end of header found */
247 /* Rats! The buffer isn't big enough to hold the entire header value. */
248 while (c = getc(fp), c != EOF && c != '\n')
256 c-file-style: "linux"