Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 1 | #include "http.h" |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 2 | #include "pack.h" |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 3 | |
| 4 | int data_received; |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 5 | int active_requests; |
Tay Ray Chuan | e917674 | 2009-06-06 16:43:41 +0800 | [diff] [blame] | 6 | int http_is_verbose; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 7 | |
| 8 | #ifdef USE_CURL_MULTI |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 9 | static int max_requests = -1; |
| 10 | static CURLM *curlm; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 11 | #endif |
| 12 | #ifndef NO_CURL_EASY_DUPHANDLE |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 13 | static CURL *curl_default; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 14 | #endif |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 15 | |
| 16 | #define PREV_BUF_SIZE 4096 |
| 17 | #define RANGE_HEADER_SIZE 30 |
| 18 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 19 | char curl_errorstr[CURL_ERROR_SIZE]; |
| 20 | |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 21 | static int curl_ssl_verify = -1; |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 22 | static const char *ssl_cert; |
Mark Lodato | ef52aaf | 2009-06-14 22:39:00 -0400 | [diff] [blame] | 23 | #if LIBCURL_VERSION_NUM >= 0x070903 |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 24 | static const char *ssl_key; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 25 | #endif |
| 26 | #if LIBCURL_VERSION_NUM >= 0x070908 |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 27 | static const char *ssl_capath; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 28 | #endif |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 29 | static const char *ssl_cainfo; |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 30 | static long curl_low_speed_limit = -1; |
| 31 | static long curl_low_speed_time = -1; |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 32 | static int curl_ftp_no_epsv; |
| 33 | static const char *curl_http_proxy; |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 34 | static char *user_name, *user_pass; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 35 | |
Mark Lodato | 30dd916 | 2009-05-27 23:16:02 -0400 | [diff] [blame] | 36 | #if LIBCURL_VERSION_NUM >= 0x071700 |
| 37 | /* Use CURLOPT_KEYPASSWD as is */ |
| 38 | #elif LIBCURL_VERSION_NUM >= 0x070903 |
| 39 | #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD |
| 40 | #else |
| 41 | #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD |
| 42 | #endif |
| 43 | |
| 44 | static char *ssl_cert_password; |
| 45 | static int ssl_cert_password_required; |
| 46 | |
Mike Hommey | cc3530e | 2007-12-09 18:04:57 +0100 | [diff] [blame] | 47 | static struct curl_slist *pragma_header; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 48 | static struct curl_slist *no_pragma_header; |
Tay Ray Chuan | e917674 | 2009-06-06 16:43:41 +0800 | [diff] [blame] | 49 | |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 50 | static struct active_request_slot *active_queue_head; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 51 | |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 52 | 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] | 53 | { |
| 54 | size_t size = eltsize * nmemb; |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 55 | struct buffer *buffer = buffer_; |
| 56 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 57 | if (size > buffer->buf.len - buffer->posn) |
| 58 | size = buffer->buf.len - buffer->posn; |
| 59 | memcpy(ptr, buffer->buf.buf + buffer->posn, size); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 60 | buffer->posn += size; |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 61 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 62 | return size; |
| 63 | } |
| 64 | |
Martin Storsjö | 3944ba0 | 2009-04-01 19:48:24 +0300 | [diff] [blame] | 65 | #ifndef NO_CURL_IOCTL |
| 66 | curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp) |
| 67 | { |
| 68 | struct buffer *buffer = clientp; |
| 69 | |
| 70 | switch (cmd) { |
| 71 | case CURLIOCMD_NOP: |
| 72 | return CURLIOE_OK; |
| 73 | |
| 74 | case CURLIOCMD_RESTARTREAD: |
| 75 | buffer->posn = 0; |
| 76 | return CURLIOE_OK; |
| 77 | |
| 78 | default: |
| 79 | return CURLIOE_UNKNOWNCMD; |
| 80 | } |
| 81 | } |
| 82 | #endif |
| 83 | |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | size_t size = eltsize * nmemb; |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 87 | struct strbuf *buffer = buffer_; |
| 88 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 89 | strbuf_add(buffer, ptr, size); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 90 | data_received++; |
| 91 | return size; |
| 92 | } |
| 93 | |
Junio C Hamano | f444e52 | 2008-07-04 00:37:40 -0700 | [diff] [blame] | 94 | 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] | 95 | { |
| 96 | data_received++; |
| 97 | return eltsize * nmemb; |
| 98 | } |
| 99 | |
| 100 | static void finish_active_slot(struct active_request_slot *slot); |
| 101 | |
| 102 | #ifdef USE_CURL_MULTI |
| 103 | static void process_curl_messages(void) |
| 104 | { |
| 105 | int num_messages; |
| 106 | struct active_request_slot *slot; |
| 107 | CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); |
| 108 | |
| 109 | while (curl_message != NULL) { |
| 110 | if (curl_message->msg == CURLMSG_DONE) { |
| 111 | int curl_result = curl_message->data.result; |
| 112 | slot = active_queue_head; |
| 113 | while (slot != NULL && |
| 114 | slot->curl != curl_message->easy_handle) |
| 115 | slot = slot->next; |
| 116 | if (slot != NULL) { |
| 117 | curl_multi_remove_handle(curlm, slot->curl); |
| 118 | slot->curl_result = curl_result; |
| 119 | finish_active_slot(slot); |
| 120 | } else { |
| 121 | fprintf(stderr, "Received DONE message for unknown request!\n"); |
| 122 | } |
| 123 | } else { |
| 124 | fprintf(stderr, "Unknown CURL message received: %d\n", |
| 125 | (int)curl_message->msg); |
| 126 | } |
| 127 | curl_message = curl_multi_info_read(curlm, &num_messages); |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 132 | static int http_options(const char *var, const char *value, void *cb) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 133 | { |
| 134 | if (!strcmp("http.sslverify", var)) { |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 135 | curl_ssl_verify = git_config_bool(var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 136 | return 0; |
| 137 | } |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 138 | if (!strcmp("http.sslcert", var)) |
| 139 | return git_config_string(&ssl_cert, var, value); |
Mark Lodato | ef52aaf | 2009-06-14 22:39:00 -0400 | [diff] [blame] | 140 | #if LIBCURL_VERSION_NUM >= 0x070903 |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 141 | if (!strcmp("http.sslkey", var)) |
| 142 | return git_config_string(&ssl_key, var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 143 | #endif |
| 144 | #if LIBCURL_VERSION_NUM >= 0x070908 |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 145 | if (!strcmp("http.sslcapath", var)) |
| 146 | return git_config_string(&ssl_capath, var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 147 | #endif |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 148 | if (!strcmp("http.sslcainfo", var)) |
| 149 | return git_config_string(&ssl_cainfo, var, value); |
Mark Lodato | 754ae19 | 2009-05-27 23:16:03 -0400 | [diff] [blame] | 150 | if (!strcmp("http.sslcertpasswordprotected", var)) { |
| 151 | if (git_config_bool(var, value)) |
| 152 | ssl_cert_password_required = 1; |
| 153 | return 0; |
| 154 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 155 | #ifdef USE_CURL_MULTI |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 156 | if (!strcmp("http.maxrequests", var)) { |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 157 | max_requests = git_config_int(var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | #endif |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 161 | if (!strcmp("http.lowspeedlimit", var)) { |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 162 | curl_low_speed_limit = (long)git_config_int(var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | if (!strcmp("http.lowspeedtime", var)) { |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 166 | curl_low_speed_time = (long)git_config_int(var, value); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 170 | if (!strcmp("http.noepsv", var)) { |
| 171 | curl_ftp_no_epsv = git_config_bool(var, value); |
| 172 | return 0; |
| 173 | } |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 174 | if (!strcmp("http.proxy", var)) |
| 175 | return git_config_string(&curl_http_proxy, var, value); |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 176 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 177 | /* Fall back on the default ones */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 178 | return git_default_config(var, value, cb); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 181 | static void init_curl_http_auth(CURL *result) |
| 182 | { |
Junio C Hamano | 750d930 | 2009-03-12 22:34:43 -0700 | [diff] [blame] | 183 | if (user_name) { |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 184 | struct strbuf up = STRBUF_INIT; |
| 185 | if (!user_pass) |
| 186 | user_pass = xstrdup(getpass("Password: ")); |
| 187 | strbuf_addf(&up, "%s:%s", user_name, user_pass); |
| 188 | curl_easy_setopt(result, CURLOPT_USERPWD, |
| 189 | strbuf_detach(&up, NULL)); |
| 190 | } |
| 191 | } |
| 192 | |
Mark Lodato | 30dd916 | 2009-05-27 23:16:02 -0400 | [diff] [blame] | 193 | static int has_cert_password(void) |
| 194 | { |
| 195 | if (ssl_cert_password != NULL) |
| 196 | return 1; |
| 197 | if (ssl_cert == NULL || ssl_cert_password_required != 1) |
| 198 | return 0; |
| 199 | /* Only prompt the user once. */ |
| 200 | ssl_cert_password_required = -1; |
| 201 | ssl_cert_password = getpass("Certificate Password: "); |
| 202 | if (ssl_cert_password != NULL) { |
| 203 | ssl_cert_password = xstrdup(ssl_cert_password); |
| 204 | return 1; |
| 205 | } else |
| 206 | return 0; |
| 207 | } |
| 208 | |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 209 | static CURL *get_curl_handle(void) |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 210 | { |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 211 | CURL *result = curl_easy_init(); |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 212 | |
Junio C Hamano | a5ccc59 | 2008-02-21 15:10:37 -0800 | [diff] [blame] | 213 | if (!curl_ssl_verify) { |
| 214 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0); |
| 215 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0); |
| 216 | } else { |
| 217 | /* Verify authenticity of the peer's certificate */ |
| 218 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1); |
| 219 | /* The name in the cert must match whom we tried to connect */ |
| 220 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2); |
| 221 | } |
| 222 | |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 223 | #if LIBCURL_VERSION_NUM >= 0x070907 |
| 224 | curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); |
| 225 | #endif |
| 226 | |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 227 | init_curl_http_auth(result); |
| 228 | |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 229 | if (ssl_cert != NULL) |
| 230 | curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert); |
Mark Lodato | 30dd916 | 2009-05-27 23:16:02 -0400 | [diff] [blame] | 231 | if (has_cert_password()) |
| 232 | curl_easy_setopt(result, CURLOPT_KEYPASSWD, ssl_cert_password); |
Mark Lodato | ef52aaf | 2009-06-14 22:39:00 -0400 | [diff] [blame] | 233 | #if LIBCURL_VERSION_NUM >= 0x070903 |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 234 | if (ssl_key != NULL) |
| 235 | curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key); |
| 236 | #endif |
| 237 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| 238 | if (ssl_capath != NULL) |
| 239 | curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath); |
| 240 | #endif |
| 241 | if (ssl_cainfo != NULL) |
| 242 | curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); |
| 243 | curl_easy_setopt(result, CURLOPT_FAILONERROR, 1); |
| 244 | |
| 245 | if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { |
| 246 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT, |
| 247 | curl_low_speed_limit); |
| 248 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME, |
| 249 | curl_low_speed_time); |
| 250 | } |
| 251 | |
| 252 | curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1); |
| 253 | |
Mark Wooding | 7982d74 | 2006-02-01 11:44:37 +0000 | [diff] [blame] | 254 | if (getenv("GIT_CURL_VERBOSE")) |
| 255 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1); |
| 256 | |
Nick Hengeveld | 20fc9bc | 2006-04-04 10:11:29 -0700 | [diff] [blame] | 257 | curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT); |
| 258 | |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 259 | if (curl_ftp_no_epsv) |
| 260 | curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0); |
| 261 | |
Sam Vilain | 9c5665a | 2007-11-23 13:07:00 +1300 | [diff] [blame] | 262 | if (curl_http_proxy) |
| 263 | curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy); |
| 264 | |
Junio C Hamano | 11979b9 | 2005-11-18 17:06:46 -0800 | [diff] [blame] | 265 | return result; |
| 266 | } |
| 267 | |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 268 | static void http_auth_init(const char *url) |
| 269 | { |
| 270 | char *at, *colon, *cp, *slash; |
| 271 | int len; |
| 272 | |
| 273 | cp = strstr(url, "://"); |
| 274 | if (!cp) |
| 275 | return; |
| 276 | |
| 277 | /* |
| 278 | * Ok, the URL looks like "proto://something". Which one? |
| 279 | * "proto://<user>:<pass>@<host>/...", |
| 280 | * "proto://<user>@<host>/...", or just |
| 281 | * "proto://<host>/..."? |
| 282 | */ |
| 283 | cp += 3; |
| 284 | at = strchr(cp, '@'); |
| 285 | colon = strchr(cp, ':'); |
| 286 | slash = strchrnul(cp, '/'); |
| 287 | if (!at || slash <= at) |
| 288 | return; /* No credentials */ |
| 289 | if (!colon || at <= colon) { |
| 290 | /* Only username */ |
| 291 | len = at - cp; |
| 292 | user_name = xmalloc(len + 1); |
| 293 | memcpy(user_name, cp, len); |
| 294 | user_name[len] = '\0'; |
| 295 | user_pass = NULL; |
| 296 | } else { |
| 297 | len = colon - cp; |
| 298 | user_name = xmalloc(len + 1); |
| 299 | memcpy(user_name, cp, len); |
| 300 | user_name[len] = '\0'; |
| 301 | len = at - (colon + 1); |
| 302 | user_pass = xmalloc(len + 1); |
| 303 | memcpy(user_pass, colon + 1, len); |
| 304 | user_pass[len] = '\0'; |
| 305 | } |
| 306 | } |
| 307 | |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 308 | static void set_from_env(const char **var, const char *envname) |
| 309 | { |
| 310 | const char *val = getenv(envname); |
| 311 | if (val) |
| 312 | *var = val; |
| 313 | } |
| 314 | |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 315 | void http_init(struct remote *remote) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 316 | { |
| 317 | char *low_speed_limit; |
| 318 | char *low_speed_time; |
| 319 | |
Tay Ray Chuan | e917674 | 2009-06-06 16:43:41 +0800 | [diff] [blame] | 320 | http_is_verbose = 0; |
| 321 | |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 322 | git_config(http_options, NULL); |
| 323 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 324 | curl_global_init(CURL_GLOBAL_ALL); |
| 325 | |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 326 | if (remote && remote->http_proxy) |
| 327 | curl_http_proxy = xstrdup(remote->http_proxy); |
| 328 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 329 | pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache"); |
Tay Ray Chuan | e917674 | 2009-06-06 16:43:41 +0800 | [diff] [blame] | 330 | no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:"); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 331 | |
| 332 | #ifdef USE_CURL_MULTI |
| 333 | { |
| 334 | char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); |
| 335 | if (http_max_requests != NULL) |
| 336 | max_requests = atoi(http_max_requests); |
| 337 | } |
| 338 | |
| 339 | curlm = curl_multi_init(); |
| 340 | if (curlm == NULL) { |
| 341 | fprintf(stderr, "Error creating curl multi handle.\n"); |
| 342 | exit(1); |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | if (getenv("GIT_SSL_NO_VERIFY")) |
| 347 | curl_ssl_verify = 0; |
| 348 | |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 349 | set_from_env(&ssl_cert, "GIT_SSL_CERT"); |
Mark Lodato | ef52aaf | 2009-06-14 22:39:00 -0400 | [diff] [blame] | 350 | #if LIBCURL_VERSION_NUM >= 0x070903 |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 351 | set_from_env(&ssl_key, "GIT_SSL_KEY"); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 352 | #endif |
| 353 | #if LIBCURL_VERSION_NUM >= 0x070908 |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 354 | set_from_env(&ssl_capath, "GIT_SSL_CAPATH"); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 355 | #endif |
Junio C Hamano | 7059cd9 | 2009-03-09 19:00:30 -0700 | [diff] [blame] | 356 | set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO"); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 357 | |
| 358 | low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT"); |
| 359 | if (low_speed_limit != NULL) |
| 360 | curl_low_speed_limit = strtol(low_speed_limit, NULL, 10); |
| 361 | low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME"); |
| 362 | if (low_speed_time != NULL) |
| 363 | curl_low_speed_time = strtol(low_speed_time, NULL, 10); |
| 364 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 365 | if (curl_ssl_verify == -1) |
| 366 | curl_ssl_verify = 1; |
| 367 | |
| 368 | #ifdef USE_CURL_MULTI |
| 369 | if (max_requests < 1) |
| 370 | max_requests = DEFAULT_MAX_REQUESTS; |
| 371 | #endif |
| 372 | |
Sasha Khapyorsky | 3ea099d | 2006-09-29 03:10:44 +0300 | [diff] [blame] | 373 | if (getenv("GIT_CURL_FTP_NO_EPSV")) |
| 374 | curl_ftp_no_epsv = 1; |
| 375 | |
Mark Lodato | 30dd916 | 2009-05-27 23:16:02 -0400 | [diff] [blame] | 376 | if (remote && remote->url && remote->url[0]) { |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 377 | http_auth_init(remote->url[0]); |
Mark Lodato | 754ae19 | 2009-05-27 23:16:03 -0400 | [diff] [blame] | 378 | if (!ssl_cert_password_required && |
| 379 | getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") && |
| 380 | !prefixcmp(remote->url[0], "https://")) |
Mark Lodato | 30dd916 | 2009-05-27 23:16:02 -0400 | [diff] [blame] | 381 | ssl_cert_password_required = 1; |
| 382 | } |
Junio C Hamano | c33976c | 2009-03-09 23:34:25 -0700 | [diff] [blame] | 383 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 384 | #ifndef NO_CURL_EASY_DUPHANDLE |
| 385 | curl_default = get_curl_handle(); |
| 386 | #endif |
| 387 | } |
| 388 | |
| 389 | void http_cleanup(void) |
| 390 | { |
| 391 | struct active_request_slot *slot = active_queue_head; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 392 | |
| 393 | while (slot != NULL) { |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 394 | struct active_request_slot *next = slot->next; |
Mike Hommey | f23d1f7 | 2008-03-03 20:30:16 +0100 | [diff] [blame] | 395 | if (slot->curl != NULL) { |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 396 | #ifdef USE_CURL_MULTI |
Mike Hommey | f23d1f7 | 2008-03-03 20:30:16 +0100 | [diff] [blame] | 397 | curl_multi_remove_handle(curlm, slot->curl); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 398 | #endif |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 399 | curl_easy_cleanup(slot->curl); |
Mike Hommey | f23d1f7 | 2008-03-03 20:30:16 +0100 | [diff] [blame] | 400 | } |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 401 | free(slot); |
| 402 | slot = next; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 403 | } |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 404 | active_queue_head = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 405 | |
| 406 | #ifndef NO_CURL_EASY_DUPHANDLE |
| 407 | curl_easy_cleanup(curl_default); |
| 408 | #endif |
| 409 | |
| 410 | #ifdef USE_CURL_MULTI |
| 411 | curl_multi_cleanup(curlm); |
| 412 | #endif |
| 413 | curl_global_cleanup(); |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 414 | |
| 415 | curl_slist_free_all(pragma_header); |
Shawn O. Pearce | 3278cd0 | 2007-09-15 03:23:00 -0400 | [diff] [blame] | 416 | pragma_header = NULL; |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 417 | |
Tay Ray Chuan | e917674 | 2009-06-06 16:43:41 +0800 | [diff] [blame] | 418 | curl_slist_free_all(no_pragma_header); |
| 419 | no_pragma_header = NULL; |
| 420 | |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 421 | if (curl_http_proxy) { |
Miklos Vajna | e4a80ec | 2008-12-07 01:45:37 +0100 | [diff] [blame] | 422 | free((void *)curl_http_proxy); |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 423 | curl_http_proxy = NULL; |
| 424 | } |
Mark Lodato | 30dd916 | 2009-05-27 23:16:02 -0400 | [diff] [blame] | 425 | |
| 426 | if (ssl_cert_password != NULL) { |
| 427 | memset(ssl_cert_password, 0, strlen(ssl_cert_password)); |
| 428 | free(ssl_cert_password); |
| 429 | ssl_cert_password = NULL; |
| 430 | } |
| 431 | ssl_cert_password_required = 0; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 434 | struct active_request_slot *get_active_slot(void) |
| 435 | { |
| 436 | struct active_request_slot *slot = active_queue_head; |
| 437 | struct active_request_slot *newslot; |
| 438 | |
| 439 | #ifdef USE_CURL_MULTI |
| 440 | int num_transfers; |
| 441 | |
| 442 | /* Wait for a slot to open up if the queue is full */ |
| 443 | while (active_requests >= max_requests) { |
| 444 | curl_multi_perform(curlm, &num_transfers); |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 445 | if (num_transfers < active_requests) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 446 | process_curl_messages(); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 447 | } |
| 448 | #endif |
| 449 | |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 450 | while (slot != NULL && slot->in_use) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 451 | slot = slot->next; |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 452 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 453 | if (slot == NULL) { |
| 454 | newslot = xmalloc(sizeof(*newslot)); |
| 455 | newslot->curl = NULL; |
| 456 | newslot->in_use = 0; |
| 457 | newslot->next = NULL; |
| 458 | |
| 459 | slot = active_queue_head; |
| 460 | if (slot == NULL) { |
| 461 | active_queue_head = newslot; |
| 462 | } else { |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 463 | while (slot->next != NULL) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 464 | slot = slot->next; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 465 | slot->next = newslot; |
| 466 | } |
| 467 | slot = newslot; |
| 468 | } |
| 469 | |
| 470 | if (slot->curl == NULL) { |
| 471 | #ifdef NO_CURL_EASY_DUPHANDLE |
| 472 | slot->curl = get_curl_handle(); |
| 473 | #else |
| 474 | slot->curl = curl_easy_duphandle(curl_default); |
| 475 | #endif |
| 476 | } |
| 477 | |
| 478 | active_requests++; |
| 479 | slot->in_use = 1; |
| 480 | slot->local = NULL; |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 481 | slot->results = NULL; |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 482 | slot->finished = NULL; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 483 | slot->callback_data = NULL; |
| 484 | slot->callback_func = NULL; |
| 485 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 486 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
Nick Hengeveld | 9094950 | 2006-05-31 16:25:03 -0700 | [diff] [blame] | 487 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL); |
| 488 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL); |
| 489 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL); |
| 490 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0); |
| 491 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 492 | |
| 493 | return slot; |
| 494 | } |
| 495 | |
| 496 | int start_active_slot(struct active_request_slot *slot) |
| 497 | { |
| 498 | #ifdef USE_CURL_MULTI |
| 499 | CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 500 | int num_transfers; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 501 | |
| 502 | if (curlm_result != CURLM_OK && |
| 503 | curlm_result != CURLM_CALL_MULTI_PERFORM) { |
| 504 | active_requests--; |
| 505 | slot->in_use = 0; |
| 506 | return 0; |
| 507 | } |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * We know there must be something to do, since we just added |
| 511 | * something. |
| 512 | */ |
| 513 | curl_multi_perform(curlm, &num_transfers); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 514 | #endif |
| 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | #ifdef USE_CURL_MULTI |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 519 | struct fill_chain { |
| 520 | void *data; |
| 521 | int (*fill)(void *); |
| 522 | struct fill_chain *next; |
| 523 | }; |
| 524 | |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 525 | static struct fill_chain *fill_cfg; |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 526 | |
| 527 | void add_fill_function(void *data, int (*fill)(void *)) |
| 528 | { |
Dotan Barak | e8eec71 | 2008-09-09 21:57:10 +0300 | [diff] [blame] | 529 | struct fill_chain *new = xmalloc(sizeof(*new)); |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 530 | struct fill_chain **linkp = &fill_cfg; |
| 531 | new->data = data; |
| 532 | new->fill = fill; |
| 533 | new->next = NULL; |
| 534 | while (*linkp) |
| 535 | linkp = &(*linkp)->next; |
| 536 | *linkp = new; |
| 537 | } |
| 538 | |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 539 | void fill_active_slots(void) |
| 540 | { |
| 541 | struct active_request_slot *slot = active_queue_head; |
| 542 | |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 543 | while (active_requests < max_requests) { |
| 544 | struct fill_chain *fill; |
| 545 | for (fill = fill_cfg; fill; fill = fill->next) |
| 546 | if (fill->fill(fill->data)) |
| 547 | break; |
| 548 | |
| 549 | if (!fill) |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 550 | break; |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 551 | } |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 552 | |
| 553 | while (slot != NULL) { |
| 554 | if (!slot->in_use && slot->curl != NULL) { |
| 555 | curl_easy_cleanup(slot->curl); |
| 556 | slot->curl = NULL; |
| 557 | } |
| 558 | slot = slot->next; |
| 559 | } |
| 560 | } |
| 561 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 562 | void step_active_slots(void) |
| 563 | { |
| 564 | int num_transfers; |
| 565 | CURLMcode curlm_result; |
| 566 | |
| 567 | do { |
| 568 | curlm_result = curl_multi_perform(curlm, &num_transfers); |
| 569 | } while (curlm_result == CURLM_CALL_MULTI_PERFORM); |
| 570 | if (num_transfers < active_requests) { |
| 571 | process_curl_messages(); |
| 572 | fill_active_slots(); |
| 573 | } |
| 574 | } |
| 575 | #endif |
| 576 | |
| 577 | void run_active_slot(struct active_request_slot *slot) |
| 578 | { |
| 579 | #ifdef USE_CURL_MULTI |
| 580 | long last_pos = 0; |
| 581 | long current_pos; |
| 582 | fd_set readfds; |
| 583 | fd_set writefds; |
| 584 | fd_set excfds; |
| 585 | int max_fd; |
| 586 | struct timeval select_timeout; |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 587 | int finished = 0; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 588 | |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 589 | slot->finished = &finished; |
| 590 | while (!finished) { |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 591 | data_received = 0; |
| 592 | step_active_slots(); |
| 593 | |
| 594 | if (!data_received && slot->local != NULL) { |
| 595 | current_pos = ftell(slot->local); |
| 596 | if (current_pos > last_pos) |
| 597 | data_received++; |
| 598 | last_pos = current_pos; |
| 599 | } |
| 600 | |
| 601 | if (slot->in_use && !data_received) { |
| 602 | max_fd = 0; |
| 603 | FD_ZERO(&readfds); |
| 604 | FD_ZERO(&writefds); |
| 605 | FD_ZERO(&excfds); |
| 606 | select_timeout.tv_sec = 0; |
| 607 | select_timeout.tv_usec = 50000; |
| 608 | select(max_fd, &readfds, &writefds, |
| 609 | &excfds, &select_timeout); |
| 610 | } |
| 611 | } |
| 612 | #else |
| 613 | while (slot->in_use) { |
| 614 | slot->curl_result = curl_easy_perform(slot->curl); |
| 615 | finish_active_slot(slot); |
| 616 | } |
| 617 | #endif |
| 618 | } |
| 619 | |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 620 | static void closedown_active_slot(struct active_request_slot *slot) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 621 | { |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 622 | active_requests--; |
| 623 | slot->in_use = 0; |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void release_active_slot(struct active_request_slot *slot) |
| 627 | { |
| 628 | closedown_active_slot(slot); |
| 629 | if (slot->curl) { |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 630 | #ifdef USE_CURL_MULTI |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 631 | curl_multi_remove_handle(curlm, slot->curl); |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 632 | #endif |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 633 | curl_easy_cleanup(slot->curl); |
| 634 | slot->curl = NULL; |
| 635 | } |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 636 | #ifdef USE_CURL_MULTI |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 637 | fill_active_slots(); |
Nick Hengeveld | b3ca4e4e | 2006-06-06 09:41:32 -0700 | [diff] [blame] | 638 | #endif |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static void finish_active_slot(struct active_request_slot *slot) |
| 642 | { |
| 643 | closedown_active_slot(slot); |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 644 | curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code); |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 645 | |
Nick Hengeveld | baa7b67 | 2006-03-10 20:18:01 -0800 | [diff] [blame] | 646 | if (slot->finished != NULL) |
| 647 | (*slot->finished) = 1; |
| 648 | |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 649 | /* Store slot results so they can be read after the slot is reused */ |
| 650 | if (slot->results != NULL) { |
| 651 | slot->results->curl_result = slot->curl_result; |
| 652 | slot->results->http_code = slot->http_code; |
| 653 | } |
| 654 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 655 | /* Run callback if appropriate */ |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 656 | if (slot->callback_func != NULL) |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 657 | slot->callback_func(slot->callback_data); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | void finish_all_active_slots(void) |
| 661 | { |
| 662 | struct active_request_slot *slot = active_queue_head; |
| 663 | |
| 664 | while (slot != NULL) |
| 665 | if (slot->in_use) { |
| 666 | run_active_slot(slot); |
| 667 | slot = active_queue_head; |
| 668 | } else { |
| 669 | slot = slot->next; |
| 670 | } |
| 671 | } |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 672 | |
Tay Ray Chuan | 5ace994 | 2009-06-06 16:43:43 +0800 | [diff] [blame] | 673 | /* Helpers for modifying and creating URLs */ |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 674 | static inline int needs_quote(int ch) |
| 675 | { |
| 676 | if (((ch >= 'A') && (ch <= 'Z')) |
| 677 | || ((ch >= 'a') && (ch <= 'z')) |
| 678 | || ((ch >= '0') && (ch <= '9')) |
| 679 | || (ch == '/') |
| 680 | || (ch == '-') |
| 681 | || (ch == '.')) |
| 682 | return 0; |
| 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | static inline int hex(int v) |
| 687 | { |
Junio C Hamano | 4251ccb | 2009-03-09 18:47:29 -0700 | [diff] [blame] | 688 | if (v < 10) |
| 689 | return '0' + v; |
| 690 | else |
| 691 | return 'A' + v - 10; |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Tay Ray Chuan | 5ace994 | 2009-06-06 16:43:43 +0800 | [diff] [blame] | 694 | static void end_url_with_slash(struct strbuf *buf, const char *url) |
| 695 | { |
| 696 | strbuf_addstr(buf, url); |
| 697 | if (buf->len && buf->buf[buf->len - 1] != '/') |
| 698 | strbuf_addstr(buf, "/"); |
| 699 | } |
| 700 | |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 701 | static char *quote_ref_url(const char *base, const char *ref) |
| 702 | { |
Tay Ray Chuan | 113106e | 2009-03-08 00:47:21 +0800 | [diff] [blame] | 703 | struct strbuf buf = STRBUF_INIT; |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 704 | const char *cp; |
Tay Ray Chuan | 113106e | 2009-03-08 00:47:21 +0800 | [diff] [blame] | 705 | int ch; |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 706 | |
Tay Ray Chuan | 5ace994 | 2009-06-06 16:43:43 +0800 | [diff] [blame] | 707 | end_url_with_slash(&buf, base); |
Tay Ray Chuan | 113106e | 2009-03-08 00:47:21 +0800 | [diff] [blame] | 708 | |
| 709 | for (cp = ref; (ch = *cp) != 0; cp++) |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 710 | if (needs_quote(ch)) |
Tay Ray Chuan | 113106e | 2009-03-08 00:47:21 +0800 | [diff] [blame] | 711 | strbuf_addf(&buf, "%%%02x", ch); |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 712 | else |
Tay Ray Chuan | 113106e | 2009-03-08 00:47:21 +0800 | [diff] [blame] | 713 | strbuf_addch(&buf, *cp); |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 714 | |
Tay Ray Chuan | 113106e | 2009-03-08 00:47:21 +0800 | [diff] [blame] | 715 | return strbuf_detach(&buf, NULL); |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 718 | void append_remote_object_url(struct strbuf *buf, const char *url, |
| 719 | const char *hex, |
| 720 | int only_two_digit_prefix) |
| 721 | { |
Tay Ray Chuan | 800324c | 2009-08-17 17:09:43 +0800 | [diff] [blame] | 722 | end_url_with_slash(buf, url); |
| 723 | |
| 724 | strbuf_addf(buf, "objects/%.*s/", 2, hex); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 725 | if (!only_two_digit_prefix) |
| 726 | strbuf_addf(buf, "%s", hex+2); |
| 727 | } |
| 728 | |
| 729 | char *get_remote_object_url(const char *url, const char *hex, |
| 730 | int only_two_digit_prefix) |
| 731 | { |
| 732 | struct strbuf buf = STRBUF_INIT; |
| 733 | append_remote_object_url(&buf, url, hex, only_two_digit_prefix); |
| 734 | return strbuf_detach(&buf, NULL); |
| 735 | } |
| 736 | |
Mike Hommey | e929cd2 | 2009-06-06 16:43:53 +0800 | [diff] [blame] | 737 | /* http_request() targets */ |
| 738 | #define HTTP_REQUEST_STRBUF 0 |
| 739 | #define HTTP_REQUEST_FILE 1 |
| 740 | |
| 741 | static int http_request(const char *url, void *result, int target, int options) |
| 742 | { |
| 743 | struct active_request_slot *slot; |
| 744 | struct slot_results results; |
| 745 | struct curl_slist *headers = NULL; |
| 746 | struct strbuf buf = STRBUF_INIT; |
| 747 | int ret; |
| 748 | |
| 749 | slot = get_active_slot(); |
| 750 | slot->results = &results; |
| 751 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); |
| 752 | |
| 753 | if (result == NULL) { |
| 754 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1); |
| 755 | } else { |
| 756 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
| 757 | curl_easy_setopt(slot->curl, CURLOPT_FILE, result); |
| 758 | |
| 759 | if (target == HTTP_REQUEST_FILE) { |
| 760 | long posn = ftell(result); |
| 761 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, |
| 762 | fwrite); |
| 763 | if (posn > 0) { |
| 764 | strbuf_addf(&buf, "Range: bytes=%ld-", posn); |
| 765 | headers = curl_slist_append(headers, buf.buf); |
| 766 | strbuf_reset(&buf); |
| 767 | } |
| 768 | slot->local = result; |
| 769 | } else |
| 770 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, |
| 771 | fwrite_buffer); |
| 772 | } |
| 773 | |
| 774 | strbuf_addstr(&buf, "Pragma:"); |
| 775 | if (options & HTTP_NO_CACHE) |
| 776 | strbuf_addstr(&buf, " no-cache"); |
| 777 | |
| 778 | headers = curl_slist_append(headers, buf.buf); |
| 779 | |
| 780 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
| 781 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); |
| 782 | |
| 783 | if (start_active_slot(slot)) { |
| 784 | run_active_slot(slot); |
| 785 | if (results.curl_result == CURLE_OK) |
| 786 | ret = HTTP_OK; |
| 787 | else if (missing_target(&results)) |
| 788 | ret = HTTP_MISSING_TARGET; |
| 789 | else |
| 790 | ret = HTTP_ERROR; |
| 791 | } else { |
| 792 | error("Unable to start HTTP request for %s", url); |
| 793 | ret = HTTP_START_FAILED; |
| 794 | } |
| 795 | |
| 796 | slot->local = NULL; |
| 797 | curl_slist_free_all(headers); |
| 798 | strbuf_release(&buf); |
| 799 | |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | int http_get_strbuf(const char *url, struct strbuf *result, int options) |
| 804 | { |
| 805 | return http_request(url, result, HTTP_REQUEST_STRBUF, options); |
| 806 | } |
| 807 | |
| 808 | int http_get_file(const char *url, const char *filename, int options) |
| 809 | { |
| 810 | int ret; |
| 811 | struct strbuf tmpfile = STRBUF_INIT; |
| 812 | FILE *result; |
| 813 | |
| 814 | strbuf_addf(&tmpfile, "%s.temp", filename); |
| 815 | result = fopen(tmpfile.buf, "a"); |
| 816 | if (! result) { |
| 817 | error("Unable to open local file %s", tmpfile.buf); |
| 818 | ret = HTTP_ERROR; |
| 819 | goto cleanup; |
| 820 | } |
| 821 | |
| 822 | ret = http_request(url, result, HTTP_REQUEST_FILE, options); |
| 823 | fclose(result); |
| 824 | |
| 825 | if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename)) |
| 826 | ret = HTTP_ERROR; |
| 827 | cleanup: |
| 828 | strbuf_release(&tmpfile); |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | int http_error(const char *url, int ret) |
| 833 | { |
| 834 | /* http_request has already handled HTTP_START_FAILED. */ |
| 835 | if (ret != HTTP_START_FAILED) |
| 836 | error("%s while accessing %s\n", curl_errorstr, url); |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 841 | int http_fetch_ref(const char *base, struct ref *ref) |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 842 | { |
| 843 | char *url; |
| 844 | struct strbuf buffer = STRBUF_INIT; |
Mike Hommey | 0d5896e | 2009-06-06 16:43:55 +0800 | [diff] [blame] | 845 | int ret = -1; |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 846 | |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 847 | url = quote_ref_url(base, ref->name); |
Mike Hommey | 0d5896e | 2009-06-06 16:43:55 +0800 | [diff] [blame] | 848 | if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) { |
| 849 | strbuf_rtrim(&buffer); |
| 850 | if (buffer.len == 40) |
| 851 | ret = get_sha1_hex(buffer.buf, ref->old_sha1); |
| 852 | else if (!prefixcmp(buffer.buf, "ref: ")) { |
| 853 | ref->symref = xstrdup(buffer.buf + 5); |
| 854 | ret = 0; |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 855 | } |
Mike Hommey | d7e9280 | 2007-12-11 00:08:25 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | strbuf_release(&buffer); |
| 859 | free(url); |
| 860 | return ret; |
| 861 | } |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 862 | |
| 863 | /* Helpers for fetching packs */ |
| 864 | static int fetch_pack_index(unsigned char *sha1, const char *base_url) |
| 865 | { |
| 866 | int ret = 0; |
| 867 | char *hex = xstrdup(sha1_to_hex(sha1)); |
| 868 | char *filename; |
Junio C Hamano | b202514 | 2009-09-14 14:48:15 -0700 | [diff] [blame] | 869 | char *url = NULL; |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 870 | struct strbuf buf = STRBUF_INIT; |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 871 | |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 872 | if (has_pack_index(sha1)) { |
| 873 | ret = 0; |
Tay Ray Chuan | 39dc52c | 2009-06-06 16:44:00 +0800 | [diff] [blame] | 874 | goto cleanup; |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | if (http_is_verbose) |
| 878 | fprintf(stderr, "Getting index for pack %s\n", hex); |
| 879 | |
| 880 | end_url_with_slash(&buf, base_url); |
| 881 | strbuf_addf(&buf, "objects/pack/pack-%s.idx", hex); |
| 882 | url = strbuf_detach(&buf, NULL); |
| 883 | |
| 884 | filename = sha1_pack_index_name(sha1); |
Tay Ray Chuan | 39dc52c | 2009-06-06 16:44:00 +0800 | [diff] [blame] | 885 | if (http_get_file(url, filename, 0) != HTTP_OK) |
| 886 | ret = error("Unable to get pack index %s\n", url); |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 887 | |
Tay Ray Chuan | 39dc52c | 2009-06-06 16:44:00 +0800 | [diff] [blame] | 888 | cleanup: |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 889 | free(hex); |
| 890 | free(url); |
| 891 | return ret; |
| 892 | } |
| 893 | |
| 894 | static int fetch_and_setup_pack_index(struct packed_git **packs_head, |
| 895 | unsigned char *sha1, const char *base_url) |
| 896 | { |
| 897 | struct packed_git *new_pack; |
| 898 | |
| 899 | if (fetch_pack_index(sha1, base_url)) |
| 900 | return -1; |
| 901 | |
| 902 | new_pack = parse_pack_index(sha1); |
| 903 | if (!new_pack) |
| 904 | return -1; /* parse_pack_index() already issued error message */ |
| 905 | new_pack->next = *packs_head; |
| 906 | *packs_head = new_pack; |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | int http_get_info_packs(const char *base_url, struct packed_git **packs_head) |
| 911 | { |
| 912 | int ret = 0, i = 0; |
| 913 | char *url, *data; |
| 914 | struct strbuf buf = STRBUF_INIT; |
| 915 | unsigned char sha1[20]; |
| 916 | |
| 917 | end_url_with_slash(&buf, base_url); |
| 918 | strbuf_addstr(&buf, "objects/info/packs"); |
| 919 | url = strbuf_detach(&buf, NULL); |
| 920 | |
| 921 | ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE); |
| 922 | if (ret != HTTP_OK) |
| 923 | goto cleanup; |
| 924 | |
| 925 | data = buf.buf; |
| 926 | while (i < buf.len) { |
| 927 | switch (data[i]) { |
| 928 | case 'P': |
| 929 | i++; |
| 930 | if (i + 52 <= buf.len && |
| 931 | !prefixcmp(data + i, " pack-") && |
| 932 | !prefixcmp(data + i + 46, ".pack\n")) { |
| 933 | get_sha1_hex(data + i + 6, sha1); |
| 934 | fetch_and_setup_pack_index(packs_head, sha1, |
| 935 | base_url); |
| 936 | i += 51; |
| 937 | break; |
| 938 | } |
| 939 | default: |
| 940 | while (i < buf.len && data[i] != '\n') |
| 941 | i++; |
| 942 | } |
| 943 | i++; |
| 944 | } |
| 945 | |
| 946 | cleanup: |
| 947 | free(url); |
| 948 | return ret; |
| 949 | } |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 950 | |
| 951 | void release_http_pack_request(struct http_pack_request *preq) |
| 952 | { |
| 953 | if (preq->packfile != NULL) { |
| 954 | fclose(preq->packfile); |
| 955 | preq->packfile = NULL; |
| 956 | preq->slot->local = NULL; |
| 957 | } |
| 958 | if (preq->range_header != NULL) { |
| 959 | curl_slist_free_all(preq->range_header); |
| 960 | preq->range_header = NULL; |
| 961 | } |
| 962 | preq->slot = NULL; |
| 963 | free(preq->url); |
| 964 | } |
| 965 | |
| 966 | int finish_http_pack_request(struct http_pack_request *preq) |
| 967 | { |
| 968 | int ret; |
| 969 | struct packed_git **lst; |
| 970 | |
| 971 | preq->target->pack_size = ftell(preq->packfile); |
| 972 | |
| 973 | if (preq->packfile != NULL) { |
| 974 | fclose(preq->packfile); |
| 975 | preq->packfile = NULL; |
| 976 | preq->slot->local = NULL; |
| 977 | } |
| 978 | |
| 979 | ret = move_temp_to_file(preq->tmpfile, preq->filename); |
| 980 | if (ret) |
| 981 | return ret; |
| 982 | |
| 983 | lst = preq->lst; |
| 984 | while (*lst != preq->target) |
| 985 | lst = &((*lst)->next); |
| 986 | *lst = (*lst)->next; |
| 987 | |
| 988 | if (verify_pack(preq->target)) |
| 989 | return -1; |
| 990 | install_packed_git(preq->target); |
| 991 | |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | struct http_pack_request *new_http_pack_request( |
| 996 | struct packed_git *target, const char *base_url) |
| 997 | { |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 998 | char *filename; |
| 999 | long prev_posn = 0; |
| 1000 | char range[RANGE_HEADER_SIZE]; |
| 1001 | struct strbuf buf = STRBUF_INIT; |
| 1002 | struct http_pack_request *preq; |
| 1003 | |
| 1004 | preq = xmalloc(sizeof(*preq)); |
| 1005 | preq->target = target; |
| 1006 | preq->range_header = NULL; |
| 1007 | |
| 1008 | end_url_with_slash(&buf, base_url); |
| 1009 | strbuf_addf(&buf, "objects/pack/pack-%s.pack", |
| 1010 | sha1_to_hex(target->sha1)); |
Tay Ray Chuan | bb99190 | 2009-08-10 23:59:55 +0800 | [diff] [blame] | 1011 | preq->url = strbuf_detach(&buf, NULL); |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 1012 | |
| 1013 | filename = sha1_pack_name(target->sha1); |
| 1014 | snprintf(preq->filename, sizeof(preq->filename), "%s", filename); |
| 1015 | snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp", filename); |
| 1016 | preq->packfile = fopen(preq->tmpfile, "a"); |
| 1017 | if (!preq->packfile) { |
| 1018 | error("Unable to open local file %s for pack", |
| 1019 | preq->tmpfile); |
| 1020 | goto abort; |
| 1021 | } |
| 1022 | |
| 1023 | preq->slot = get_active_slot(); |
| 1024 | preq->slot->local = preq->packfile; |
| 1025 | curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile); |
| 1026 | curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite); |
Tay Ray Chuan | bb99190 | 2009-08-10 23:59:55 +0800 | [diff] [blame] | 1027 | curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url); |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 1028 | curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, |
| 1029 | no_pragma_header); |
| 1030 | |
| 1031 | /* |
| 1032 | * If there is data present from a previous transfer attempt, |
| 1033 | * resume where it left off |
| 1034 | */ |
| 1035 | prev_posn = ftell(preq->packfile); |
| 1036 | if (prev_posn>0) { |
| 1037 | if (http_is_verbose) |
| 1038 | fprintf(stderr, |
| 1039 | "Resuming fetch of pack %s at byte %ld\n", |
| 1040 | sha1_to_hex(target->sha1), prev_posn); |
| 1041 | sprintf(range, "Range: bytes=%ld-", prev_posn); |
| 1042 | preq->range_header = curl_slist_append(NULL, range); |
| 1043 | curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, |
| 1044 | preq->range_header); |
| 1045 | } |
| 1046 | |
| 1047 | return preq; |
| 1048 | |
| 1049 | abort: |
| 1050 | free(filename); |
Tay Ray Chuan | bb99190 | 2009-08-10 23:59:55 +0800 | [diff] [blame] | 1051 | free(preq->url); |
Tay Ray Chuan | 5ae9ebf | 2009-08-10 23:55:48 +0800 | [diff] [blame] | 1052 | free(preq); |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 1053 | return NULL; |
| 1054 | } |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1055 | |
| 1056 | /* Helpers for fetching objects (loose) */ |
| 1057 | static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, |
| 1058 | void *data) |
| 1059 | { |
| 1060 | unsigned char expn[4096]; |
| 1061 | size_t size = eltsize * nmemb; |
| 1062 | int posn = 0; |
| 1063 | struct http_object_request *freq = |
| 1064 | (struct http_object_request *)data; |
| 1065 | do { |
| 1066 | ssize_t retval = xwrite(freq->localfile, |
| 1067 | (char *) ptr + posn, size - posn); |
| 1068 | if (retval < 0) |
| 1069 | return posn; |
| 1070 | posn += retval; |
| 1071 | } while (posn < size); |
| 1072 | |
| 1073 | freq->stream.avail_in = size; |
| 1074 | freq->stream.next_in = ptr; |
| 1075 | do { |
| 1076 | freq->stream.next_out = expn; |
| 1077 | freq->stream.avail_out = sizeof(expn); |
| 1078 | freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH); |
| 1079 | git_SHA1_Update(&freq->c, expn, |
| 1080 | sizeof(expn) - freq->stream.avail_out); |
| 1081 | } while (freq->stream.avail_in && freq->zret == Z_OK); |
| 1082 | data_received++; |
| 1083 | return size; |
| 1084 | } |
| 1085 | |
| 1086 | struct http_object_request *new_http_object_request(const char *base_url, |
| 1087 | unsigned char *sha1) |
| 1088 | { |
| 1089 | char *hex = sha1_to_hex(sha1); |
| 1090 | char *filename; |
| 1091 | char prevfile[PATH_MAX]; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1092 | int prevlocal; |
| 1093 | unsigned char prev_buf[PREV_BUF_SIZE]; |
| 1094 | ssize_t prev_read = 0; |
| 1095 | long prev_posn = 0; |
| 1096 | char range[RANGE_HEADER_SIZE]; |
| 1097 | struct curl_slist *range_header = NULL; |
| 1098 | struct http_object_request *freq; |
| 1099 | |
| 1100 | freq = xmalloc(sizeof(*freq)); |
| 1101 | hashcpy(freq->sha1, sha1); |
| 1102 | freq->localfile = -1; |
| 1103 | |
| 1104 | filename = sha1_file_name(sha1); |
| 1105 | snprintf(freq->filename, sizeof(freq->filename), "%s", filename); |
| 1106 | snprintf(freq->tmpfile, sizeof(freq->tmpfile), |
| 1107 | "%s.temp", filename); |
| 1108 | |
| 1109 | snprintf(prevfile, sizeof(prevfile), "%s.prev", filename); |
| 1110 | unlink_or_warn(prevfile); |
| 1111 | rename(freq->tmpfile, prevfile); |
| 1112 | unlink_or_warn(freq->tmpfile); |
| 1113 | |
| 1114 | if (freq->localfile != -1) |
| 1115 | error("fd leakage in start: %d", freq->localfile); |
| 1116 | freq->localfile = open(freq->tmpfile, |
| 1117 | O_WRONLY | O_CREAT | O_EXCL, 0666); |
| 1118 | /* |
| 1119 | * This could have failed due to the "lazy directory creation"; |
| 1120 | * try to mkdir the last path component. |
| 1121 | */ |
| 1122 | if (freq->localfile < 0 && errno == ENOENT) { |
| 1123 | char *dir = strrchr(freq->tmpfile, '/'); |
| 1124 | if (dir) { |
| 1125 | *dir = 0; |
| 1126 | mkdir(freq->tmpfile, 0777); |
| 1127 | *dir = '/'; |
| 1128 | } |
| 1129 | freq->localfile = open(freq->tmpfile, |
| 1130 | O_WRONLY | O_CREAT | O_EXCL, 0666); |
| 1131 | } |
| 1132 | |
| 1133 | if (freq->localfile < 0) { |
| 1134 | error("Couldn't create temporary file %s for %s: %s", |
| 1135 | freq->tmpfile, freq->filename, strerror(errno)); |
| 1136 | goto abort; |
| 1137 | } |
| 1138 | |
| 1139 | memset(&freq->stream, 0, sizeof(freq->stream)); |
| 1140 | |
| 1141 | git_inflate_init(&freq->stream); |
| 1142 | |
| 1143 | git_SHA1_Init(&freq->c); |
| 1144 | |
Tay Ray Chuan | bb99190 | 2009-08-10 23:59:55 +0800 | [diff] [blame] | 1145 | freq->url = get_remote_object_url(base_url, hex, 0); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1146 | |
| 1147 | /* |
| 1148 | * If a previous temp file is present, process what was already |
| 1149 | * fetched. |
| 1150 | */ |
| 1151 | prevlocal = open(prevfile, O_RDONLY); |
| 1152 | if (prevlocal != -1) { |
| 1153 | do { |
| 1154 | prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE); |
| 1155 | if (prev_read>0) { |
| 1156 | if (fwrite_sha1_file(prev_buf, |
| 1157 | 1, |
| 1158 | prev_read, |
| 1159 | freq) == prev_read) { |
| 1160 | prev_posn += prev_read; |
| 1161 | } else { |
| 1162 | prev_read = -1; |
| 1163 | } |
| 1164 | } |
| 1165 | } while (prev_read > 0); |
| 1166 | close(prevlocal); |
| 1167 | } |
| 1168 | unlink_or_warn(prevfile); |
| 1169 | |
| 1170 | /* |
| 1171 | * Reset inflate/SHA1 if there was an error reading the previous temp |
| 1172 | * file; also rewind to the beginning of the local file. |
| 1173 | */ |
| 1174 | if (prev_read == -1) { |
| 1175 | memset(&freq->stream, 0, sizeof(freq->stream)); |
| 1176 | git_inflate_init(&freq->stream); |
| 1177 | git_SHA1_Init(&freq->c); |
| 1178 | if (prev_posn>0) { |
| 1179 | prev_posn = 0; |
| 1180 | lseek(freq->localfile, 0, SEEK_SET); |
Jeff Lasslett | 0c4f21e | 2009-08-11 00:05:06 +0800 | [diff] [blame] | 1181 | if (ftruncate(freq->localfile, 0) < 0) { |
| 1182 | error("Couldn't truncate temporary file %s for %s: %s", |
| 1183 | freq->tmpfile, freq->filename, strerror(errno)); |
| 1184 | goto abort; |
| 1185 | } |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | freq->slot = get_active_slot(); |
| 1190 | |
| 1191 | curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq); |
| 1192 | curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); |
| 1193 | curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr); |
Tay Ray Chuan | bb99190 | 2009-08-10 23:59:55 +0800 | [diff] [blame] | 1194 | curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1195 | curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); |
| 1196 | |
| 1197 | /* |
| 1198 | * If we have successfully processed data from a previous fetch |
| 1199 | * attempt, only fetch the data we don't already have. |
| 1200 | */ |
| 1201 | if (prev_posn>0) { |
| 1202 | if (http_is_verbose) |
| 1203 | fprintf(stderr, |
| 1204 | "Resuming fetch of object %s at byte %ld\n", |
| 1205 | hex, prev_posn); |
| 1206 | sprintf(range, "Range: bytes=%ld-", prev_posn); |
| 1207 | range_header = curl_slist_append(range_header, range); |
| 1208 | curl_easy_setopt(freq->slot->curl, |
| 1209 | CURLOPT_HTTPHEADER, range_header); |
| 1210 | } |
| 1211 | |
| 1212 | return freq; |
| 1213 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1214 | abort: |
| 1215 | free(filename); |
Tay Ray Chuan | bb99190 | 2009-08-10 23:59:55 +0800 | [diff] [blame] | 1216 | free(freq->url); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1217 | free(freq); |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
| 1221 | void process_http_object_request(struct http_object_request *freq) |
| 1222 | { |
| 1223 | if (freq->slot == NULL) |
| 1224 | return; |
| 1225 | freq->curl_result = freq->slot->curl_result; |
| 1226 | freq->http_code = freq->slot->http_code; |
| 1227 | freq->slot = NULL; |
| 1228 | } |
| 1229 | |
| 1230 | int finish_http_object_request(struct http_object_request *freq) |
| 1231 | { |
| 1232 | struct stat st; |
| 1233 | |
| 1234 | close(freq->localfile); |
| 1235 | freq->localfile = -1; |
| 1236 | |
| 1237 | process_http_object_request(freq); |
| 1238 | |
| 1239 | if (freq->http_code == 416) { |
| 1240 | fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n"); |
| 1241 | } else if (freq->curl_result != CURLE_OK) { |
| 1242 | if (stat(freq->tmpfile, &st) == 0) |
| 1243 | if (st.st_size == 0) |
| 1244 | unlink_or_warn(freq->tmpfile); |
| 1245 | return -1; |
| 1246 | } |
| 1247 | |
| 1248 | git_inflate_end(&freq->stream); |
| 1249 | git_SHA1_Final(freq->real_sha1, &freq->c); |
| 1250 | if (freq->zret != Z_STREAM_END) { |
| 1251 | unlink_or_warn(freq->tmpfile); |
| 1252 | return -1; |
| 1253 | } |
| 1254 | if (hashcmp(freq->sha1, freq->real_sha1)) { |
| 1255 | unlink_or_warn(freq->tmpfile); |
| 1256 | return -1; |
| 1257 | } |
| 1258 | freq->rename = |
| 1259 | move_temp_to_file(freq->tmpfile, freq->filename); |
| 1260 | |
| 1261 | return freq->rename; |
| 1262 | } |
| 1263 | |
| 1264 | void abort_http_object_request(struct http_object_request *freq) |
| 1265 | { |
| 1266 | unlink_or_warn(freq->tmpfile); |
| 1267 | |
| 1268 | release_http_object_request(freq); |
| 1269 | } |
| 1270 | |
| 1271 | void release_http_object_request(struct http_object_request *freq) |
| 1272 | { |
| 1273 | if (freq->localfile != -1) { |
| 1274 | close(freq->localfile); |
| 1275 | freq->localfile = -1; |
| 1276 | } |
| 1277 | if (freq->url != NULL) { |
| 1278 | free(freq->url); |
| 1279 | freq->url = NULL; |
| 1280 | } |
Tay Ray Chuan | 4b9fa0e | 2009-08-26 20:20:53 +0800 | [diff] [blame] | 1281 | if (freq->slot != NULL) { |
| 1282 | freq->slot->callback_func = NULL; |
| 1283 | freq->slot->callback_data = NULL; |
| 1284 | release_active_slot(freq->slot); |
| 1285 | freq->slot = NULL; |
| 1286 | } |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 1287 | } |