Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 1 | #include "http.h" |
| 2 | |
| 3 | int data_received; |
| 4 | int active_requests = 0; |
| 5 | |
| 6 | #ifdef USE_CURL_MULTI |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 7 | static int max_requests = -1; |
| 8 | static CURLM *curlm; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 9 | #endif |
| 10 | #ifndef NO_CURL_EASY_DUPHANDLE |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 11 | static CURL *curl_default; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 12 | #endif |
| 13 | char curl_errorstr[CURL_ERROR_SIZE]; |
| 14 | |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 15 | static int curl_ssl_verify = -1; |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 16 | static const char *ssl_cert = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 17 | #if LIBCURL_VERSION_NUM >= 0x070902 |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 18 | static const char *ssl_key = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 19 | #endif |
| 20 | #if LIBCURL_VERSION_NUM >= 0x070908 |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 21 | static const char *ssl_capath = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 22 | #endif |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 23 | static const char *ssl_cainfo = NULL; |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 24 | static long curl_low_speed_limit = -1; |
| 25 | static long curl_low_speed_time = -1; |
| 26 | static int curl_ftp_no_epsv = 0; |
| 27 | static char *curl_http_proxy = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 28 | |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 29 | static struct curl_slist *pragma_header; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 30 | |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 31 | static struct active_request_slot *active_queue_head = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 32 | |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 33 | size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 34 | { |
| 35 | size_t size = eltsize * nmemb; |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 36 | struct buffer *buffer = buffer_; |
| 37 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 38 | if (size > buffer->buf.len - buffer->posn) |
| 39 | size = buffer->buf.len - buffer->posn; |
| 40 | memcpy(ptr, buffer->buf.buf + buffer->posn, size); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 41 | buffer->posn += size; |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 42 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 43 | return size; |
| 44 | } |
| 45 | |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 46 | size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 47 | { |
| 48 | size_t size = eltsize * nmemb; |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 49 | struct strbuf *buffer = buffer_; |
| 50 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 51 | strbuf_add(buffer, ptr, size); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 52 | data_received++; |
| 53 | return size; |
| 54 | } |
| 55 | |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 56 | size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 57 | { |
| 58 | data_received++; |
| 59 | return eltsize * nmemb; |
| 60 | } |
| 61 | |
| 62 | static void finish_active_slot(struct active_request_slot *slot); |
| 63 | |
| 64 | #ifdef USE_CURL_MULTI |
| 65 | static void process_curl_messages(void) |
| 66 | { |
| 67 | int num_messages; |
| 68 | struct active_request_slot *slot; |
| 69 | CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); |
| 70 | |
| 71 | while (curl_message != NULL) { |
| 72 | if (curl_message->msg == CURLMSG_DONE) { |
| 73 | int curl_result = curl_message->data.result; |
| 74 | slot = active_queue_head; |
| 75 | while (slot != NULL && |
| 76 | slot->curl != curl_message->easy_handle) |
| 77 | slot = slot->next; |
| 78 | if (slot != NULL) { |
| 79 | curl_multi_remove_handle(curlm, slot->curl); |
| 80 | slot->curl_result = curl_result; |
| 81 | finish_active_slot(slot); |
| 82 | } else { |
| 83 | fprintf(stderr, "Received DONE message for unknown request!\n"); |
| 84 | } |
| 85 | } else { |
| 86 | fprintf(stderr, "Unknown CURL message received: %d\n", |
| 87 | (int)curl_message->msg); |
| 88 | } |
| 89 | curl_message = curl_multi_info_read(curlm, &num_messages); |
| 90 | } |
| 91 | } |
| 92 | #endif |
| 93 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 94 | static int http_options(const char *var, const char *value, void *cb) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 95 | { |
| 96 | if (!strcmp("http.sslverify", var)) { |
| 97 | if (curl_ssl_verify == -1) { |
| 98 | curl_ssl_verify = git_config_bool(var, value); |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | if (!strcmp("http.sslcert", var)) { |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 104 | if (ssl_cert == NULL) |
| 105 | return git_config_string(&ssl_cert, var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | #if LIBCURL_VERSION_NUM >= 0x070902 |
| 109 | if (!strcmp("http.sslkey", var)) { |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 110 | if (ssl_key == NULL) |
| 111 | return git_config_string(&ssl_key, var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | #endif |
| 115 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| 116 | if (!strcmp("http.sslcapath", var)) { |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 117 | if (ssl_capath == NULL) |
| 118 | return git_config_string(&ssl_capath, var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | #endif |
| 122 | if (!strcmp("http.sslcainfo", var)) { |
Brian Hetro | 7ef8ea7 | 2008-07-05 01:24:44 -0400 | [diff] [blame] | 123 | if (ssl_cainfo == NULL) |
| 124 | return git_config_string(&ssl_cainfo, var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 128 | #ifdef USE_CURL_MULTI |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 129 | if (!strcmp("http.maxrequests", var)) { |
| 130 | if (max_requests == -1) |
| 131 | max_requests = git_config_int(var, value); |
| 132 | return 0; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | if (!strcmp("http.lowspeedlimit", var)) { |
| 137 | if (curl_low_speed_limit == -1) |
| 138 | curl_low_speed_limit = (long)git_config_int(var, value); |
| 139 | return 0; |
| 140 | } |
| 141 | if (!strcmp("http.lowspeedtime", var)) { |
| 142 | if (curl_low_speed_time == -1) |
| 143 | curl_low_speed_time = (long)git_config_int(var, value); |
| 144 | return 0; |
| 145 | } |
| 146 | |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 147 | if (!strcmp("http.noepsv", var)) { |
| 148 | curl_ftp_no_epsv = git_config_bool(var, value); |
| 149 | return 0; |
| 150 | } |
Sam Vilain | 9c5665a | 2007-11-23 13:07:00 +1300 | [diff] [blame] | 151 | if (!strcmp("http.proxy", var)) { |
| 152 | if (curl_http_proxy == NULL) { |
Junio C Hamano | 111dd25 | 2008-02-11 10:57:22 -0800 | [diff] [blame] | 153 | if (!value) |
| 154 | return config_error_nonbool(var); |
| 155 | curl_http_proxy = xstrdup(value); |
Sam Vilain | 9c5665a | 2007-11-23 13:07:00 +1300 | [diff] [blame] | 156 | } |
| 157 | return 0; |
| 158 | } |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 159 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 160 | /* Fall back on the default ones */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 161 | return git_default_config(var, value, cb); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 164 | static CURL* get_curl_handle(void) |
| 165 | { |
| 166 | CURL* result = curl_easy_init(); |
| 167 | |
Junio C Hamano | a5ccc59 | 2008-02-21 15:10:37 -0800 | [diff] [blame] | 168 | if (!curl_ssl_verify) { |
| 169 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0); |
| 170 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0); |
| 171 | } else { |
| 172 | /* Verify authenticity of the peer's certificate */ |
| 173 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1); |
| 174 | /* The name in the cert must match whom we tried to connect */ |
| 175 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2); |
| 176 | } |
| 177 | |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 178 | #if LIBCURL_VERSION_NUM >= 0x070907 |
| 179 | curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); |
| 180 | #endif |
| 181 | |
| 182 | if (ssl_cert != NULL) |
| 183 | curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert); |
| 184 | #if LIBCURL_VERSION_NUM >= 0x070902 |
| 185 | if (ssl_key != NULL) |
| 186 | curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key); |
| 187 | #endif |
| 188 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| 189 | if (ssl_capath != NULL) |
| 190 | curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath); |
| 191 | #endif |
| 192 | if (ssl_cainfo != NULL) |
| 193 | curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); |
| 194 | curl_easy_setopt(result, CURLOPT_FAILONERROR, 1); |
| 195 | |
| 196 | if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { |
| 197 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT, |
| 198 | curl_low_speed_limit); |
| 199 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME, |
| 200 | curl_low_speed_time); |
| 201 | } |
| 202 | |
| 203 | curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1); |
| 204 | |
Mark Wooding | 7982d74 | 2006-02-01 11:44:37 +0000 | [diff] [blame] | 205 | if (getenv("GIT_CURL_VERBOSE")) |
| 206 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1); |
| 207 | |
Nick Hengeveld | 20fc9bc | 2006-04-04 10:11:29 -0700 | [diff] [blame] | 208 | curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT); |
| 209 | |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 210 | if (curl_ftp_no_epsv) |
| 211 | curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0); |
| 212 | |
Sam Vilain | 9c5665a | 2007-11-23 13:07:00 +1300 | [diff] [blame] | 213 | if (curl_http_proxy) |
| 214 | curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy); |
| 215 | |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 216 | return result; |
| 217 | } |
| 218 | |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 219 | void http_init(struct remote *remote) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 220 | { |
| 221 | char *low_speed_limit; |
| 222 | char *low_speed_time; |
| 223 | |
| 224 | curl_global_init(CURL_GLOBAL_ALL); |
| 225 | |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 226 | if (remote && remote->http_proxy) |
| 227 | curl_http_proxy = xstrdup(remote->http_proxy); |
| 228 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 229 | pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache"); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 230 | |
| 231 | #ifdef USE_CURL_MULTI |
| 232 | { |
| 233 | char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); |
| 234 | if (http_max_requests != NULL) |
| 235 | max_requests = atoi(http_max_requests); |
| 236 | } |
| 237 | |
| 238 | curlm = curl_multi_init(); |
| 239 | if (curlm == NULL) { |
| 240 | fprintf(stderr, "Error creating curl multi handle.\n"); |
| 241 | exit(1); |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | if (getenv("GIT_SSL_NO_VERIFY")) |
| 246 | curl_ssl_verify = 0; |
| 247 | |
| 248 | ssl_cert = getenv("GIT_SSL_CERT"); |
| 249 | #if LIBCURL_VERSION_NUM >= 0x070902 |
| 250 | ssl_key = getenv("GIT_SSL_KEY"); |
| 251 | #endif |
| 252 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| 253 | ssl_capath = getenv("GIT_SSL_CAPATH"); |
| 254 | #endif |
| 255 | ssl_cainfo = getenv("GIT_SSL_CAINFO"); |
| 256 | |
| 257 | low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT"); |
| 258 | if (low_speed_limit != NULL) |
| 259 | curl_low_speed_limit = strtol(low_speed_limit, NULL, 10); |
| 260 | low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME"); |
| 261 | if (low_speed_time != NULL) |
| 262 | curl_low_speed_time = strtol(low_speed_time, NULL, 10); |
| 263 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 264 | git_config(http_options, NULL); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 265 | |
| 266 | if (curl_ssl_verify == -1) |
| 267 | curl_ssl_verify = 1; |
| 268 | |
| 269 | #ifdef USE_CURL_MULTI |
| 270 | if (max_requests < 1) |
| 271 | max_requests = DEFAULT_MAX_REQUESTS; |
| 272 | #endif |
| 273 | |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 274 | if (getenv("GIT_CURL_FTP_NO_EPSV")) |
| 275 | curl_ftp_no_epsv = 1; |
| 276 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 277 | #ifndef NO_CURL_EASY_DUPHANDLE |
| 278 | curl_default = get_curl_handle(); |
| 279 | #endif |
| 280 | } |
| 281 | |
| 282 | void http_cleanup(void) |
| 283 | { |
| 284 | struct active_request_slot *slot = active_queue_head; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 285 | |
| 286 | while (slot != NULL) { |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 287 | struct active_request_slot *next = slot->next; |
Mike Hommey | f23d1f7 | 2008-03-03 20:30:16 +0100 | [diff] [blame] | 288 | if (slot->curl != NULL) { |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 289 | #ifdef USE_CURL_MULTI |
Mike Hommey | f23d1f7 | 2008-03-03 20:30:16 +0100 | [diff] [blame] | 290 | curl_multi_remove_handle(curlm, slot->curl); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 291 | #endif |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 292 | curl_easy_cleanup(slot->curl); |
Mike Hommey | f23d1f7 | 2008-03-03 20:30:16 +0100 | [diff] [blame] | 293 | } |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 294 | free(slot); |
| 295 | slot = next; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 296 | } |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 297 | active_queue_head = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 298 | |
| 299 | #ifndef NO_CURL_EASY_DUPHANDLE |
| 300 | curl_easy_cleanup(curl_default); |
| 301 | #endif |
| 302 | |
| 303 | #ifdef USE_CURL_MULTI |
| 304 | curl_multi_cleanup(curlm); |
| 305 | #endif |
| 306 | curl_global_cleanup(); |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 307 | |
| 308 | curl_slist_free_all(pragma_header); |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 309 | pragma_header = NULL; |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 310 | |
| 311 | if (curl_http_proxy) { |
| 312 | free(curl_http_proxy); |
| 313 | curl_http_proxy = NULL; |
| 314 | } |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 317 | struct active_request_slot *get_active_slot(void) |
| 318 | { |
| 319 | struct active_request_slot *slot = active_queue_head; |
| 320 | struct active_request_slot *newslot; |
| 321 | |
| 322 | #ifdef USE_CURL_MULTI |
| 323 | int num_transfers; |
| 324 | |
| 325 | /* Wait for a slot to open up if the queue is full */ |
| 326 | while (active_requests >= max_requests) { |
| 327 | curl_multi_perform(curlm, &num_transfers); |
| 328 | if (num_transfers < active_requests) { |
| 329 | process_curl_messages(); |
| 330 | } |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | while (slot != NULL && slot->in_use) { |
| 335 | slot = slot->next; |
| 336 | } |
| 337 | if (slot == NULL) { |
| 338 | newslot = xmalloc(sizeof(*newslot)); |
| 339 | newslot->curl = NULL; |
| 340 | newslot->in_use = 0; |
| 341 | newslot->next = NULL; |
| 342 | |
| 343 | slot = active_queue_head; |
| 344 | if (slot == NULL) { |
| 345 | active_queue_head = newslot; |
| 346 | } else { |
| 347 | while (slot->next != NULL) { |
| 348 | slot = slot->next; |
| 349 | } |
| 350 | slot->next = newslot; |
| 351 | } |
| 352 | slot = newslot; |
| 353 | } |
| 354 | |
| 355 | if (slot->curl == NULL) { |
| 356 | #ifdef NO_CURL_EASY_DUPHANDLE |
| 357 | slot->curl = get_curl_handle(); |
| 358 | #else |
| 359 | slot->curl = curl_easy_duphandle(curl_default); |
| 360 | #endif |
| 361 | } |
| 362 | |
| 363 | active_requests++; |
| 364 | slot->in_use = 1; |
| 365 | slot->local = NULL; |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 366 | slot->results = NULL; |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 367 | slot->finished = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 368 | slot->callback_data = NULL; |
| 369 | slot->callback_func = NULL; |
| 370 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 371 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
Nick Hengeveld | 9094950 | 2006-05-31 16:25:03 -0700 | [diff] [blame] | 372 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL); |
| 373 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL); |
| 374 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL); |
| 375 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0); |
| 376 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 377 | |
| 378 | return slot; |
| 379 | } |
| 380 | |
| 381 | int start_active_slot(struct active_request_slot *slot) |
| 382 | { |
| 383 | #ifdef USE_CURL_MULTI |
| 384 | CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 385 | int num_transfers; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 386 | |
| 387 | if (curlm_result != CURLM_OK && |
| 388 | curlm_result != CURLM_CALL_MULTI_PERFORM) { |
| 389 | active_requests--; |
| 390 | slot->in_use = 0; |
| 391 | return 0; |
| 392 | } |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * We know there must be something to do, since we just added |
| 396 | * something. |
| 397 | */ |
| 398 | curl_multi_perform(curlm, &num_transfers); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 399 | #endif |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | #ifdef USE_CURL_MULTI |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 404 | struct fill_chain { |
| 405 | void *data; |
| 406 | int (*fill)(void *); |
| 407 | struct fill_chain *next; |
| 408 | }; |
| 409 | |
| 410 | static struct fill_chain *fill_cfg = NULL; |
| 411 | |
| 412 | void add_fill_function(void *data, int (*fill)(void *)) |
| 413 | { |
Dotan Barak | e8eec71 | 2008-09-09 21:57:10 +0300 | [diff] [blame] | 414 | struct fill_chain *new = xmalloc(sizeof(*new)); |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 415 | struct fill_chain **linkp = &fill_cfg; |
| 416 | new->data = data; |
| 417 | new->fill = fill; |
| 418 | new->next = NULL; |
| 419 | while (*linkp) |
| 420 | linkp = &(*linkp)->next; |
| 421 | *linkp = new; |
| 422 | } |
| 423 | |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 424 | void fill_active_slots(void) |
| 425 | { |
| 426 | struct active_request_slot *slot = active_queue_head; |
| 427 | |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 428 | while (active_requests < max_requests) { |
| 429 | struct fill_chain *fill; |
| 430 | for (fill = fill_cfg; fill; fill = fill->next) |
| 431 | if (fill->fill(fill->data)) |
| 432 | break; |
| 433 | |
| 434 | if (!fill) |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 435 | break; |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 436 | } |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 437 | |
| 438 | while (slot != NULL) { |
| 439 | if (!slot->in_use && slot->curl != NULL) { |
| 440 | curl_easy_cleanup(slot->curl); |
| 441 | slot->curl = NULL; |
| 442 | } |
| 443 | slot = slot->next; |
| 444 | } |
| 445 | } |
| 446 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 447 | void step_active_slots(void) |
| 448 | { |
| 449 | int num_transfers; |
| 450 | CURLMcode curlm_result; |
| 451 | |
| 452 | do { |
| 453 | curlm_result = curl_multi_perform(curlm, &num_transfers); |
| 454 | } while (curlm_result == CURLM_CALL_MULTI_PERFORM); |
| 455 | if (num_transfers < active_requests) { |
| 456 | process_curl_messages(); |
| 457 | fill_active_slots(); |
| 458 | } |
| 459 | } |
| 460 | #endif |
| 461 | |
| 462 | void run_active_slot(struct active_request_slot *slot) |
| 463 | { |
| 464 | #ifdef USE_CURL_MULTI |
| 465 | long last_pos = 0; |
| 466 | long current_pos; |
| 467 | fd_set readfds; |
| 468 | fd_set writefds; |
| 469 | fd_set excfds; |
| 470 | int max_fd; |
| 471 | struct timeval select_timeout; |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 472 | int finished = 0; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 473 | |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 474 | slot->finished = &finished; |
| 475 | while (!finished) { |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 476 | data_received = 0; |
| 477 | step_active_slots(); |
| 478 | |
| 479 | if (!data_received && slot->local != NULL) { |
| 480 | current_pos = ftell(slot->local); |
| 481 | if (current_pos > last_pos) |
| 482 | data_received++; |
| 483 | last_pos = current_pos; |
| 484 | } |
| 485 | |
| 486 | if (slot->in_use && !data_received) { |
| 487 | max_fd = 0; |
| 488 | FD_ZERO(&readfds); |
| 489 | FD_ZERO(&writefds); |
| 490 | FD_ZERO(&excfds); |
| 491 | select_timeout.tv_sec = 0; |
| 492 | select_timeout.tv_usec = 50000; |
| 493 | select(max_fd, &readfds, &writefds, |
| 494 | &excfds, &select_timeout); |
| 495 | } |
| 496 | } |
| 497 | #else |
| 498 | while (slot->in_use) { |
| 499 | slot->curl_result = curl_easy_perform(slot->curl); |
| 500 | finish_active_slot(slot); |
| 501 | } |
| 502 | #endif |
| 503 | } |
| 504 | |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 505 | static void closedown_active_slot(struct active_request_slot *slot) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 506 | { |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 507 | active_requests--; |
| 508 | slot->in_use = 0; |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void release_active_slot(struct active_request_slot *slot) |
| 512 | { |
| 513 | closedown_active_slot(slot); |
| 514 | if (slot->curl) { |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 515 | #ifdef USE_CURL_MULTI |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 516 | curl_multi_remove_handle(curlm, slot->curl); |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 517 | #endif |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 518 | curl_easy_cleanup(slot->curl); |
| 519 | slot->curl = NULL; |
| 520 | } |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 521 | #ifdef USE_CURL_MULTI |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 522 | fill_active_slots(); |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 523 | #endif |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void finish_active_slot(struct active_request_slot *slot) |
| 527 | { |
| 528 | closedown_active_slot(slot); |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 529 | curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code); |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 530 | |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 531 | if (slot->finished != NULL) |
| 532 | (*slot->finished) = 1; |
| 533 | |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 534 | /* Store slot results so they can be read after the slot is reused */ |
| 535 | if (slot->results != NULL) { |
| 536 | slot->results->curl_result = slot->curl_result; |
| 537 | slot->results->http_code = slot->http_code; |
| 538 | } |
| 539 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 540 | /* Run callback if appropriate */ |
| 541 | if (slot->callback_func != NULL) { |
| 542 | slot->callback_func(slot->callback_data); |
| 543 | } |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void finish_all_active_slots(void) |
| 547 | { |
| 548 | struct active_request_slot *slot = active_queue_head; |
| 549 | |
| 550 | while (slot != NULL) |
| 551 | if (slot->in_use) { |
| 552 | run_active_slot(slot); |
| 553 | slot = active_queue_head; |
| 554 | } else { |
| 555 | slot = slot->next; |
| 556 | } |
| 557 | } |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 558 | |
| 559 | static inline int needs_quote(int ch) |
| 560 | { |
| 561 | if (((ch >= 'A') && (ch <= 'Z')) |
| 562 | || ((ch >= 'a') && (ch <= 'z')) |
| 563 | || ((ch >= '0') && (ch <= '9')) |
| 564 | || (ch == '/') |
| 565 | || (ch == '-') |
| 566 | || (ch == '.')) |
| 567 | return 0; |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | static inline int hex(int v) |
| 572 | { |
| 573 | if (v < 10) return '0' + v; |
| 574 | else return 'A' + v - 10; |
| 575 | } |
| 576 | |
| 577 | static char *quote_ref_url(const char *base, const char *ref) |
| 578 | { |
| 579 | const char *cp; |
| 580 | char *dp, *qref; |
| 581 | int len, baselen, ch; |
| 582 | |
| 583 | baselen = strlen(base); |
Mike Hommey | 7c1a9e7 | 2008-06-14 12:02:22 +0200 | [diff] [blame] | 584 | len = baselen + 2; /* '/' after base and terminating NUL */ |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 585 | for (cp = ref; (ch = *cp) != 0; cp++, len++) |
| 586 | if (needs_quote(ch)) |
| 587 | len += 2; /* extra two hex plus replacement % */ |
| 588 | qref = xmalloc(len); |
| 589 | memcpy(qref, base, baselen); |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 590 | dp = qref + baselen; |
| 591 | *(dp++) = '/'; |
| 592 | for (cp = ref; (ch = *cp) != 0; cp++) { |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 593 | if (needs_quote(ch)) { |
| 594 | *dp++ = '%'; |
| 595 | *dp++ = hex((ch >> 4) & 0xF); |
| 596 | *dp++ = hex(ch & 0xF); |
| 597 | } |
| 598 | else |
| 599 | *dp++ = ch; |
| 600 | } |
| 601 | *dp = 0; |
| 602 | |
| 603 | return qref; |
| 604 | } |
| 605 | |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 606 | int http_fetch_ref(const char *base, struct ref *ref) |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 607 | { |
| 608 | char *url; |
| 609 | struct strbuf buffer = STRBUF_INIT; |
| 610 | struct active_request_slot *slot; |
| 611 | struct slot_results results; |
| 612 | int ret; |
| 613 | |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 614 | url = quote_ref_url(base, ref->name); |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 615 | slot = get_active_slot(); |
| 616 | slot->results = &results; |
| 617 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); |
| 618 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
| 619 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL); |
| 620 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
| 621 | if (start_active_slot(slot)) { |
| 622 | run_active_slot(slot); |
| 623 | if (results.curl_result == CURLE_OK) { |
| 624 | strbuf_rtrim(&buffer); |
| 625 | if (buffer.len == 40) |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 626 | ret = get_sha1_hex(buffer.buf, ref->old_sha1); |
Daniel Barkalow | be885d9 | 2008-04-26 15:53:12 -0400 | [diff] [blame] | 627 | else if (!prefixcmp(buffer.buf, "ref: ")) { |
| 628 | ref->symref = xstrdup(buffer.buf + 5); |
| 629 | ret = 0; |
| 630 | } else |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 631 | ret = 1; |
| 632 | } else { |
| 633 | ret = error("Couldn't get %s for %s\n%s", |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 634 | url, ref->name, curl_errorstr); |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 635 | } |
| 636 | } else { |
| 637 | ret = error("Unable to start request"); |
| 638 | } |
| 639 | |
| 640 | strbuf_release(&buffer); |
| 641 | free(url); |
| 642 | return ret; |
| 643 | } |