Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 581 lines (500 sloc) 14.087 kB
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
1 #include "cache.h"
2 #include "commit.h"
30ae764 Modularize commit-walker
Daniel Barkalow authored
3 #include "walker.h"
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
4 #include "http.h"
7baa3e8 @dscho Some curl versions lack curl_easy_duphandle()
dscho authored
5
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
6 struct alt_base
7 {
2afea3b http-fetch: don't use double-slash as directory separator in URLs
Gerrit Pape authored
8 char *base;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
9 int got_indices;
10 struct packed_git *packs;
11 struct alt_base *next;
12 };
13
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
14 enum object_request_state {
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
15 WAITING,
16 ABORTED,
17 ACTIVE,
18 COMPLETE,
19 };
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
20
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
21 struct object_request
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
22 {
30ae764 Modularize commit-walker
Daniel Barkalow authored
23 struct walker *walker;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
24 unsigned char sha1[20];
25 struct alt_base *repo;
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
26 enum object_request_state state;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
27 struct http_object_request *req;
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
28 struct object_request *next;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
29 };
30
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
31 struct alternates_request {
30ae764 Modularize commit-walker
Daniel Barkalow authored
32 struct walker *walker;
8e29f6a Teach git-http-fetch the --stdin switch
Petr Baudis authored
33 const char *base;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
34 char *url;
028c297 @glandium Use strbuf in http code
glandium authored
35 struct strbuf *buffer;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
36 struct active_request_slot *slot;
37 int http_specific;
38 };
39
30ae764 Modularize commit-walker
Daniel Barkalow authored
40 struct walker_data {
41 const char *url;
42 int got_alternates;
43 struct alt_base *alt;
44 };
45
96f1e58 remove unnecessary initializations
David Rientjes authored
46 static struct object_request *object_queue_head;
bc8f265 @sigprof git-http-fetch: Remove size limit for objects/info/{packs,alternates}
sigprof authored
47
30ae764 Modularize commit-walker
Daniel Barkalow authored
48 static void fetch_alternates(struct walker *walker, const char *base);
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
49
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
50 static void process_object_response(void *callback_data);
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
51
30ae764 Modularize commit-walker
Daniel Barkalow authored
52 static void start_object_request(struct walker *walker,
53 struct object_request *obj_req)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
54 {
55 struct active_request_slot *slot;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
56 struct http_object_request *req;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
57
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
58 req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
59 if (req == NULL) {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
60 obj_req->state = ABORTED;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
61 return;
62 }
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
63 obj_req->req = req;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
64
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
65 slot = req->slot;
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
66 slot->callback_func = process_object_response;
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
67 slot->callback_data = obj_req;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
68
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
69 /* Try to get the request started, abort the request on error */
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
70 obj_req->state = ACTIVE;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
71 if (!start_active_slot(slot)) {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
72 obj_req->state = ABORTED;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
73 release_http_object_request(req);
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
74 return;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
75 }
76 }
77
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
78 static void finish_object_request(struct object_request *obj_req)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
79 {
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
80 if (finish_http_object_request(obj_req->req))
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
81 return;
82
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
83 if (obj_req->req->rename == 0)
30ae764 Modularize commit-walker
Daniel Barkalow authored
84 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
85 }
86
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
87 static void process_object_response(void *callback_data)
88 {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
89 struct object_request *obj_req =
90 (struct object_request *)callback_data;
30ae764 Modularize commit-walker
Daniel Barkalow authored
91 struct walker *walker = obj_req->walker;
92 struct walker_data *data = walker->data;
93 struct alt_base *alt = data->alt;
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
94
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
95 process_http_object_request(obj_req->req);
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
96 obj_req->state = COMPLETE;
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
97
98 /* Use alternates if necessary */
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
99 if (missing_target(obj_req->req)) {
30ae764 Modularize commit-walker
Daniel Barkalow authored
100 fetch_alternates(walker, alt->base);
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
101 if (obj_req->repo->next != NULL) {
102 obj_req->repo =
103 obj_req->repo->next;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
104 release_http_object_request(obj_req->req);
30ae764 Modularize commit-walker
Daniel Barkalow authored
105 start_object_request(walker, obj_req);
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
106 return;
107 }
108 }
109
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
110 finish_object_request(obj_req);
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
111 }
112
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
113 static void release_object_request(struct object_request *obj_req)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
114 {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
115 struct object_request *entry = object_queue_head;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
116
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
117 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
118 error("fd leakage in release: %d", obj_req->req->localfile);
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
119 if (obj_req == object_queue_head) {
120 object_queue_head = obj_req->next;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
121 } else {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
122 while (entry->next != NULL && entry->next != obj_req)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
123 entry = entry->next;
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
124 if (entry->next == obj_req)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
125 entry->next = entry->next->next;
126 }
127
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
128 free(obj_req);
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
129 }
130
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
131 #ifdef USE_CURL_MULTI
30ae764 Modularize commit-walker
Daniel Barkalow authored
132 static int fill_active_slot(struct walker *walker)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
133 {
45c1741 Refactor http.h USE_CURL_MULTI fill_active_slots().
Daniel Barkalow authored
134 struct object_request *obj_req;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
135
45c1741 Refactor http.h USE_CURL_MULTI fill_active_slots().
Daniel Barkalow authored
136 for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
137 if (obj_req->state == WAITING) {
138 if (has_sha1_file(obj_req->sha1))
09db444 http-fetch: Fix object list corruption in fill_active_slots().
Mark Wooding authored
139 obj_req->state = COMPLETE;
45c1741 Refactor http.h USE_CURL_MULTI fill_active_slots().
Daniel Barkalow authored
140 else {
30ae764 Modularize commit-walker
Daniel Barkalow authored
141 start_object_request(walker, obj_req);
45c1741 Refactor http.h USE_CURL_MULTI fill_active_slots().
Daniel Barkalow authored
142 return 1;
143 }
f1a906a [PATCH 1/3] Clean up CURL handles in unused request slots
Nick Hengeveld authored
144 }
8fcf7f9 whitespace cleanup.
Junio C Hamano authored
145 }
45c1741 Refactor http.h USE_CURL_MULTI fill_active_slots().
Daniel Barkalow authored
146 return 0;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
147 }
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
148 #endif
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
149
30ae764 Modularize commit-walker
Daniel Barkalow authored
150 static void prefetch(struct walker *walker, unsigned char *sha1)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
151 {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
152 struct object_request *newreq;
153 struct object_request *tail;
30ae764 Modularize commit-walker
Daniel Barkalow authored
154 struct walker_data *data = walker->data;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
155
156 newreq = xmalloc(sizeof(*newreq));
30ae764 Modularize commit-walker
Daniel Barkalow authored
157 newreq->walker = walker;
e702496 @spearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
158 hashcpy(newreq->sha1, sha1);
30ae764 Modularize commit-walker
Daniel Barkalow authored
159 newreq->repo = data->alt;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
160 newreq->state = WAITING;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
161 newreq->req = NULL;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
162 newreq->next = NULL;
163
e917674 @rctay http*: move common variables and macros to http.[ch]
rctay authored
164 http_is_verbose = walker->get_verbosely;
165
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
166 if (object_queue_head == NULL) {
167 object_queue_head = newreq;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
168 } else {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
169 tail = object_queue_head;
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
170 while (tail->next != NULL)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
171 tail = tail->next;
172 tail->next = newreq;
173 }
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
174
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
175 #ifdef USE_CURL_MULTI
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
176 fill_active_slots();
177 step_active_slots();
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
178 #endif
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
179 }
180
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
181 static void process_alternates_response(void *callback_data)
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
182 {
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
183 struct alternates_request *alt_req =
184 (struct alternates_request *)callback_data;
30ae764 Modularize commit-walker
Daniel Barkalow authored
185 struct walker *walker = alt_req->walker;
186 struct walker_data *cdata = walker->data;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
187 struct active_request_slot *slot = alt_req->slot;
30ae764 Modularize commit-walker
Daniel Barkalow authored
188 struct alt_base *tail = cdata->alt;
8e29f6a Teach git-http-fetch the --stdin switch
Petr Baudis authored
189 const char *base = alt_req->base;
bc8f265 @sigprof git-http-fetch: Remove size limit for objects/info/{packs,alternates}
sigprof authored
190 static const char null_byte = '\0';
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
191 char *data;
192 int i = 0;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
193
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
194 if (alt_req->http_specific) {
195 if (slot->curl_result != CURLE_OK ||
028c297 @glandium Use strbuf in http code
glandium authored
196 !alt_req->buffer->len) {
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
197
198 /* Try reusing the slot to get non-http alternates */
199 alt_req->http_specific = 0;
200 sprintf(alt_req->url, "%s/objects/info/alternates",
201 base);
202 curl_easy_setopt(slot->curl, CURLOPT_URL,
203 alt_req->url);
204 active_requests++;
205 slot->in_use = 1;
c982647 Fix broken slot reuse when fetching alternates
Nick Hengeveld authored
206 if (slot->finished != NULL)
207 (*slot->finished) = 0;
a3f583c http-fetch: Tidy control flow in process_alternate_response
Mark Wooding authored
208 if (!start_active_slot(slot)) {
30ae764 Modularize commit-walker
Daniel Barkalow authored
209 cdata->got_alternates = -1;
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
210 slot->in_use = 0;
c982647 Fix broken slot reuse when fetching alternates
Nick Hengeveld authored
211 if (slot->finished != NULL)
212 (*slot->finished) = 1;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
213 }
a3f583c http-fetch: Tidy control flow in process_alternate_response
Mark Wooding authored
214 return;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
215 }
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
216 } else if (slot->curl_result != CURLE_OK) {
be4a015 http-fetch.c: consolidate code to detect missing fetch target
Junio C Hamano authored
217 if (!missing_target(slot)) {
30ae764 Modularize commit-walker
Daniel Barkalow authored
218 cdata->got_alternates = -1;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
219 return;
220 }
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
221 }
222
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
223 fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
028c297 @glandium Use strbuf in http code
glandium authored
224 alt_req->buffer->len--;
225 data = alt_req->buffer->buf;
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
226
028c297 @glandium Use strbuf in http code
glandium authored
227 while (i < alt_req->buffer->len) {
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
228 int posn = i;
028c297 @glandium Use strbuf in http code
glandium authored
229 while (posn < alt_req->buffer->len && data[posn] != '\n')
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
230 posn++;
231 if (data[posn] == '\n') {
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
232 int okay = 0;
233 int serverlen = 0;
234 struct alt_base *newalt;
235 char *target = NULL;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
236 if (data[i] == '/') {
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
237 /*
238 * This counts
5df1e0d http-fetch: fix alternates handling.
Junio C Hamano authored
239 * http://git.host/pub/scm/linux.git/
240 * -----------here^
241 * so memcpy(dst, base, serverlen) will
242 * copy up to "...git.host".
243 */
244 const char *colon_ss = strstr(base,"://");
245 if (colon_ss) {
246 serverlen = (strchr(colon_ss + 3, '/')
247 - base);
248 okay = 1;
249 }
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
250 } else if (!memcmp(data + i, "../", 3)) {
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
251 /*
252 * Relative URL; chop the corresponding
5df1e0d http-fetch: fix alternates handling.
Junio C Hamano authored
253 * number of subpath from base (and ../
254 * from data), and concatenate the result.
255 *
256 * The code first drops ../ from data, and
257 * then drops one ../ from data and one path
258 * from base. IOW, one extra ../ is dropped
259 * from data than path is dropped from base.
260 *
261 * This is not wrong. The alternate in
262 * http://git.host/pub/scm/linux.git/
263 * to borrow from
264 * http://git.host/pub/scm/linus.git/
265 * is ../../linus.git/objects/. You need
266 * two ../../ to borrow from your direct
267 * neighbour.
268 */
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
269 i += 3;
270 serverlen = strlen(base);
8fcf7f9 whitespace cleanup.
Junio C Hamano authored
271 while (i + 2 < posn &&
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
272 !memcmp(data + i, "../", 3)) {
273 do {
274 serverlen--;
275 } while (serverlen &&
276 base[serverlen - 1] != '/');
277 i += 3;
278 }
a9486b0 @proski Avoid C99 comments, use old-style C comments instead.
proski authored
279 /* If the server got removed, give up. */
8fcf7f9 whitespace cleanup.
Junio C Hamano authored
280 okay = strchr(base, ':') - base + 3 <
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
281 serverlen;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
282 } else if (alt_req->http_specific) {
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
283 char *colon = strchr(data + i, ':');
284 char *slash = strchr(data + i, '/');
285 if (colon && slash && colon < data + posn &&
286 slash < data + posn && colon < slash) {
287 okay = 1;
288 }
289 }
5df1e0d http-fetch: fix alternates handling.
Junio C Hamano authored
290 /* skip "objects\n" at end */
1b0c1e6 [PATCH] Support alternates and http-alternates in http-fetch
Daniel Barkalow authored
291 if (okay) {
292 target = xmalloc(serverlen + posn - i - 6);
5df1e0d http-fetch: fix alternates handling.
Junio C Hamano authored
293 memcpy(target, base, serverlen);
294 memcpy(target + serverlen, data + i,
295 posn - i - 7);
296 target[serverlen + posn - i - 7] = 0;
30ae764 Modularize commit-walker
Daniel Barkalow authored
297 if (walker->get_verbosely)
8fcf7f9 whitespace cleanup.
Junio C Hamano authored
298 fprintf(stderr,
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
299 "Also look at %s\n", target);
300 newalt = xmalloc(sizeof(*newalt));
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
301 newalt->next = NULL;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
302 newalt->base = target;
303 newalt->got_indices = 0;
304 newalt->packs = NULL;
8d9fbe5 http-fetch: add optional DAV-based pack list
Nick Hengeveld authored
305
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
306 while (tail->next != NULL)
307 tail = tail->next;
308 tail->next = newalt;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
309 }
310 }
311 i = posn + 1;
312 }
bc8f265 @sigprof git-http-fetch: Remove size limit for objects/info/{packs,alternates}
sigprof authored
313
30ae764 Modularize commit-walker
Daniel Barkalow authored
314 cdata->got_alternates = 1;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
315 }
316
30ae764 Modularize commit-walker
Daniel Barkalow authored
317 static void fetch_alternates(struct walker *walker, const char *base)
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
318 {
028c297 @glandium Use strbuf in http code
glandium authored
319 struct strbuf buffer = STRBUF_INIT;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
320 char *url;
321 struct active_request_slot *slot;
cb754fd Use local structs for HTTP slot callback data
Nick Hengeveld authored
322 struct alternates_request alt_req;
30ae764 Modularize commit-walker
Daniel Barkalow authored
323 struct walker_data *cdata = walker->data;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
324
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
325 /*
326 * If another request has already started fetching alternates,
327 * wait for them to arrive and return to processing this request's
328 * curl message
329 */
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
330 #ifdef USE_CURL_MULTI
30ae764 Modularize commit-walker
Daniel Barkalow authored
331 while (cdata->got_alternates == 0) {
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
332 step_active_slots();
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
333 }
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
334 #endif
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
335
336 /* Nothing to do if they've already been fetched */
30ae764 Modularize commit-walker
Daniel Barkalow authored
337 if (cdata->got_alternates == 1)
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
338 return;
339
340 /* Start the fetch */
30ae764 Modularize commit-walker
Daniel Barkalow authored
341 cdata->got_alternates = 0;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
342
30ae764 Modularize commit-walker
Daniel Barkalow authored
343 if (walker->get_verbosely)
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
344 fprintf(stderr, "Getting alternates list for %s\n", base);
8fcf7f9 whitespace cleanup.
Junio C Hamano authored
345
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
346 url = xmalloc(strlen(base) + 31);
347 sprintf(url, "%s/objects/info/http-alternates", base);
348
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
349 /*
350 * Use a callback to process the result, since another request
351 * may fail and need to have alternates loaded before continuing
352 */
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
353 slot = get_active_slot();
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
354 slot->callback_func = process_alternates_response;
30ae764 Modularize commit-walker
Daniel Barkalow authored
355 alt_req.walker = walker;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
356 slot->callback_data = &alt_req;
357
358 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
359 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
360 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
361
362 alt_req.base = base;
363 alt_req.url = url;
364 alt_req.buffer = &buffer;
365 alt_req.http_specific = 1;
366 alt_req.slot = slot;
367
368 if (start_active_slot(slot))
369 run_active_slot(slot);
370 else
30ae764 Modularize commit-walker
Daniel Barkalow authored
371 cdata->got_alternates = -1;
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
372
028c297 @glandium Use strbuf in http code
glandium authored
373 strbuf_release(&buffer);
acc075a Fix for multiple alternates requests in http-fetch
Nick Hengeveld authored
374 free(url);
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
375 }
376
30ae764 Modularize commit-walker
Daniel Barkalow authored
377 static int fetch_indices(struct walker *walker, struct alt_base *repo)
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
378 {
b8caac2 @rctay http*: add http_get_info_packs
rctay authored
379 int ret;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
380
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
381 if (repo->got_indices)
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
382 return 0;
383
30ae764 Modularize commit-walker
Daniel Barkalow authored
384 if (walker->get_verbosely)
6fd72e3 Show URL in the "Getting <foo> list" http-fetch messages
Petr Baudis authored
385 fprintf(stderr, "Getting pack list for %s\n", repo->base);
8fcf7f9 whitespace cleanup.
Junio C Hamano authored
386
b8caac2 @rctay http*: add http_get_info_packs
rctay authored
387 switch (http_get_info_packs(repo->base, &repo->packs)) {
388 case HTTP_OK:
389 case HTTP_MISSING_TARGET:
390 repo->got_indices = 1;
391 ret = 0;
392 break;
393 default:
5e3a769 Improve pack list response handling
Nick Hengeveld authored
394 repo->got_indices = 0;
b8caac2 @rctay http*: add http_get_info_packs
rctay authored
395 ret = -1;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
396 }
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
397
3a462bc @glandium Fix various memory leaks in http-push.c and http-walker.c
glandium authored
398 return ret;
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
399 }
400
30ae764 Modularize commit-walker
Daniel Barkalow authored
401 static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
402 {
403 struct packed_git *target;
49a0f24 [PATCH] HTTP partial transfer support for object, pack, and index tra…
Nick Hengeveld authored
404 int ret;
cb754fd Use local structs for HTTP slot callback data
Nick Hengeveld authored
405 struct slot_results results;
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
406 struct http_pack_request *preq;
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
407
30ae764 Modularize commit-walker
Daniel Barkalow authored
408 if (fetch_indices(walker, repo))
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
409 return -1;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
410 target = find_sha1_pack(sha1, repo->packs);
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
411 if (!target)
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
412 return -1;
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
413
30ae764 Modularize commit-walker
Daniel Barkalow authored
414 if (walker->get_verbosely) {
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
415 fprintf(stderr, "Getting pack %s\n",
416 sha1_to_hex(target->sha1));
417 fprintf(stderr, " which contains %s\n",
418 sha1_to_hex(sha1));
419 }
420
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
421 preq = new_http_pack_request(target, repo->base);
422 if (preq == NULL)
423 goto abort;
424 preq->lst = &repo->packs;
425 preq->slot->results = &results;
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
426
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
427 if (start_active_slot(preq->slot)) {
428 run_active_slot(preq->slot);
c8568e1 Fix HTTP request result processing after slot reuse
Nick Hengeveld authored
429 if (results.curl_result != CURLE_OK) {
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
430 error("Unable to get pack file %s\n%s", preq->url,
431 curl_errorstr);
432 goto abort;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
433 }
434 } else {
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
435 error("Unable to start request");
436 goto abort;
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
437 }
438
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
439 ret = finish_http_pack_request(preq);
440 release_http_pack_request(preq);
49a0f24 [PATCH] HTTP partial transfer support for object, pack, and index tra…
Nick Hengeveld authored
441 if (ret)
b721e01 Use the same move_temp_to_file in git-http-fetch.
Junio C Hamano authored
442 return ret;
49a0f24 [PATCH] HTTP partial transfer support for object, pack, and index tra…
Nick Hengeveld authored
443
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
444 return 0;
2264dfa @rctay http*: add helper methods for fetching packs
rctay authored
445
446 abort:
447 return -1;
182005b [PATCH] Support downloading packs by HTTP (whitespace fixed)
barkalow@iabervon.org authored
448 }
449
53f3138 http-fetch: Abort requests for objects which arrived in packs
Mark Wooding authored
450 static void abort_object_request(struct object_request *obj_req)
451 {
452 release_object_request(obj_req);
453 }
454
30ae764 Modularize commit-walker
Daniel Barkalow authored
455 static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
456 {
457 char *hex = sha1_to_hex(sha1);
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
458 int ret = 0;
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
459 struct object_request *obj_req = object_queue_head;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
460 struct http_object_request *req;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
461
a89fccd Do not use memcmp(sha1_1, sha1_2, 20) with hardcoded length.
David Rientjes authored
462 while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
463 obj_req = obj_req->next;
464 if (obj_req == NULL)
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
465 return error("Couldn't find request for %s in the queue", hex);
466
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
467 if (has_sha1_file(obj_req->sha1)) {
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
468 if (obj_req->req != NULL)
469 abort_http_object_request(obj_req->req);
53f3138 http-fetch: Abort requests for objects which arrived in packs
Mark Wooding authored
470 abort_object_request(obj_req);
11f0daf [PATCH] Don't fetch objects that exist in the local repository
Nick Hengeveld authored
471 return 0;
472 }
473
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
474 #ifdef USE_CURL_MULTI
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
475 while (obj_req->state == WAITING)
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
476 step_active_slots();
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
477 #else
30ae764 Modularize commit-walker
Daniel Barkalow authored
478 start_object_request(walker, obj_req);
a7a8d37 Only compile parallel HTTP support with CURL >= 7.9.8
Nick Hengeveld authored
479 #endif
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
480
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
481 /*
482 * obj_req->req might change when fetching alternates in the callback
483 * process_object_response; therefore, the "shortcut" variable, req,
484 * is used only after we're done with slots.
485 */
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
486 while (obj_req->state == ACTIVE)
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
487 run_active_slot(obj_req->req->slot);
488
489 req = obj_req->req;
4c42aa1 @rctay http-push, http-walker: style fixes
rctay authored
490
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
491 if (req->localfile != -1) {
492 close(req->localfile);
493 req->localfile = -1;
313c471 Fix bunch of fd leaks in http-fetch
Petr Baudis authored
494 }
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
495
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
496 if (obj_req->state == ABORTED) {
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
497 ret = error("Request for %s aborted", hex);
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
498 } else if (req->curl_result != CURLE_OK &&
499 req->http_code != 416) {
500 if (missing_target(req))
e2029eb Silence confusing and false-positive curl error message
Petr Baudis authored
501 ret = -1; /* Be silent, it is probably in a pack. */
502 else
503 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
504 req->errorstr, req->curl_result,
505 req->http_code, hex);
506 } else if (req->zret != Z_STREAM_END) {
30ae764 Modularize commit-walker
Daniel Barkalow authored
507 walker->corrupt_object_found++;
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
508 ret = error("File %s (%s) corrupt", hex, req->url);
509 } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
bd2afde Give no terminating LF to error() function.
Junio C Hamano authored
510 ret = error("File %s has bad hash", hex);
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
511 } else if (req->rename < 0) {
7b934ec http-fetch: Fix message reporting rename of object file.
Mark Wooding authored
512 ret = error("unable to write sha1 filename %s",
0da8b2e @spearce http.c: Don't store destination name in request structures
spearce authored
513 sha1_file_name(req->sha1));
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
514 }
49a0f24 [PATCH] HTTP partial transfer support for object, pack, and index tra…
Nick Hengeveld authored
515
5424bc5 @rctay http*: add helper methods for fetching objects (loose)
rctay authored
516 release_http_object_request(req);
e388ab7 Make http-fetch request types more clear
Nick Hengeveld authored
517 release_object_request(obj_req);
29508e1 Isolate shared HTTP request functionality
Nick Hengeveld authored
518 return ret;
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
519 }
520
30ae764 Modularize commit-walker
Daniel Barkalow authored
521 static int fetch(struct walker *walker, unsigned char *sha1)
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
522 {
30ae764 Modularize commit-walker
Daniel Barkalow authored
523 struct walker_data *data = walker->data;
524 struct alt_base *altbase = data->alt;
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
525
30ae764 Modularize commit-walker
Daniel Barkalow authored
526 if (!fetch_object(walker, altbase, sha1))
1d389ab Add support for parallel HTTP transfers
Nick Hengeveld authored
527 return 0;
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
528 while (altbase) {
30ae764 Modularize commit-walker
Daniel Barkalow authored
529 if (!fetch_pack(walker, altbase, sha1))
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
530 return 0;
30ae764 Modularize commit-walker
Daniel Barkalow authored
531 fetch_alternates(walker, data->alt->base);
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
532 altbase = altbase->next;
533 }
bd2afde Give no terminating LF to error() function.
Junio C Hamano authored
534 return error("Unable to find %s under %s", sha1_to_hex(sha1),
30ae764 Modularize commit-walker
Daniel Barkalow authored
535 data->alt->base);
b366156 [PATCH] Add support for alternates in HTTP
Daniel Barkalow authored
536 }
537
c13b263 Make walker.fetch_ref() take a struct ref.
Daniel Barkalow authored
538 static int fetch_ref(struct walker *walker, struct ref *ref)
cd541a6 [PATCH] Generic support for pulling refs
Daniel Barkalow authored
539 {
30ae764 Modularize commit-walker
Daniel Barkalow authored
540 struct walker_data *data = walker->data;
c13b263 Make walker.fetch_ref() take a struct ref.
Daniel Barkalow authored
541 return http_fetch_ref(data->alt->base, ref);
cd541a6 [PATCH] Generic support for pulling refs
Daniel Barkalow authored
542 }
543
30ae764 Modularize commit-walker
Daniel Barkalow authored
544 static void cleanup(struct walker *walker)
545 {
546 http_cleanup();
547 }
548
9fc6440 @glandium Set proxy override with http_init()
glandium authored
549 struct walker *get_http_walker(const char *url, struct remote *remote)
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
550 {
9c880b3 http-fetch: remove path_len from struct alt_base, it was computed but…
Gerrit Pape authored
551 char *s;
30ae764 Modularize commit-walker
Daniel Barkalow authored
552 struct walker_data *data = xmalloc(sizeof(struct walker_data));
553 struct walker *walker = xmalloc(sizeof(struct walker));
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
554
9fc6440 @glandium Set proxy override with http_init()
glandium authored
555 http_init(remote);
d402d55 Use config file settings for http
Nick Hengeveld authored
556
30ae764 Modularize commit-walker
Daniel Barkalow authored
557 data->alt = xmalloc(sizeof(*data->alt));
558 data->alt->base = xmalloc(strlen(url) + 1);
559 strcpy(data->alt->base, url);
560 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
9c880b3 http-fetch: remove path_len from struct alt_base, it was computed but…
Gerrit Pape authored
561 *s = 0;
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
562
30ae764 Modularize commit-walker
Daniel Barkalow authored
563 data->alt->got_indices = 0;
564 data->alt->packs = NULL;
565 data->alt->next = NULL;
566 data->got_alternates = -1;
fc57b6a Make function to refill http queue a callback
Daniel Barkalow authored
567
30ae764 Modularize commit-walker
Daniel Barkalow authored
568 walker->corrupt_object_found = 0;
569 walker->fetch = fetch;
570 walker->fetch_ref = fetch_ref;
571 walker->prefetch = prefetch;
572 walker->cleanup = cleanup;
573 walker->data = data;
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
574
30ae764 Modularize commit-walker
Daniel Barkalow authored
575 #ifdef USE_CURL_MULTI
576 add_fill_function(walker, (int (*)(void *)) fill_active_slot);
577 #endif
8e29f6a Teach git-http-fetch the --stdin switch
Petr Baudis authored
578
30ae764 Modularize commit-walker
Daniel Barkalow authored
579 return walker;
6eb7ed5 [PATCH] Various transport programs
Daniel Barkalow authored
580 }
Something went wrong with that request. Please try again.