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