From 817d14a87a90481b60a1c28f79f7d838d985a45c Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sat, 31 Jan 2009 07:51:55 +0800 Subject: http-push: refactor request url creation Introduce two helper functions append_remote_object_url() and get_remote_object_url() and use them to remove various places that allocate and format the URL by hand. These functions generate a URL that point at the fan-out directory inside the remote object store (e.g. http://host/path/to/repo/objects/a1/) or at an individual loose object file. Signed-off-by: Tay Ray Chuan Signed-off-by: Junio C Hamano --- http-push.c | 64 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) (limited to 'http-push.c') diff --git a/http-push.c b/http-push.c index 59037df502..ba5cc32584 100644 --- a/http-push.c +++ b/http-push.c @@ -209,6 +209,22 @@ static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum d return dav_headers; } +static void append_remote_object_url(struct strbuf *buf, const char *url, + const char *hex, + int only_two_digit_prefix) +{ + strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex); + if (!only_two_digit_prefix) + strbuf_addf(buf, "%s", hex+2); +} + +static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix) +{ + struct strbuf buf = STRBUF_INIT; + append_remote_object_url(&buf, url, hex, only_two_digit_prefix); + return strbuf_detach(&buf, NULL); +} + static void finish_request(struct transfer_request *request); static void release_request(struct transfer_request *request); @@ -255,7 +271,6 @@ static void start_fetch_loose(struct transfer_request *request) char *filename; char prevfile[PATH_MAX]; char *url; - char *posn; int prevlocal; unsigned char prev_buf[PREV_BUF_SIZE]; ssize_t prev_read = 0; @@ -305,17 +320,8 @@ static void start_fetch_loose(struct transfer_request *request) git_SHA1_Init(&request->c); - url = xmalloc(strlen(remote->url) + 50); - request->url = xmalloc(strlen(remote->url) + 50); - strcpy(url, remote->url); - posn = url + strlen(remote->url); - strcpy(posn, "objects/"); - posn += 8; - memcpy(posn, hex, 2); - posn += 2; - *(posn++) = '/'; - strcpy(posn, hex + 2); - strcpy(request->url, url); + url = get_remote_object_url(remote->url, hex, 0); + request->url = xstrdup(url); /* If a previous temp file is present, process what was already fetched. */ @@ -388,16 +394,8 @@ static void start_mkcol(struct transfer_request *request) { char *hex = sha1_to_hex(request->obj->sha1); struct active_request_slot *slot; - char *posn; - request->url = xmalloc(strlen(remote->url) + 13); - strcpy(request->url, remote->url); - posn = request->url + strlen(remote->url); - strcpy(posn, "objects/"); - posn += 8; - memcpy(posn, hex, 2); - posn += 2; - strcpy(posn, "/"); + request->url = get_remote_object_url(remote->url, hex, 1); slot = get_active_slot(); slot->callback_func = process_response; @@ -512,7 +510,7 @@ static void start_put(struct transfer_request *request) { char *hex = sha1_to_hex(request->obj->sha1); struct active_request_slot *slot; - char *posn; + struct strbuf buf = STRBUF_INIT; enum object_type type; char hdr[50]; void *unpacked; @@ -551,21 +549,13 @@ static void start_put(struct transfer_request *request) request->buffer.buf.len = stream.total_out; - request->url = xmalloc(strlen(remote->url) + - strlen(request->lock->token) + 51); - strcpy(request->url, remote->url); - posn = request->url + strlen(remote->url); - strcpy(posn, "objects/"); - posn += 8; - memcpy(posn, hex, 2); - posn += 2; - *(posn++) = '/'; - strcpy(posn, hex + 2); - request->dest = xmalloc(strlen(request->url) + 14); - sprintf(request->dest, "Destination: %s", request->url); - posn += 38; - *(posn++) = '_'; - strcpy(posn, request->lock->token); + strbuf_addstr(&buf, "Destination: "); + append_remote_object_url(&buf, remote->url, hex, 0); + request->dest = strbuf_detach(&buf, NULL); + + append_remote_object_url(&buf, remote->url, hex, 0); + strbuf_addstr(&buf, request->lock->token); + request->url = strbuf_detach(&buf, NULL); slot = get_active_slot(); slot->callback_func = process_response; -- cgit v1.2.3 From dcdb3335c13c018cbc75b49c170080817c123834 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 2 Feb 2009 22:24:40 -0800 Subject: http-push.c: get_remote_object_url() is only used under USE_CURL_MULTI Otherwise -Wunused-function (which is implied by -Wall) triggers. Signed-off-by: Junio C Hamano --- http-push.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'http-push.c') diff --git a/http-push.c b/http-push.c index ba5cc32584..203c0750fe 100644 --- a/http-push.c +++ b/http-push.c @@ -218,13 +218,6 @@ static void append_remote_object_url(struct strbuf *buf, const char *url, strbuf_addf(buf, "%s", hex+2); } -static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix) -{ - struct strbuf buf = STRBUF_INIT; - append_remote_object_url(&buf, url, hex, only_two_digit_prefix); - return strbuf_detach(&buf, NULL); -} - static void finish_request(struct transfer_request *request); static void release_request(struct transfer_request *request); @@ -237,6 +230,14 @@ static void process_response(void *callback_data) } #ifdef USE_CURL_MULTI + +static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix) +{ + struct strbuf buf = STRBUF_INIT; + append_remote_object_url(&buf, url, hex, only_two_digit_prefix); + return strbuf_detach(&buf, NULL); +} + static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, void *data) { -- cgit v1.2.3 From 223bd9317643f8ccef3447583944e58c13c10db9 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Tue, 3 Feb 2009 21:07:26 +0800 Subject: http-push: add back underscore separator before lock token 817d14a (http-push: refactor request url creation, 2009-01-31) removed the underscore separator between the object path and the appended lock token. This patch adds it back. This would be keeping in line with the aforementioned patch's objective of refactoring, without changing the behaviour and effect, of the code. This would also be useful for testing if the lock token has been indeed appended to the object url. Signed-off-by: Tay Ray Chuan Signed-off-by: Junio C Hamano --- http-push.c | 1 + 1 file changed, 1 insertion(+) (limited to 'http-push.c') diff --git a/http-push.c b/http-push.c index 203c0750fe..4b941f4d3e 100644 --- a/http-push.c +++ b/http-push.c @@ -555,6 +555,7 @@ static void start_put(struct transfer_request *request) request->dest = strbuf_detach(&buf, NULL); append_remote_object_url(&buf, remote->url, hex, 0); + strbuf_addstr(&buf, "_"); strbuf_addstr(&buf, request->lock->token); request->url = strbuf_detach(&buf, NULL); -- cgit v1.2.3 From 2d20b7ebf672c28b18da4558d96f100613e4e4ca Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Tue, 3 Feb 2009 20:39:00 +0800 Subject: http-push: wrap signature of get_remote_object_url The signature of get_remote_object_url stands at 96 characters (as pointed out by Dscho); this patch wraps it so that it conforms to the 80 characters guideline. Signed-off-by: Junio C Hamano --- http-push.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'http-push.c') diff --git a/http-push.c b/http-push.c index 4b941f4d3e..bb00728f14 100644 --- a/http-push.c +++ b/http-push.c @@ -231,7 +231,8 @@ static void process_response(void *callback_data) #ifdef USE_CURL_MULTI -static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix) +static char *get_remote_object_url(const char *url, const char *hex, + int only_two_digit_prefix) { struct strbuf buf = STRBUF_INIT; append_remote_object_url(&buf, url, hex, only_two_digit_prefix); -- cgit v1.2.3