| David Aguilar | 1c4b660 | 2014-09-14 07:40:45 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 2 | #include "http.h" |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 3 | #include "pack.h" |
| Shawn O. Pearce | de1a2fd | 2009-10-31 00:47:41 | [diff] [blame] | 4 | #include "sideband.h" |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 5 | #include "run-command.h" |
| Gabriel Corona | f39f72d | 2010-11-14 01:51:15 | [diff] [blame] | 6 | #include "url.h" |
| Kyle J. McKay | 6a56993 | 2013-08-05 20:20:36 | [diff] [blame] | 7 | #include "urlmatch.h" |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 8 | #include "credential.h" |
| Jeff King | 745c7c8 | 2012-06-02 19:03:08 | [diff] [blame] | 9 | #include "version.h" |
| Jeff King | 047ec60 | 2013-02-20 20:02:45 | [diff] [blame] | 10 | #include "pkt-line.h" |
| Jeff King | 93f7d91 | 2015-02-26 03:04:16 | [diff] [blame] | 11 | #include "gettext.h" |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 12 | |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 13 | int active_requests; |
| Tay Ray Chuan | e917674 | 2009-06-06 08:43:41 | [diff] [blame] | 14 | int http_is_verbose; |
| Shawn O. Pearce | de1a2fd | 2009-10-31 00:47:41 | [diff] [blame] | 15 | size_t http_post_buffer = 16 * LARGE_PACKET_MAX; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 16 | |
| Martin Storsjö | b8ac923 | 2009-11-27 15:43:08 | [diff] [blame] | 17 | #if LIBCURL_VERSION_NUM >= 0x070a06 |
| 18 | #define LIBCURL_CAN_HANDLE_AUTH_ANY |
| 19 | #endif |
| 20 | |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 21 | static int min_curl_sessions = 1; |
| 22 | static int curl_session_count; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 23 | #ifdef USE_CURL_MULTI |
| Mike Hommey | cc3530e | 2007-12-09 17:04:57 | [diff] [blame] | 24 | static int max_requests = -1; |
| 25 | static CURLM *curlm; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 26 | #endif |
| 27 | #ifndef NO_CURL_EASY_DUPHANDLE |
| Mike Hommey | cc3530e | 2007-12-09 17:04:57 | [diff] [blame] | 28 | static CURL *curl_default; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 29 | #endif |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 30 | |
| 31 | #define PREV_BUF_SIZE 4096 |
| 32 | #define RANGE_HEADER_SIZE 30 |
| 33 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 34 | char curl_errorstr[CURL_ERROR_SIZE]; |
| 35 | |
| Mike Hommey | cc3530e | 2007-12-09 17:04:57 | [diff] [blame] | 36 | static int curl_ssl_verify = -1; |
| Modestas Vainius | 4bc444e | 2013-04-07 19:10:39 | [diff] [blame] | 37 | static int curl_ssl_try; |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 38 | static const char *ssl_cert; |
| Mark Lodato | ef52aaf | 2009-06-15 02:39:00 | [diff] [blame] | 39 | #if LIBCURL_VERSION_NUM >= 0x070903 |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 40 | static const char *ssl_key; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 41 | #endif |
| 42 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 43 | static const char *ssl_capath; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 44 | #endif |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 45 | static const char *ssl_cainfo; |
| Mike Hommey | cc3530e | 2007-12-09 17:04:57 | [diff] [blame] | 46 | static long curl_low_speed_limit = -1; |
| 47 | static long curl_low_speed_time = -1; |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 48 | static int curl_ftp_no_epsv; |
| 49 | static const char *curl_http_proxy; |
| Duncan Brown | bcfb95d | 2011-06-02 20:31:25 | [diff] [blame] | 50 | static const char *curl_cookie_file; |
| Dave Borowitz | 912b2ac | 2013-07-23 22:40:17 | [diff] [blame] | 51 | static int curl_save_cookies; |
| Jeff King | 2501aff | 2013-09-28 08:31:45 | [diff] [blame] | 52 | struct credential http_auth = CREDENTIAL_INIT; |
| Jeff King | a4ddbc3 | 2011-12-14 00:11:56 | [diff] [blame] | 53 | static int http_proactive_auth; |
| Spencer E. Olson | b1d1058 | 2010-08-11 20:40:38 | [diff] [blame] | 54 | static const char *user_agent; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 55 | |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 56 | #if LIBCURL_VERSION_NUM >= 0x071700 |
| 57 | /* Use CURLOPT_KEYPASSWD as is */ |
| 58 | #elif LIBCURL_VERSION_NUM >= 0x070903 |
| 59 | #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD |
| 60 | #else |
| 61 | #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD |
| 62 | #endif |
| 63 | |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 64 | static struct credential cert_auth = CREDENTIAL_INIT; |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 65 | static int ssl_cert_password_required; |
| brian m. carlson | 4dbe664 | 2015-01-08 00:29:20 | [diff] [blame] | 66 | #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY |
| 67 | static unsigned long http_auth_methods = CURLAUTH_ANY; |
| 68 | #endif |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 69 | |
| Mike Hommey | cc3530e | 2007-12-09 17:04:57 | [diff] [blame] | 70 | static struct curl_slist *pragma_header; |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 71 | static struct curl_slist *no_pragma_header; |
| Tay Ray Chuan | e917674 | 2009-06-06 08:43:41 | [diff] [blame] | 72 | |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 73 | static struct active_request_slot *active_queue_head; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 74 | |
| Yi EungJun | f18604b | 2015-01-28 12:04:37 | [diff] [blame] | 75 | static char *cached_accept_language; |
| 76 | |
| Dan McGee | a04ff3e | 2011-05-03 15:47:27 | [diff] [blame] | 77 | size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 78 | { |
| 79 | size_t size = eltsize * nmemb; |
| Junio C Hamano | f444e52 | 2008-07-04 07:37:40 | [diff] [blame] | 80 | struct buffer *buffer = buffer_; |
| 81 | |
| Mike Hommey | 028c297 | 2007-12-09 19:30:59 | [diff] [blame] | 82 | if (size > buffer->buf.len - buffer->posn) |
| 83 | size = buffer->buf.len - buffer->posn; |
| 84 | memcpy(ptr, buffer->buf.buf + buffer->posn, size); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 85 | buffer->posn += size; |
| Mike Hommey | 028c297 | 2007-12-09 19:30:59 | [diff] [blame] | 86 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 87 | return size; |
| 88 | } |
| 89 | |
| Martin Storsjö | 3944ba0 | 2009-04-01 16:48:24 | [diff] [blame] | 90 | #ifndef NO_CURL_IOCTL |
| 91 | curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp) |
| 92 | { |
| 93 | struct buffer *buffer = clientp; |
| 94 | |
| 95 | switch (cmd) { |
| 96 | case CURLIOCMD_NOP: |
| 97 | return CURLIOE_OK; |
| 98 | |
| 99 | case CURLIOCMD_RESTARTREAD: |
| 100 | buffer->posn = 0; |
| 101 | return CURLIOE_OK; |
| 102 | |
| 103 | default: |
| 104 | return CURLIOE_UNKNOWNCMD; |
| 105 | } |
| 106 | } |
| 107 | #endif |
| 108 | |
| Dan McGee | a04ff3e | 2011-05-03 15:47:27 | [diff] [blame] | 109 | size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 110 | { |
| 111 | size_t size = eltsize * nmemb; |
| Junio C Hamano | f444e52 | 2008-07-04 07:37:40 | [diff] [blame] | 112 | struct strbuf *buffer = buffer_; |
| 113 | |
| Mike Hommey | 028c297 | 2007-12-09 19:30:59 | [diff] [blame] | 114 | strbuf_add(buffer, ptr, size); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 115 | return size; |
| 116 | } |
| 117 | |
| Dan McGee | a04ff3e | 2011-05-03 15:47:27 | [diff] [blame] | 118 | size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 119 | { |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 120 | return eltsize * nmemb; |
| 121 | } |
| 122 | |
| Junio C Hamano | b90a3d7 | 2015-01-14 23:40:46 | [diff] [blame] | 123 | static void closedown_active_slot(struct active_request_slot *slot) |
| 124 | { |
| 125 | active_requests--; |
| 126 | slot->in_use = 0; |
| 127 | } |
| 128 | |
| 129 | static void finish_active_slot(struct active_request_slot *slot) |
| 130 | { |
| 131 | closedown_active_slot(slot); |
| 132 | curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code); |
| 133 | |
| 134 | if (slot->finished != NULL) |
| 135 | (*slot->finished) = 1; |
| 136 | |
| 137 | /* Store slot results so they can be read after the slot is reused */ |
| 138 | if (slot->results != NULL) { |
| 139 | slot->results->curl_result = slot->curl_result; |
| 140 | slot->results->http_code = slot->http_code; |
| 141 | #if LIBCURL_VERSION_NUM >= 0x070a08 |
| 142 | curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL, |
| 143 | &slot->results->auth_avail); |
| 144 | #else |
| 145 | slot->results->auth_avail = 0; |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | /* Run callback if appropriate */ |
| 150 | if (slot->callback_func != NULL) |
| 151 | slot->callback_func(slot->callback_data); |
| 152 | } |
| 153 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 154 | #ifdef USE_CURL_MULTI |
| 155 | static void process_curl_messages(void) |
| 156 | { |
| 157 | int num_messages; |
| 158 | struct active_request_slot *slot; |
| 159 | CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); |
| 160 | |
| 161 | while (curl_message != NULL) { |
| 162 | if (curl_message->msg == CURLMSG_DONE) { |
| 163 | int curl_result = curl_message->data.result; |
| 164 | slot = active_queue_head; |
| 165 | while (slot != NULL && |
| 166 | slot->curl != curl_message->easy_handle) |
| 167 | slot = slot->next; |
| 168 | if (slot != NULL) { |
| 169 | curl_multi_remove_handle(curlm, slot->curl); |
| 170 | slot->curl_result = curl_result; |
| 171 | finish_active_slot(slot); |
| 172 | } else { |
| 173 | fprintf(stderr, "Received DONE message for unknown request!\n"); |
| 174 | } |
| 175 | } else { |
| 176 | fprintf(stderr, "Unknown CURL message received: %d\n", |
| 177 | (int)curl_message->msg); |
| 178 | } |
| 179 | curl_message = curl_multi_info_read(curlm, &num_messages); |
| 180 | } |
| 181 | } |
| 182 | #endif |
| 183 | |
| Johannes Schindelin | ef90d6d | 2008-05-14 17:46:53 | [diff] [blame] | 184 | static int http_options(const char *var, const char *value, void *cb) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 185 | { |
| 186 | if (!strcmp("http.sslverify", var)) { |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 187 | curl_ssl_verify = git_config_bool(var, value); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 190 | if (!strcmp("http.sslcert", var)) |
| 191 | return git_config_string(&ssl_cert, var, value); |
| Mark Lodato | ef52aaf | 2009-06-15 02:39:00 | [diff] [blame] | 192 | #if LIBCURL_VERSION_NUM >= 0x070903 |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 193 | if (!strcmp("http.sslkey", var)) |
| 194 | return git_config_string(&ssl_key, var, value); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 195 | #endif |
| 196 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 197 | if (!strcmp("http.sslcapath", var)) |
| 198 | return git_config_string(&ssl_capath, var, value); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 199 | #endif |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 200 | if (!strcmp("http.sslcainfo", var)) |
| 201 | return git_config_string(&ssl_cainfo, var, value); |
| Mark Lodato | 754ae19 | 2009-05-28 03:16:03 | [diff] [blame] | 202 | if (!strcmp("http.sslcertpasswordprotected", var)) { |
| Junio C Hamano | 3f4ccd2 | 2013-07-12 18:52:47 | [diff] [blame] | 203 | ssl_cert_password_required = git_config_bool(var, value); |
| Mark Lodato | 754ae19 | 2009-05-28 03:16:03 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| Modestas Vainius | 4bc444e | 2013-04-07 19:10:39 | [diff] [blame] | 206 | if (!strcmp("http.ssltry", var)) { |
| 207 | curl_ssl_try = git_config_bool(var, value); |
| 208 | return 0; |
| 209 | } |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 210 | if (!strcmp("http.minsessions", var)) { |
| 211 | min_curl_sessions = git_config_int(var, value); |
| 212 | #ifndef USE_CURL_MULTI |
| 213 | if (min_curl_sessions > 1) |
| 214 | min_curl_sessions = 1; |
| 215 | #endif |
| 216 | return 0; |
| 217 | } |
| Junio C Hamano | a6080a0 | 2007-06-07 07:04:01 | [diff] [blame] | 218 | #ifdef USE_CURL_MULTI |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 219 | if (!strcmp("http.maxrequests", var)) { |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 220 | max_requests = git_config_int(var, value); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | #endif |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 224 | if (!strcmp("http.lowspeedlimit", var)) { |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 225 | curl_low_speed_limit = (long)git_config_int(var, value); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | if (!strcmp("http.lowspeedtime", var)) { |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 229 | curl_low_speed_time = (long)git_config_int(var, value); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
| Sasha Khapyorsky | 3ea099d | 2006-09-29 00:10:44 | [diff] [blame] | 233 | if (!strcmp("http.noepsv", var)) { |
| 234 | curl_ftp_no_epsv = git_config_bool(var, value); |
| 235 | return 0; |
| 236 | } |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 237 | if (!strcmp("http.proxy", var)) |
| 238 | return git_config_string(&curl_http_proxy, var, value); |
| Sasha Khapyorsky | 3ea099d | 2006-09-29 00:10:44 | [diff] [blame] | 239 | |
| Duncan Brown | bcfb95d | 2011-06-02 20:31:25 | [diff] [blame] | 240 | if (!strcmp("http.cookiefile", var)) |
| 241 | return git_config_string(&curl_cookie_file, var, value); |
| Dave Borowitz | 912b2ac | 2013-07-23 22:40:17 | [diff] [blame] | 242 | if (!strcmp("http.savecookies", var)) { |
| 243 | curl_save_cookies = git_config_bool(var, value); |
| 244 | return 0; |
| 245 | } |
| Duncan Brown | bcfb95d | 2011-06-02 20:31:25 | [diff] [blame] | 246 | |
| Shawn O. Pearce | de1a2fd | 2009-10-31 00:47:41 | [diff] [blame] | 247 | if (!strcmp("http.postbuffer", var)) { |
| 248 | http_post_buffer = git_config_int(var, value); |
| 249 | if (http_post_buffer < LARGE_PACKET_MAX) |
| 250 | http_post_buffer = LARGE_PACKET_MAX; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| Spencer E. Olson | b1d1058 | 2010-08-11 20:40:38 | [diff] [blame] | 254 | if (!strcmp("http.useragent", var)) |
| 255 | return git_config_string(&user_agent, var, value); |
| 256 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 257 | /* Fall back on the default ones */ |
| Johannes Schindelin | ef90d6d | 2008-05-14 17:46:53 | [diff] [blame] | 258 | return git_default_config(var, value, cb); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 259 | } |
| 260 | |
| Junio C Hamano | c33976c | 2009-03-10 06:34:25 | [diff] [blame] | 261 | static void init_curl_http_auth(CURL *result) |
| 262 | { |
| Jeff King | 6f4c347 | 2012-04-13 06:19:25 | [diff] [blame] | 263 | if (!http_auth.username) |
| 264 | return; |
| 265 | |
| 266 | credential_fill(&http_auth); |
| 267 | |
| 268 | #if LIBCURL_VERSION_NUM >= 0x071301 |
| 269 | curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username); |
| 270 | curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password); |
| 271 | #else |
| 272 | { |
| Jeff King | aa0834a | 2012-04-13 06:18:35 | [diff] [blame] | 273 | static struct strbuf up = STRBUF_INIT; |
| Brandon Casey | a94cf2c | 2013-06-19 02:43:49 | [diff] [blame] | 274 | /* |
| 275 | * Note that we assume we only ever have a single set of |
| 276 | * credentials in a given program run, so we do not have |
| 277 | * to worry about updating this buffer, only setting its |
| 278 | * initial value. |
| 279 | */ |
| 280 | if (!up.len) |
| 281 | strbuf_addf(&up, "%s:%s", |
| 282 | http_auth.username, http_auth.password); |
| Jeff King | aa0834a | 2012-04-13 06:18:35 | [diff] [blame] | 283 | curl_easy_setopt(result, CURLOPT_USERPWD, up.buf); |
| Junio C Hamano | c33976c | 2009-03-10 06:34:25 | [diff] [blame] | 284 | } |
| Jeff King | 6f4c347 | 2012-04-13 06:19:25 | [diff] [blame] | 285 | #endif |
| Junio C Hamano | c33976c | 2009-03-10 06:34:25 | [diff] [blame] | 286 | } |
| 287 | |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 288 | static int has_cert_password(void) |
| 289 | { |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 290 | if (ssl_cert == NULL || ssl_cert_password_required != 1) |
| 291 | return 0; |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 292 | if (!cert_auth.password) { |
| 293 | cert_auth.protocol = xstrdup("cert"); |
| Rene Bredlau | 75e9a40 | 2012-12-21 16:31:19 | [diff] [blame] | 294 | cert_auth.username = xstrdup(""); |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 295 | cert_auth.path = xstrdup(ssl_cert); |
| 296 | credential_fill(&cert_auth); |
| 297 | } |
| 298 | return 1; |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 299 | } |
| 300 | |
| Jeff King | 47ce115 | 2013-10-15 00:06:14 | [diff] [blame] | 301 | #if LIBCURL_VERSION_NUM >= 0x071900 |
| 302 | static void set_curl_keepalive(CURL *c) |
| 303 | { |
| 304 | curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1); |
| 305 | } |
| 306 | |
| 307 | #elif LIBCURL_VERSION_NUM >= 0x071000 |
| Eric Wong | a15d069 | 2013-10-12 22:29:40 | [diff] [blame] | 308 | static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type) |
| 309 | { |
| 310 | int ka = 1; |
| 311 | int rc; |
| 312 | socklen_t len = (socklen_t)sizeof(ka); |
| 313 | |
| 314 | if (type != CURLSOCKTYPE_IPCXN) |
| 315 | return 0; |
| 316 | |
| 317 | rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len); |
| 318 | if (rc < 0) |
| 319 | warning("unable to set SO_KEEPALIVE on socket %s", |
| 320 | strerror(errno)); |
| 321 | |
| 322 | return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */ |
| 323 | } |
| 324 | |
| Jeff King | 47ce115 | 2013-10-15 00:06:14 | [diff] [blame] | 325 | static void set_curl_keepalive(CURL *c) |
| 326 | { |
| 327 | curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback); |
| 328 | } |
| 329 | |
| 330 | #else |
| 331 | static void set_curl_keepalive(CURL *c) |
| 332 | { |
| 333 | /* not supported on older curl versions */ |
| 334 | } |
| 335 | #endif |
| 336 | |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 337 | static CURL *get_curl_handle(void) |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 338 | { |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 339 | CURL *result = curl_easy_init(); |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 340 | |
| Bernhard Reiter | faa3807 | 2014-08-13 17:31:24 | [diff] [blame] | 341 | if (!result) |
| 342 | die("curl_easy_init failed"); |
| 343 | |
| Junio C Hamano | a5ccc59 | 2008-02-21 23:10:37 | [diff] [blame] | 344 | if (!curl_ssl_verify) { |
| 345 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0); |
| 346 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0); |
| 347 | } else { |
| 348 | /* Verify authenticity of the peer's certificate */ |
| 349 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1); |
| 350 | /* The name in the cert must match whom we tried to connect */ |
| 351 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2); |
| 352 | } |
| 353 | |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 354 | #if LIBCURL_VERSION_NUM >= 0x070907 |
| 355 | curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); |
| 356 | #endif |
| Martin Storsjö | b8ac923 | 2009-11-27 15:43:08 | [diff] [blame] | 357 | #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY |
| Junio C Hamano | 525ecd2 | 2009-12-28 18:04:24 | [diff] [blame] | 358 | curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
| Martin Storsjö | b8ac923 | 2009-11-27 15:43:08 | [diff] [blame] | 359 | #endif |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 360 | |
| Jeff King | a4ddbc3 | 2011-12-14 00:11:56 | [diff] [blame] | 361 | if (http_proactive_auth) |
| 362 | init_curl_http_auth(result); |
| 363 | |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 364 | if (ssl_cert != NULL) |
| 365 | curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert); |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 366 | if (has_cert_password()) |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 367 | curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password); |
| Mark Lodato | ef52aaf | 2009-06-15 02:39:00 | [diff] [blame] | 368 | #if LIBCURL_VERSION_NUM >= 0x070903 |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 369 | if (ssl_key != NULL) |
| 370 | curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key); |
| 371 | #endif |
| 372 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| 373 | if (ssl_capath != NULL) |
| 374 | curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath); |
| 375 | #endif |
| 376 | if (ssl_cainfo != NULL) |
| 377 | curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 378 | |
| 379 | if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { |
| 380 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT, |
| 381 | curl_low_speed_limit); |
| 382 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME, |
| 383 | curl_low_speed_time); |
| 384 | } |
| 385 | |
| 386 | curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1); |
| Tay Ray Chuan | 311e2ea | 2010-09-25 04:20:35 | [diff] [blame] | 387 | #if LIBCURL_VERSION_NUM >= 0x071301 |
| 388 | curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); |
| 389 | #elif LIBCURL_VERSION_NUM >= 0x071101 |
| 390 | curl_easy_setopt(result, CURLOPT_POST301, 1); |
| 391 | #endif |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 392 | |
| Mark Wooding | 7982d74 | 2006-02-01 11:44:37 | [diff] [blame] | 393 | if (getenv("GIT_CURL_VERBOSE")) |
| 394 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1); |
| 395 | |
| Spencer E. Olson | b1d1058 | 2010-08-11 20:40:38 | [diff] [blame] | 396 | curl_easy_setopt(result, CURLOPT_USERAGENT, |
| Jeff King | 745c7c8 | 2012-06-02 19:03:08 | [diff] [blame] | 397 | user_agent ? user_agent : git_user_agent()); |
| Nick Hengeveld | 20fc9bc | 2006-04-04 17:11:29 | [diff] [blame] | 398 | |
| Sasha Khapyorsky | 3ea099d | 2006-09-29 00:10:44 | [diff] [blame] | 399 | if (curl_ftp_no_epsv) |
| 400 | curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0); |
| 401 | |
| Modestas Vainius | 4bc444e | 2013-04-07 19:10:39 | [diff] [blame] | 402 | #ifdef CURLOPT_USE_SSL |
| 403 | if (curl_ssl_try) |
| 404 | curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY); |
| 405 | #endif |
| 406 | |
| Nelson Benitez Leon | dd61399 | 2012-03-02 13:55:57 | [diff] [blame] | 407 | if (curl_http_proxy) { |
| Sam Vilain | 9c5665a | 2007-11-23 00:07:00 | [diff] [blame] | 408 | curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy); |
| Tom G. Christensen | 1c2dbf2 | 2015-02-03 17:30:08 | [diff] [blame] | 409 | #if LIBCURL_VERSION_NUM >= 0x070a07 |
| Nelson Benitez Leon | dd61399 | 2012-03-02 13:55:57 | [diff] [blame] | 410 | curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
| Tom G. Christensen | 1c2dbf2 | 2015-02-03 17:30:08 | [diff] [blame] | 411 | #endif |
| Nelson Benitez Leon | dd61399 | 2012-03-02 13:55:57 | [diff] [blame] | 412 | } |
| Sam Vilain | 9c5665a | 2007-11-23 00:07:00 | [diff] [blame] | 413 | |
| Jeff King | 47ce115 | 2013-10-15 00:06:14 | [diff] [blame] | 414 | set_curl_keepalive(result); |
| Eric Wong | a15d069 | 2013-10-12 22:29:40 | [diff] [blame] | 415 | |
| Junio C Hamano | 11979b9 | 2005-11-19 01:06:46 | [diff] [blame] | 416 | return result; |
| 417 | } |
| 418 | |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 419 | static void set_from_env(const char **var, const char *envname) |
| 420 | { |
| 421 | const char *val = getenv(envname); |
| 422 | if (val) |
| 423 | *var = val; |
| 424 | } |
| 425 | |
| Jeff King | a4ddbc3 | 2011-12-14 00:11:56 | [diff] [blame] | 426 | void http_init(struct remote *remote, const char *url, int proactive_auth) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 427 | { |
| 428 | char *low_speed_limit; |
| 429 | char *low_speed_time; |
| Kyle J. McKay | 6a56993 | 2013-08-05 20:20:36 | [diff] [blame] | 430 | char *normalized_url; |
| 431 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; |
| 432 | |
| 433 | config.section = "http"; |
| 434 | config.key = NULL; |
| 435 | config.collect_fn = http_options; |
| 436 | config.cascade_fn = git_default_config; |
| 437 | config.cb = NULL; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 438 | |
| Tay Ray Chuan | e917674 | 2009-06-06 08:43:41 | [diff] [blame] | 439 | http_is_verbose = 0; |
| Kyle J. McKay | 6a56993 | 2013-08-05 20:20:36 | [diff] [blame] | 440 | normalized_url = url_normalize(url, &config.url); |
| Tay Ray Chuan | e917674 | 2009-06-06 08:43:41 | [diff] [blame] | 441 | |
| Kyle J. McKay | 6a56993 | 2013-08-05 20:20:36 | [diff] [blame] | 442 | git_config(urlmatch_config_entry, &config); |
| 443 | free(normalized_url); |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 444 | |
| Bernhard Reiter | faa3807 | 2014-08-13 17:31:24 | [diff] [blame] | 445 | if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) |
| 446 | die("curl_global_init failed"); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 447 | |
| Jeff King | a4ddbc3 | 2011-12-14 00:11:56 | [diff] [blame] | 448 | http_proactive_auth = proactive_auth; |
| 449 | |
| Mike Hommey | 9fc6440 | 2008-02-27 20:35:50 | [diff] [blame] | 450 | if (remote && remote->http_proxy) |
| 451 | curl_http_proxy = xstrdup(remote->http_proxy); |
| 452 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 453 | pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache"); |
| Tay Ray Chuan | e917674 | 2009-06-06 08:43:41 | [diff] [blame] | 454 | no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:"); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 455 | |
| 456 | #ifdef USE_CURL_MULTI |
| 457 | { |
| 458 | char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); |
| 459 | if (http_max_requests != NULL) |
| 460 | max_requests = atoi(http_max_requests); |
| 461 | } |
| 462 | |
| 463 | curlm = curl_multi_init(); |
| Jeff King | 8837eb4 | 2014-08-17 07:35:53 | [diff] [blame] | 464 | if (!curlm) |
| 465 | die("curl_multi_init failed"); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 466 | #endif |
| 467 | |
| 468 | if (getenv("GIT_SSL_NO_VERIFY")) |
| 469 | curl_ssl_verify = 0; |
| 470 | |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 471 | set_from_env(&ssl_cert, "GIT_SSL_CERT"); |
| Mark Lodato | ef52aaf | 2009-06-15 02:39:00 | [diff] [blame] | 472 | #if LIBCURL_VERSION_NUM >= 0x070903 |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 473 | set_from_env(&ssl_key, "GIT_SSL_KEY"); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 474 | #endif |
| 475 | #if LIBCURL_VERSION_NUM >= 0x070908 |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 476 | set_from_env(&ssl_capath, "GIT_SSL_CAPATH"); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 477 | #endif |
| Junio C Hamano | 7059cd9 | 2009-03-10 02:00:30 | [diff] [blame] | 478 | set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO"); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 479 | |
| Spencer E. Olson | b1d1058 | 2010-08-11 20:40:38 | [diff] [blame] | 480 | set_from_env(&user_agent, "GIT_HTTP_USER_AGENT"); |
| 481 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 482 | low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT"); |
| 483 | if (low_speed_limit != NULL) |
| 484 | curl_low_speed_limit = strtol(low_speed_limit, NULL, 10); |
| 485 | low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME"); |
| 486 | if (low_speed_time != NULL) |
| 487 | curl_low_speed_time = strtol(low_speed_time, NULL, 10); |
| 488 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 489 | if (curl_ssl_verify == -1) |
| 490 | curl_ssl_verify = 1; |
| 491 | |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 492 | curl_session_count = 0; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 493 | #ifdef USE_CURL_MULTI |
| 494 | if (max_requests < 1) |
| 495 | max_requests = DEFAULT_MAX_REQUESTS; |
| 496 | #endif |
| 497 | |
| Sasha Khapyorsky | 3ea099d | 2006-09-29 00:10:44 | [diff] [blame] | 498 | if (getenv("GIT_CURL_FTP_NO_EPSV")) |
| 499 | curl_ftp_no_epsv = 1; |
| 500 | |
| Jeff King | deba493 | 2011-10-14 07:40:40 | [diff] [blame] | 501 | if (url) { |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 502 | credential_from_url(&http_auth, url); |
| Mark Lodato | 754ae19 | 2009-05-28 03:16:03 | [diff] [blame] | 503 | if (!ssl_cert_password_required && |
| 504 | getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") && |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 505 | starts_with(url, "https://")) |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 506 | ssl_cert_password_required = 1; |
| 507 | } |
| Junio C Hamano | c33976c | 2009-03-10 06:34:25 | [diff] [blame] | 508 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 509 | #ifndef NO_CURL_EASY_DUPHANDLE |
| 510 | curl_default = get_curl_handle(); |
| 511 | #endif |
| 512 | } |
| 513 | |
| 514 | void http_cleanup(void) |
| 515 | { |
| 516 | struct active_request_slot *slot = active_queue_head; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 517 | |
| 518 | while (slot != NULL) { |
| Shawn O. Pearce | 3278cd0 | 2007-09-15 07:23:00 | [diff] [blame] | 519 | struct active_request_slot *next = slot->next; |
| Mike Hommey | f23d1f7 | 2008-03-03 19:30:16 | [diff] [blame] | 520 | if (slot->curl != NULL) { |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 521 | #ifdef USE_CURL_MULTI |
| Mike Hommey | f23d1f7 | 2008-03-03 19:30:16 | [diff] [blame] | 522 | curl_multi_remove_handle(curlm, slot->curl); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 523 | #endif |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 524 | curl_easy_cleanup(slot->curl); |
| Mike Hommey | f23d1f7 | 2008-03-03 19:30:16 | [diff] [blame] | 525 | } |
| Shawn O. Pearce | 3278cd0 | 2007-09-15 07:23:00 | [diff] [blame] | 526 | free(slot); |
| 527 | slot = next; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 528 | } |
| Shawn O. Pearce | 3278cd0 | 2007-09-15 07:23:00 | [diff] [blame] | 529 | active_queue_head = NULL; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 530 | |
| 531 | #ifndef NO_CURL_EASY_DUPHANDLE |
| 532 | curl_easy_cleanup(curl_default); |
| 533 | #endif |
| 534 | |
| 535 | #ifdef USE_CURL_MULTI |
| 536 | curl_multi_cleanup(curlm); |
| 537 | #endif |
| 538 | curl_global_cleanup(); |
| Nick Hengeveld | b3ca4e4e | 2006-06-06 16:41:32 | [diff] [blame] | 539 | |
| 540 | curl_slist_free_all(pragma_header); |
| Shawn O. Pearce | 3278cd0 | 2007-09-15 07:23:00 | [diff] [blame] | 541 | pragma_header = NULL; |
| Mike Hommey | 9fc6440 | 2008-02-27 20:35:50 | [diff] [blame] | 542 | |
| Tay Ray Chuan | e917674 | 2009-06-06 08:43:41 | [diff] [blame] | 543 | curl_slist_free_all(no_pragma_header); |
| 544 | no_pragma_header = NULL; |
| 545 | |
| Mike Hommey | 9fc6440 | 2008-02-27 20:35:50 | [diff] [blame] | 546 | if (curl_http_proxy) { |
| Miklos Vajna | e4a80ec | 2008-12-07 00:45:37 | [diff] [blame] | 547 | free((void *)curl_http_proxy); |
| Mike Hommey | 9fc6440 | 2008-02-27 20:35:50 | [diff] [blame] | 548 | curl_http_proxy = NULL; |
| 549 | } |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 550 | |
| Jeff King | 148bb6a | 2011-12-10 10:31:21 | [diff] [blame] | 551 | if (cert_auth.password != NULL) { |
| 552 | memset(cert_auth.password, 0, strlen(cert_auth.password)); |
| 553 | free(cert_auth.password); |
| 554 | cert_auth.password = NULL; |
| Mark Lodato | 30dd916 | 2009-05-28 03:16:02 | [diff] [blame] | 555 | } |
| 556 | ssl_cert_password_required = 0; |
| Yi EungJun | f18604b | 2015-01-28 12:04:37 | [diff] [blame] | 557 | |
| 558 | free(cached_accept_language); |
| 559 | cached_accept_language = NULL; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 560 | } |
| 561 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 562 | struct active_request_slot *get_active_slot(void) |
| 563 | { |
| 564 | struct active_request_slot *slot = active_queue_head; |
| 565 | struct active_request_slot *newslot; |
| 566 | |
| 567 | #ifdef USE_CURL_MULTI |
| 568 | int num_transfers; |
| 569 | |
| 570 | /* Wait for a slot to open up if the queue is full */ |
| 571 | while (active_requests >= max_requests) { |
| 572 | curl_multi_perform(curlm, &num_transfers); |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 573 | if (num_transfers < active_requests) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 574 | process_curl_messages(); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 575 | } |
| 576 | #endif |
| 577 | |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 578 | while (slot != NULL && slot->in_use) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 579 | slot = slot->next; |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 580 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 581 | if (slot == NULL) { |
| 582 | newslot = xmalloc(sizeof(*newslot)); |
| 583 | newslot->curl = NULL; |
| 584 | newslot->in_use = 0; |
| 585 | newslot->next = NULL; |
| 586 | |
| 587 | slot = active_queue_head; |
| 588 | if (slot == NULL) { |
| 589 | active_queue_head = newslot; |
| 590 | } else { |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 591 | while (slot->next != NULL) |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 592 | slot = slot->next; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 593 | slot->next = newslot; |
| 594 | } |
| 595 | slot = newslot; |
| 596 | } |
| 597 | |
| 598 | if (slot->curl == NULL) { |
| 599 | #ifdef NO_CURL_EASY_DUPHANDLE |
| 600 | slot->curl = get_curl_handle(); |
| 601 | #else |
| 602 | slot->curl = curl_easy_duphandle(curl_default); |
| 603 | #endif |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 604 | curl_session_count++; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | active_requests++; |
| 608 | slot->in_use = 1; |
| Nick Hengeveld | c8568e1 | 2006-01-31 19:06:55 | [diff] [blame] | 609 | slot->results = NULL; |
| Nick Hengeveld | baa7b67 | 2006-03-11 04:18:01 | [diff] [blame] | 610 | slot->finished = NULL; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 611 | slot->callback_data = NULL; |
| 612 | slot->callback_func = NULL; |
| Duncan Brown | bcfb95d | 2011-06-02 20:31:25 | [diff] [blame] | 613 | curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file); |
| Dave Borowitz | 912b2ac | 2013-07-23 22:40:17 | [diff] [blame] | 614 | if (curl_save_cookies) |
| 615 | curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 616 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 617 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
| Nick Hengeveld | 9094950 | 2006-05-31 23:25:03 | [diff] [blame] | 618 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL); |
| 619 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL); |
| 620 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL); |
| Junio C Hamano | 1e41827 | 2011-04-26 15:04:49 | [diff] [blame] | 621 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL); |
| Nick Hengeveld | 9094950 | 2006-05-31 23:25:03 | [diff] [blame] | 622 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0); |
| 623 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); |
| Jeff King | b793acf | 2013-04-16 00:30:38 | [diff] [blame] | 624 | curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1); |
| brian m. carlson | 4dbe664 | 2015-01-08 00:29:20 | [diff] [blame] | 625 | #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY |
| 626 | curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods); |
| 627 | #endif |
| Jeff King | dfa1725 | 2012-04-10 09:53:40 | [diff] [blame] | 628 | if (http_auth.password) |
| 629 | init_curl_http_auth(slot->curl); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 630 | |
| 631 | return slot; |
| 632 | } |
| 633 | |
| 634 | int start_active_slot(struct active_request_slot *slot) |
| 635 | { |
| 636 | #ifdef USE_CURL_MULTI |
| 637 | CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 638 | int num_transfers; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 639 | |
| 640 | if (curlm_result != CURLM_OK && |
| 641 | curlm_result != CURLM_CALL_MULTI_PERFORM) { |
| 642 | active_requests--; |
| 643 | slot->in_use = 0; |
| 644 | return 0; |
| 645 | } |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 646 | |
| 647 | /* |
| 648 | * We know there must be something to do, since we just added |
| 649 | * something. |
| 650 | */ |
| 651 | curl_multi_perform(curlm, &num_transfers); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 652 | #endif |
| 653 | return 1; |
| 654 | } |
| 655 | |
| 656 | #ifdef USE_CURL_MULTI |
| Daniel Barkalow | fc57b6a | 2007-09-11 03:02:34 | [diff] [blame] | 657 | struct fill_chain { |
| 658 | void *data; |
| 659 | int (*fill)(void *); |
| 660 | struct fill_chain *next; |
| 661 | }; |
| 662 | |
| Junio C Hamano | 4251ccb | 2009-03-10 01:47:29 | [diff] [blame] | 663 | static struct fill_chain *fill_cfg; |
| Daniel Barkalow | fc57b6a | 2007-09-11 03:02:34 | [diff] [blame] | 664 | |
| 665 | void add_fill_function(void *data, int (*fill)(void *)) |
| 666 | { |
| Dotan Barak | e8eec71 | 2008-09-09 18:57:10 | [diff] [blame] | 667 | struct fill_chain *new = xmalloc(sizeof(*new)); |
| Daniel Barkalow | fc57b6a | 2007-09-11 03:02:34 | [diff] [blame] | 668 | struct fill_chain **linkp = &fill_cfg; |
| 669 | new->data = data; |
| 670 | new->fill = fill; |
| 671 | new->next = NULL; |
| 672 | while (*linkp) |
| 673 | linkp = &(*linkp)->next; |
| 674 | *linkp = new; |
| 675 | } |
| 676 | |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 677 | void fill_active_slots(void) |
| 678 | { |
| 679 | struct active_request_slot *slot = active_queue_head; |
| 680 | |
| Daniel Barkalow | fc57b6a | 2007-09-11 03:02:34 | [diff] [blame] | 681 | while (active_requests < max_requests) { |
| 682 | struct fill_chain *fill; |
| 683 | for (fill = fill_cfg; fill; fill = fill->next) |
| 684 | if (fill->fill(fill->data)) |
| 685 | break; |
| 686 | |
| 687 | if (!fill) |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 688 | break; |
| Daniel Barkalow | fc57b6a | 2007-09-11 03:02:34 | [diff] [blame] | 689 | } |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 690 | |
| 691 | while (slot != NULL) { |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 692 | if (!slot->in_use && slot->curl != NULL |
| 693 | && curl_session_count > min_curl_sessions) { |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 694 | curl_easy_cleanup(slot->curl); |
| 695 | slot->curl = NULL; |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 696 | curl_session_count--; |
| Daniel Barkalow | 45c1741 | 2007-09-11 03:02:28 | [diff] [blame] | 697 | } |
| 698 | slot = slot->next; |
| 699 | } |
| 700 | } |
| 701 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 702 | void step_active_slots(void) |
| 703 | { |
| 704 | int num_transfers; |
| 705 | CURLMcode curlm_result; |
| 706 | |
| 707 | do { |
| 708 | curlm_result = curl_multi_perform(curlm, &num_transfers); |
| 709 | } while (curlm_result == CURLM_CALL_MULTI_PERFORM); |
| 710 | if (num_transfers < active_requests) { |
| 711 | process_curl_messages(); |
| 712 | fill_active_slots(); |
| 713 | } |
| 714 | } |
| 715 | #endif |
| 716 | |
| 717 | void run_active_slot(struct active_request_slot *slot) |
| 718 | { |
| 719 | #ifdef USE_CURL_MULTI |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 720 | fd_set readfds; |
| 721 | fd_set writefds; |
| 722 | fd_set excfds; |
| 723 | int max_fd; |
| 724 | struct timeval select_timeout; |
| Nick Hengeveld | baa7b67 | 2006-03-11 04:18:01 | [diff] [blame] | 725 | int finished = 0; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 726 | |
| Nick Hengeveld | baa7b67 | 2006-03-11 04:18:01 | [diff] [blame] | 727 | slot->finished = &finished; |
| 728 | while (!finished) { |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 729 | step_active_slots(); |
| 730 | |
| Mika Fischer | df26c47 | 2011-11-04 14:19:27 | [diff] [blame] | 731 | if (slot->in_use) { |
| Mika Fischer | eb56c82 | 2011-11-04 14:19:26 | [diff] [blame] | 732 | #if LIBCURL_VERSION_NUM >= 0x070f04 |
| 733 | long curl_timeout; |
| 734 | curl_multi_timeout(curlm, &curl_timeout); |
| 735 | if (curl_timeout == 0) { |
| 736 | continue; |
| 737 | } else if (curl_timeout == -1) { |
| 738 | select_timeout.tv_sec = 0; |
| 739 | select_timeout.tv_usec = 50000; |
| 740 | } else { |
| 741 | select_timeout.tv_sec = curl_timeout / 1000; |
| 742 | select_timeout.tv_usec = (curl_timeout % 1000) * 1000; |
| 743 | } |
| 744 | #else |
| 745 | select_timeout.tv_sec = 0; |
| 746 | select_timeout.tv_usec = 50000; |
| 747 | #endif |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 748 | |
| Mika Fischer | 6f9dd67 | 2011-11-04 14:19:25 | [diff] [blame] | 749 | max_fd = -1; |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 750 | FD_ZERO(&readfds); |
| 751 | FD_ZERO(&writefds); |
| 752 | FD_ZERO(&excfds); |
| Mika Fischer | 6f9dd67 | 2011-11-04 14:19:25 | [diff] [blame] | 753 | curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd); |
| Mika Fischer | eb56c82 | 2011-11-04 14:19:26 | [diff] [blame] | 754 | |
| Stefan Zager | 7202b81 | 2012-10-19 21:04:20 | [diff] [blame] | 755 | /* |
| 756 | * It can happen that curl_multi_timeout returns a pathologically |
| 757 | * long timeout when curl_multi_fdset returns no file descriptors |
| 758 | * to read. See commit message for more details. |
| 759 | */ |
| 760 | if (max_fd < 0 && |
| 761 | (select_timeout.tv_sec > 0 || |
| 762 | select_timeout.tv_usec > 50000)) { |
| 763 | select_timeout.tv_sec = 0; |
| 764 | select_timeout.tv_usec = 50000; |
| 765 | } |
| 766 | |
| Mika Fischer | 6f9dd67 | 2011-11-04 14:19:25 | [diff] [blame] | 767 | select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout); |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | #else |
| 771 | while (slot->in_use) { |
| 772 | slot->curl_result = curl_easy_perform(slot->curl); |
| 773 | finish_active_slot(slot); |
| 774 | } |
| 775 | #endif |
| 776 | } |
| 777 | |
| Junio C Hamano | 83e41e2 | 2010-01-12 06:26:08 | [diff] [blame] | 778 | static void release_active_slot(struct active_request_slot *slot) |
| Mark Wooding | 53f3138 | 2006-02-07 10:07:39 | [diff] [blame] | 779 | { |
| 780 | closedown_active_slot(slot); |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 781 | if (slot->curl && curl_session_count > min_curl_sessions) { |
| Nick Hengeveld | b3ca4e4e | 2006-06-06 16:41:32 | [diff] [blame] | 782 | #ifdef USE_CURL_MULTI |
| Mark Wooding | 53f3138 | 2006-02-07 10:07:39 | [diff] [blame] | 783 | curl_multi_remove_handle(curlm, slot->curl); |
| Nick Hengeveld | b3ca4e4e | 2006-06-06 16:41:32 | [diff] [blame] | 784 | #endif |
| Mark Wooding | 53f3138 | 2006-02-07 10:07:39 | [diff] [blame] | 785 | curl_easy_cleanup(slot->curl); |
| 786 | slot->curl = NULL; |
| Tay Ray Chuan | ad75ebe | 2009-11-27 15:42:26 | [diff] [blame] | 787 | curl_session_count--; |
| Mark Wooding | 53f3138 | 2006-02-07 10:07:39 | [diff] [blame] | 788 | } |
| Nick Hengeveld | b3ca4e4e | 2006-06-06 16:41:32 | [diff] [blame] | 789 | #ifdef USE_CURL_MULTI |
| Mark Wooding | 53f3138 | 2006-02-07 10:07:39 | [diff] [blame] | 790 | fill_active_slots(); |
| Nick Hengeveld | b3ca4e4e | 2006-06-06 16:41:32 | [diff] [blame] | 791 | #endif |
| Mark Wooding | 53f3138 | 2006-02-07 10:07:39 | [diff] [blame] | 792 | } |
| 793 | |
| Nick Hengeveld | 29508e1 | 2005-11-18 19:02:58 | [diff] [blame] | 794 | void finish_all_active_slots(void) |
| 795 | { |
| 796 | struct active_request_slot *slot = active_queue_head; |
| 797 | |
| 798 | while (slot != NULL) |
| 799 | if (slot->in_use) { |
| 800 | run_active_slot(slot); |
| 801 | slot = active_queue_head; |
| 802 | } else { |
| 803 | slot = slot->next; |
| 804 | } |
| 805 | } |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 806 | |
| Tay Ray Chuan | 5ace994 | 2009-06-06 08:43:43 | [diff] [blame] | 807 | /* Helpers for modifying and creating URLs */ |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 808 | static inline int needs_quote(int ch) |
| 809 | { |
| 810 | if (((ch >= 'A') && (ch <= 'Z')) |
| 811 | || ((ch >= 'a') && (ch <= 'z')) |
| 812 | || ((ch >= '0') && (ch <= '9')) |
| 813 | || (ch == '/') |
| 814 | || (ch == '-') |
| 815 | || (ch == '.')) |
| 816 | return 0; |
| 817 | return 1; |
| 818 | } |
| 819 | |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 820 | static char *quote_ref_url(const char *base, const char *ref) |
| 821 | { |
| Tay Ray Chuan | 113106e | 2009-03-07 16:47:21 | [diff] [blame] | 822 | struct strbuf buf = STRBUF_INIT; |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 823 | const char *cp; |
| Tay Ray Chuan | 113106e | 2009-03-07 16:47:21 | [diff] [blame] | 824 | int ch; |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 825 | |
| Tay Ray Chuan | 5ace994 | 2009-06-06 08:43:43 | [diff] [blame] | 826 | end_url_with_slash(&buf, base); |
| Tay Ray Chuan | 113106e | 2009-03-07 16:47:21 | [diff] [blame] | 827 | |
| 828 | for (cp = ref; (ch = *cp) != 0; cp++) |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 829 | if (needs_quote(ch)) |
| Tay Ray Chuan | 113106e | 2009-03-07 16:47:21 | [diff] [blame] | 830 | strbuf_addf(&buf, "%%%02x", ch); |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 831 | else |
| Tay Ray Chuan | 113106e | 2009-03-07 16:47:21 | [diff] [blame] | 832 | strbuf_addch(&buf, *cp); |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 833 | |
| Tay Ray Chuan | 113106e | 2009-03-07 16:47:21 | [diff] [blame] | 834 | return strbuf_detach(&buf, NULL); |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 835 | } |
| 836 | |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 837 | void append_remote_object_url(struct strbuf *buf, const char *url, |
| 838 | const char *hex, |
| 839 | int only_two_digit_prefix) |
| 840 | { |
| Tay Ray Chuan | 800324c | 2009-08-17 09:09:43 | [diff] [blame] | 841 | end_url_with_slash(buf, url); |
| 842 | |
| 843 | strbuf_addf(buf, "objects/%.*s/", 2, hex); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 844 | if (!only_two_digit_prefix) |
| 845 | strbuf_addf(buf, "%s", hex+2); |
| 846 | } |
| 847 | |
| 848 | char *get_remote_object_url(const char *url, const char *hex, |
| 849 | int only_two_digit_prefix) |
| 850 | { |
| 851 | struct strbuf buf = STRBUF_INIT; |
| 852 | append_remote_object_url(&buf, url, hex, only_two_digit_prefix); |
| 853 | return strbuf_detach(&buf, NULL); |
| 854 | } |
| 855 | |
| Junio C Hamano | b90a3d7 | 2015-01-14 23:40:46 | [diff] [blame] | 856 | static int handle_curl_result(struct slot_results *results) |
| Jeff King | 8809703 | 2012-08-27 13:26:04 | [diff] [blame] | 857 | { |
| Jeff King | 6d052d7 | 2013-04-05 22:14:06 | [diff] [blame] | 858 | /* |
| 859 | * If we see a failing http code with CURLE_OK, we have turned off |
| 860 | * FAILONERROR (to keep the server's custom error response), and should |
| 861 | * translate the code into failure here. |
| 862 | */ |
| 863 | if (results->curl_result == CURLE_OK && |
| 864 | results->http_code >= 400) { |
| 865 | results->curl_result = CURLE_HTTP_RETURNED_ERROR; |
| 866 | /* |
| 867 | * Normally curl will already have put the "reason phrase" |
| 868 | * from the server into curl_errorstr; unfortunately without |
| 869 | * FAILONERROR it is lost, so we can give only the numeric |
| 870 | * status code. |
| 871 | */ |
| 872 | snprintf(curl_errorstr, sizeof(curl_errorstr), |
| 873 | "The requested URL returned error: %ld", |
| 874 | results->http_code); |
| 875 | } |
| 876 | |
| Jeff King | 8809703 | 2012-08-27 13:26:04 | [diff] [blame] | 877 | if (results->curl_result == CURLE_OK) { |
| 878 | credential_approve(&http_auth); |
| 879 | return HTTP_OK; |
| 880 | } else if (missing_target(results)) |
| 881 | return HTTP_MISSING_TARGET; |
| 882 | else if (results->http_code == 401) { |
| 883 | if (http_auth.username && http_auth.password) { |
| 884 | credential_reject(&http_auth); |
| 885 | return HTTP_NOAUTH; |
| 886 | } else { |
| brian m. carlson | 4dbe664 | 2015-01-08 00:29:20 | [diff] [blame] | 887 | #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY |
| 888 | http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE; |
| 889 | #endif |
| Jeff King | 8809703 | 2012-08-27 13:26:04 | [diff] [blame] | 890 | return HTTP_REAUTH; |
| 891 | } |
| 892 | } else { |
| Junio C Hamano | 3503e9a | 2012-09-12 21:08:05 | [diff] [blame] | 893 | #if LIBCURL_VERSION_NUM >= 0x070c00 |
| Jeff King | 8809703 | 2012-08-27 13:26:04 | [diff] [blame] | 894 | if (!curl_errorstr[0]) |
| 895 | strlcpy(curl_errorstr, |
| 896 | curl_easy_strerror(results->curl_result), |
| 897 | sizeof(curl_errorstr)); |
| Junio C Hamano | 3503e9a | 2012-09-12 21:08:05 | [diff] [blame] | 898 | #endif |
| Jeff King | 8809703 | 2012-08-27 13:26:04 | [diff] [blame] | 899 | return HTTP_ERROR; |
| 900 | } |
| 901 | } |
| 902 | |
| Jeff King | beed336 | 2014-02-18 10:34:20 | [diff] [blame] | 903 | int run_one_slot(struct active_request_slot *slot, |
| 904 | struct slot_results *results) |
| 905 | { |
| 906 | slot->results = results; |
| 907 | if (!start_active_slot(slot)) { |
| 908 | snprintf(curl_errorstr, sizeof(curl_errorstr), |
| 909 | "failed to start HTTP request"); |
| 910 | return HTTP_START_FAILED; |
| 911 | } |
| 912 | |
| 913 | run_active_slot(slot); |
| 914 | return handle_curl_result(results); |
| 915 | } |
| 916 | |
| Jeff King | 132b70a | 2013-09-28 08:31:11 | [diff] [blame] | 917 | static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf) |
| 918 | { |
| 919 | char *ptr; |
| 920 | CURLcode ret; |
| 921 | |
| 922 | strbuf_reset(buf); |
| 923 | ret = curl_easy_getinfo(curl, info, &ptr); |
| 924 | if (!ret && ptr) |
| 925 | strbuf_addstr(buf, ptr); |
| 926 | return ret; |
| 927 | } |
| 928 | |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 929 | /* |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 930 | * Check for and extract a content-type parameter. "raw" |
| 931 | * should be positioned at the start of the potential |
| 932 | * parameter, with any whitespace already removed. |
| 933 | * |
| 934 | * "name" is the name of the parameter. The value is appended |
| 935 | * to "out". |
| 936 | */ |
| 937 | static int extract_param(const char *raw, const char *name, |
| 938 | struct strbuf *out) |
| 939 | { |
| 940 | size_t len = strlen(name); |
| 941 | |
| 942 | if (strncasecmp(raw, name, len)) |
| 943 | return -1; |
| 944 | raw += len; |
| 945 | |
| 946 | if (*raw != '=') |
| 947 | return -1; |
| 948 | raw++; |
| 949 | |
| Yi EungJun | f34a655 | 2014-06-17 22:11:53 | [diff] [blame] | 950 | while (*raw && !isspace(*raw) && *raw != ';') |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 951 | strbuf_addch(out, *raw++); |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | /* |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 956 | * Extract a normalized version of the content type, with any |
| 957 | * spaces suppressed, all letters lowercased, and no trailing ";" |
| 958 | * or parameters. |
| 959 | * |
| 960 | * Note that we will silently remove even invalid whitespace. For |
| 961 | * example, "text / plain" is specifically forbidden by RFC 2616, |
| 962 | * but "text/plain" is the only reasonable output, and this keeps |
| 963 | * our code simple. |
| 964 | * |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 965 | * If the "charset" argument is not NULL, store the value of any |
| 966 | * charset parameter there. |
| 967 | * |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 968 | * Example: |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 969 | * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8" |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 970 | * "text / plain" -> "text/plain" |
| 971 | */ |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 972 | static void extract_content_type(struct strbuf *raw, struct strbuf *type, |
| 973 | struct strbuf *charset) |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 974 | { |
| 975 | const char *p; |
| 976 | |
| 977 | strbuf_reset(type); |
| 978 | strbuf_grow(type, raw->len); |
| 979 | for (p = raw->buf; *p; p++) { |
| 980 | if (isspace(*p)) |
| 981 | continue; |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 982 | if (*p == ';') { |
| 983 | p++; |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 984 | break; |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 985 | } |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 986 | strbuf_addch(type, tolower(*p)); |
| 987 | } |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 988 | |
| 989 | if (!charset) |
| 990 | return; |
| 991 | |
| 992 | strbuf_reset(charset); |
| 993 | while (*p) { |
| Yi EungJun | f34a655 | 2014-06-17 22:11:53 | [diff] [blame] | 994 | while (isspace(*p) || *p == ';') |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 995 | p++; |
| 996 | if (!extract_param(p, "charset", charset)) |
| 997 | return; |
| 998 | while (*p && !isspace(*p)) |
| 999 | p++; |
| 1000 | } |
| Jeff King | c553fd1 | 2014-05-22 09:36:12 | [diff] [blame] | 1001 | |
| 1002 | if (!charset->len && starts_with(type->buf, "text/")) |
| 1003 | strbuf_addstr(charset, "ISO-8859-1"); |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 1004 | } |
| 1005 | |
| Yi EungJun | f18604b | 2015-01-28 12:04:37 | [diff] [blame] | 1006 | static void write_accept_language(struct strbuf *buf) |
| 1007 | { |
| 1008 | /* |
| 1009 | * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than |
| 1010 | * that, q-value will be smaller than 0.001, the minimum q-value the |
| 1011 | * HTTP specification allows. See |
| 1012 | * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value. |
| 1013 | */ |
| 1014 | const int MAX_DECIMAL_PLACES = 3; |
| 1015 | const int MAX_LANGUAGE_TAGS = 1000; |
| 1016 | const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000; |
| 1017 | char **language_tags = NULL; |
| 1018 | int num_langs = 0; |
| 1019 | const char *s = get_preferred_languages(); |
| 1020 | int i; |
| 1021 | struct strbuf tag = STRBUF_INIT; |
| 1022 | |
| 1023 | /* Don't add Accept-Language header if no language is preferred. */ |
| 1024 | if (!s) |
| 1025 | return; |
| 1026 | |
| 1027 | /* |
| 1028 | * Split the colon-separated string of preferred languages into |
| 1029 | * language_tags array. |
| 1030 | */ |
| 1031 | do { |
| 1032 | /* collect language tag */ |
| 1033 | for (; *s && (isalnum(*s) || *s == '_'); s++) |
| 1034 | strbuf_addch(&tag, *s == '_' ? '-' : *s); |
| 1035 | |
| 1036 | /* skip .codeset, @modifier and any other unnecessary parts */ |
| 1037 | while (*s && *s != ':') |
| 1038 | s++; |
| 1039 | |
| 1040 | if (tag.len) { |
| 1041 | num_langs++; |
| 1042 | REALLOC_ARRAY(language_tags, num_langs); |
| 1043 | language_tags[num_langs - 1] = strbuf_detach(&tag, NULL); |
| 1044 | if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */ |
| 1045 | break; |
| 1046 | } |
| 1047 | } while (*s++); |
| 1048 | |
| 1049 | /* write Accept-Language header into buf */ |
| 1050 | if (num_langs) { |
| 1051 | int last_buf_len = 0; |
| 1052 | int max_q; |
| 1053 | int decimal_places; |
| 1054 | char q_format[32]; |
| 1055 | |
| 1056 | /* add '*' */ |
| 1057 | REALLOC_ARRAY(language_tags, num_langs + 1); |
| 1058 | language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */ |
| 1059 | |
| 1060 | /* compute decimal_places */ |
| 1061 | for (max_q = 1, decimal_places = 0; |
| 1062 | max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES; |
| 1063 | decimal_places++, max_q *= 10) |
| 1064 | ; |
| 1065 | |
| 1066 | sprintf(q_format, ";q=0.%%0%dd", decimal_places); |
| 1067 | |
| 1068 | strbuf_addstr(buf, "Accept-Language: "); |
| 1069 | |
| 1070 | for (i = 0; i < num_langs; i++) { |
| 1071 | if (i > 0) |
| 1072 | strbuf_addstr(buf, ", "); |
| 1073 | |
| 1074 | strbuf_addstr(buf, language_tags[i]); |
| 1075 | |
| 1076 | if (i > 0) |
| 1077 | strbuf_addf(buf, q_format, max_q - i); |
| 1078 | |
| 1079 | if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) { |
| 1080 | strbuf_remove(buf, last_buf_len, buf->len - last_buf_len); |
| 1081 | break; |
| 1082 | } |
| 1083 | |
| 1084 | last_buf_len = buf->len; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /* free language tags -- last one is a static '*' */ |
| 1089 | for (i = 0; i < num_langs - 1; i++) |
| 1090 | free(language_tags[i]); |
| 1091 | free(language_tags); |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Get an Accept-Language header which indicates user's preferred languages. |
| 1096 | * |
| 1097 | * Examples: |
| 1098 | * LANGUAGE= -> "" |
| 1099 | * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1" |
| 1100 | * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1" |
| 1101 | * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1" |
| 1102 | * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1" |
| 1103 | * LANGUAGE= LANG=C -> "" |
| 1104 | */ |
| 1105 | static const char *get_accept_language(void) |
| 1106 | { |
| 1107 | if (!cached_accept_language) { |
| 1108 | struct strbuf buf = STRBUF_INIT; |
| 1109 | write_accept_language(&buf); |
| 1110 | if (buf.len > 0) |
| 1111 | cached_accept_language = strbuf_detach(&buf, NULL); |
| 1112 | } |
| 1113 | |
| 1114 | return cached_accept_language; |
| 1115 | } |
| 1116 | |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1117 | /* http_request() targets */ |
| 1118 | #define HTTP_REQUEST_STRBUF 0 |
| 1119 | #define HTTP_REQUEST_FILE 1 |
| 1120 | |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1121 | static int http_request(const char *url, |
| 1122 | void *result, int target, |
| 1123 | const struct http_get_options *options) |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1124 | { |
| 1125 | struct active_request_slot *slot; |
| 1126 | struct slot_results results; |
| 1127 | struct curl_slist *headers = NULL; |
| 1128 | struct strbuf buf = STRBUF_INIT; |
| Yi EungJun | f18604b | 2015-01-28 12:04:37 | [diff] [blame] | 1129 | const char *accept_language; |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1130 | int ret; |
| 1131 | |
| 1132 | slot = get_active_slot(); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1133 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); |
| 1134 | |
| 1135 | if (result == NULL) { |
| 1136 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1); |
| 1137 | } else { |
| 1138 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
| 1139 | curl_easy_setopt(slot->curl, CURLOPT_FILE, result); |
| 1140 | |
| 1141 | if (target == HTTP_REQUEST_FILE) { |
| 1142 | long posn = ftell(result); |
| 1143 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, |
| 1144 | fwrite); |
| 1145 | if (posn > 0) { |
| 1146 | strbuf_addf(&buf, "Range: bytes=%ld-", posn); |
| 1147 | headers = curl_slist_append(headers, buf.buf); |
| 1148 | strbuf_reset(&buf); |
| 1149 | } |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1150 | } else |
| 1151 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, |
| 1152 | fwrite_buffer); |
| 1153 | } |
| 1154 | |
| Yi EungJun | f18604b | 2015-01-28 12:04:37 | [diff] [blame] | 1155 | accept_language = get_accept_language(); |
| 1156 | |
| 1157 | if (accept_language) |
| 1158 | headers = curl_slist_append(headers, accept_language); |
| 1159 | |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1160 | strbuf_addstr(&buf, "Pragma:"); |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1161 | if (options && options->no_cache) |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1162 | strbuf_addstr(&buf, " no-cache"); |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1163 | if (options && options->keep_error) |
| Jeff King | 6d052d7 | 2013-04-05 22:14:06 | [diff] [blame] | 1164 | curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1165 | |
| 1166 | headers = curl_slist_append(headers, buf.buf); |
| 1167 | |
| 1168 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
| 1169 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); |
| Shawn O. Pearce | aa90b96 | 2012-09-19 23:12:02 | [diff] [blame] | 1170 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip"); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1171 | |
| Jeff King | beed336 | 2014-02-18 10:34:20 | [diff] [blame] | 1172 | ret = run_one_slot(slot, &results); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1173 | |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 1174 | if (options && options->content_type) { |
| 1175 | struct strbuf raw = STRBUF_INIT; |
| 1176 | curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw); |
| Jeff King | e313162 | 2014-05-22 09:30:05 | [diff] [blame] | 1177 | extract_content_type(&raw, options->content_type, |
| 1178 | options->charset); |
| Jeff King | bf197fd | 2014-05-22 09:29:47 | [diff] [blame] | 1179 | strbuf_release(&raw); |
| 1180 | } |
| Shawn Pearce | 4656bf4 | 2013-01-31 21:02:07 | [diff] [blame] | 1181 | |
| Jeff King | 7886896 | 2013-09-28 08:32:02 | [diff] [blame] | 1182 | if (options && options->effective_url) |
| 1183 | curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL, |
| 1184 | options->effective_url); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1185 | |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1186 | curl_slist_free_all(headers); |
| 1187 | strbuf_release(&buf); |
| 1188 | |
| 1189 | return ret; |
| 1190 | } |
| 1191 | |
| Jeff King | c93c92f | 2013-09-28 08:34:05 | [diff] [blame] | 1192 | /* |
| 1193 | * Update the "base" url to a more appropriate value, as deduced by |
| 1194 | * redirects seen when requesting a URL starting with "url". |
| 1195 | * |
| 1196 | * The "asked" parameter is a URL that we asked curl to access, and must begin |
| 1197 | * with "base". |
| 1198 | * |
| 1199 | * The "got" parameter is the URL that curl reported to us as where we ended |
| 1200 | * up. |
| 1201 | * |
| 1202 | * Returns 1 if we updated the base url, 0 otherwise. |
| 1203 | * |
| 1204 | * Our basic strategy is to compare "base" and "asked" to find the bits |
| 1205 | * specific to our request. We then strip those bits off of "got" to yield the |
| 1206 | * new base. So for example, if our base is "http://example.com/foo.git", |
| 1207 | * and we ask for "http://example.com/foo.git/info/refs", we might end up |
| 1208 | * with "https://other.example.com/foo.git/info/refs". We would want the |
| 1209 | * new URL to become "https://other.example.com/foo.git". |
| 1210 | * |
| 1211 | * Note that this assumes a sane redirect scheme. It's entirely possible |
| 1212 | * in the example above to end up at a URL that does not even end in |
| 1213 | * "info/refs". In such a case we simply punt, as there is not much we can |
| 1214 | * do (and such a scheme is unlikely to represent a real git repository, |
| 1215 | * which means we are likely about to abort anyway). |
| 1216 | */ |
| 1217 | static int update_url_from_redirect(struct strbuf *base, |
| 1218 | const char *asked, |
| 1219 | const struct strbuf *got) |
| Jeff King | 8d677ed | 2011-07-18 07:50:14 | [diff] [blame] | 1220 | { |
| Jeff King | c93c92f | 2013-09-28 08:34:05 | [diff] [blame] | 1221 | const char *tail; |
| 1222 | size_t tail_len; |
| 1223 | |
| 1224 | if (!strcmp(asked, got->buf)) |
| 1225 | return 0; |
| 1226 | |
| Jeff King | de8118e | 2014-06-18 19:57:17 | [diff] [blame] | 1227 | if (!skip_prefix(asked, base->buf, &tail)) |
| Jeff King | c93c92f | 2013-09-28 08:34:05 | [diff] [blame] | 1228 | die("BUG: update_url_from_redirect: %s is not a superset of %s", |
| 1229 | asked, base->buf); |
| 1230 | |
| Jeff King | c93c92f | 2013-09-28 08:34:05 | [diff] [blame] | 1231 | tail_len = strlen(tail); |
| 1232 | |
| 1233 | if (got->len < tail_len || |
| 1234 | strcmp(tail, got->buf + got->len - tail_len)) |
| 1235 | return 0; /* insane redirect scheme */ |
| 1236 | |
| 1237 | strbuf_reset(base); |
| 1238 | strbuf_add(base, got->buf, got->len - tail_len); |
| 1239 | return 1; |
| 1240 | } |
| 1241 | |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1242 | static int http_request_reauth(const char *url, |
| Jeff King | 8d677ed | 2011-07-18 07:50:14 | [diff] [blame] | 1243 | void *result, int target, |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1244 | struct http_get_options *options) |
| Jeff King | 8d677ed | 2011-07-18 07:50:14 | [diff] [blame] | 1245 | { |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1246 | int ret = http_request(url, result, target, options); |
| Jeff King | c93c92f | 2013-09-28 08:34:05 | [diff] [blame] | 1247 | |
| 1248 | if (options && options->effective_url && options->base_url) { |
| 1249 | if (update_url_from_redirect(options->base_url, |
| 1250 | url, options->effective_url)) { |
| 1251 | credential_from_url(&http_auth, options->base_url->buf); |
| 1252 | url = options->effective_url->buf; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| Jeff King | 8d677ed | 2011-07-18 07:50:14 | [diff] [blame] | 1256 | if (ret != HTTP_REAUTH) |
| 1257 | return ret; |
| Jeff King | 6d052d7 | 2013-04-05 22:14:06 | [diff] [blame] | 1258 | |
| 1259 | /* |
| 1260 | * If we are using KEEP_ERROR, the previous request may have |
| 1261 | * put cruft into our output stream; we should clear it out before |
| 1262 | * making our next request. We only know how to do this for |
| 1263 | * the strbuf case, but that is enough to satisfy current callers. |
| 1264 | */ |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1265 | if (options && options->keep_error) { |
| Jeff King | 6d052d7 | 2013-04-05 22:14:06 | [diff] [blame] | 1266 | switch (target) { |
| 1267 | case HTTP_REQUEST_STRBUF: |
| 1268 | strbuf_reset(result); |
| 1269 | break; |
| 1270 | default: |
| 1271 | die("BUG: HTTP_KEEP_ERROR is only supported with strbufs"); |
| 1272 | } |
| 1273 | } |
| Jeff King | 2501aff | 2013-09-28 08:31:45 | [diff] [blame] | 1274 | |
| 1275 | credential_fill(&http_auth); |
| 1276 | |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1277 | return http_request(url, result, target, options); |
| Jeff King | 8d677ed | 2011-07-18 07:50:14 | [diff] [blame] | 1278 | } |
| 1279 | |
| Shawn Pearce | 4656bf4 | 2013-01-31 21:02:07 | [diff] [blame] | 1280 | int http_get_strbuf(const char *url, |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1281 | struct strbuf *result, |
| 1282 | struct http_get_options *options) |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1283 | { |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1284 | return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1285 | } |
| 1286 | |
| Junio C Hamano | 83e41e2 | 2010-01-12 06:26:08 | [diff] [blame] | 1287 | /* |
| Jim Meyering | a7793a7 | 2012-03-28 08:41:54 | [diff] [blame] | 1288 | * Downloads a URL and stores the result in the given file. |
| Junio C Hamano | 83e41e2 | 2010-01-12 06:26:08 | [diff] [blame] | 1289 | * |
| 1290 | * If a previous interrupted download is detected (i.e. a previous temporary |
| 1291 | * file is still around) the download is resumed. |
| 1292 | */ |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1293 | static int http_get_file(const char *url, const char *filename, |
| 1294 | struct http_get_options *options) |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1295 | { |
| 1296 | int ret; |
| 1297 | struct strbuf tmpfile = STRBUF_INIT; |
| 1298 | FILE *result; |
| 1299 | |
| 1300 | strbuf_addf(&tmpfile, "%s.temp", filename); |
| 1301 | result = fopen(tmpfile.buf, "a"); |
| Jeff King | 3d1fb76 | 2013-09-28 08:31:00 | [diff] [blame] | 1302 | if (!result) { |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1303 | error("Unable to open local file %s", tmpfile.buf); |
| 1304 | ret = HTTP_ERROR; |
| 1305 | goto cleanup; |
| 1306 | } |
| 1307 | |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1308 | ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options); |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1309 | fclose(result); |
| 1310 | |
| Jeff King | 3d1fb76 | 2013-09-28 08:31:00 | [diff] [blame] | 1311 | if (ret == HTTP_OK && move_temp_to_file(tmpfile.buf, filename)) |
| Mike Hommey | e929cd2 | 2009-06-06 08:43:53 | [diff] [blame] | 1312 | ret = HTTP_ERROR; |
| 1313 | cleanup: |
| 1314 | strbuf_release(&tmpfile); |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
| Daniel Barkalow | c13b263 | 2008-04-26 19:53:09 | [diff] [blame] | 1318 | int http_fetch_ref(const char *base, struct ref *ref) |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 1319 | { |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1320 | struct http_get_options options = {0}; |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 1321 | char *url; |
| 1322 | struct strbuf buffer = STRBUF_INIT; |
| Mike Hommey | 0d5896e | 2009-06-06 08:43:55 | [diff] [blame] | 1323 | int ret = -1; |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 1324 | |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1325 | options.no_cache = 1; |
| 1326 | |
| Daniel Barkalow | c13b263 | 2008-04-26 19:53:09 | [diff] [blame] | 1327 | url = quote_ref_url(base, ref->name); |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1328 | if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) { |
| Mike Hommey | 0d5896e | 2009-06-06 08:43:55 | [diff] [blame] | 1329 | strbuf_rtrim(&buffer); |
| 1330 | if (buffer.len == 40) |
| 1331 | ret = get_sha1_hex(buffer.buf, ref->old_sha1); |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1332 | else if (starts_with(buffer.buf, "ref: ")) { |
| Mike Hommey | 0d5896e | 2009-06-06 08:43:55 | [diff] [blame] | 1333 | ref->symref = xstrdup(buffer.buf + 5); |
| 1334 | ret = 0; |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 1335 | } |
| Mike Hommey | d7e9280 | 2007-12-10 23:08:25 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | strbuf_release(&buffer); |
| 1339 | free(url); |
| 1340 | return ret; |
| 1341 | } |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1342 | |
| 1343 | /* Helpers for fetching packs */ |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1344 | static char *fetch_pack_index(unsigned char *sha1, const char *base_url) |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1345 | { |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1346 | char *url, *tmp; |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1347 | struct strbuf buf = STRBUF_INIT; |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1348 | |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1349 | if (http_is_verbose) |
| Shawn O. Pearce | 162eb5f | 2010-04-19 14:23:05 | [diff] [blame] | 1350 | fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1)); |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1351 | |
| 1352 | end_url_with_slash(&buf, base_url); |
| Shawn O. Pearce | 162eb5f | 2010-04-19 14:23:05 | [diff] [blame] | 1353 | strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1)); |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1354 | url = strbuf_detach(&buf, NULL); |
| 1355 | |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1356 | strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1)); |
| 1357 | tmp = strbuf_detach(&buf, NULL); |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1358 | |
| Ramsay Jones | 70900ed | 2013-10-24 20:17:19 | [diff] [blame] | 1359 | if (http_get_file(url, tmp, NULL) != HTTP_OK) { |
| Pete Wyckoff | 82247e9 | 2012-04-30 00:28:45 | [diff] [blame] | 1360 | error("Unable to get pack index %s", url); |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1361 | free(tmp); |
| 1362 | tmp = NULL; |
| 1363 | } |
| 1364 | |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1365 | free(url); |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1366 | return tmp; |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static int fetch_and_setup_pack_index(struct packed_git **packs_head, |
| 1370 | unsigned char *sha1, const char *base_url) |
| 1371 | { |
| 1372 | struct packed_git *new_pack; |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1373 | char *tmp_idx = NULL; |
| 1374 | int ret; |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1375 | |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1376 | if (has_pack_index(sha1)) { |
| Jeff King | 8b9c2dd | 2015-01-27 20:02:27 | [diff] [blame] | 1377 | new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1)); |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1378 | if (!new_pack) |
| 1379 | return -1; /* parse_pack_index() already issued error message */ |
| 1380 | goto add_pack; |
| 1381 | } |
| 1382 | |
| 1383 | tmp_idx = fetch_pack_index(sha1, base_url); |
| 1384 | if (!tmp_idx) |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1385 | return -1; |
| 1386 | |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1387 | new_pack = parse_pack_index(sha1, tmp_idx); |
| 1388 | if (!new_pack) { |
| 1389 | unlink(tmp_idx); |
| 1390 | free(tmp_idx); |
| 1391 | |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1392 | return -1; /* parse_pack_index() already issued error message */ |
| Shawn O. Pearce | 750ef42 | 2010-04-19 14:23:10 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | ret = verify_pack_index(new_pack); |
| 1396 | if (!ret) { |
| 1397 | close_pack_index(new_pack); |
| 1398 | ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1)); |
| 1399 | } |
| 1400 | free(tmp_idx); |
| 1401 | if (ret) |
| 1402 | return -1; |
| 1403 | |
| 1404 | add_pack: |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1405 | new_pack->next = *packs_head; |
| 1406 | *packs_head = new_pack; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | int http_get_info_packs(const char *base_url, struct packed_git **packs_head) |
| 1411 | { |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1412 | struct http_get_options options = {0}; |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1413 | int ret = 0, i = 0; |
| 1414 | char *url, *data; |
| 1415 | struct strbuf buf = STRBUF_INIT; |
| 1416 | unsigned char sha1[20]; |
| 1417 | |
| 1418 | end_url_with_slash(&buf, base_url); |
| 1419 | strbuf_addstr(&buf, "objects/info/packs"); |
| 1420 | url = strbuf_detach(&buf, NULL); |
| 1421 | |
| Jeff King | 1bbcc22 | 2013-09-28 08:31:23 | [diff] [blame] | 1422 | options.no_cache = 1; |
| 1423 | ret = http_get_strbuf(url, &buf, &options); |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1424 | if (ret != HTTP_OK) |
| 1425 | goto cleanup; |
| 1426 | |
| 1427 | data = buf.buf; |
| 1428 | while (i < buf.len) { |
| 1429 | switch (data[i]) { |
| 1430 | case 'P': |
| 1431 | i++; |
| 1432 | if (i + 52 <= buf.len && |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1433 | starts_with(data + i, " pack-") && |
| 1434 | starts_with(data + i + 46, ".pack\n")) { |
| Tay Ray Chuan | b8caac2 | 2009-06-06 08:43:59 | [diff] [blame] | 1435 | get_sha1_hex(data + i + 6, sha1); |
| 1436 | fetch_and_setup_pack_index(packs_head, sha1, |
| 1437 | base_url); |
| 1438 | i += 51; |
| 1439 | break; |
| 1440 | } |
| 1441 | default: |
| 1442 | while (i < buf.len && data[i] != '\n') |
| 1443 | i++; |
| 1444 | } |
| 1445 | i++; |
| 1446 | } |
| 1447 | |
| 1448 | cleanup: |
| 1449 | free(url); |
| 1450 | return ret; |
| 1451 | } |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1452 | |
| 1453 | void release_http_pack_request(struct http_pack_request *preq) |
| 1454 | { |
| 1455 | if (preq->packfile != NULL) { |
| 1456 | fclose(preq->packfile); |
| 1457 | preq->packfile = NULL; |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1458 | } |
| 1459 | if (preq->range_header != NULL) { |
| 1460 | curl_slist_free_all(preq->range_header); |
| 1461 | preq->range_header = NULL; |
| 1462 | } |
| 1463 | preq->slot = NULL; |
| 1464 | free(preq->url); |
| Stefan Beller | 826aed5 | 2015-03-21 00:28:06 | [diff] [blame] | 1465 | free(preq); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | int finish_http_pack_request(struct http_pack_request *preq) |
| 1469 | { |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1470 | struct packed_git **lst; |
| Shawn O. Pearce | 021ab6f | 2010-04-17 20:07:36 | [diff] [blame] | 1471 | struct packed_git *p = preq->target; |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 1472 | char *tmp_idx; |
| René Scharfe | d318027 | 2014-08-19 19:09:35 | [diff] [blame] | 1473 | struct child_process ip = CHILD_PROCESS_INIT; |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 1474 | const char *ip_argv[8]; |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1475 | |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 1476 | close_pack_index(p); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1477 | |
| Shawn O. Pearce | 3065274 | 2010-04-17 20:07:37 | [diff] [blame] | 1478 | fclose(preq->packfile); |
| 1479 | preq->packfile = NULL; |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1480 | |
| 1481 | lst = preq->lst; |
| Shawn O. Pearce | 021ab6f | 2010-04-17 20:07:36 | [diff] [blame] | 1482 | while (*lst != p) |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1483 | lst = &((*lst)->next); |
| 1484 | *lst = (*lst)->next; |
| 1485 | |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 1486 | tmp_idx = xstrdup(preq->tmpfile); |
| 1487 | strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"), |
| 1488 | ".idx.temp"); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1489 | |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 1490 | ip_argv[0] = "index-pack"; |
| 1491 | ip_argv[1] = "-o"; |
| 1492 | ip_argv[2] = tmp_idx; |
| 1493 | ip_argv[3] = preq->tmpfile; |
| 1494 | ip_argv[4] = NULL; |
| 1495 | |
| Shawn O. Pearce | fe72d42 | 2010-04-19 14:23:09 | [diff] [blame] | 1496 | ip.argv = ip_argv; |
| 1497 | ip.git_cmd = 1; |
| 1498 | ip.no_stdin = 1; |
| 1499 | ip.no_stdout = 1; |
| 1500 | |
| 1501 | if (run_command(&ip)) { |
| 1502 | unlink(preq->tmpfile); |
| 1503 | unlink(tmp_idx); |
| 1504 | free(tmp_idx); |
| 1505 | return -1; |
| 1506 | } |
| 1507 | |
| 1508 | unlink(sha1_pack_index_name(p->sha1)); |
| 1509 | |
| 1510 | if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1)) |
| 1511 | || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) { |
| 1512 | free(tmp_idx); |
| 1513 | return -1; |
| 1514 | } |
| 1515 | |
| 1516 | install_packed_git(p); |
| 1517 | free(tmp_idx); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | struct http_pack_request *new_http_pack_request( |
| 1522 | struct packed_git *target, const char *base_url) |
| 1523 | { |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1524 | long prev_posn = 0; |
| 1525 | char range[RANGE_HEADER_SIZE]; |
| 1526 | struct strbuf buf = STRBUF_INIT; |
| 1527 | struct http_pack_request *preq; |
| 1528 | |
| Tay Ray Chuan | ec99c9a | 2011-08-03 11:54:03 | [diff] [blame] | 1529 | preq = xcalloc(1, sizeof(*preq)); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1530 | preq->target = target; |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1531 | |
| 1532 | end_url_with_slash(&buf, base_url); |
| 1533 | strbuf_addf(&buf, "objects/pack/pack-%s.pack", |
| 1534 | sha1_to_hex(target->sha1)); |
| Tay Ray Chuan | bb99190 | 2009-08-10 15:59:55 | [diff] [blame] | 1535 | preq->url = strbuf_detach(&buf, NULL); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1536 | |
| Tay Ray Chuan | 90d0571 | 2010-04-19 14:46:43 | [diff] [blame] | 1537 | snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp", |
| 1538 | sha1_pack_name(target->sha1)); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1539 | preq->packfile = fopen(preq->tmpfile, "a"); |
| 1540 | if (!preq->packfile) { |
| 1541 | error("Unable to open local file %s for pack", |
| 1542 | preq->tmpfile); |
| 1543 | goto abort; |
| 1544 | } |
| 1545 | |
| 1546 | preq->slot = get_active_slot(); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1547 | curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile); |
| 1548 | curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite); |
| Tay Ray Chuan | bb99190 | 2009-08-10 15:59:55 | [diff] [blame] | 1549 | curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1550 | curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, |
| 1551 | no_pragma_header); |
| 1552 | |
| 1553 | /* |
| 1554 | * If there is data present from a previous transfer attempt, |
| 1555 | * resume where it left off |
| 1556 | */ |
| 1557 | prev_posn = ftell(preq->packfile); |
| 1558 | if (prev_posn>0) { |
| 1559 | if (http_is_verbose) |
| 1560 | fprintf(stderr, |
| 1561 | "Resuming fetch of pack %s at byte %ld\n", |
| 1562 | sha1_to_hex(target->sha1), prev_posn); |
| 1563 | sprintf(range, "Range: bytes=%ld-", prev_posn); |
| 1564 | preq->range_header = curl_slist_append(NULL, range); |
| 1565 | curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, |
| 1566 | preq->range_header); |
| 1567 | } |
| 1568 | |
| 1569 | return preq; |
| 1570 | |
| 1571 | abort: |
| Tay Ray Chuan | bb99190 | 2009-08-10 15:59:55 | [diff] [blame] | 1572 | free(preq->url); |
| Tay Ray Chuan | 5ae9ebf | 2009-08-10 15:55:48 | [diff] [blame] | 1573 | free(preq); |
| Tay Ray Chuan | 2264dfa | 2009-06-06 08:44:01 | [diff] [blame] | 1574 | return NULL; |
| 1575 | } |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1576 | |
| 1577 | /* Helpers for fetching objects (loose) */ |
| Dan McGee | a04ff3e | 2011-05-03 15:47:27 | [diff] [blame] | 1578 | static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb, |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1579 | void *data) |
| 1580 | { |
| 1581 | unsigned char expn[4096]; |
| 1582 | size_t size = eltsize * nmemb; |
| 1583 | int posn = 0; |
| 1584 | struct http_object_request *freq = |
| 1585 | (struct http_object_request *)data; |
| 1586 | do { |
| 1587 | ssize_t retval = xwrite(freq->localfile, |
| 1588 | (char *) ptr + posn, size - posn); |
| 1589 | if (retval < 0) |
| 1590 | return posn; |
| 1591 | posn += retval; |
| 1592 | } while (posn < size); |
| 1593 | |
| 1594 | freq->stream.avail_in = size; |
| Dan McGee | a04ff3e | 2011-05-03 15:47:27 | [diff] [blame] | 1595 | freq->stream.next_in = (void *)ptr; |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1596 | do { |
| 1597 | freq->stream.next_out = expn; |
| 1598 | freq->stream.avail_out = sizeof(expn); |
| 1599 | freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH); |
| 1600 | git_SHA1_Update(&freq->c, expn, |
| 1601 | sizeof(expn) - freq->stream.avail_out); |
| 1602 | } while (freq->stream.avail_in && freq->zret == Z_OK); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1603 | return size; |
| 1604 | } |
| 1605 | |
| 1606 | struct http_object_request *new_http_object_request(const char *base_url, |
| 1607 | unsigned char *sha1) |
| 1608 | { |
| 1609 | char *hex = sha1_to_hex(sha1); |
| Michael Haggerty | 30d6c6e | 2014-02-21 16:32:05 | [diff] [blame] | 1610 | const char *filename; |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1611 | char prevfile[PATH_MAX]; |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1612 | int prevlocal; |
| Dan McGee | a04ff3e | 2011-05-03 15:47:27 | [diff] [blame] | 1613 | char prev_buf[PREV_BUF_SIZE]; |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1614 | ssize_t prev_read = 0; |
| 1615 | long prev_posn = 0; |
| 1616 | char range[RANGE_HEADER_SIZE]; |
| 1617 | struct curl_slist *range_header = NULL; |
| 1618 | struct http_object_request *freq; |
| 1619 | |
| Tay Ray Chuan | ec99c9a | 2011-08-03 11:54:03 | [diff] [blame] | 1620 | freq = xcalloc(1, sizeof(*freq)); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1621 | hashcpy(freq->sha1, sha1); |
| 1622 | freq->localfile = -1; |
| 1623 | |
| 1624 | filename = sha1_file_name(sha1); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1625 | snprintf(freq->tmpfile, sizeof(freq->tmpfile), |
| 1626 | "%s.temp", filename); |
| 1627 | |
| 1628 | snprintf(prevfile, sizeof(prevfile), "%s.prev", filename); |
| 1629 | unlink_or_warn(prevfile); |
| 1630 | rename(freq->tmpfile, prevfile); |
| 1631 | unlink_or_warn(freq->tmpfile); |
| 1632 | |
| 1633 | if (freq->localfile != -1) |
| 1634 | error("fd leakage in start: %d", freq->localfile); |
| 1635 | freq->localfile = open(freq->tmpfile, |
| 1636 | O_WRONLY | O_CREAT | O_EXCL, 0666); |
| 1637 | /* |
| 1638 | * This could have failed due to the "lazy directory creation"; |
| 1639 | * try to mkdir the last path component. |
| 1640 | */ |
| 1641 | if (freq->localfile < 0 && errno == ENOENT) { |
| 1642 | char *dir = strrchr(freq->tmpfile, '/'); |
| 1643 | if (dir) { |
| 1644 | *dir = 0; |
| 1645 | mkdir(freq->tmpfile, 0777); |
| 1646 | *dir = '/'; |
| 1647 | } |
| 1648 | freq->localfile = open(freq->tmpfile, |
| 1649 | O_WRONLY | O_CREAT | O_EXCL, 0666); |
| 1650 | } |
| 1651 | |
| 1652 | if (freq->localfile < 0) { |
| Shawn O. Pearce | 0da8b2e | 2010-04-17 20:07:38 | [diff] [blame] | 1653 | error("Couldn't create temporary file %s: %s", |
| 1654 | freq->tmpfile, strerror(errno)); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1655 | goto abort; |
| 1656 | } |
| 1657 | |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1658 | git_inflate_init(&freq->stream); |
| 1659 | |
| 1660 | git_SHA1_Init(&freq->c); |
| 1661 | |
| Tay Ray Chuan | bb99190 | 2009-08-10 15:59:55 | [diff] [blame] | 1662 | freq->url = get_remote_object_url(base_url, hex, 0); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1663 | |
| 1664 | /* |
| 1665 | * If a previous temp file is present, process what was already |
| 1666 | * fetched. |
| 1667 | */ |
| 1668 | prevlocal = open(prevfile, O_RDONLY); |
| 1669 | if (prevlocal != -1) { |
| 1670 | do { |
| 1671 | prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE); |
| 1672 | if (prev_read>0) { |
| 1673 | if (fwrite_sha1_file(prev_buf, |
| 1674 | 1, |
| 1675 | prev_read, |
| 1676 | freq) == prev_read) { |
| 1677 | prev_posn += prev_read; |
| 1678 | } else { |
| 1679 | prev_read = -1; |
| 1680 | } |
| 1681 | } |
| 1682 | } while (prev_read > 0); |
| 1683 | close(prevlocal); |
| 1684 | } |
| 1685 | unlink_or_warn(prevfile); |
| 1686 | |
| 1687 | /* |
| 1688 | * Reset inflate/SHA1 if there was an error reading the previous temp |
| 1689 | * file; also rewind to the beginning of the local file. |
| 1690 | */ |
| 1691 | if (prev_read == -1) { |
| 1692 | memset(&freq->stream, 0, sizeof(freq->stream)); |
| 1693 | git_inflate_init(&freq->stream); |
| 1694 | git_SHA1_Init(&freq->c); |
| 1695 | if (prev_posn>0) { |
| 1696 | prev_posn = 0; |
| 1697 | lseek(freq->localfile, 0, SEEK_SET); |
| Jeff Lasslett | 0c4f21e | 2009-08-10 16:05:06 | [diff] [blame] | 1698 | if (ftruncate(freq->localfile, 0) < 0) { |
| Shawn O. Pearce | 0da8b2e | 2010-04-17 20:07:38 | [diff] [blame] | 1699 | error("Couldn't truncate temporary file %s: %s", |
| 1700 | freq->tmpfile, strerror(errno)); |
| Jeff Lasslett | 0c4f21e | 2009-08-10 16:05:06 | [diff] [blame] | 1701 | goto abort; |
| 1702 | } |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | freq->slot = get_active_slot(); |
| 1707 | |
| 1708 | curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq); |
| 1709 | curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); |
| 1710 | curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr); |
| Tay Ray Chuan | bb99190 | 2009-08-10 15:59:55 | [diff] [blame] | 1711 | curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1712 | curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); |
| 1713 | |
| 1714 | /* |
| 1715 | * If we have successfully processed data from a previous fetch |
| 1716 | * attempt, only fetch the data we don't already have. |
| 1717 | */ |
| 1718 | if (prev_posn>0) { |
| 1719 | if (http_is_verbose) |
| 1720 | fprintf(stderr, |
| 1721 | "Resuming fetch of object %s at byte %ld\n", |
| 1722 | hex, prev_posn); |
| 1723 | sprintf(range, "Range: bytes=%ld-", prev_posn); |
| 1724 | range_header = curl_slist_append(range_header, range); |
| 1725 | curl_easy_setopt(freq->slot->curl, |
| 1726 | CURLOPT_HTTPHEADER, range_header); |
| 1727 | } |
| 1728 | |
| 1729 | return freq; |
| 1730 | |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1731 | abort: |
| Tay Ray Chuan | bb99190 | 2009-08-10 15:59:55 | [diff] [blame] | 1732 | free(freq->url); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1733 | free(freq); |
| 1734 | return NULL; |
| 1735 | } |
| 1736 | |
| 1737 | void process_http_object_request(struct http_object_request *freq) |
| 1738 | { |
| 1739 | if (freq->slot == NULL) |
| 1740 | return; |
| 1741 | freq->curl_result = freq->slot->curl_result; |
| 1742 | freq->http_code = freq->slot->http_code; |
| 1743 | freq->slot = NULL; |
| 1744 | } |
| 1745 | |
| 1746 | int finish_http_object_request(struct http_object_request *freq) |
| 1747 | { |
| 1748 | struct stat st; |
| 1749 | |
| 1750 | close(freq->localfile); |
| 1751 | freq->localfile = -1; |
| 1752 | |
| 1753 | process_http_object_request(freq); |
| 1754 | |
| 1755 | if (freq->http_code == 416) { |
| Thiago Farina | bd757c1 | 2010-01-03 16:20:30 | [diff] [blame] | 1756 | warning("requested range invalid; we may already have all the data."); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1757 | } else if (freq->curl_result != CURLE_OK) { |
| 1758 | if (stat(freq->tmpfile, &st) == 0) |
| 1759 | if (st.st_size == 0) |
| 1760 | unlink_or_warn(freq->tmpfile); |
| 1761 | return -1; |
| 1762 | } |
| 1763 | |
| 1764 | git_inflate_end(&freq->stream); |
| 1765 | git_SHA1_Final(freq->real_sha1, &freq->c); |
| 1766 | if (freq->zret != Z_STREAM_END) { |
| 1767 | unlink_or_warn(freq->tmpfile); |
| 1768 | return -1; |
| 1769 | } |
| 1770 | if (hashcmp(freq->sha1, freq->real_sha1)) { |
| 1771 | unlink_or_warn(freq->tmpfile); |
| 1772 | return -1; |
| 1773 | } |
| 1774 | freq->rename = |
| Shawn O. Pearce | 0da8b2e | 2010-04-17 20:07:38 | [diff] [blame] | 1775 | move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1)); |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1776 | |
| 1777 | return freq->rename; |
| 1778 | } |
| 1779 | |
| 1780 | void abort_http_object_request(struct http_object_request *freq) |
| 1781 | { |
| 1782 | unlink_or_warn(freq->tmpfile); |
| 1783 | |
| 1784 | release_http_object_request(freq); |
| 1785 | } |
| 1786 | |
| 1787 | void release_http_object_request(struct http_object_request *freq) |
| 1788 | { |
| 1789 | if (freq->localfile != -1) { |
| 1790 | close(freq->localfile); |
| 1791 | freq->localfile = -1; |
| 1792 | } |
| 1793 | if (freq->url != NULL) { |
| 1794 | free(freq->url); |
| 1795 | freq->url = NULL; |
| 1796 | } |
| Tay Ray Chuan | 4b9fa0e | 2009-08-26 12:20:53 | [diff] [blame] | 1797 | if (freq->slot != NULL) { |
| 1798 | freq->slot->callback_func = NULL; |
| 1799 | freq->slot->callback_data = NULL; |
| 1800 | release_active_slot(freq->slot); |
| 1801 | freq->slot = NULL; |
| 1802 | } |
| Tay Ray Chuan | 5424bc5 | 2009-06-06 08:44:02 | [diff] [blame] | 1803 | } |