Skip to contentSkip to conversation

http*: add helper methods for fetching objects/info/packs

PATCH In review Tay Ray Chuan 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 http*: add helper methods for fetching objects/info/packs

The code handling the fetching of objects/info/packs in http-push.c and http-walker.c have been refactored into new methods and a new struct in http.c. They are not meant to be invoked elsewhere.

The new methods in http.c are
*new_http_info_packs_request
*finish_http_info_packs_request
*release_http_info_packs_request

and the new struct is http_info_packs_request.

A callback function wraps the invocation of fetch_http_pack_index, invoked in finish_http_info_packs_request when parsing info/packs.

The fetching code lives in three methods to accomodate http-walker.c's implementation, which involves the variable repo->got_indices.

---
 http-push.c   |   59 +++++++++++++++----------------------------------------
 http-walker.c |   59 ++++++++++++++++++------------------------------------
 http.c        |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 http.h        |   18 ++++++++++++++++
 4 files changed, 115 insertions(+), 82 deletions(-)
http-push.c +15 -42
index 2694d16..3ded6ec 100644
@@ -895,67 +895,40 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)
 	return 1;
 }
 
-static int fetch_indices(void)
+static void fetch_indices_request_callback(unsigned char *sha1,
+	void *callback_data)
 {
-	unsigned char sha1[20];
-	char *url;
-	struct strbuf buffer = STRBUF_INIT;
-	char *data;
-	int i = 0;
+	fetch_http_pack_index(&repo->packs, sha1, repo->url);
+}
 
-	struct active_request_slot *slot;
+static int fetch_indices(void)
+{
 	struct slot_results results;
+	struct http_info_packs_request *req;
 
 	if (push_verbosely)
 		fprintf(stderr, "Getting pack list\n");
 
-	url = xmalloc(strlen(repo->url) + 20);
-	sprintf(url, "%sobjects/info/packs", repo->url);
+	req = new_http_info_packs_request(repo->url);
+	req->slot->results = &results;
+	req->callback_func = fetch_indices_request_callback;
 
-	slot = get_active_slot();
-	slot->results = &results;
-	curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
-	curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
-	curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
-	if (start_active_slot(slot)) {
-		run_active_slot(slot);
+	if (start_active_slot(req->slot)) {
+		run_active_slot(req->slot);
 		if (results.curl_result != CURLE_OK) {
-			strbuf_release(&buffer);
-			free(url);
+			release_http_info_packs_request(req);
 			if (results.http_code == 404)
 				return 0;
 			else
 				return error("%s", curl_errorstr);
 		}
 	} else {
-		strbuf_release(&buffer);
-		free(url);
+		release_http_info_packs_request(req);
 		return error("Unable to start request");
 	}
-	free(url);
 
-	data = buffer.buf;
-	while (i < buffer.len) {
-		switch (data[i]) {
-		case 'P':
-			i++;
-			if (i + 52 < buffer.len &&
-			    !prefixcmp(data + i, " pack-") &&
-			    !prefixcmp(data + i + 46, ".pack\n")) {
-				get_sha1_hex(data + i + 6, sha1);
-				fetch_http_pack_index(&repo->packs, sha1, repo->url);
-				i += 51;
-				break;
-			}
-		default:
-			while (data[i] != '\n')
-				i++;
-		}
-		i++;
-	}
+	finish_http_info_packs_request(req);
 
-	strbuf_release(&buffer);
 	return 0;
 }
 
http-walker.c +20 -39

84 lines not on this page · raw message

http.c +61 -0

64 lines not on this page · raw message

http.h +18 -0
index c72bcc1..c4b1204 100644
@@ -137,4 +137,22 @@ extern struct http_pack_request *new_http_pack_request(
 extern int finish_http_pack_request(struct http_pack_request *preq);
 extern void release_http_pack_request(struct http_pack_request *preq);
 
+/* Helpers for fetching objects/info/packs */
+struct http_info_packs_request
+{
+	char *url;
+	struct strbuf *buffer;
+	struct active_request_slot *slot;
+
+	void *callback_data;
+	void (*callback_func)(unsigned char *sha1, void *callback_data);
+};
+
+extern struct http_info_packs_request *new_http_info_packs_request(
+	const char *base_url);
+extern void finish_http_info_packs_request(
+	struct http_info_packs_request *req);
+extern void release_http_info_packs_request(
+	struct http_info_packs_request *req);
+
 #endif /* HTTP_H */
Signature · 1 line
1.6.3.rc0