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);
60 if (argc - optind != 1)
63 /* Guess an output filename */
65 fname_out = get_last_path_component(argv[optind]);
68 if (do_continue && !fname_out)
69 fatalError("wget: cannot specify continue (-c) without a filename (-O)\n");
72 * Parse url into components.
74 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
77 * Open socket to server.
79 sfp = open_socket(uri_host, uri_port);
82 * Open the output stream.
84 if (fname_out != NULL) {
85 if (freopen(fname_out, (do_continue ? "a" : "w"), stdout) == NULL)
86 fatalError("wget: freopen(%s): %s\n", fname_out, strerror(errno));
90 * Determine where to start transfer.
94 if (fstat(fileno(stdout), &sbuf) < 0)
95 fatalError("wget: fstat(): %s\n", strerror(errno));
97 beg_range = sbuf.st_size;
105 fprintf(sfp, "GET %s HTTP/1.1\r\nHost: %s\r\n", uri_path, uri_host);
107 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
108 fputs("Connection: close\r\n\r\n", sfp);
111 * Retrieve HTTP response line and check for "200" status code.
113 if (fgets(buf, sizeof(buf), sfp) == NULL)
114 fatalError("wget: no response from server\n");
115 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
117 for ( ; isspace(*s) ; ++s)
123 fatalError("wget: cannot continue - server does not properly support ranges\n");
129 fatalError("wget: server returned error: %s", buf);
133 * Retrieve HTTP headers.
135 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
136 if (strcmp(buf, "content-length") == 0) {
141 if (strcmp(buf, "transfer-encoding") == 0) {
142 fatalError("wget: i do not do transfer encodings, server wants to do: %s\n", s);
148 * Retrieve HTTP body.
150 while (clen > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) {
151 fwrite(buf, 1, n, stdout);
155 if (n == 0 && ferror(sfp))
156 fatalError("wget: network read error: %s", strerror(errno));
162 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
168 if (strncmp(url, "http://", 7) != 0)
169 fatalError("wget: not an http url: %s\n", url);
171 /* pull the host portion to the front of the buffer */
172 for (s = url, h = url+7 ; *h != '/' ; ++h) {
174 fatalError("wget: cannot parse url: %s\n", url);
176 *uri_port = atoi(h+1);
187 FILE *open_socket(char *host, int port)
189 struct sockaddr_in sin;
194 memzero(&sin, sizeof(sin));
195 sin.sin_family = AF_INET;
196 if ((hp = (struct hostent *) gethostbyname(host)) == NULL)
197 fatalError("wget: cannot resolve %s\n", host);
198 memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
199 sin.sin_port = htons(port);
202 * Get the server onto a stdio stream.
204 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
205 fatalError("wget: socket(): %s\n", strerror(errno));
206 if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0)
207 fatalError("wget: connect(%s): %s\n", host, strerror(errno));
208 if ((fp = fdopen(fd, "r+")) == NULL)
209 fatalError("wget: fdopen(): %s\n", strerror(errno));
215 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
222 /* retrieve header line */
223 if (fgets(buf, bufsiz, fp) == NULL)
226 /* see if we are at the end of the headers */
227 for (s = buf ; *s == '\r' ; ++s)
232 /* convert the header name to lower case */
233 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
236 /* verify we are at the end of the header name */
238 fatalError("wget: bad header line: %s\n", buf);
240 /* locate the start of the header value */
241 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
245 /* locate the end of header */
246 while (*s != '\0' && *s != '\r' && *s != '\n')
249 /* end of header found */
255 /* Rats! The buffer isn't big enough to hold the entire header value. */
256 while (c = getc(fp), c != EOF && c != '\n')
264 c-file-style: "linux"