wget: new option FEATURE_WGET_FTP to enable/disable FTP

Introduce a separate option FTPS_SUPPORTED instead of not obvious ENABLE_FEATURE_WGET_HTTPS.

function                                             old     new   delta
P_FTP                                                  4       -      -4
P_FTPS                                                 5       -      -5
reset_beg_range_to_zero                               41       -     -41
parse_url                                            431     366     -65
parse_pasv_epsv                                      154       -    -154
.rodata                                           115566  115408    -158
ftpcmd                                               204       -    -204
spawn_ssl_client                                     291       -    -291
wget_main                                           2998    2664    -334
------------------------------------------------------------------------------
(add/remove: 0/7 grow/shrink: 0/3 up/down: 0/-1256)         Total: -1256 bytes

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Sergey Ponomarev 2021-01-17 20:35:08 +02:00 committed by Denys Vlasenko
parent 0b25e810ed
commit b6e6c83ab3

View File

@ -25,6 +25,13 @@
//config: default y //config: default y
//config: depends on WGET //config: depends on WGET
//config: //config:
//config:config FEATURE_WGET_FTP
//config: bool "Enable FTP protocol (+1k)"
//config: default y
//config: depends on WGET
//config: help
//config: To support FTPS, enable FEATURE_WGET_HTTPS as well.
//config:
//config:config FEATURE_WGET_AUTHENTICATION //config:config FEATURE_WGET_AUTHENTICATION
//config: bool "Enable HTTP authentication" //config: bool "Enable HTTP authentication"
//config: default y //config: default y
@ -48,12 +55,12 @@
//config: //config:
//config:config FEATURE_WGET_HTTPS //config:config FEATURE_WGET_HTTPS
//config: bool "Support HTTPS using internal TLS code" //config: bool "Support HTTPS using internal TLS code"
//it also enables FTPS support, but it's not well tested yet
//config: default y //config: default y
//config: depends on WGET //config: depends on WGET
//config: select TLS //config: select TLS
//config: help //config: help
//config: wget will use internal TLS code to connect to https:// URLs. //config: wget will use internal TLS code to connect to https:// URLs.
//config: It also enables FTPS support, but it's not well tested yet.
//config: Note: //config: Note:
//config: On NOMMU machines, ssl_helper applet should be available //config: On NOMMU machines, ssl_helper applet should be available
//config: in the $PATH for this to work. Make sure to select that applet. //config: in the $PATH for this to work. Make sure to select that applet.
@ -173,6 +180,7 @@
#define SSL_SUPPORTED (ENABLE_FEATURE_WGET_OPENSSL || ENABLE_FEATURE_WGET_HTTPS) #define SSL_SUPPORTED (ENABLE_FEATURE_WGET_OPENSSL || ENABLE_FEATURE_WGET_HTTPS)
#define FTPS_SUPPORTED (ENABLE_FEATURE_WGET_FTP && ENABLE_FEATURE_WGET_HTTPS)
struct host_info { struct host_info {
char *allocated; char *allocated;
@ -182,14 +190,16 @@ struct host_info {
char *host; char *host;
int port; int port;
}; };
static const char P_FTP[] ALIGN1 = "ftp";
static const char P_HTTP[] ALIGN1 = "http"; static const char P_HTTP[] ALIGN1 = "http";
#if SSL_SUPPORTED #if SSL_SUPPORTED
# if ENABLE_FEATURE_WGET_HTTPS
static const char P_FTPS[] ALIGN1 = "ftps";
# endif
static const char P_HTTPS[] ALIGN1 = "https"; static const char P_HTTPS[] ALIGN1 = "https";
#endif #endif
#if ENABLE_FEATURE_WGET_FTP
static const char P_FTP[] ALIGN1 = "ftp";
#endif
#if FTPS_SUPPORTED
static const char P_FTPS[] ALIGN1 = "ftps";
#endif
#if ENABLE_FEATURE_WGET_LONG_OPTIONS #if ENABLE_FEATURE_WGET_LONG_OPTIONS
/* User-specified headers prevent using our corresponding built-in headers. */ /* User-specified headers prevent using our corresponding built-in headers. */
@ -482,6 +492,7 @@ static char fgets_trim_sanitize(FILE *fp, const char *fmt)
return c; return c;
} }
#if ENABLE_FEATURE_WGET_FTP
static int ftpcmd(const char *s1, const char *s2, FILE *fp) static int ftpcmd(const char *s1, const char *s2, FILE *fp)
{ {
int result; int result;
@ -507,6 +518,7 @@ static int ftpcmd(const char *s1, const char *s2, FILE *fp)
G.wget_buf[3] = ' '; G.wget_buf[3] = ' ';
return result; return result;
} }
#endif
static void parse_url(const char *src_url, struct host_info *h) static void parse_url(const char *src_url, struct host_info *h)
{ {
@ -515,30 +527,31 @@ static void parse_url(const char *src_url, struct host_info *h)
free(h->allocated); free(h->allocated);
h->allocated = url = xstrdup(src_url); h->allocated = url = xstrdup(src_url);
h->protocol = P_FTP; h->protocol = P_HTTP;
p = strstr(url, "://"); p = strstr(url, "://");
if (p) { if (p) {
*p = '\0'; *p = '\0';
h->host = p + 3; h->host = p + 3;
#if ENABLE_FEATURE_WGET_FTP
if (strcmp(url, P_FTP) == 0) { if (strcmp(url, P_FTP) == 0) {
h->port = bb_lookup_std_port(P_FTP, "tcp", 21); h->port = bb_lookup_std_port(P_FTP, "tcp", 21);
h->protocol = P_FTP;
} else } else
#if SSL_SUPPORTED #endif
# if ENABLE_FEATURE_WGET_HTTPS #if FTPS_SUPPORTED
if (strcmp(url, P_FTPS) == 0) { if (strcmp(url, P_FTPS) == 0) {
h->port = bb_lookup_std_port(P_FTPS, "tcp", 990); h->port = bb_lookup_std_port(P_FTPS, "tcp", 990);
h->protocol = P_FTPS; h->protocol = P_FTPS;
} else } else
# endif #endif
#if SSL_SUPPORTED
if (strcmp(url, P_HTTPS) == 0) { if (strcmp(url, P_HTTPS) == 0) {
h->port = bb_lookup_std_port(P_HTTPS, "tcp", 443); h->port = bb_lookup_std_port(P_HTTPS, "tcp", 443);
h->protocol = P_HTTPS; h->protocol = P_HTTPS;
} else } else
#endif #endif
if (strcmp(url, P_HTTP) == 0) { if (strcmp(url, P_HTTP) == 0) {
http: goto http;
h->port = bb_lookup_std_port(P_HTTP, "tcp", 80);
h->protocol = P_HTTP;
} else { } else {
*p = ':'; *p = ':';
bb_error_msg_and_die("not an http or ftp url: %s", url); bb_error_msg_and_die("not an http or ftp url: %s", url);
@ -546,7 +559,8 @@ static void parse_url(const char *src_url, struct host_info *h)
} else { } else {
// GNU wget is user-friendly and falls back to http:// // GNU wget is user-friendly and falls back to http://
h->host = url; h->host = url;
goto http; http:
h->port = bb_lookup_std_port(P_HTTP, "tcp", 80);
} }
// FYI: // FYI:
@ -796,6 +810,7 @@ static void spawn_ssl_client(const char *host, int network_fd, int flags)
} }
#endif #endif
#if ENABLE_FEATURE_WGET_FTP
static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_sockaddr *lsa) static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_sockaddr *lsa)
{ {
FILE *sfp; FILE *sfp;
@ -803,7 +818,7 @@ static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_
int port; int port;
sfp = open_socket(lsa); sfp = open_socket(lsa);
#if ENABLE_FEATURE_WGET_HTTPS #if FTPS_SUPPORTED
if (target->protocol == P_FTPS) if (target->protocol == P_FTPS)
spawn_ssl_client(target->host, fileno(sfp), TLSLOOP_EXIT_ON_LOCAL_EOF); spawn_ssl_client(target->host, fileno(sfp), TLSLOOP_EXIT_ON_LOCAL_EOF);
#endif #endif
@ -859,7 +874,7 @@ static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_
*dfpp = open_socket(lsa); *dfpp = open_socket(lsa);
#if ENABLE_FEATURE_WGET_HTTPS #if FTPS_SUPPORTED
if (target->protocol == P_FTPS) { if (target->protocol == P_FTPS) {
/* "PROT P" enables encryption of data stream. /* "PROT P" enables encryption of data stream.
* Without it (or with "PROT C"), data is sent unencrypted. * Without it (or with "PROT C"), data is sent unencrypted.
@ -885,6 +900,7 @@ static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_
return sfp; return sfp;
} }
#endif
static void NOINLINE retrieve_file_data(FILE *dfp) static void NOINLINE retrieve_file_data(FILE *dfp)
{ {
@ -1407,10 +1423,12 @@ However, in real world it was observed that some web servers
/* For HTTP, data is pumped over the same connection */ /* For HTTP, data is pumped over the same connection */
dfp = sfp; dfp = sfp;
} else { } else {
#if ENABLE_FEATURE_WGET_FTP
/* /*
* FTP session * FTP session
*/ */
sfp = prepare_ftp_session(&dfp, &target, lsa); sfp = prepare_ftp_session(&dfp, &target, lsa);
#endif
} }
free(lsa); free(lsa);
@ -1428,6 +1446,7 @@ However, in real world it was observed that some web servers
fprintf(stderr, "remote file exists\n"); fprintf(stderr, "remote file exists\n");
} }
#if ENABLE_FEATURE_WGET_FTP
if (dfp != sfp) { if (dfp != sfp) {
/* It's ftp. Close data connection properly */ /* It's ftp. Close data connection properly */
fclose(dfp); fclose(dfp);
@ -1435,6 +1454,7 @@ However, in real world it was observed that some web servers
bb_error_msg_and_die("ftp error: %s", G.wget_buf); bb_error_msg_and_die("ftp error: %s", G.wget_buf);
/* ftpcmd("QUIT", NULL, sfp); - why bother? */ /* ftpcmd("QUIT", NULL, sfp); - why bother? */
} }
#endif
fclose(sfp); fclose(sfp);
free(server.allocated); free(server.allocated);