Merge branch 'sg/subtree-signed-commits' into pu
[git] / http-walker.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "walker.h"
4 #include "http.h"
5 #include "list.h"
6 #include "transport.h"
7 #include "packfile.h"
8
9 struct alt_base {
10         char *base;
11         int got_indices;
12         struct packed_git *packs;
13         struct alt_base *next;
14 };
15
16 enum object_request_state {
17         WAITING,
18         ABORTED,
19         ACTIVE,
20         COMPLETE
21 };
22
23 struct object_request {
24         struct walker *walker;
25         unsigned char sha1[20];
26         struct alt_base *repo;
27         enum object_request_state state;
28         struct http_object_request *req;
29         struct list_head node;
30 };
31
32 struct alternates_request {
33         struct walker *walker;
34         const char *base;
35         struct strbuf *url;
36         struct strbuf *buffer;
37         struct active_request_slot *slot;
38         int http_specific;
39 };
40
41 struct walker_data {
42         const char *url;
43         int got_alternates;
44         struct alt_base *alt;
45 };
46
47 static LIST_HEAD(object_queue_head);
48
49 static void fetch_alternates(struct walker *walker, const char *base);
50
51 static void process_object_response(void *callback_data);
52
53 static void start_object_request(struct walker *walker,
54                                  struct object_request *obj_req)
55 {
56         struct active_request_slot *slot;
57         struct http_object_request *req;
58
59         req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
60         if (req == NULL) {
61                 obj_req->state = ABORTED;
62                 return;
63         }
64         obj_req->req = req;
65
66         slot = req->slot;
67         slot->callback_func = process_object_response;
68         slot->callback_data = obj_req;
69
70         /* Try to get the request started, abort the request on error */
71         obj_req->state = ACTIVE;
72         if (!start_active_slot(slot)) {
73                 obj_req->state = ABORTED;
74                 release_http_object_request(req);
75                 return;
76         }
77 }
78
79 static void finish_object_request(struct object_request *obj_req)
80 {
81         if (finish_http_object_request(obj_req->req))
82                 return;
83
84         if (obj_req->req->rename == 0)
85                 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
86 }
87
88 static void process_object_response(void *callback_data)
89 {
90         struct object_request *obj_req =
91                 (struct object_request *)callback_data;
92         struct walker *walker = obj_req->walker;
93         struct walker_data *data = walker->data;
94         struct alt_base *alt = data->alt;
95
96         process_http_object_request(obj_req->req);
97         obj_req->state = COMPLETE;
98
99         /* Use alternates if necessary */
100         if (missing_target(obj_req->req)) {
101                 fetch_alternates(walker, alt->base);
102                 if (obj_req->repo->next != NULL) {
103                         obj_req->repo =
104                                 obj_req->repo->next;
105                         release_http_object_request(obj_req->req);
106                         start_object_request(walker, obj_req);
107                         return;
108                 }
109         }
110
111         finish_object_request(obj_req);
112 }
113
114 static void release_object_request(struct object_request *obj_req)
115 {
116         if (obj_req->req !=NULL && obj_req->req->localfile != -1)
117                 error("fd leakage in release: %d", obj_req->req->localfile);
118
119         list_del(&obj_req->node);
120         free(obj_req);
121 }
122
123 static int fill_active_slot(struct walker *walker)
124 {
125         struct object_request *obj_req;
126         struct list_head *pos, *tmp, *head = &object_queue_head;
127
128         list_for_each_safe(pos, tmp, head) {
129                 obj_req = list_entry(pos, struct object_request, node);
130                 if (obj_req->state == WAITING) {
131                         if (has_sha1_file(obj_req->sha1))
132                                 obj_req->state = COMPLETE;
133                         else {
134                                 start_object_request(walker, obj_req);
135                                 return 1;
136                         }
137                 }
138         }
139         return 0;
140 }
141
142 static void prefetch(struct walker *walker, unsigned char *sha1)
143 {
144         struct object_request *newreq;
145         struct walker_data *data = walker->data;
146
147         newreq = xmalloc(sizeof(*newreq));
148         newreq->walker = walker;
149         hashcpy(newreq->sha1, sha1);
150         newreq->repo = data->alt;
151         newreq->state = WAITING;
152         newreq->req = NULL;
153
154         http_is_verbose = walker->get_verbosely;
155         list_add_tail(&newreq->node, &object_queue_head);
156
157         fill_active_slots();
158         step_active_slots();
159 }
160
161 static int is_alternate_allowed(const char *url)
162 {
163         const char *protocols[] = {
164                 "http", "https", "ftp", "ftps"
165         };
166         int i;
167
168         if (http_follow_config != HTTP_FOLLOW_ALWAYS) {
169                 warning("alternate disabled by http.followRedirects: %s", url);
170                 return 0;
171         }
172
173         for (i = 0; i < ARRAY_SIZE(protocols); i++) {
174                 const char *end;
175                 if (skip_prefix(url, protocols[i], &end) &&
176                     starts_with(end, "://"))
177                         break;
178         }
179
180         if (i >= ARRAY_SIZE(protocols)) {
181                 warning("ignoring alternate with unknown protocol: %s", url);
182                 return 0;
183         }
184         if (!is_transport_allowed(protocols[i], 0)) {
185                 warning("ignoring alternate with restricted protocol: %s", url);
186                 return 0;
187         }
188
189         return 1;
190 }
191
192 static void process_alternates_response(void *callback_data)
193 {
194         struct alternates_request *alt_req =
195                 (struct alternates_request *)callback_data;
196         struct walker *walker = alt_req->walker;
197         struct walker_data *cdata = walker->data;
198         struct active_request_slot *slot = alt_req->slot;
199         struct alt_base *tail = cdata->alt;
200         const char *base = alt_req->base;
201         const char null_byte = '\0';
202         char *data;
203         int i = 0;
204
205         if (alt_req->http_specific) {
206                 if (slot->curl_result != CURLE_OK ||
207                     !alt_req->buffer->len) {
208
209                         /* Try reusing the slot to get non-http alternates */
210                         alt_req->http_specific = 0;
211                         strbuf_reset(alt_req->url);
212                         strbuf_addf(alt_req->url, "%s/objects/info/alternates",
213                                     base);
214                         curl_easy_setopt(slot->curl, CURLOPT_URL,
215                                          alt_req->url->buf);
216                         active_requests++;
217                         slot->in_use = 1;
218                         if (slot->finished != NULL)
219                                 (*slot->finished) = 0;
220                         if (!start_active_slot(slot)) {
221                                 cdata->got_alternates = -1;
222                                 slot->in_use = 0;
223                                 if (slot->finished != NULL)
224                                         (*slot->finished) = 1;
225                         }
226                         return;
227                 }
228         } else if (slot->curl_result != CURLE_OK) {
229                 if (!missing_target(slot)) {
230                         cdata->got_alternates = -1;
231                         return;
232                 }
233         }
234
235         fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
236         alt_req->buffer->len--;
237         data = alt_req->buffer->buf;
238
239         while (i < alt_req->buffer->len) {
240                 int posn = i;
241                 while (posn < alt_req->buffer->len && data[posn] != '\n')
242                         posn++;
243                 if (data[posn] == '\n') {
244                         int okay = 0;
245                         int serverlen = 0;
246                         struct alt_base *newalt;
247                         if (data[i] == '/') {
248                                 /*
249                                  * This counts
250                                  * http://git.host/pub/scm/linux.git/
251                                  * -----------here^
252                                  * so memcpy(dst, base, serverlen) will
253                                  * copy up to "...git.host".
254                                  */
255                                 const char *colon_ss = strstr(base,"://");
256                                 if (colon_ss) {
257                                         serverlen = (strchr(colon_ss + 3, '/')
258                                                      - base);
259                                         okay = 1;
260                                 }
261                         } else if (!memcmp(data + i, "../", 3)) {
262                                 /*
263                                  * Relative URL; chop the corresponding
264                                  * number of subpath from base (and ../
265                                  * from data), and concatenate the result.
266                                  *
267                                  * The code first drops ../ from data, and
268                                  * then drops one ../ from data and one path
269                                  * from base.  IOW, one extra ../ is dropped
270                                  * from data than path is dropped from base.
271                                  *
272                                  * This is not wrong.  The alternate in
273                                  *     http://git.host/pub/scm/linux.git/
274                                  * to borrow from
275                                  *     http://git.host/pub/scm/linus.git/
276                                  * is ../../linus.git/objects/.  You need
277                                  * two ../../ to borrow from your direct
278                                  * neighbour.
279                                  */
280                                 i += 3;
281                                 serverlen = strlen(base);
282                                 while (i + 2 < posn &&
283                                        !memcmp(data + i, "../", 3)) {
284                                         do {
285                                                 serverlen--;
286                                         } while (serverlen &&
287                                                  base[serverlen - 1] != '/');
288                                         i += 3;
289                                 }
290                                 /* If the server got removed, give up. */
291                                 okay = strchr(base, ':') - base + 3 <
292                                        serverlen;
293                         } else if (alt_req->http_specific) {
294                                 char *colon = strchr(data + i, ':');
295                                 char *slash = strchr(data + i, '/');
296                                 if (colon && slash && colon < data + posn &&
297                                     slash < data + posn && colon < slash) {
298                                         okay = 1;
299                                 }
300                         }
301                         if (okay) {
302                                 struct strbuf target = STRBUF_INIT;
303                                 strbuf_add(&target, base, serverlen);
304                                 strbuf_add(&target, data + i, posn - i);
305                                 if (!strbuf_strip_suffix(&target, "objects")) {
306                                         warning("ignoring alternate that does"
307                                                 " not end in 'objects': %s",
308                                                 target.buf);
309                                         strbuf_release(&target);
310                                 } else if (is_alternate_allowed(target.buf)) {
311                                         warning("adding alternate object store: %s",
312                                                 target.buf);
313                                         newalt = xmalloc(sizeof(*newalt));
314                                         newalt->next = NULL;
315                                         newalt->base = strbuf_detach(&target, NULL);
316                                         newalt->got_indices = 0;
317                                         newalt->packs = NULL;
318
319                                         while (tail->next != NULL)
320                                                 tail = tail->next;
321                                         tail->next = newalt;
322                                 } else {
323                                         strbuf_release(&target);
324                                 }
325                         }
326                 }
327                 i = posn + 1;
328         }
329
330         cdata->got_alternates = 1;
331 }
332
333 static void fetch_alternates(struct walker *walker, const char *base)
334 {
335         struct strbuf buffer = STRBUF_INIT;
336         struct strbuf url = STRBUF_INIT;
337         struct active_request_slot *slot;
338         struct alternates_request alt_req;
339         struct walker_data *cdata = walker->data;
340
341         /*
342          * If another request has already started fetching alternates,
343          * wait for them to arrive and return to processing this request's
344          * curl message
345          */
346         while (cdata->got_alternates == 0) {
347                 step_active_slots();
348         }
349
350         /* Nothing to do if they've already been fetched */
351         if (cdata->got_alternates == 1)
352                 return;
353
354         /* Start the fetch */
355         cdata->got_alternates = 0;
356
357         if (walker->get_verbosely)
358                 fprintf(stderr, "Getting alternates list for %s\n", base);
359
360         strbuf_addf(&url, "%s/objects/info/http-alternates", base);
361
362         /*
363          * Use a callback to process the result, since another request
364          * may fail and need to have alternates loaded before continuing
365          */
366         slot = get_active_slot();
367         slot->callback_func = process_alternates_response;
368         alt_req.walker = walker;
369         slot->callback_data = &alt_req;
370
371         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
372         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
373         curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
374
375         alt_req.base = base;
376         alt_req.url = &url;
377         alt_req.buffer = &buffer;
378         alt_req.http_specific = 1;
379         alt_req.slot = slot;
380
381         if (start_active_slot(slot))
382                 run_active_slot(slot);
383         else
384                 cdata->got_alternates = -1;
385
386         strbuf_release(&buffer);
387         strbuf_release(&url);
388 }
389
390 static int fetch_indices(struct walker *walker, struct alt_base *repo)
391 {
392         int ret;
393
394         if (repo->got_indices)
395                 return 0;
396
397         if (walker->get_verbosely)
398                 fprintf(stderr, "Getting pack list for %s\n", repo->base);
399
400         switch (http_get_info_packs(repo->base, &repo->packs)) {
401         case HTTP_OK:
402         case HTTP_MISSING_TARGET:
403                 repo->got_indices = 1;
404                 ret = 0;
405                 break;
406         default:
407                 repo->got_indices = 0;
408                 ret = -1;
409         }
410
411         return ret;
412 }
413
414 static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
415 {
416         struct packed_git *target;
417         int ret;
418         struct slot_results results;
419         struct http_pack_request *preq;
420
421         if (fetch_indices(walker, repo))
422                 return -1;
423         target = find_sha1_pack(sha1, repo->packs);
424         if (!target)
425                 return -1;
426
427         if (walker->get_verbosely) {
428                 fprintf(stderr, "Getting pack %s\n",
429                         sha1_to_hex(target->sha1));
430                 fprintf(stderr, " which contains %s\n",
431                         sha1_to_hex(sha1));
432         }
433
434         preq = new_http_pack_request(target, repo->base);
435         if (preq == NULL)
436                 goto abort;
437         preq->lst = &repo->packs;
438         preq->slot->results = &results;
439
440         if (start_active_slot(preq->slot)) {
441                 run_active_slot(preq->slot);
442                 if (results.curl_result != CURLE_OK) {
443                         error("Unable to get pack file %s\n%s", preq->url,
444                               curl_errorstr);
445                         goto abort;
446                 }
447         } else {
448                 error("Unable to start request");
449                 goto abort;
450         }
451
452         ret = finish_http_pack_request(preq);
453         release_http_pack_request(preq);
454         if (ret)
455                 return ret;
456
457         return 0;
458
459 abort:
460         return -1;
461 }
462
463 static void abort_object_request(struct object_request *obj_req)
464 {
465         release_object_request(obj_req);
466 }
467
468 static int fetch_object(struct walker *walker, unsigned char *sha1)
469 {
470         char *hex = sha1_to_hex(sha1);
471         int ret = 0;
472         struct object_request *obj_req = NULL;
473         struct http_object_request *req;
474         struct list_head *pos, *head = &object_queue_head;
475
476         list_for_each(pos, head) {
477                 obj_req = list_entry(pos, struct object_request, node);
478                 if (!hashcmp(obj_req->sha1, sha1))
479                         break;
480         }
481         if (obj_req == NULL)
482                 return error("Couldn't find request for %s in the queue", hex);
483
484         if (has_sha1_file(obj_req->sha1)) {
485                 if (obj_req->req != NULL)
486                         abort_http_object_request(obj_req->req);
487                 abort_object_request(obj_req);
488                 return 0;
489         }
490
491         while (obj_req->state == WAITING)
492                 step_active_slots();
493
494         /*
495          * obj_req->req might change when fetching alternates in the callback
496          * process_object_response; therefore, the "shortcut" variable, req,
497          * is used only after we're done with slots.
498          */
499         while (obj_req->state == ACTIVE)
500                 run_active_slot(obj_req->req->slot);
501
502         req = obj_req->req;
503
504         if (req->localfile != -1) {
505                 close(req->localfile);
506                 req->localfile = -1;
507         }
508
509         /*
510          * we turned off CURLOPT_FAILONERROR to avoid losing a
511          * persistent connection and got CURLE_OK.
512          */
513         if (req->http_code >= 300 && req->curl_result == CURLE_OK &&
514                         (starts_with(req->url, "http://") ||
515                          starts_with(req->url, "https://"))) {
516                 req->curl_result = CURLE_HTTP_RETURNED_ERROR;
517                 xsnprintf(req->errorstr, sizeof(req->errorstr),
518                           "HTTP request failed");
519         }
520
521         if (obj_req->state == ABORTED) {
522                 ret = error("Request for %s aborted", hex);
523         } else if (req->curl_result != CURLE_OK &&
524                    req->http_code != 416) {
525                 if (missing_target(req))
526                         ret = -1; /* Be silent, it is probably in a pack. */
527                 else
528                         ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
529                                     req->errorstr, req->curl_result,
530                                     req->http_code, hex);
531         } else if (req->zret != Z_STREAM_END) {
532                 walker->corrupt_object_found++;
533                 ret = error("File %s (%s) corrupt", hex, req->url);
534         } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
535                 ret = error("File %s has bad hash", hex);
536         } else if (req->rename < 0) {
537                 struct strbuf buf = STRBUF_INIT;
538                 sha1_file_name(&buf, req->sha1);
539                 ret = error("unable to write sha1 filename %s", buf.buf);
540                 strbuf_release(&buf);
541         }
542
543         release_http_object_request(req);
544         release_object_request(obj_req);
545         return ret;
546 }
547
548 static int fetch(struct walker *walker, unsigned char *sha1)
549 {
550         struct walker_data *data = walker->data;
551         struct alt_base *altbase = data->alt;
552
553         if (!fetch_object(walker, sha1))
554                 return 0;
555         while (altbase) {
556                 if (!http_fetch_pack(walker, altbase, sha1))
557                         return 0;
558                 fetch_alternates(walker, data->alt->base);
559                 altbase = altbase->next;
560         }
561         return error("Unable to find %s under %s", sha1_to_hex(sha1),
562                      data->alt->base);
563 }
564
565 static int fetch_ref(struct walker *walker, struct ref *ref)
566 {
567         struct walker_data *data = walker->data;
568         return http_fetch_ref(data->alt->base, ref);
569 }
570
571 static void cleanup(struct walker *walker)
572 {
573         struct walker_data *data = walker->data;
574         struct alt_base *alt, *alt_next;
575
576         if (data) {
577                 alt = data->alt;
578                 while (alt) {
579                         alt_next = alt->next;
580
581                         free(alt->base);
582                         free(alt);
583
584                         alt = alt_next;
585                 }
586                 free(data);
587                 walker->data = NULL;
588         }
589 }
590
591 struct walker *get_http_walker(const char *url)
592 {
593         char *s;
594         struct walker_data *data = xmalloc(sizeof(struct walker_data));
595         struct walker *walker = xmalloc(sizeof(struct walker));
596
597         data->alt = xmalloc(sizeof(*data->alt));
598         data->alt->base = xstrdup(url);
599         for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
600                 *s = 0;
601
602         data->alt->got_indices = 0;
603         data->alt->packs = NULL;
604         data->alt->next = NULL;
605         data->got_alternates = -1;
606
607         walker->corrupt_object_found = 0;
608         walker->fetch = fetch;
609         walker->fetch_ref = fetch_ref;
610         walker->prefetch = prefetch;
611         walker->cleanup = cleanup;
612         walker->data = data;
613
614         add_fill_function(walker, (int (*)(void *)) fill_active_slot);
615
616         return walker;
617 }