uclient-fetch: Extract opt_post variable
authorSergey Ponomarev <stokito@gmail.com>
Mon, 9 May 2022 18:53:48 +0000 (21:53 +0300)
committerFelix Fietkau <nbd@nbd.name>
Fri, 13 Feb 2026 07:37:47 +0000 (07:37 +0000)
The var means that ether --post-data or --post-file was set.

Remove duplicated code to set the Content-Type header.

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
uclient-fetch.c

index 598969b29f8ac15d91826954ab18cc8a7604f219..8f4a28220dd46a670e4644f60dc435bb2973cadb 100644 (file)
@@ -49,6 +49,7 @@ struct header {
 static const char *user_agent = "uclient-fetch";
 static const char *post_data;
 static const char *post_file;
+static char opt_post = 0;  /* 1 when --post-data/file is used */
 static struct ustream_ssl_ctx *ssl_ctx;
 static const struct ustream_ssl_ops *ssl_ops;
 static int quiet = false;
@@ -344,7 +345,7 @@ 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, opt_post ? "POST" : "GET");
        if (rc)
                return rc;
 
@@ -352,7 +353,7 @@ static int init_request(struct uclient *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;
@@ -368,14 +369,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)
@@ -682,9 +684,19 @@ 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_SPIDER: