aboutsummaryrefslogtreecommitdiff
path: root/http.c
diff options
context:
space:
mode:
authorMike Hommey <mh@glandium.org>2009-06-06 16:43:53 +0800
committerJunio C Hamano <gitster@pobox.com>2009-06-06 10:56:27 -0700
commite929cd20bb3557833765f95fe0e9143a47f806e7 (patch)
treedd136a97f02f20db4402173cbd270f8ac04fd830 /http.c
parent5ace994f3384726fa993de526fc059d1343a6463 (diff)
downloadgit-e929cd20bb3557833765f95fe0e9143a47f806e7.tar.gz
git-e929cd20bb3557833765f95fe0e9143a47f806e7.tar.xz
http.c: new functions for the http API
The new functions added are: - http_request() (internal function) - http_get_strbuf() - http_get_file() - http_error() http_get_strbuf and http_get_file allow respectively to retrieve contents of an URL to a strbuf or an opened file handle. http_error prints out an error message containing the URL and the curl error (in curl_errorstr). Signed-off-by: Mike Hommey <mh@glandium.org> Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'http.c')
-rw-r--r--http.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/http.c b/http.c
index 75fce9e94..df2218077 100644
--- a/http.c
+++ b/http.c
@@ -665,6 +665,110 @@ static char *quote_ref_url(const char *base, const char *ref)
return strbuf_detach(&buf, NULL);
}
+/* 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;
+ int ret;
+
+ 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);
+
+ 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)
+ ret = HTTP_OK;
+ else if (missing_target(&results))
+ ret = HTTP_MISSING_TARGET;
+ else
+ ret = HTTP_ERROR;
+ } else {
+ error("Unable to start HTTP request for %s", url);
+ ret = HTTP_START_FAILED;
+ }
+
+ slot->local = NULL;
+ curl_slist_free_all(headers);
+ strbuf_release(&buf);
+
+ return ret;
+}
+
+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;
+}
+
+int http_error(const char *url, int ret)
+{
+ /* http_request has already handled HTTP_START_FAILED. */
+ if (ret != HTTP_START_FAILED)
+ error("%s while accessing %s\n", curl_errorstr, url);
+
+ return ret;
+}
+
int http_fetch_ref(const char *base, struct ref *ref)
{
char *url;