Code

Provide git_config with a callback-data parameter
[git.git] / http.c
diff --git a/http.c b/http.c
index d2c11aee9077e0834c5c56b1284df478eb8debbc..2a21ccbb76351d108301d2b9c334e2c5a5feacd9 100644 (file)
--- a/http.c
+++ b/http.c
@@ -90,7 +90,7 @@ static void process_curl_messages(void)
 }
 #endif
 
-static int http_options(const char *var, const char *value)
+static int http_options(const char *var, const char *value, void *cb)
 {
        if (!strcmp("http.sslverify", var)) {
                if (curl_ssl_verify == -1) {
@@ -101,16 +101,18 @@ static int http_options(const char *var, const char *value)
 
        if (!strcmp("http.sslcert", var)) {
                if (ssl_cert == NULL) {
-                       ssl_cert = xmalloc(strlen(value)+1);
-                       strcpy(ssl_cert, value);
+                       if (!value)
+                               return config_error_nonbool(var);
+                       ssl_cert = xstrdup(value);
                }
                return 0;
        }
 #if LIBCURL_VERSION_NUM >= 0x070902
        if (!strcmp("http.sslkey", var)) {
                if (ssl_key == NULL) {
-                       ssl_key = xmalloc(strlen(value)+1);
-                       strcpy(ssl_key, value);
+                       if (!value)
+                               return config_error_nonbool(var);
+                       ssl_key = xstrdup(value);
                }
                return 0;
        }
@@ -118,16 +120,18 @@ static int http_options(const char *var, const char *value)
 #if LIBCURL_VERSION_NUM >= 0x070908
        if (!strcmp("http.sslcapath", var)) {
                if (ssl_capath == NULL) {
-                       ssl_capath = xmalloc(strlen(value)+1);
-                       strcpy(ssl_capath, value);
+                       if (!value)
+                               return config_error_nonbool(var);
+                       ssl_capath = xstrdup(value);
                }
                return 0;
        }
 #endif
        if (!strcmp("http.sslcainfo", var)) {
                if (ssl_cainfo == NULL) {
-                       ssl_cainfo = xmalloc(strlen(value)+1);
-                       strcpy(ssl_cainfo, value);
+                       if (!value)
+                               return config_error_nonbool(var);
+                       ssl_cainfo = xstrdup(value);
                }
                return 0;
        }
@@ -157,14 +161,15 @@ static int http_options(const char *var, const char *value)
        }
        if (!strcmp("http.proxy", var)) {
                if (curl_http_proxy == NULL) {
-                       curl_http_proxy = xmalloc(strlen(value)+1);
-                       strcpy(curl_http_proxy, value);
+                       if (!value)
+                               return config_error_nonbool(var);
+                       curl_http_proxy = xstrdup(value);
                }
                return 0;
        }
 
        /* Fall back on the default ones */
-       return git_default_config(var, value);
+       return git_default_config(var, value, cb);
 }
 
 static CURL* get_curl_handle(void)
@@ -213,13 +218,16 @@ static CURL* get_curl_handle(void)
        return result;
 }
 
-void http_init(void)
+void http_init(struct remote *remote)
 {
        char *low_speed_limit;
        char *low_speed_time;
 
        curl_global_init(CURL_GLOBAL_ALL);
 
+       if (remote && remote->http_proxy)
+               curl_http_proxy = xstrdup(remote->http_proxy);
+
        pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
 
 #ifdef USE_CURL_MULTI
@@ -255,7 +263,7 @@ void http_init(void)
        if (low_speed_time != NULL)
                curl_low_speed_time = strtol(low_speed_time, NULL, 10);
 
-       git_config(http_options);
+       git_config(http_options, NULL);
 
        if (curl_ssl_verify == -1)
                curl_ssl_verify = 1;
@@ -276,23 +284,15 @@ void http_init(void)
 void http_cleanup(void)
 {
        struct active_request_slot *slot = active_queue_head;
-#ifdef USE_CURL_MULTI
-       char *wait_url;
-#endif
 
        while (slot != NULL) {
                struct active_request_slot *next = slot->next;
+               if (slot->curl != NULL) {
 #ifdef USE_CURL_MULTI
-               if (slot->in_use) {
-                       curl_easy_getinfo(slot->curl,
-                                         CURLINFO_EFFECTIVE_URL,
-                                         &wait_url);
-                       fprintf(stderr, "Waiting for %s\n", wait_url);
-                       run_active_slot(slot);
-               }
+                       curl_multi_remove_handle(curlm, slot->curl);
 #endif
-               if (slot->curl != NULL)
                        curl_easy_cleanup(slot->curl);
+               }
                free(slot);
                slot = next;
        }
@@ -309,6 +309,11 @@ void http_cleanup(void)
 
        curl_slist_free_all(pragma_header);
        pragma_header = NULL;
+
+       if (curl_http_proxy) {
+               free(curl_http_proxy);
+               curl_http_proxy = NULL;
+       }
 }
 
 struct active_request_slot *get_active_slot(void)
@@ -584,8 +589,9 @@ static char *quote_ref_url(const char *base, const char *ref)
                        len += 2; /* extra two hex plus replacement % */
        qref = xmalloc(len);
        memcpy(qref, base, baselen);
-       memcpy(qref + baselen, "/refs/", 6);
-       for (cp = ref, dp = qref + baselen + 6; (ch = *cp) != 0; cp++) {
+       dp = qref + baselen;
+       *(dp++) = '/';
+       for (cp = ref; (ch = *cp) != 0; cp++) {
                if (needs_quote(ch)) {
                        *dp++ = '%';
                        *dp++ = hex((ch >> 4) & 0xF);
@@ -599,7 +605,7 @@ static char *quote_ref_url(const char *base, const char *ref)
        return qref;
 }
 
-int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
+int http_fetch_ref(const char *base, struct ref *ref)
 {
        char *url;
        struct strbuf buffer = STRBUF_INIT;
@@ -607,7 +613,7 @@ int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
        struct slot_results results;
        int ret;
 
-       url = quote_ref_url(base, ref);
+       url = quote_ref_url(base, ref->name);
        slot = get_active_slot();
        slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
@@ -619,12 +625,15 @@ int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1)
                if (results.curl_result == CURLE_OK) {
                        strbuf_rtrim(&buffer);
                        if (buffer.len == 40)
-                               ret = get_sha1_hex(buffer.buf, sha1);
-                       else
+                               ret = get_sha1_hex(buffer.buf, ref->old_sha1);
+                       else if (!prefixcmp(buffer.buf, "ref: ")) {
+                               ref->symref = xstrdup(buffer.buf + 5);
+                               ret = 0;
+                       } else
                                ret = 1;
                } else {
                        ret = error("Couldn't get %s for %s\n%s",
-                                   url, ref, curl_errorstr);
+                                   url, ref->name, curl_errorstr);
                }
        } else {
                ret = error("Unable to start request");