- add libbb function str_tolower to convert a string to lowercase.
- shrink wget a bit
This commit is contained in:
parent
3f3aa2a57d
commit
7e8a53a335
@ -77,6 +77,7 @@ lib-y += sha1.o
|
|||||||
lib-y += simplify_path.o
|
lib-y += simplify_path.o
|
||||||
lib-y += skip_whitespace.o
|
lib-y += skip_whitespace.o
|
||||||
lib-y += speed_table.o
|
lib-y += speed_table.o
|
||||||
|
lib-y += str_tolower.o
|
||||||
lib-y += trim.o
|
lib-y += trim.o
|
||||||
lib-y += u_signal_names.o
|
lib-y += u_signal_names.o
|
||||||
lib-y += udp_io.o
|
lib-y += udp_io.o
|
||||||
|
13
libbb/str_tolower.c
Normal file
13
libbb/str_tolower.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/* vi set: sw=4 ts=4: */
|
||||||
|
/* Convert string str to lowercase, return str.
|
||||||
|
*
|
||||||
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
||||||
|
*/
|
||||||
|
#include "libbb.h"
|
||||||
|
char* str_tolower(char *str)
|
||||||
|
{
|
||||||
|
char *c;
|
||||||
|
for (c = str; *c; ++c)
|
||||||
|
*c = tolower(*c);
|
||||||
|
return str;
|
||||||
|
}
|
@ -34,7 +34,7 @@ static off_t beg_range; /* Range at which continue begins */
|
|||||||
#if ENABLE_FEATURE_WGET_STATUSBAR
|
#if ENABLE_FEATURE_WGET_STATUSBAR
|
||||||
static off_t transferred; /* Number of bytes transferred so far */
|
static off_t transferred; /* Number of bytes transferred so far */
|
||||||
#endif
|
#endif
|
||||||
static int chunked; /* chunked transfer encoding */
|
static bool chunked; /* chunked transfer encoding */
|
||||||
#if ENABLE_FEATURE_WGET_STATUSBAR
|
#if ENABLE_FEATURE_WGET_STATUSBAR
|
||||||
static void progressmeter(int flag);
|
static void progressmeter(int flag);
|
||||||
static const char *curfile; /* Name of current file being transferred */
|
static const char *curfile; /* Name of current file being transferred */
|
||||||
@ -76,9 +76,7 @@ static char *safe_fgets(char *s, int size, FILE *stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if ENABLE_FEATURE_WGET_AUTHENTICATION
|
#if ENABLE_FEATURE_WGET_AUTHENTICATION
|
||||||
/*
|
/* Base64-encode character string and return the string. */
|
||||||
* Base64-encode character string and return the string.
|
|
||||||
*/
|
|
||||||
static char *base64enc(unsigned char *p, char *buf, int len)
|
static char *base64enc(unsigned char *p, char *buf, int len)
|
||||||
{
|
{
|
||||||
bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
|
bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
|
||||||
@ -96,7 +94,7 @@ int wget_main(int argc, char **argv)
|
|||||||
int port;
|
int port;
|
||||||
int try = 5;
|
int try = 5;
|
||||||
unsigned opt;
|
unsigned opt;
|
||||||
char *s;
|
char *str;
|
||||||
char *proxy = 0;
|
char *proxy = 0;
|
||||||
char *dir_prefix = NULL;
|
char *dir_prefix = NULL;
|
||||||
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
|
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
|
||||||
@ -104,20 +102,20 @@ int wget_main(int argc, char **argv)
|
|||||||
llist_t *headers_llist = NULL;
|
llist_t *headers_llist = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* server.allocated = target.allocated = NULL; */
|
|
||||||
|
|
||||||
FILE *sfp = NULL; /* socket to web/ftp server */
|
FILE *sfp = NULL; /* socket to web/ftp server */
|
||||||
FILE *dfp = NULL; /* socket to ftp server (data) */
|
FILE *dfp = NULL; /* socket to ftp server (data) */
|
||||||
char *fname_out = NULL; /* where to direct output (-O) */
|
char *fname_out = NULL; /* where to direct output (-O) */
|
||||||
int got_clen = 0; /* got content-length: from server */
|
bool got_clen = 0; /* got content-length: from server */
|
||||||
int output_fd = -1;
|
int output_fd = -1;
|
||||||
int use_proxy = 1; /* Use proxies if env vars are set */
|
bool use_proxy = 1; /* Use proxies if env vars are set */
|
||||||
const char *proxy_flag = "on"; /* Use proxies if env vars are set */
|
const char *proxy_flag = "on"; /* Use proxies if env vars are set */
|
||||||
const char *user_agent = "Wget";/* Content of the "User-Agent" header field */
|
const char *user_agent = "Wget";/* "User-Agent" header field */
|
||||||
|
static const char * const keywords[] = {
|
||||||
/*
|
"content-length", "transfer-encoding", "chunked", "location", NULL
|
||||||
* Crack command line.
|
};
|
||||||
*/
|
enum {
|
||||||
|
KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
|
||||||
|
};
|
||||||
enum {
|
enum {
|
||||||
WGET_OPT_CONTINUE = 0x1,
|
WGET_OPT_CONTINUE = 0x1,
|
||||||
WGET_OPT_SPIDER = 0x2,
|
WGET_OPT_SPIDER = 0x2,
|
||||||
@ -131,7 +129,7 @@ int wget_main(int argc, char **argv)
|
|||||||
};
|
};
|
||||||
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
|
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
|
||||||
static const struct option wget_long_options[] = {
|
static const struct option wget_long_options[] = {
|
||||||
// name, has_arg, flag, val
|
/* name, has_arg, flag, val */
|
||||||
{ "continue", no_argument, NULL, 'c' },
|
{ "continue", no_argument, NULL, 'c' },
|
||||||
{ "spider", no_argument, NULL, 's' },
|
{ "spider", no_argument, NULL, 's' },
|
||||||
{ "quiet", no_argument, NULL, 'q' },
|
{ "quiet", no_argument, NULL, 'q' },
|
||||||
@ -145,6 +143,7 @@ int wget_main(int argc, char **argv)
|
|||||||
};
|
};
|
||||||
applet_long_options = wget_long_options;
|
applet_long_options = wget_long_options;
|
||||||
#endif
|
#endif
|
||||||
|
/* server.allocated = target.allocated = NULL; */
|
||||||
opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
|
opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
|
||||||
opt = getopt32(argc, argv, "csqO:P:Y:U:",
|
opt = getopt32(argc, argv, "csqO:P:Y:U:",
|
||||||
&fname_out, &dir_prefix,
|
&fname_out, &dir_prefix,
|
||||||
@ -152,7 +151,7 @@ int wget_main(int argc, char **argv)
|
|||||||
USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
|
USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
|
||||||
);
|
);
|
||||||
if (strcmp(proxy_flag, "off") == 0) {
|
if (strcmp(proxy_flag, "off") == 0) {
|
||||||
/* Use the proxy if necessary. */
|
/* Use the proxy if necessary */
|
||||||
use_proxy = 0;
|
use_proxy = 0;
|
||||||
}
|
}
|
||||||
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
|
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
|
||||||
@ -176,9 +175,7 @@ int wget_main(int argc, char **argv)
|
|||||||
server.host = target.host;
|
server.host = target.host;
|
||||||
server.port = target.port;
|
server.port = target.port;
|
||||||
|
|
||||||
/*
|
/* Use the proxy if necessary */
|
||||||
* Use the proxy if necessary.
|
|
||||||
*/
|
|
||||||
if (use_proxy) {
|
if (use_proxy) {
|
||||||
proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
|
proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
|
||||||
if (proxy && *proxy) {
|
if (proxy && *proxy) {
|
||||||
@ -217,9 +214,7 @@ int wget_main(int argc, char **argv)
|
|||||||
if ((opt & WGET_OPT_CONTINUE) && !fname_out)
|
if ((opt & WGET_OPT_CONTINUE) && !fname_out)
|
||||||
bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
|
bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
|
||||||
|
|
||||||
/*
|
/* Determine where to start transfer */
|
||||||
* Determine where to start transfer.
|
|
||||||
*/
|
|
||||||
if (LONE_DASH(fname_out)) {
|
if (LONE_DASH(fname_out)) {
|
||||||
output_fd = 1;
|
output_fd = 1;
|
||||||
opt &= ~WGET_OPT_CONTINUE;
|
opt &= ~WGET_OPT_CONTINUE;
|
||||||
@ -253,15 +248,11 @@ int wget_main(int argc, char **argv)
|
|||||||
if (!--try)
|
if (!--try)
|
||||||
bb_error_msg_and_die("too many redirections");
|
bb_error_msg_and_die("too many redirections");
|
||||||
|
|
||||||
/*
|
/* Open socket to http server */
|
||||||
* Open socket to http server
|
|
||||||
*/
|
|
||||||
if (sfp) fclose(sfp);
|
if (sfp) fclose(sfp);
|
||||||
sfp = open_socket(lsa);
|
sfp = open_socket(lsa);
|
||||||
|
|
||||||
/*
|
/* Send HTTP request. */
|
||||||
* Send HTTP request.
|
|
||||||
*/
|
|
||||||
if (use_proxy) {
|
if (use_proxy) {
|
||||||
fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
|
fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
|
||||||
target.is_ftp ? "f" : "ht", target.host,
|
target.is_ftp ? "f" : "ht", target.host,
|
||||||
@ -299,12 +290,12 @@ int wget_main(int argc, char **argv)
|
|||||||
if (fgets(buf, sizeof(buf), sfp) == NULL)
|
if (fgets(buf, sizeof(buf), sfp) == NULL)
|
||||||
bb_error_msg_and_die("no response from server");
|
bb_error_msg_and_die("no response from server");
|
||||||
|
|
||||||
s = buf;
|
str = buf;
|
||||||
while (*s != '\0' && !isspace(*s)) ++s;
|
str = skip_non_whitespace(str);
|
||||||
s = skip_whitespace(s);
|
str = skip_whitespace(str);
|
||||||
// FIXME: no error check
|
// FIXME: no error check
|
||||||
// xatou wouldn't work: "200 OK"
|
// xatou wouldn't work: "200 OK"
|
||||||
status = atoi(s);
|
status = atoi(str);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 0:
|
case 0:
|
||||||
case 100:
|
case 100:
|
||||||
@ -331,26 +322,28 @@ int wget_main(int argc, char **argv)
|
|||||||
/*
|
/*
|
||||||
* Retrieve HTTP headers.
|
* Retrieve HTTP headers.
|
||||||
*/
|
*/
|
||||||
while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
|
while ((str = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
|
||||||
if (strcasecmp(buf, "content-length") == 0) {
|
/* gethdr did already convert the "FOO:" string to lowercase */
|
||||||
content_len = BB_STRTOOFF(s, NULL, 10);
|
smalluint key = index_in_str_array(keywords, *&buf) + 1;
|
||||||
|
if (key == KEY_content_length) {
|
||||||
|
content_len = BB_STRTOOFF(str, NULL, 10);
|
||||||
if (errno || content_len < 0) {
|
if (errno || content_len < 0) {
|
||||||
bb_error_msg_and_die("content-length %s is garbage", s);
|
bb_error_msg_and_die("content-length %s is garbage", str);
|
||||||
}
|
}
|
||||||
got_clen = 1;
|
got_clen = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (strcasecmp(buf, "transfer-encoding") == 0) {
|
if (key == KEY_transfer_encoding) {
|
||||||
if (strcasecmp(s, "chunked") != 0)
|
if (index_in_str_array(keywords, str_tolower(str)) + 1 != KEY_chunked)
|
||||||
bb_error_msg_and_die("server wants to do %s transfer encoding", s);
|
bb_error_msg_and_die("server wants to do %s transfer encoding", str);
|
||||||
chunked = got_clen = 1;
|
chunked = got_clen = 1;
|
||||||
}
|
}
|
||||||
if (strcasecmp(buf, "location") == 0) {
|
if (key == KEY_location) {
|
||||||
if (s[0] == '/')
|
if (str[0] == '/')
|
||||||
/* free(target.allocated); */
|
/* free(target.allocated); */
|
||||||
target.path = /* target.allocated = */ xstrdup(s+1);
|
target.path = /* target.allocated = */ xstrdup(str+1);
|
||||||
else {
|
else {
|
||||||
parse_url(s, &target);
|
parse_url(str, &target);
|
||||||
if (use_proxy == 0) {
|
if (use_proxy == 0) {
|
||||||
server.host = target.host;
|
server.host = target.host;
|
||||||
server.port = target.port;
|
server.port = target.port;
|
||||||
@ -381,14 +374,14 @@ int wget_main(int argc, char **argv)
|
|||||||
* Splitting username:password pair,
|
* Splitting username:password pair,
|
||||||
* trying to log in
|
* trying to log in
|
||||||
*/
|
*/
|
||||||
s = strchr(target.user, ':');
|
str = strchr(target.user, ':');
|
||||||
if (s)
|
if (str)
|
||||||
*(s++) = '\0';
|
*(str++) = '\0';
|
||||||
switch (ftpcmd("USER ", target.user, sfp, buf)) {
|
switch (ftpcmd("USER ", target.user, sfp, buf)) {
|
||||||
case 230:
|
case 230:
|
||||||
break;
|
break;
|
||||||
case 331:
|
case 331:
|
||||||
if (ftpcmd("PASS ", s, sfp, buf) == 230)
|
if (ftpcmd("PASS ", str, sfp, buf) == 230)
|
||||||
break;
|
break;
|
||||||
/* FALLTHRU (failed login) */
|
/* FALLTHRU (failed login) */
|
||||||
default:
|
default:
|
||||||
@ -418,15 +411,15 @@ int wget_main(int argc, char **argv)
|
|||||||
// Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
|
// Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
|
||||||
// Server's IP is N1.N2.N3.N4 (we ignore it)
|
// Server's IP is N1.N2.N3.N4 (we ignore it)
|
||||||
// Server's port for data connection is P1*256+P2
|
// Server's port for data connection is P1*256+P2
|
||||||
s = strrchr(buf, ')');
|
str = strrchr(buf, ')');
|
||||||
if (s) s[0] = '\0';
|
if (str) str[0] = '\0';
|
||||||
s = strrchr(buf, ',');
|
str = strrchr(buf, ',');
|
||||||
if (!s) goto pasv_error;
|
if (!str) goto pasv_error;
|
||||||
port = xatou_range(s+1, 0, 255);
|
port = xatou_range(str+1, 0, 255);
|
||||||
*s = '\0';
|
*str = '\0';
|
||||||
s = strrchr(buf, ',');
|
str = strrchr(buf, ',');
|
||||||
if (!s) goto pasv_error;
|
if (!str) goto pasv_error;
|
||||||
port += xatou_range(s+1, 0, 255) * 256;
|
port += xatou_range(str+1, 0, 255) * 256;
|
||||||
set_nport(lsa, htons(port));
|
set_nport(lsa, htons(port));
|
||||||
dfp = open_socket(lsa);
|
dfp = open_socket(lsa);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user