git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mike Hommey <mh@glandium.org>
To: git@vger.kernel.org, gitster@pobox.com
Cc: johannes.schindelin@gmx.de
Subject: [WIP Patch 03/12] Two new functions for the http API
Date: Sun, 18 Jan 2009 09:04:28 +0100	[thread overview]
Message-ID: <1232265877-3649-4-git-send-email-mh@glandium.org> (raw)
In-Reply-To: <1232265877-3649-3-git-send-email-mh@glandium.org>

http_get_strbuf and http_get_file allow respectively to retrieve contents of
an URL to a strbuf or an opened file handle.

Both these functions are the beginning of the http code refactoring.

Signed-off-by: Mike Hommey <mh@glandium.org>
---
 http.c |   85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 http.h |   17 +++++++++++++
 2 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/http.c b/http.c
index ee58799..82534cf 100644
--- a/http.c
+++ b/http.c
@@ -638,3 +638,88 @@ int http_fetch_ref(const char *base, struct ref *ref)
 	free(url);
 	return ret;
 }
+
+/* http_request() targets */
+#define HTTP_REQUEST_STRBUF	0
+#define HTTP_REQUEST_FILE	1
+
+static int http_request(const char *url, void *result, int target, int options)
+{
+	struct active_request_slot *slot;
+	struct slot_results results;
+	struct curl_slist *headers = NULL;
+	struct strbuf buf = STRBUF_INIT;
+
+	slot = get_active_slot();
+	slot->results = &results;
+	curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
+
+	if (result == NULL) {
+		curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
+	} else {
+		curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
+		curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
+
+		if (target == HTTP_REQUEST_FILE) {
+			long posn = ftell(result);
+			curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
+					 fwrite);
+			if (posn > 0) {
+				strbuf_addf(&buf, "Range: bytes=%ld-", posn);
+				headers = curl_slist_append(headers, buf.buf);
+				strbuf_reset(&buf);
+			}
+			slot->local = result;
+		} else
+			curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
+					 fwrite_buffer);
+	}
+
+	strbuf_addstr(&buf, "Pragma:");
+	if (options & HTTP_NO_CACHE)
+		strbuf_addstr(&buf, " no-cache");
+
+	headers = curl_slist_append(headers, buf.buf);
+	strbuf_release(&buf);
+
+	curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
+
+	if (start_active_slot(slot)) {
+		run_active_slot(slot);
+		if (results.curl_result == CURLE_OK)
+			return HTTP_OK;
+		else if (missing_target(&results))
+			return HTTP_MISSING_TARGET;
+	}
+	return HTTP_ERROR;
+}
+
+int http_get_strbuf(const char *url, struct strbuf *result, int options)
+{
+	return http_request(url, result, HTTP_REQUEST_STRBUF, options);
+}
+
+int http_get_file(const char *url, const char *filename, int options)
+{
+	int ret;
+	struct strbuf tmpfile = STRBUF_INIT;
+	FILE *result;
+
+	strbuf_addf(&tmpfile, "%s.temp", filename);
+	result = fopen(tmpfile.buf, "a");
+	if (! result) {
+		error("Unable to open local file %s", tmpfile.buf);
+		ret = HTTP_ERROR;
+		goto cleanup;
+	}
+
+	ret = http_request(url, result, HTTP_REQUEST_FILE, options);
+	fclose(result);
+
+	if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
+		ret = HTTP_ERROR;
+cleanup:
+	strbuf_release(&tmpfile);
+	return ret;
+}
diff --git a/http.h b/http.h
index 905b462..323c780 100644
--- a/http.h
+++ b/http.h
@@ -104,4 +104,21 @@ static inline int missing__target(int code, int result)
 
 extern int http_fetch_ref(const char *base, struct ref *ref);
 
