{"thread":{"id":"17339","subject":"PATCH] http-push: refactor request url creation","startedAt":"2009-01-24T16:16:19Z","lastAt":"2009-01-24T17:03:34Z","messageCount":2,"participants":["Ray Chuan","Johannes Schindelin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"101748","messageId":"be6fef0d0901240816r25e6a9c9q2d437d413a524362@mail.gmail.com","threadId":"17339","inReplyTo":null,"subject":"PATCH] http-push: refactor request url creation","fromName":"Ray Chuan","fromEmail":"rctay89@gmail.com","sentAt":"2009-01-24T16:16:19Z","receivedAt":"2009-01-24T16:16:19Z","isPatch":false,"sender":{"key":"rctay89@gmail.com","avatar":"https://avatars.githubusercontent.com/u/61553?v=4"},"body":"Currently, functions that deal with objects on the remote repository\nhave to allocate and do strcpys to generate the URL.\n\nThis patch saves them this trouble, by providing a function that\nreturns a URL: either the object's 2-digit hex directory (eg.\n/objects/a1/) or the complete object location (eg. /objects/a1/b2).\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Tay Ray Chuan <rctay89@gmail.com>\n---\n http-push.c |   58 +++++++++++++++++++---------------------------------------\n 1 files changed, 19 insertions(+), 39 deletions(-)\n\ndiff --git a/http-push.c b/http-push.c\nindex cb5bf95..715954e 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -177,6 +177,16 @@ struct remote_ls_ctx\n \tstruct remote_ls_ctx *parent;\n };\n\n+static char *get_remote_object_url(const char *url, const char *hex,\nint only_two_digit_postfix) {\n+\tstruct strbuf buf = STRBUF_INIT;\n+\n+\tstrbuf_addf(&buf, \"%sobjects/%.*s/\", url, 2, hex);\n+\tif(!only_two_digit_postfix)\n+\t\tstrbuf_addf(&buf, \"%s\", hex+2);\n+\n+\treturn strbuf_detach(&buf, NULL);\n+}\n+\n static void finish_request(struct transfer_request *request);\n static void release_request(struct transfer_request *request);\n\n@@ -222,8 +232,6 @@ static void start_fetch_loose(struct\ntransfer_request *request)\n \tchar *hex = sha1_to_hex(request->obj->sha1);\n \tchar *filename;\n \tchar prevfile[PATH_MAX];\n-\tchar *url;\n-\tchar *posn;\n \tint prevlocal;\n \tunsigned char prev_buf[PREV_BUF_SIZE];\n \tssize_t prev_read = 0;\n@@ -273,17 +281,7 @@ static void start_fetch_loose(struct\ntransfer_request *request)\n\n \tgit_SHA1_Init(&request->c);\n\n-\turl = xmalloc(strlen(remote->url) + 50);\n-\trequest->url = xmalloc(strlen(remote->url) + 50);\n-\tstrcpy(url, remote->url);\n-\tposn = url + strlen(remote->url);\n-\tstrcpy(posn, \"objects/\");\n-\tposn += 8;\n-\tmemcpy(posn, hex, 2);\n-\tposn += 2;\n-\t*(posn++) = '/';\n-\tstrcpy(posn, hex + 2);\n-\tstrcpy(request->url, url);\n+\trequest->url = get_remote_object_url(remote->url, hex, 0);\n\n \t/* If a previous temp file is present, process what was already\n \t   fetched. */\n@@ -327,7 +325,7 @@ static void start_fetch_loose(struct\ntransfer_request *request)\n \tcurl_easy_setopt(slot->curl, CURLOPT_FILE, request);\n \tcurl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);\n \tcurl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);\n-\tcurl_easy_setopt(slot->curl, CURLOPT_URL, url);\n+\tcurl_easy_setopt(slot->curl, CURLOPT_URL, request->url);\n \tcurl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);\n\n \t/* If we have successfully processed data from a previous fetch\n@@ -356,16 +354,8 @@ static void start_mkcol(struct transfer_request *request)\n {\n \tchar *hex = sha1_to_hex(request->obj->sha1);\n \tstruct active_request_slot *slot;\n-\tchar *posn;\n\n-\trequest->url = xmalloc(strlen(remote->url) + 13);\n-\tstrcpy(request->url, remote->url);\n-\tposn = request->url + strlen(remote->url);\n-\tstrcpy(posn, \"objects/\");\n-\tposn += 8;\n-\tmemcpy(posn, hex, 2);\n-\tposn += 2;\n-\tstrcpy(posn, \"/\");\n+\trequest->url = get_remote_object_url(remote->url, hex, 1);\n\n \tslot = get_active_slot();\n \tslot->callback_func = process_response;\n@@ -480,7 +470,7 @@ static void start_put(struct transfer_request *request)\n {\n \tchar *hex = sha1_to_hex(request->obj->sha1);\n \tstruct active_request_slot *slot;\n-\tchar *posn;\n+\tstruct strbuf url_buf = STRBUF_INIT;\n \tenum object_type type;\n \tchar hdr[50];\n \tvoid *unpacked;\n@@ -519,21 +509,11 @@ static void start_put(struct transfer_request *request)\n\n \trequest->buffer.buf.len = stream.total_out;\n\n-\trequest->url = xmalloc(strlen(remote->url) +\n-\t\t\t       strlen(request->lock->token) + 51);\n-\tstrcpy(request->url, remote->url);\n-\tposn = request->url + strlen(remote->url);\n-\tstrcpy(posn, \"objects/\");\n-\tposn += 8;\n-\tmemcpy(posn, hex, 2);\n-\tposn += 2;\n-\t*(posn++) = '/';\n-\tstrcpy(posn, hex + 2);\n-\trequest->dest = xmalloc(strlen(request->url) + 14);\n-\tsprintf(request->dest, \"Destination: %s\", request->url);\n-\tposn += 38;\n-\t*(posn++) = '_';\n-\tstrcpy(posn, request->lock->token);\n+\tstrbuf_addf(&url_buf, \"Destination: %s\",\nget_remote_object_url(remote->url, hex, 0));\n+\trequest->dest = strbuf_detach(&url_buf, NULL);\n+\n+\tstrbuf_addf(&url_buf, \"%s_%s\", get_remote_object_url(remote->url,\nhex, 0), request->lock->token);\n+\trequest->url = strbuf_detach(&url_buf, NULL);\n\n \tslot = get_active_slot();\n \tslot->callback_func = process_response;\n-- \n1.6.0.4\n"},{"id":"101751","messageId":"alpine.DEB.1.00.0901241758200.13232@racer","threadId":"17339","inReplyTo":"be6fef0d0901240816r25e6a9c9q2d437d413a524362@mail.gmail.com","subject":"Re: PATCH] http-push: refactor request url creation","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-01-24T17:03:34Z","receivedAt":"2009-01-24T17:03:34Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 25 Jan 2009, Ray Chuan wrote:\n\n> Currently, functions that deal with objects on the remote repository\n> have to allocate and do strcpys to generate the URL.\n> \n> This patch saves them this trouble, by providing a function that\n> returns a URL: either the object's 2-digit hex directory (eg.\n> /objects/a1/) or the complete object location (eg. /objects/a1/b2).\n> \n> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n\nMake that an Acked-by: okay?\n\n> diff --git a/http-push.c b/http-push.c\n> index cb5bf95..715954e 100644\n> --- a/http-push.c\n> +++ b/http-push.c\n> @@ -177,6 +177,16 @@ struct remote_ls_ctx\n>  \tstruct remote_ls_ctx *parent;\n>  };\n> \n> +static char *get_remote_object_url(const char *url, const char *hex,\n> int only_two_digit_postfix) {\n\nIt is actually a prefix.\n\nCiao,\nDscho\n"}]}