Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/cram/test-san_uclient-fetch.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ check uclient-fetch usage:
\t--user-agent | -U <str>\t\tSet HTTP user agent (esc)
\t--post-data=STRING\t\tuse the POST method; send STRING as the data (esc)
\t--post-file=FILE\t\tuse the POST method; send FILE as the data (esc)
\t--method=METHOD\t\tuse the HTTP method e.g. PUT (esc)
\t--body-data=STRING\t\twith --method send the STRING in body (esc)
\t--body-file=FILE\t\twith --method send the FILE content in body (esc)
\t--spider | -s\t\t\tSpider mode - only check file existence (esc)
\t--timeout=N | -T N\t\tSet connect/request timeout to N seconds (esc)
\t--proxy=on | -Y on\t\tEnable interpretation of proxy env vars (default) (esc)
Expand Down
3 changes: 3 additions & 0 deletions tests/cram/test_uclient-fetch.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ check uclient-fetch usage:
\t--user-agent | -U <str>\t\tSet HTTP user agent (esc)
\t--post-data=STRING\t\tuse the POST method; send STRING as the data (esc)
\t--post-file=FILE\t\tuse the POST method; send FILE as the data (esc)
\t--method=METHOD\t\tuse the HTTP method e.g. PUT (esc)
\t--body-data=STRING\t\twith --method send the STRING in body (esc)
\t--body-file=FILE\t\twith --method send the FILE content in body (esc)
\t--spider | -s\t\t\tSpider mode - only check file existence (esc)
\t--timeout=N | -T N\t\tSet connect/request timeout to N seconds (esc)
\t--proxy=on | -Y on\t\tEnable interpretation of proxy env vars (default) (esc)
Expand Down
70 changes: 66 additions & 4 deletions uclient-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ struct header {
};

static const char *user_agent = "uclient-fetch";
static const char *method = NULL;
static const char *post_data;
static const char *post_file;
static char opt_post = 0; /* 1 when --post-data/file is used, 2 when --body-data/file is used */
static struct ustream_ssl_ctx *ssl_ctx;
static const struct ustream_ssl_ops *ssl_ops;
static int quiet = false;
Expand Down Expand Up @@ -344,15 +346,15 @@ static int init_request(struct uclient *cl)

msg_connecting(cl);

rc = uclient_http_set_request_type(cl, post_data || post_file ? "POST" : "GET");
rc = uclient_http_set_request_type(cl, method);
if (rc)
return rc;

uclient_http_reset_headers(cl);

list_for_each_entry(h, &headers, list) {
if (!strcasecmp(h->name, "Content-Type")) {
if (!post_data && !post_file)
if (!opt_post)
return -EINVAL;

content_type = h->value;
Expand All @@ -368,14 +370,15 @@ static int init_request(struct uclient *cl)
if (cur_resume)
check_resume_offset(cl);

if (post_data) {
if (opt_post) {
uclient_http_set_header(cl, "Content-Type", content_type);
}
if (post_data) {
uclient_write(cl, post_data, strlen(post_data));
}
else if(post_file)
{
FILE *input_file;
uclient_http_set_header(cl, "Content-Type", content_type);

input_file = fopen(post_file, "r");
if (!input_file)
Expand Down Expand Up @@ -526,6 +529,9 @@ static void usage(const char *progname)
" --user-agent | -U <str> Set HTTP user agent\n"
" --post-data=STRING use the POST method; send STRING as the data\n"
" --post-file=FILE use the POST method; send FILE as the data\n"
" --method=METHOD use the HTTP method e.g. PUT\n"
" --body-data=STRING with --method send the STRING in body\n"
" --body-file=FILE with --method send the FILE content in body\n"
" --spider | -s Spider mode - only check file existence\n"
" --timeout=N | -T N Set connect/request timeout to N seconds\n"
" --proxy=on | -Y on Enable interpretation of proxy env vars (default)\n"
Expand Down Expand Up @@ -592,6 +598,9 @@ enum {
L_USER_AGENT,
L_POST_DATA,
L_POST_FILE,
L_METHOD,
L_BODY_DATA,
L_BODY_FILE,
L_SPIDER,
L_TIMEOUT,
L_CONTINUE,
Expand All @@ -611,6 +620,9 @@ static const struct option longopts[] = {
[L_USER_AGENT] = { "user-agent", required_argument, NULL, 0 },
[L_POST_DATA] = { "post-data", required_argument, NULL, 0 },
[L_POST_FILE] = { "post-file", required_argument, NULL, 0 },
[L_METHOD] = { "method", required_argument, NULL, 0 },
[L_BODY_DATA] = { "body-data", required_argument, NULL, 0 },
[L_BODY_FILE] = { "body-file", required_argument, NULL, 0 },
[L_SPIDER] = { "spider", no_argument, NULL, 0 },
[L_TIMEOUT] = { "timeout", required_argument, NULL, 0 },
[L_CONTINUE] = { "continue", no_argument, NULL, 0 },
Expand Down Expand Up @@ -682,9 +694,38 @@ int main(int argc, char **argv)
user_agent = optarg;
break;
case L_POST_DATA:
if (opt_post) {
usage(progname);
goto out;
}
opt_post = 1;
post_data = optarg;
break;
case L_POST_FILE:
if (opt_post) {
usage(progname);
goto out;
}
opt_post = 1;
post_file = optarg;
break;
case L_METHOD:
method = optarg;
break;
case L_BODY_DATA:
if (opt_post) {
usage(progname);
goto out;
}
opt_post = 2;
post_data = optarg;
break;
case L_BODY_FILE:
if (opt_post) {
usage(progname);
goto out;
}
opt_post = 2;
post_file = optarg;
break;
case L_SPIDER:
Expand Down Expand Up @@ -783,6 +824,27 @@ int main(int argc, char **argv)
}
}

if (method) {
if (opt_post == 1 || no_output) {
/* --post-data/file or --spider can't be used with --method */
usage(progname);
goto out;
}
} else {
if (opt_post == 1) {
method = "POST";
} else if (opt_post == 2) {
/* --body-data/file specified but no --method */
usage(progname);
goto out;
} else if (no_output) {
/* Note: GNU wget --spider sends a HEAD and if it failed repeats with a GET */
method = "HEAD";
} else {
method = "GET";
}
}

argv += optind;
argc -= optind;

Expand Down