diff options
-rw-r--r-- | toys/pending/wget.c | 117 |
1 files changed, 49 insertions, 68 deletions
diff --git a/toys/pending/wget.c b/toys/pending/wget.c index 0cf74ce4..4283fe40 100644 --- a/toys/pending/wget.c +++ b/toys/pending/wget.c @@ -25,61 +25,53 @@ GLOBALS( char *filename; ) -#define HN_LEN 128 // HOSTNAME MAX LENGTH -#define PATH_LEN 256 // PATH MAX LENGTH - -struct httpinfo { - char hostname[HN_LEN]; - char port[6]; // MAX port value: 65535 - char path[PATH_LEN]; -}; - // extract hostname from url -static unsigned int get_hn(char *url, char *hn) { - unsigned int i; +static unsigned get_hn(const char *url, char *hostname) { + unsigned i; for (i = 0; url[i] != '\0' && url[i] != ':' && url[i] != '/'; i++) { - if(i >= HN_LEN) - error_exit("The hostname's length is lower than %d.", HN_LEN); - hn[i] = url[i]; + if(i >= 1024) error_exit("too long hostname in URL"); + hostname[i] = url[i]; } - hn[i] = '\0'; + hostname[i] = '\0'; return i; } // extract port number -static void get_port(char *url, char *port, unsigned int *url_i) { - unsigned int i; +static unsigned get_port(const char *url, char *port, unsigned url_i) { + unsigned i; - for (i = 0; url[i] != '\0' && url[i] != '/'; i++, (*url_i)++) { + for (i = 0; url[i] != '\0' && url[i] != '/'; i++, url_i++) { if('0' <= url[i] && url[i] <= '9') port[i] = url[i]; - else error_exit("Port is invalid"); + else error_exit("wrong decimal port number"); } if(i <= 6) port[i] = '\0'; - else error_exit("Port number is too long"); + else error_exit("too long port number"); + + return url_i; } // get http infos in URL -static void get_info(struct httpinfo *hi, char *url) { - unsigned int i = 7, len; +static void get_info(const char *url, char* hostname, char *port, char *path) { + unsigned i = 7, len; - if (strncmp(url, "http://", i)) error_exit("Only HTTP can be supported."); - len = get_hn(url+i, hi->hostname); + if (strncmp(url, "http://", i)) error_exit("only HTTP support"); + len = get_hn(url+i, hostname); i += len; // get port if exists if (url[i] == ':') { i++; - get_port(url+i, hi->port, &i); - } else strcpy(hi->port, "80"); + i = get_port(url+i, port, i); + } else strcpy(port, "80"); // get uri in URL - if (url[i] == '\0') strcpy(hi->path, "/"); + if (url[i] == '\0') strcpy(path, "/"); else if (url[i] == '/') { - if (strlen(url+i) < PATH_LEN) strcpy(hi->path, url+i); - else error_exit("The URL path's length is less than %d.", PATH_LEN); - } else error_exit("The URL is NOT valid."); + if (strlen(url+i) < 1024) strcpy(path, url+i); + else error_exit("too long path in URL"); + } else error_exit("wrong URL"); } // connect to any IPv4 or IPv6 server @@ -100,16 +92,16 @@ static int conn_svr(const char *hostname, const char *port) { for (rp = result; rp; rp = rp->ai_next) { if ((sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1) { - perror_msg("Socket Error"); + perror_msg("socket error"); continue; } if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break; // succeed in connecting to any server IP - else perror_msg("Connect Error"); + else perror_msg("connect error"); close(sock); } freeaddrinfo(result); - if(!rp) error_exit("Can not connect to HTTP server"); + if(!rp) error_exit("can't connect"); return sock; } @@ -122,46 +114,37 @@ static void mk_fld(char *name, char *value) { strcat(toybuf, "\r\n"); } -// make http request -static void mk_rq(char *path) { - strcpy(toybuf, "GET "); - strcat(toybuf, path); - strcat(toybuf, " HTTP/1.1\r\n"); -} - // get http response body starting address and its length -static char *get_body(size_t len, size_t *body_len) { - unsigned int i; +static char *get_body(ssize_t len, ssize_t *body_len) { + int i; for (i = 0; i < len-4; i++) if (!strncmp(toybuf+i, "\r\n\r\n", 4)) break; - *body_len = len-i-4; + *body_len = len - i - 4; return toybuf+i+4; } void wget_main(void) { int sock; - struct httpinfo hi; FILE *fp; - size_t len, body_len; - char *body, *result, *rc, *r_str, ua[18] = "toybox wget/", ver[6]; + ssize_t len, body_len; + char *body, *result, *rc, *r_str; + char ua[18] = "toybox wget/", ver[6], hostname[1024], port[6], path[1024]; // TODO extract filename to be saved from URL - if (!(toys.optflags & FLAG_f)) - help_exit("The filename to be saved should be needed."); - if (fopen(TT.filename, "r")) - error_exit("The file(%s) you specified already exists.", TT.filename); + if (!(toys.optflags & FLAG_f)) help_exit("no filename"); + if (fopen(TT.filename, "r")) perror_exit("file already exists"); - if(!toys.optargs[0]) help_exit("The URL should be specified."); - get_info(&hi, toys.optargs[0]); + if(!toys.optargs[0]) help_exit("no URL"); + get_info(toys.optargs[0], hostname, port, path); - sock = conn_svr(hi.hostname, hi.port); + sock = conn_svr(hostname, port); // compose HTTP request - mk_rq(hi.path); - mk_fld("Host", hi.hostname); + sprintf(toybuf, "GET %s HTTP/1.1\r\n", path); + mk_fld("Host", hostname); strncpy(ver, TOYBOX_VERSION, 5); strcat(ua, ver); mk_fld("User-Agent", ua); @@ -170,13 +153,11 @@ void wget_main(void) // send the HTTP request len = strlen(toybuf); - if (write(sock, toybuf, len) != len) perror_exit("HTTP GET failed."); + if (write(sock, toybuf, len) != len) perror_exit("write error"); // read HTTP response - if ((len = read(sock, toybuf, 4096)) == -1) - perror_exit("HTTP response failed."); - if (!strstr(toybuf, "\r\n\r\n")) - error_exit("HTTP response header is too long."); + if ((len = read(sock, toybuf, 4096)) == -1) perror_exit("read error"); + if (!strstr(toybuf, "\r\n\r\n")) error_exit("too long HTTP response"); body = get_body(len, &body_len); result = strtok(toybuf, "\r"); strtok(result, " "); @@ -185,13 +166,13 @@ void wget_main(void) // HTTP res code check // TODO handle HTTP 302 Found(Redirection) - if (strcmp(rc, "200")) error_exit("HTTP response: %s(%s).", rc, r_str); + if (strcmp(rc, "200")) error_exit("res: %s(%s)", rc, r_str); - if (!(fp = fopen(TT.filename, "w"))) perror_exit("File write error"); - if (fwrite(body, sizeof(char), body_len, fp) != body_len) - error_exit("File write is not successful."); + if (!(fp = fopen(TT.filename, "w"))) perror_exit("fopen error"); + if (fwrite(body, 1, body_len, fp) != body_len) + error_exit("fwrite error"); while ((len = read(sock, toybuf, 4096)) > 0) - if (fwrite(toybuf, sizeof(char), len, fp) != len) - error_exit("File write is not successful."); - if (fclose(fp) == EOF) perror_exit("File Close Error"); -}
\ No newline at end of file + if (fwrite(toybuf, 1, len, fp) != len) + error_exit("fwrite error"); + if (fclose(fp) == EOF) perror_exit("fclose error"); +} |