ptlink
December 7, 2018, 8:44am
1
Running openwrt-18.06 branch (git-18.228.31946-f64b152)
When submitting a POST request using built-in wget command (which is actually uclient-fetch), the Content-Length is not present. Example:
wget --post-data="a=b" http://httpbin.org/post -O -
Server receive the following headers:
"headers": {
"Connection": "close",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "[httpbin.org](http://httpbin.org)",
"Transfer-Encoding": "chunked",
"User-Agent": "uclient-fetch"
Transfer-Encoding "chunked" used and there is no Content-length provided
Since the post data size is known during the request, is there a way to force Content-length header to appear?
Thanks
you can't
this is source code of uclient-fetch, also you cannot set Content-Type
static int init_request(struct uclient *cl)
320 {
321 int rc;
322
323 out_offset = 0;
324 out_bytes = 0;
325 out_len = 0;
326 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
327
328 if (timeout)
329 cl->timeout_msecs = timeout * 1000;
330
331 rc = uclient_connect(cl);
332 if (rc)
333 return rc;
334
335 msg_connecting(cl);
336
337 rc = uclient_http_set_request_type(cl, post_data ? "POST" : "GET");
338 if (rc)
339 return rc;
340
341 uclient_http_reset_headers(cl);
342 uclient_http_set_header(cl, "User-Agent", user_agent);
343 if (cur_resume)
344 check_resume_offset(cl);
345
346 if (post_data) {
347 uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded");
348 uclient_write(cl, post_data, strlen(post_data));
349 }
350
351 rc = uclient_request(cl);
352 if (rc)
353 return rc;
354
355 return 0;
356 }
Discussion when the code was added:
https://patchwork.ozlabs.org/project/lede/patch/CAHQM780agD+6-cCQmWuHzguHYVQiKiS-xAw+R7u=a_PLvKP3Cg@mail.gmail.com/#1382522
The uclient-fetch has inconvenient design in that place so it can send only with chunked encoding even while we already know the Content-Length.
But technically it's easy to add the CL header but I don't know if webservers can handle it correctly if CL is used with a chunked.
BusyBox httpd has a bug when sends the body with chunks as is into CGI script . So you can't use uclient-fetch with BB httpd. But uhttpd works correctly.