[OpenWrt-Devel] [PATCH uclient] Add option to read POST data from file

Gio gio at diveni.re
Fri Jun 12 06:59:52 EDT 2020


Passing post data in command line is convenient but has limited size, and may 
become tricky to correctly escape passed data expecially in scripts.
This patch add the option --post-file so the data to post can be read from a 
file.
I haven't compiled the patch (it is not clear to me where fiddle into openwrt 
build system so this patch get included) but it should work fine, the patch is 
in attachment too just in case my mail client mangle the pasted text...

Signed-off-by: Gioacchino Mazzurco <gio at eigenlab.org>
---

diff --git a/uclient-fetch.c b/uclient-fetch.c
index a06be5d..5c70316 100644
--- a/uclient-fetch.c
+++ b/uclient-fetch.c
@@ -43,6 +43,7 @@
 
 static const char *user_agent = "uclient-fetch";
 static const char *post_data;
+static const char *post_file;
 static struct ustream_ssl_ctx *ssl_ctx;
 static const struct ustream_ssl_ops *ssl_ops;
 static int quiet = false;
@@ -334,7 +335,7 @@ static int init_request(struct uclient *cl)
 
 	msg_connecting(cl);
 
-	rc = uclient_http_set_request_type(cl, post_data ? "POST" : "GET");
+	rc = uclient_http_set_request_type(cl, post_data || post_file ? 
"POST" : "GET");
 	if (rc)
 		return rc;
 
@@ -347,6 +348,25 @@ static int init_request(struct uclient *cl)
 		uclient_http_set_header(cl, "Content-Type", 
"application/x-www-form-urlencoded");
 		uclient_write(cl, post_data, strlen(post_data));
 	}
+	else if(post_file)
+	{
+		uclient_http_set_header(cl, "Content-Type", 
"application/x-www-form-urlencoded");
+
+		FILE input_file = fopen(post_file, "r");
+		if (!input_file)
+			return errno;
+
+		char tbuf[1000];
+		size_t rlen = 0;
+		do
+		{
+			rlen = fread(tbuf, 1, 1000, input_file);
+			uclient_write(cl, tbuf, rlen);
+		}
+		while(rlen)
+
+		fclose(input_file)
+	}
 
 	rc = uclient_request(cl);
 	if (rc)
@@ -516,6 +536,7 @@ enum {
 	L_PASSWORD,
 	L_USER_AGENT,
 	L_POST_DATA,
+	L_POST_FILE,
 	L_SPIDER,
 	L_TIMEOUT,
 	L_CONTINUE,
@@ -532,6 +553,7 @@ static const struct option longopts[] = {
 	[L_PASSWORD] = { "password", required_argument },
 	[L_USER_AGENT] = { "user-agent", required_argument },
 	[L_POST_DATA] = { "post-data", required_argument },
+	[L_POST_FILE] = { "post-file", required_argument },
 	[L_SPIDER] = { "spider", no_argument },
 	[L_TIMEOUT] = { "timeout", required_argument },
 	[L_CONTINUE] = { "continue", no_argument },
@@ -598,6 +620,9 @@ int main(int argc, char **argv)
 			case L_POST_DATA:
 				post_data = optarg;
 				break;
+			case L_POST_FILE:
+				post_file = optarg;
+				break;
 			case L_SPIDER:
 				no_output = true;
 				break;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: post_file.patch
Type: text/x-patch
Size: 2024 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20200612/b38cce2d/attachment.bin>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list