git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 04/10] Extending internal CURL wrapper for POST upload

From
AWAnton Wuerfel <anton.wuerfel@fau.de>
Date
Mar 17, 2016, 18:46 UTC
Message-ID
<1458240421-3593-5-git-send-email-anton.wuerfel@fau.de>
In-Reply-To
<1458240421-3593-1-git-send-email-anton.wuerfel@fau.de>

This commit adds POST upload capabilities to the internal libcurl wrapper, which are necessary for uploading RFC3161 Time Stamping Queries to a Time Stamping Authority.

Therefore, the internal struct http_get_options is renamed to http_request_options and extended by 2 strbuf fields named `postfields` and `post_content_type`. If either one of them is non-NULL, the corresponding curl_easy_setopt() functions are called.

Signed-off-by: Anton Würfel <anton.wuerfel@fau.de>
Signed-off-by: Phillip Raffeck <phillip.raffeck@fau.de>
---
 http.c        | 30 +++++++++++++++++++++++-------
 http.h        | 17 +++++++++++++++--
 remote-curl.c |  2 +-
 3 files changed, 39 insertions(+), 10 deletions(-)
diff --git a/http.c b/http.c
index 69da445..a108658 100644
--- a/http.c
+++ b/http.c
@@ -1373,17 +1373,19 @@ static void http_opt_request_remainder(CURL *curl, off_t pos)
 
 static int http_request(const char *url,
 			void *result, int target,
-			const struct http_get_options *options)
+			const struct http_request_options *options)
 {
 	struct active_request_slot *slot;
 	struct slot_results results;
 	struct curl_slist *headers = NULL;
 	struct strbuf buf = STRBUF_INIT;
+	struct strbuf content_type = STRBUF_INIT;
 	const char *accept_language;
 	int ret;
 
 	slot = get_active_slot();
-	curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
+	if (options && !options->postfields)
+		curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 
 	if (result == NULL) {
 		curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
@@ -1407,6 +1409,19 @@ static int http_request(const char *url,
 	if (accept_language)
 		headers = curl_slist_append(headers, accept_language);
 
+	if (options && options->postfields && options->post_content_type) {
+		strbuf_addstr(&content_type, "Content-Type: ");
+		strbuf_addbuf(&content_type, options->post_content_type);
+		headers = curl_slist_append(headers, content_type.buf);
+	}
+
+	if (options && options->postfields) {
+		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS,
+				 options->postfields->buf);
+		curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE,
+				 options->postfields->len);
+	}
+
 	strbuf_addstr(&buf, "Pragma:");
 	if (options && options->no_cache)
 		strbuf_addstr(&buf, " no-cache");
@@ -1434,6 +1449,7 @@ static int http_request(const char *url,
 				options->effective_url);
 
 	curl_slist_free_all(headers);
+	strbuf_release(&content_type);
 	strbuf_release(&buf);
 
 	return ret;
@@ -1491,7 +1507,7 @@ static int update_url_from_redirect(struct strbuf *base,
 
 static int http_request_reauth(const char *url,
 			       void *result, int target,
-			       struct http_get_options *options)
+			       struct http_request_options *options)
 {
 	int ret = http_request(url, result, target, options);
 
@@ -1529,7 +1545,7 @@ static int http_request_reauth(const char *url,
 
 int http_get_strbuf(const char *url,
 		    struct strbuf *result,
-		    struct http_get_options *options)
+		    struct http_request_options *options)
 {
 	return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
 }
@@ -1541,7 +1557,7 @@ int http_get_strbuf(const char *url,
  * file is still around) the download is resumed.
  */
 static int http_get_file(const char *url, const char *filename,
-			 struct http_get_options *options)
+			 struct http_request_options *options)
 {
 	int ret;
 	struct strbuf tmpfile = STRBUF_INIT;
@@ -1567,7 +1583,7 @@ cleanup:
 
 int http_fetch_ref(const char *base, struct ref *ref)
 {
-	struct http_get_options options = {0};
+	struct http_request_options options = {0};
 	char *url;
 	struct strbuf buffer = STRBUF_INIT;
 	int ret = -1;
@@ -1659,7 +1675,7 @@ add_pack:
 
 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
 {
-	struct http_get_options options = {0};
+	struct http_request_options options = {0};
 	int ret = 0, i = 0;
 	char *url, *data;
 	struct strbuf buf = STRBUF_INIT;
diff --git a/http.h b/http.h
index 4ef4bbd..0b7cebf 100644
--- a/http.h
+++ b/http.h
@@ -136,7 +136,7 @@ extern char *get_remote_object_url(const char *url, const char *hex,
 				   int only_two_digit_prefix);
 
 /* Options for http_get_*() */
-struct http_get_options {
+struct http_request_options {
 	unsigned no_cache:1,
 		 keep_error:1;
 
@@ -163,6 +163,18 @@ struct http_get_options {
 	 * for details.
 	 */
 	struct strbuf *base_url;
+
+	/*
+	 * If non-NULL, a HTTP POST request is sent with the raw post data
+	 * contained in postfields.
+	 */
+	struct strbuf *postfields;
+
+	/*
+	 * If non-NULL, a content-type header is applied to the HTTP POST
+	 * request.
+	 */
+	struct strbuf *post_content_type;
 };
 
 /* Return values for http_get_*() */
@@ -178,7 +190,8 @@ struct http_get_options {
  *
  * If the result pointer is NULL, a HTTP HEAD request is made instead of GET.
  */
-int http_get_strbuf(const char *url, struct strbuf *result, struct http_get_options *options);
+int http_get_strbuf(const char *url, struct strbuf *result,
+		    struct http_request_options *options);
 
 extern int http_fetch_ref(const char *base, struct ref *ref);
 
diff --git a/remote-curl.c b/remote-curl.c
index 15e48e2..345a5e6 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -254,7 +254,7 @@ static struct discovery *discover_refs(const char *service, int for_push)
 	struct strbuf effective_url = STRBUF_INIT;
 	struct discovery *last = last_discovery;
 	int http_ret, maybe_smart = 0;
-	struct http_get_options options;
+	struct http_request_options options;
 
 	if (last && !strcmp(service, last->service))
 		return last;
-- 
2.8.0.rc0.62.gfc8aefa.dirty
Previous: Anton WuerfelNext: Anton Wuerfel
Message 5 of 11 in “Adding RFC3161 time-stamps to tags”
  1. 00/10 Adding RFC3161 time-stamps to tagsAnton Wuerfel, Mar 17, 2016
  2. 01/10 Add Testcases for time-stamping functionalityAnton Wuerfel, Mar 17, 2016
  3. 02/10 Documentation for time-stamping functionalityAnton Wuerfel, Mar 17, 2016
  4. 03/10 Documentation: Whitespace fixAnton Wuerfel, Mar 17, 2016
  5. 04/10 Extending internal CURL wrapper for POST uploadAnton Wuerfel, Mar 17, 2016
  6. 05/10 Make PGP macros globalAnton Wuerfel, Mar 17, 2016
  7. 06/10 Add basic RFC3161 functionalityAnton Wuerfel, Mar 17, 2016
  8. 07/10 Add git-http-timestamp helper toolAnton Wuerfel, Mar 17, 2016
  9. 08/10 Adding time-stamping helper toolAnton Wuerfel, Mar 17, 2016
  10. 09/10 Add time-stamping functionality to git verify-tagAnton Wuerfel, Mar 17, 2016
  11. 10/10 Add time-stamping functionality to git tagAnton Wuerfel, Mar 17, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.