[PATCH] Support alternates and http-alternates in http-fetch
This allows the remote repository to refer to additional repositories in a file objects/info/http-alternates or objects/info/alternates. Each line may be: a relative path, starting with ../, to get from the objects directory of the starting repository to the objects directory of the added repository. an absolute path of the objects directory of the added repository (on the same server). (only in http-alternates) a full URL of the objects directory of the added repository. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Daniel Barkalow committed
Sep 18, 2005 at 14:14 UTC
1b0c1e672a417c3f4a46b36497168d6d61dba0cb
1 file changed
+38
-7
http-fetch.c
+38
-7
@@ -144,10 +144,11 @@ static int fetch_alternates(char *base)
144
char *url;
145
char *data;
146
int i = 0;
147
+ int http_specific = 1;
148
if (got_alternates)
149
return 0;
150
data = xmalloc(4096);
150
- buffer.size = 4096;
151
+ buffer.size = 4095;
152
buffer.posn = 0;
153
buffer.buffer = data;
154
@@ -162,6 +163,8 @@ static int fetch_alternates(char *base)
163
curl_easy_setopt(curl, CURLOPT_URL, url);
164
165
if (curl_easy_perform(curl) || !buffer.posn) {
166
+ http_specific = 0;
167
+
168
sprintf(url, "%s/objects/info/alternates", base);
169
170
curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
@@ -173,17 +176,45 @@ static int fetch_alternates(char *base)
176
}
177
}
178
179
+ data[buffer.posn] = '\0';
180
+
181
while (i < buffer.posn) {
182
int posn = i;
183
while (posn < buffer.posn && data[posn] != '\n')
184
posn++;
185
if (data[posn] == '\n') {
186
+ int okay = 0;
187
+ int serverlen = 0;
188
+ struct alt_base *newalt;
189
+ char *target = NULL;
190
if (data[i] == '/') {
182
- int serverlen = strchr(base + 8, '/') - base;
183
- // skip 'objects' at end
184
- char *target =
185
- xmalloc(serverlen + posn - i - 6);
186
- struct alt_base *newalt;
191
+ serverlen = strchr(base + 8, '/') - base;
192
+ okay = 1;
193
+ } else if (!memcmp(data + i, "../", 3)) {
194
+ i += 3;
195
+ serverlen = strlen(base);
196
+ while (i + 2 < posn &&
197
+ !memcmp(data + i, "../", 3)) {
198
+ do {
199
+ serverlen--;
200
+ } while (serverlen &&
201
+ base[serverlen - 1] != '/');
202
+ i += 3;
203
+ }
204
+ // If the server got removed, give up.
205
+ okay = strchr(base, ':') - base + 3 <
206
+ serverlen;
207
+ } else if (http_specific) {
208
+ char *colon = strchr(data + i, ':');
209
+ char *slash = strchr(data + i, '/');
210
+ if (colon && slash && colon < data + posn &&
211
+ slash < data + posn && colon < slash) {
212
+ okay = 1;
213
+ }
214
+ }
215
+ // skip 'objects' at end
216
+ if (okay) {
217
+ target = xmalloc(serverlen + posn - i - 6);
218
strncpy(target, base, serverlen);
219
strncpy(target + serverlen, data + i,
220
posn - i - 7);
@@ -235,7 +266,7 @@ static int fetch_indices(struct alt_base *repo)
266
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
267
268
if (curl_easy_perform(curl)) {
238
- return error("Unable to get pack index %s", url);
269
+ return -1;
270
}
271
272
while (i < buffer.posn) {