From patchwork Fri Jun 17 11:19:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xinglp X-Patchwork-Id: 636915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rWHsL0t48z9t2G for ; Fri, 17 Jun 2016 21:22:14 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=kOZw0X+g; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1bDrp8-0003iL-Tl; Fri, 17 Jun 2016 11:20:30 +0000 Received: from mail-oi0-x244.google.com ([2607:f8b0:4003:c06::244]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1bDrp7-0003g0-3g for lede-dev@lists.infradead.org; Fri, 17 Jun 2016 11:20:29 +0000 Received: by mail-oi0-x244.google.com with SMTP id a64so13138505oii.1 for ; Fri, 17 Jun 2016 04:20:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=0WFYQacLS8fEEapXFItAjjisRlgb3uzBJdU5k7rEy/M=; b=kOZw0X+gmaJclKXIMGNr6c5C1Po0kkU5yBIu+xnBTB9fRB2Doy1CJrppcrOubq3yaC IEzzzoduSTPzHXho4YFpDckiAXHqWi80nag9jnj1qVs5c5gDfVLcHk8TIZx5J+Yg3Hox VyzWff0PXV3G335KJOSNjE+AOYvOnJmCRsZ5HzMdbdC6VVW/S9AZaTcj9WZTjPlEY8T+ 361jvAimVWNkWQK8dk6iwmFw0Ym/q40OjXpqyDzww41wWnfC64zzKbU36GsqKnqY2LGJ pCcDCJRkX3+LUGIOkx21zQ5FM0eqYjmWl1v8X6Jcm3Ip5C0UbhHgzIqZMzgQEq7zTAA0 yEnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=0WFYQacLS8fEEapXFItAjjisRlgb3uzBJdU5k7rEy/M=; b=kxfbXpZtUtaVM++xh67qCpl1giYLkBAlJYFttEGJQNYoZAzJbMiQb0Z7jzXM2cTRNY 4SFmBiI74jqgEuy0sK0CgRL+PEH42PEJV7adLUn/9iRC0nf+6vEgw5JvD8izUDmFJ27N S5QbhxVS4CzKct/HR+7ClOtDcMp9smdTDM8Hj9ekQHL4vbSvUxGnlO+SOKVaPWZUlBr+ I9OJ876RqgPJ2cFPbsNpWBeJEtB0pZxhw8tGfKJDF34GygXAyTh7icu8iHO53q7ZEFq2 wbdDeo91NtgKpN04zOB69Esr7RREZb60IQJNBJurbSEgqP5gvkhXQTgqW4L8mdwAkKQv UZsA== X-Gm-Message-State: ALyK8tKmOYk7FgT2q2y7b1vb8uAwRm7T0ZbOXjqxUdRPsHKps2AnLlKhgVpeqGF+IL5j9z0sF0FMhUEpg0Evow== X-Received: by 10.202.245.129 with SMTP id t123mr855038oih.92.1466162407435; Fri, 17 Jun 2016 04:20:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.202.170.86 with HTTP; Fri, 17 Jun 2016 04:19:48 -0700 (PDT) From: xinglp Date: Fri, 17 Jun 2016 19:19:48 +0800 Message-ID: To: lede-dev@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160617_042029_269689_D5EB13E2 X-CRM114-Status: GOOD ( 11.61 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2607:f8b0:4003:c06:0:0:0:244 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (xinglp[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid Subject: [LEDE-DEV] [PATCH uclient] enable --post-file, use "Content-Length" instead of chunked encoding X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org --post-file parameter only support text file not binary file. "Content-Length" is more compatible to the http server world. Signed-off-by: xinglp --- { @@ -576,9 +564,6 @@ uclient_http_send_headers(struct uclient_http *uh) blobmsg_for_each_attr(cur, uh->headers.head, rem) ustream_printf(uh->us, "%s: %s\r\n", blobmsg_name(cur), (char *) blobmsg_data(cur)); - if (uclient_request_supports_body(uh->req_type)) - ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n"); - uclient_http_add_auth_header(uh); ustream_printf(uh->us, "\r\n"); @@ -984,12 +969,16 @@ uclient_http_send_data(struct uclient *cl, const char *buf, unsigned int len) if (uh->state >= HTTP_STATE_REQUEST_DONE) return -1; + if (len > 0) { + char buf[32]; + sprintf(buf, "%d", len); + uclient_http_set_header(cl, "Content-Length", buf); + } + uclient_http_send_headers(uh); if (len > 0) { - ustream_printf(uh->us, "%X\r\n", len); ustream_write(uh->us, buf, len, false); - ustream_printf(uh->us, "\r\n"); } return len; @@ -1004,8 +993,6 @@ uclient_http_request_done(struct uclient *cl) return -1; uclient_http_send_headers(uh); - if (uclient_request_supports_body(uh->req_type)) - ustream_printf(uh->us, "0\r\n\r\n"); uh->state = HTTP_STATE_REQUEST_DONE; return 0; diff --git a/uclient-fetch.c b/uclient-fetch.c index 065742e..13f2fe2 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 char *post_file_content; static struct ustream_ssl_ctx *ssl_ctx; static const struct ustream_ssl_ops *ssl_ops; static int quiet = false; @@ -449,6 +450,7 @@ static int usage(const char *progname) " --password= HTTP authentication password\n" " --user-agent|-U 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 contents of FILE\n" " --spider|-s Spider mode - only check file existence\n" " --timeout=N|-T N Set connect/request timeout to N seconds\n" " --proxy=on|off|-Y on|off Enable/disable env var configured proxy\n" @@ -491,6 +493,30 @@ static int no_ssl(const char *progname) return 1; } +static ssize_t load_file(const char *path, char **buf) +{ + int fd; + if ((fd = open(path, O_RDONLY)) < 0) { + if (!quiet) + fprintf(stderr, "open %s: %s\n", path, strerror(errno)); + return -1; + } + struct stat st; + if (fstat(fd, &st)) { + if (!quiet) + fprintf(stderr, "fstat %s: %s\n", path, strerror(errno)); + return -1; + } + *buf = malloc(st.st_size); + if(st.st_size != read(fd, *buf, st.st_size)) { + if (!quiet) + fprintf(stderr, "read size different from fstat %s\n", path); + return -1; + } + close(fd); + return st.st_size; +} + enum { L_NO_CHECK_CERTIFICATE, L_CA_CERTIFICATE, @@ -498,6 +524,7 @@ enum { L_PASSWORD, L_USER_AGENT, L_POST_DATA, + L_POST_FILE, L_SPIDER, L_TIMEOUT, L_CONTINUE, @@ -512,6 +539,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 }, @@ -568,6 +596,11 @@ int main(int argc, char **argv) case L_POST_DATA: post_data = optarg; break; + case L_POST_FILE: + if(load_file(optarg, &post_file_content) < 0) + exit(1); + post_data = post_file_content; + break; case L_SPIDER: no_output = true; break; @@ -697,5 +730,8 @@ int main(int argc, char **argv) if (ssl_ctx) ssl_ops->context_free(ssl_ctx); + if (post_file_content) + free(post_file_content); + return error_ret; } diff --git a/uclient-http.c b/uclient-http.c index f0451cc..07de2d4 100644 --- a/uclient-http.c +++ b/uclient-http.c @@ -286,18 +286,6 @@ static void uclient_http_process_headers(struct uclient_http *uh) uh->auth_type = uclient_http_update_auth_type(uh); } -static bool uclient_request_supports_body(enum request_type req_type) -{ - switch (req_type) { - case REQ_POST: - case REQ_PUT: - case REQ_DELETE: - return true; - default: - return false; - } -} - static void uclient_http_add_auth_basic(struct uclient_http *uh)