+/* Options for http_request_*() */
+#define HTTP_NO_CACHE		1
+
+/* Return values for http_request_*() */
+#define HTTP_OK			0
+#define HTTP_MISSING_TARGET	1
+#define HTTP_ERROR		2
+
+/* Requests an url and stores the result in a strbuf.
+ * 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, int options);
+
+/* Downloads an url and stores the result in the given file.
+ * If a previous interrupted download is detected (i.e. a previous temporary
+ * file is still around) the download is resumed. */
+int http_get_file(const char *url, const char *filename, int options);
+
 #endif /* HTTP_H */
-- 
1.6.1.141.gb32a

  reply	other threads:[~2009-01-18  8:06 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-17 15:36 [PATCH] http-push: fix off-by-path_len Johannes Schindelin
2009-01-17 15:40 ` Where's Nick?, was " Johannes Schindelin
2009-01-17 15:41 ` [PATCH] t5540: clarify that http-push does not handle packed-refs on the remote Johannes Schindelin
2009-01-18  7:49 ` [PATCH] http-push: fix off-by-path_len Mike Hommey
2009-01-18  8:04   ` [WIP Patch 00/12] Refactoring the http API Mike Hommey
2009-01-18  8:04     ` [WIP Patch 01/12] Don't expect verify_pack() callers to set pack_size Mike Hommey
2009-01-18  8:04       ` [WIP Patch 02/12] Some cleanup in get_refs_via_curl() Mike Hommey
2009-01-18  8:04         ` Mike Hommey [this message]
2009-01-18  8:04           ` [WIP Patch 04/12] Use the new http API in http_fetch_ref() Mike Hommey
2009-01-18  8:04             ` [WIP Patch 05/12] Use the new http API in get_refs_via_curl() Mike Hommey
2009-01-18  8:04               ` [WIP Patch 06/12] Use the new http API in http-walker.c:fetch_indices() Mike Hommey
2009-01-18  8:04                 ` [WIP Patch 07/12] Use the new http API in http-push.c:fetch_indices() Mike Hommey
2009-01-18  8:04                   ` [WIP Patch 08/12] Use the new http API in update_remote_info_refs() Mike Hommey
2009-01-18  8:04                     ` [WIP Patch 09/12] Use the new http API in fetch_symref() Mike Hommey
2009-01-18  8:04                       ` [WIP Patch 10/12] Use the new http API in http-walker.c:fetch_index() Mike Hommey
2009-01-18  8:04                         ` [WIP Patch 11/12] Use the new http API in http-push.c:fetch_index() Mike Hommey
2009-01-18  8:04                           ` [WIP Patch 12/12] Use the new http API in http-walker.c:fetch_pack() Mike Hommey
2009-01-18 15:18                     ` [WIP Patch 08/12] Use the new http API in update_remote_info_refs() Johannes Schindelin
2009-01-18 19:23                       ` Mike Hommey
2009-01-18 15:14                   ` [WIP Patch 07/12] Use the new http API in http-push.c:fetch_indices() Johannes Schindelin
2009-01-18 15:12               ` [WIP Patch 05/12] Use the new http API in get_refs_via_curl() Johannes Schindelin
2009-01-18 15:10             ` [WIP Patch 04/12] Use the new http API in http_fetch_ref() Johannes Schindelin
2009-01-18 19:21               ` Mike Hommey
2009-01-18 15:03           ` [WIP Patch 03/12] Two new functions for the http API Johannes Schindelin
2009-01-18 19:06         ` [WIP Patch 02/12] Some cleanup in get_refs_via_curl() Johannes Schindelin
2009-01-18 19:11           ` Johannes Schindelin
2009-01-18 19:30             ` Mike Hommey
2009-01-18 21:09               ` Johannes Schindelin
2009-01-18 19:19           ` Mike Hommey
2009-01-18 21:10             ` Johannes Schindelin
2009-01-18  8:30     ` [WIP Patch 00/12] Refactoring the http API Junio C Hamano
2009-01-18  9:12       ` Mike Hommey
2009-01-18 11:29         ` Boyd Stephen Smith Jr.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1232265877-3649-4-git-send-email-mh@glandium.org \
    --to=mh@glandium.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).