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) {
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 /* Check if the file is supposed to go to stdout */
78 if (!strcmp(fname_out, "-") == 0) {
79 /* Nope -- so open the output file */
80 if (freopen(fname_out, (do_continue ? "a" : "w"), stdout) == NULL)
81 fatalError("wget: freopen(%s): %s\n", fname_out, strerror(errno));
86 * Determine where to start transfer.
90 if (fstat(fileno(stdout), &sbuf) < 0)
91 fatalError("wget: fstat(): %s\n", strerror(errno));
93 beg_range = sbuf.st_size;
101 fprintf(sfp, "GET %s HTTP/1.1\r\nHost: %s\r\n", uri_path, uri_host);
103 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
104 fputs("Connection: close\r\n\r\n", sfp);
107 * Retrieve HTTP response line and check for "200" status code.
109 if (fgets(buf, sizeof(buf), sfp) == NULL)
110 fatalError("wget: no response from server\n");
111 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
113 for ( ; isspace(*s) ; ++s)
119 fatalError("wget: cannot continue - server does not properly support ranges\n");
125 fatalError("wget: server returned error: %s", buf);
129 * Retrieve HTTP headers.
131 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
132 if (strcmp(buf, "content-length") == 0) {
137 if (strcmp(buf, "transfer-encoding") == 0) {
138 fatalError("wget: i do not do transfer encodings, server wants to do: %s\n", s);
144 * Retrieve HTTP body.
146 while (clen > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) {
147 fwrite(buf, 1, n, stdout);
151 if (n == 0 && ferror(sfp))
152 fatalError("wget: network read error: %s", strerror(errno));
158 void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
164 if (strncmp(url, "http://", 7) != 0)
165 fatalError("wget: not an http url: %s\n", url);
167 /* pull the host portion to the front of the buffer */
168 for (s = url, h = url+7 ; *h != '/' ; ++h) {
170 fatalError("wget: cannot parse url: %s\n", url);
172 *uri_port = atoi(h+1);
183 FILE *open_socket(char *host, int port)
185 struct sockaddr_in sin;
190 memzero(&sin, sizeof(sin));
191 sin.sin_family = AF_INET;
192 if ((hp = (struct hostent *) gethostbyname(host)) == NULL)
193 fatalError("wget: cannot resolve %s\n", host);
194 memcpy(&sin.sin_addr, hp->h_addr_list[0], hp->h_length);
195 sin.sin_port = htons(port);
198 * Get the server onto a stdio stream.
200 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
201 fatalError("wget: socket(): %s\n", strerror(errno));
202 if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0)
203 fatalError("wget: connect(%s): %s\n", host, strerror(errno));
204 if ((fp = fdopen(fd, "r+")) == NULL)
205 fatalError("wget: fdopen(): %s\n", strerror(errno));
211 char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
218 /* retrieve header line */
219 if (fgets(buf, bufsiz, fp) == NULL)
222 /* see if we are at the end of the headers */
223 for (s = buf ; *s == '\r' ; ++s)
228 /* convert the header name to lower case */
229 for (s = buf ; isalnum(*s) || *s == '-' ; ++s)
232 /* verify we are at the end of the header name */
234 fatalError("wget: bad header line: %s\n", buf);
236 /* locate the start of the header value */
237 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
241 /* locate the end of header */
242 while (*s != '\0' && *s != '\r' && *s != '\n')
245 /* end of header found */
251 /* Rat! The buffer isn't big enough to hold the entire header value. */
252 while (c = getc(fp), c != EOF && c != '\n')
260 c-file-style: "linux"