提交 028c2976 编写于 作者: M Mike Hommey 提交者: Junio C Hamano

Use strbuf in http code

Also, replace whitespaces with tabs in some places
Signed-off-by: NMike Hommey <mh@glandium.org>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 e8dc37e0
...@@ -494,10 +494,11 @@ static void start_put(struct transfer_request *request) ...@@ -494,10 +494,11 @@ static void start_put(struct transfer_request *request)
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
deflateInit(&stream, zlib_compression_level); deflateInit(&stream, zlib_compression_level);
size = deflateBound(&stream, len + hdrlen); size = deflateBound(&stream, len + hdrlen);
request->buffer.buffer = xmalloc(size); strbuf_init(&request->buffer.buf, size);
request->buffer.posn = 0;
/* Compress it */ /* Compress it */
stream.next_out = request->buffer.buffer; stream.next_out = (unsigned char *)request->buffer.buf.buf;
stream.avail_out = size; stream.avail_out = size;
/* First header.. */ /* First header.. */
...@@ -514,8 +515,7 @@ static void start_put(struct transfer_request *request) ...@@ -514,8 +515,7 @@ static void start_put(struct transfer_request *request)
deflateEnd(&stream); deflateEnd(&stream);
free(unpacked); free(unpacked);
request->buffer.size = stream.total_out; request->buffer.buf.len = stream.total_out;
request->buffer.posn = 0;
request->url = xmalloc(strlen(remote->url) + request->url = xmalloc(strlen(remote->url) +
strlen(request->lock->token) + 51); strlen(request->lock->token) + 51);
...@@ -537,7 +537,7 @@ static void start_put(struct transfer_request *request) ...@@ -537,7 +537,7 @@ static void start_put(struct transfer_request *request)
slot->callback_func = process_response; slot->callback_func = process_response;
slot->callback_data = request; slot->callback_data = request;
curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer); curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size); curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
...@@ -1007,18 +1007,13 @@ static int fetch_indices(void) ...@@ -1007,18 +1007,13 @@ static int fetch_indices(void)
{ {
unsigned char sha1[20]; unsigned char sha1[20];
char *url; char *url;
struct buffer buffer; struct strbuf buffer = STRBUF_INIT;
char *data; char *data;
int i = 0; int i = 0;
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
data = xcalloc(1, 4096);
buffer.size = 4096;
buffer.posn = 0;
buffer.buffer = data;
if (push_verbosely) if (push_verbosely)
fprintf(stderr, "Getting pack list\n"); fprintf(stderr, "Getting pack list\n");
...@@ -1034,7 +1029,7 @@ static int fetch_indices(void) ...@@ -1034,7 +1029,7 @@ static int fetch_indices(void)
if (start_active_slot(slot)) { if (start_active_slot(slot)) {
run_active_slot(slot); run_active_slot(slot);
if (results.curl_result != CURLE_OK) { if (results.curl_result != CURLE_OK) {
free(buffer.buffer); strbuf_release(&buffer);
free(url); free(url);
if (results.http_code == 404) if (results.http_code == 404)
return 0; return 0;
...@@ -1042,18 +1037,18 @@ static int fetch_indices(void) ...@@ -1042,18 +1037,18 @@ static int fetch_indices(void)
return error("%s", curl_errorstr); return error("%s", curl_errorstr);
} }
} else { } else {
free(buffer.buffer); strbuf_release(&buffer);
free(url); free(url);
return error("Unable to start request"); return error("Unable to start request");
} }
free(url); free(url);
data = buffer.buffer; data = buffer.buf;
while (i < buffer.posn) { while (i < buffer.len) {
switch (data[i]) { switch (data[i]) {
case 'P': case 'P':
i++; i++;
if (i + 52 < buffer.posn && if (i + 52 < buffer.len &&
!prefixcmp(data + i, " pack-") && !prefixcmp(data + i, " pack-") &&
!prefixcmp(data + i + 46, ".pack\n")) { !prefixcmp(data + i + 46, ".pack\n")) {
get_sha1_hex(data + i + 6, sha1); get_sha1_hex(data + i + 6, sha1);
...@@ -1068,7 +1063,7 @@ static int fetch_indices(void) ...@@ -1068,7 +1063,7 @@ static int fetch_indices(void)
i++; i++;
} }
free(buffer.buffer); strbuf_release(&buffer);
return 0; return 0;
} }
...@@ -1119,16 +1114,11 @@ static char *quote_ref_url(const char *base, const char *ref) ...@@ -1119,16 +1114,11 @@ static char *quote_ref_url(const char *base, const char *ref)
int fetch_ref(char *ref, unsigned char *sha1) int fetch_ref(char *ref, unsigned char *sha1)
{ {
char *url; char *url;
char hex[42]; struct strbuf buffer = STRBUF_INIT;
struct buffer buffer;
char *base = remote->url; char *base = remote->url;
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
buffer.size = 41;
buffer.posn = 0;
buffer.buffer = hex;
hex[41] = '\0';
url = quote_ref_url(base, ref); url = quote_ref_url(base, ref);
slot = get_active_slot(); slot = get_active_slot();
...@@ -1148,10 +1138,10 @@ int fetch_ref(char *ref, unsigned char *sha1) ...@@ -1148,10 +1138,10 @@ int fetch_ref(char *ref, unsigned char *sha1)
return error("Unable to start request"); return error("Unable to start request");
} }
if (buffer.posn != 41) strbuf_rtrim(&buffer);
if (buffer.len != 40)
return 1; return 1;
hex[40] = '\0'; return get_sha1_hex(buffer.buf, sha1);
return get_sha1_hex(hex, sha1);
} }
static void one_remote_object(const char *hex) static void one_remote_object(const char *hex)
...@@ -1274,10 +1264,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout) ...@@ -1274,10 +1264,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
{ {
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
struct buffer out_buffer; struct buffer out_buffer = { STRBUF_INIT, 0 };
struct buffer in_buffer; struct strbuf in_buffer = STRBUF_INIT;
char *out_data;
char *in_data;
char *url; char *url;
char *ep; char *ep;
char timeout_header[25]; char timeout_header[25];
...@@ -1317,16 +1305,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout) ...@@ -1317,16 +1305,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
ep = strchr(ep + 1, '/'); ep = strchr(ep + 1, '/');
} }
out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2; strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
out_data = xmalloc(out_buffer.size + 1);
snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email);
out_buffer.posn = 0;
out_buffer.buffer = out_data;
in_buffer.size = 4096;
in_data = xmalloc(in_buffer.size);
in_buffer.posn = 0;
in_buffer.buffer = in_data;
sprintf(timeout_header, "Timeout: Second-%ld", timeout); sprintf(timeout_header, "Timeout: Second-%ld", timeout);
dav_headers = curl_slist_append(dav_headers, timeout_header); dav_headers = curl_slist_append(dav_headers, timeout_header);
...@@ -1335,7 +1314,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout) ...@@ -1335,7 +1314,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
...@@ -1361,8 +1340,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout) ...@@ -1361,8 +1340,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
XML_SetElementHandler(parser, xml_start_tag, XML_SetElementHandler(parser, xml_start_tag,
xml_end_tag); xml_end_tag);
XML_SetCharacterDataHandler(parser, xml_cdata); XML_SetCharacterDataHandler(parser, xml_cdata);
result = XML_Parse(parser, in_buffer.buffer, result = XML_Parse(parser, in_buffer.buf,
in_buffer.posn, 1); in_buffer.len, 1);
free(ctx.name); free(ctx.name);
if (result != XML_STATUS_OK) { if (result != XML_STATUS_OK) {
fprintf(stderr, "XML error: %s\n", fprintf(stderr, "XML error: %s\n",
...@@ -1377,8 +1356,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout) ...@@ -1377,8 +1356,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
} }
curl_slist_free_all(dav_headers); curl_slist_free_all(dav_headers);
free(out_data); strbuf_release(&out_buffer.buf);
free(in_data); strbuf_release(&in_buffer);
if (lock->token == NULL || lock->timeout <= 0) { if (lock->token == NULL || lock->timeout <= 0) {
if (lock->token != NULL) if (lock->token != NULL)
...@@ -1529,10 +1508,8 @@ static void remote_ls(const char *path, int flags, ...@@ -1529,10 +1508,8 @@ static void remote_ls(const char *path, int flags,
char *url = xmalloc(strlen(remote->url) + strlen(path) + 1); char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
struct buffer in_buffer; struct strbuf in_buffer = STRBUF_INIT;
struct buffer out_buffer; struct buffer out_buffer = { STRBUF_INIT, 0 };
char *in_data;
char *out_data;
struct curl_slist *dav_headers = NULL; struct curl_slist *dav_headers = NULL;
struct xml_ctx ctx; struct xml_ctx ctx;
struct remote_ls_ctx ls; struct remote_ls_ctx ls;
...@@ -1546,16 +1523,7 @@ static void remote_ls(const char *path, int flags, ...@@ -1546,16 +1523,7 @@ static void remote_ls(const char *path, int flags,
sprintf(url, "%s%s", remote->url, path); sprintf(url, "%s%s", remote->url, path);
out_buffer.size = strlen(PROPFIND_ALL_REQUEST); strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
out_data = xmalloc(out_buffer.size + 1);
snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
out_buffer.posn = 0;
out_buffer.buffer = out_data;
in_buffer.size = 4096;
in_data = xmalloc(in_buffer.size);
in_buffer.posn = 0;
in_buffer.buffer = in_data;
dav_headers = curl_slist_append(dav_headers, "Depth: 1"); dav_headers = curl_slist_append(dav_headers, "Depth: 1");
dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
...@@ -1563,7 +1531,7 @@ static void remote_ls(const char *path, int flags, ...@@ -1563,7 +1531,7 @@ static void remote_ls(const char *path, int flags,
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
...@@ -1586,8 +1554,8 @@ static void remote_ls(const char *path, int flags, ...@@ -1586,8 +1554,8 @@ static void remote_ls(const char *path, int flags,
XML_SetElementHandler(parser, xml_start_tag, XML_SetElementHandler(parser, xml_start_tag,
xml_end_tag); xml_end_tag);
XML_SetCharacterDataHandler(parser, xml_cdata); XML_SetCharacterDataHandler(parser, xml_cdata);
result = XML_Parse(parser, in_buffer.buffer, result = XML_Parse(parser, in_buffer.buf,
in_buffer.posn, 1); in_buffer.len, 1);
free(ctx.name); free(ctx.name);
if (result != XML_STATUS_OK) { if (result != XML_STATUS_OK) {
...@@ -1603,8 +1571,8 @@ static void remote_ls(const char *path, int flags, ...@@ -1603,8 +1571,8 @@ static void remote_ls(const char *path, int flags,
free(ls.path); free(ls.path);
free(url); free(url);
free(out_data); strbuf_release(&out_buffer.buf);
free(in_buffer.buffer); strbuf_release(&in_buffer);
curl_slist_free_all(dav_headers); curl_slist_free_all(dav_headers);
} }
...@@ -1625,27 +1593,13 @@ static int locking_available(void) ...@@ -1625,27 +1593,13 @@ static int locking_available(void)
{ {
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
struct buffer in_buffer; struct strbuf in_buffer = STRBUF_INIT;
struct buffer out_buffer; struct buffer out_buffer = { STRBUF_INIT, 0 };
char *in_data;
char *out_data;
struct curl_slist *dav_headers = NULL; struct curl_slist *dav_headers = NULL;
struct xml_ctx ctx; struct xml_ctx ctx;
int lock_flags = 0; int lock_flags = 0;
out_buffer.size = strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
strlen(PROPFIND_SUPPORTEDLOCK_REQUEST) +
strlen(remote->url) - 2;
out_data = xmalloc(out_buffer.size + 1);
snprintf(out_data, out_buffer.size + 1,
PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
out_buffer.posn = 0;
out_buffer.buffer = out_data;
in_buffer.size = 4096;
in_data = xmalloc(in_buffer.size);
in_buffer.posn = 0;
in_buffer.buffer = in_data;
dav_headers = curl_slist_append(dav_headers, "Depth: 0"); dav_headers = curl_slist_append(dav_headers, "Depth: 0");
dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
...@@ -1653,7 +1607,7 @@ static int locking_available(void) ...@@ -1653,7 +1607,7 @@ static int locking_available(void)
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
...@@ -1675,8 +1629,8 @@ static int locking_available(void) ...@@ -1675,8 +1629,8 @@ static int locking_available(void)
XML_SetUserData(parser, &ctx); XML_SetUserData(parser, &ctx);
XML_SetElementHandler(parser, xml_start_tag, XML_SetElementHandler(parser, xml_start_tag,
xml_end_tag); xml_end_tag);
result = XML_Parse(parser, in_buffer.buffer, result = XML_Parse(parser, in_buffer.buf,
in_buffer.posn, 1); in_buffer.len, 1);
free(ctx.name); free(ctx.name);
if (result != XML_STATUS_OK) { if (result != XML_STATUS_OK) {
...@@ -1691,8 +1645,8 @@ static int locking_available(void) ...@@ -1691,8 +1645,8 @@ static int locking_available(void)
fprintf(stderr, "Unable to start PROPFIND request\n"); fprintf(stderr, "Unable to start PROPFIND request\n");
} }
free(out_data); strbuf_release(&out_buffer.buf);
free(in_buffer.buffer); strbuf_release(&in_buffer);
curl_slist_free_all(dav_headers); curl_slist_free_all(dav_headers);
return lock_flags; return lock_flags;
...@@ -1810,30 +1764,20 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock) ...@@ -1810,30 +1764,20 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
{ {
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
char *out_data;
char *if_header; char *if_header;
struct buffer out_buffer; struct buffer out_buffer = { STRBUF_INIT, 0 };
struct curl_slist *dav_headers = NULL; struct curl_slist *dav_headers = NULL;
int i;
if_header = xmalloc(strlen(lock->token) + 25); if_header = xmalloc(strlen(lock->token) + 25);
sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token); sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
dav_headers = curl_slist_append(dav_headers, if_header); dav_headers = curl_slist_append(dav_headers, if_header);
out_buffer.size = 41; strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
out_data = xmalloc(out_buffer.size + 1);
i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
if (i != out_buffer.size) {
fprintf(stderr, "Unable to initialize PUT request body\n");
return 0;
}
out_buffer.posn = 0;
out_buffer.buffer = out_data;
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
...@@ -1844,7 +1788,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock) ...@@ -1844,7 +1788,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
if (start_active_slot(slot)) { if (start_active_slot(slot)) {
run_active_slot(slot); run_active_slot(slot);
free(out_data); strbuf_release(&out_buffer.buf);
free(if_header); free(if_header);
if (results.curl_result != CURLE_OK) { if (results.curl_result != CURLE_OK) {
fprintf(stderr, fprintf(stderr,
...@@ -1854,7 +1798,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock) ...@@ -1854,7 +1798,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
return 0; return 0;
} }
} else { } else {
free(out_data); strbuf_release(&out_buffer.buf);
free(if_header); free(if_header);
fprintf(stderr, "Unable to start PUT request\n"); fprintf(stderr, "Unable to start PUT request\n");
return 0; return 0;
...@@ -2011,7 +1955,7 @@ static void mark_edges_uninteresting(struct commit_list *list) ...@@ -2011,7 +1955,7 @@ static void mark_edges_uninteresting(struct commit_list *list)
static void add_remote_info_ref(struct remote_ls_ctx *ls) static void add_remote_info_ref(struct remote_ls_ctx *ls)
{ {
struct buffer *buf = (struct buffer *)ls->userData; struct strbuf *buf = (struct strbuf *)ls->userData;
unsigned char remote_sha1[20]; unsigned char remote_sha1[20];
struct object *o; struct object *o;
int len; int len;
...@@ -2056,17 +2000,14 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls) ...@@ -2056,17 +2000,14 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
static void update_remote_info_refs(struct remote_lock *lock) static void update_remote_info_refs(struct remote_lock *lock)
{ {
struct buffer buffer; struct buffer buffer = { STRBUF_INIT, 0 };
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
char *if_header; char *if_header;
struct curl_slist *dav_headers = NULL; struct curl_slist *dav_headers = NULL;
buffer.buffer = xcalloc(1, 4096);
buffer.size = 4096;
buffer.posn = 0;
remote_ls("refs/", (PROCESS_FILES | RECURSIVE), remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
add_remote_info_ref, &buffer); add_remote_info_ref, &buffer.buf);
if (!aborted) { if (!aborted) {
if_header = xmalloc(strlen(lock->token) + 25); if_header = xmalloc(strlen(lock->token) + 25);
sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token); sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
...@@ -2075,7 +2016,7 @@ static void update_remote_info_refs(struct remote_lock *lock) ...@@ -2075,7 +2016,7 @@ static void update_remote_info_refs(struct remote_lock *lock)
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer); curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.posn); curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
...@@ -2084,8 +2025,6 @@ static void update_remote_info_refs(struct remote_lock *lock) ...@@ -2084,8 +2025,6 @@ static void update_remote_info_refs(struct remote_lock *lock)
curl_easy_setopt(slot->curl, CURLOPT_PUT, 1); curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url); curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
buffer.posn = 0;
if (start_active_slot(slot)) { if (start_active_slot(slot)) {
run_active_slot(slot); run_active_slot(slot);
if (results.curl_result != CURLE_OK) { if (results.curl_result != CURLE_OK) {
...@@ -2096,7 +2035,7 @@ static void update_remote_info_refs(struct remote_lock *lock) ...@@ -2096,7 +2035,7 @@ static void update_remote_info_refs(struct remote_lock *lock)
} }
free(if_header); free(if_header);
} }
free(buffer.buffer); strbuf_release(&buffer.buf);
} }
static int remote_exists(const char *path) static int remote_exists(const char *path)
...@@ -2107,12 +2046,12 @@ static int remote_exists(const char *path) ...@@ -2107,12 +2046,12 @@ static int remote_exists(const char *path)
sprintf(url, "%s%s", remote->url, path); sprintf(url, "%s%s", remote->url, path);
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_URL, url); curl_easy_setopt(slot->curl, CURLOPT_URL, url);
curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1); curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
if (start_active_slot(slot)) { if (start_active_slot(slot)) {
run_active_slot(slot); run_active_slot(slot);
free(url); free(url);
if (results.http_code == 404) if (results.http_code == 404)
...@@ -2132,17 +2071,13 @@ static int remote_exists(const char *path) ...@@ -2132,17 +2071,13 @@ static int remote_exists(const char *path)
static void fetch_symref(const char *path, char **symref, unsigned char *sha1) static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
{ {
char *url; char *url;
struct buffer buffer; struct strbuf buffer = STRBUF_INIT;
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
url = xmalloc(strlen(remote->url) + strlen(path) + 1); url = xmalloc(strlen(remote->url) + strlen(path) + 1);
sprintf(url, "%s%s", remote->url, path); sprintf(url, "%s%s", remote->url, path);
buffer.size = 4096;
buffer.posn = 0;
buffer.buffer = xmalloc(buffer.size);
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
...@@ -2165,17 +2100,17 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1) ...@@ -2165,17 +2100,17 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
*symref = NULL; *symref = NULL;
hashclr(sha1); hashclr(sha1);
if (buffer.posn == 0) if (buffer.len == 0)
return; return;
/* If it's a symref, set the refname; otherwise try for a sha1 */ /* If it's a symref, set the refname; otherwise try for a sha1 */
if (!prefixcmp((char *)buffer.buffer, "ref: ")) { if (!prefixcmp((char *)buffer.buf, "ref: ")) {
*symref = xmemdupz((char *)buffer.buffer + 5, buffer.posn - 6); *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
} else { } else {
get_sha1_hex(buffer.buffer, sha1); get_sha1_hex(buffer.buf, sha1);
} }
free(buffer.buffer); strbuf_release(&buffer);
} }
static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1) static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
......
...@@ -48,7 +48,7 @@ struct alternates_request { ...@@ -48,7 +48,7 @@ struct alternates_request {
struct walker *walker; struct walker *walker;
const char *base; const char *base;
char *url; char *url;
struct buffer *buffer; struct strbuf *buffer;
struct active_request_slot *slot; struct active_request_slot *slot;
int http_specific; int http_specific;
}; };
...@@ -462,7 +462,7 @@ static void process_alternates_response(void *callback_data) ...@@ -462,7 +462,7 @@ static void process_alternates_response(void *callback_data)
if (alt_req->http_specific) { if (alt_req->http_specific) {
if (slot->curl_result != CURLE_OK || if (slot->curl_result != CURLE_OK ||
!alt_req->buffer->posn) { !alt_req->buffer->len) {
/* Try reusing the slot to get non-http alternates */ /* Try reusing the slot to get non-http alternates */
alt_req->http_specific = 0; alt_req->http_specific = 0;
...@@ -490,12 +490,12 @@ static void process_alternates_response(void *callback_data) ...@@ -490,12 +490,12 @@ static void process_alternates_response(void *callback_data)
} }
fwrite_buffer(&null_byte, 1, 1, alt_req->buffer); fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
alt_req->buffer->posn--; alt_req->buffer->len--;
data = alt_req->buffer->buffer; data = alt_req->buffer->buf;
while (i < alt_req->buffer->posn) { while (i < alt_req->buffer->len) {
int posn = i; int posn = i;
while (posn < alt_req->buffer->posn && data[posn] != '\n') while (posn < alt_req->buffer->len && data[posn] != '\n')
posn++; posn++;
if (data[posn] == '\n') { if (data[posn] == '\n') {
int okay = 0; int okay = 0;
...@@ -583,9 +583,8 @@ static void process_alternates_response(void *callback_data) ...@@ -583,9 +583,8 @@ static void process_alternates_response(void *callback_data)
static void fetch_alternates(struct walker *walker, const char *base) static void fetch_alternates(struct walker *walker, const char *base)
{ {
struct buffer buffer; struct strbuf buffer = STRBUF_INIT;
char *url; char *url;
char *data;
struct active_request_slot *slot; struct active_request_slot *slot;
struct alternates_request alt_req; struct alternates_request alt_req;
struct walker_data *cdata = walker->data; struct walker_data *cdata = walker->data;
...@@ -606,11 +605,6 @@ static void fetch_alternates(struct walker *walker, const char *base) ...@@ -606,11 +605,6 @@ static void fetch_alternates(struct walker *walker, const char *base)
/* Start the fetch */ /* Start the fetch */
cdata->got_alternates = 0; cdata->got_alternates = 0;
data = xmalloc(4096);
buffer.size = 4096;
buffer.posn = 0;
buffer.buffer = data;
if (walker->get_verbosely) if (walker->get_verbosely)
fprintf(stderr, "Getting alternates list for %s\n", base); fprintf(stderr, "Getting alternates list for %s\n", base);
...@@ -639,7 +633,7 @@ static void fetch_alternates(struct walker *walker, const char *base) ...@@ -639,7 +633,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
else else
cdata->got_alternates = -1; cdata->got_alternates = -1;
free(data); strbuf_release(&buffer);
free(url); free(url);
} }
...@@ -647,7 +641,7 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo) ...@@ -647,7 +641,7 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
{ {
unsigned char sha1[20]; unsigned char sha1[20];
char *url; char *url;
struct buffer buffer; struct strbuf buffer = STRBUF_INIT;
char *data; char *data;
int i = 0; int i = 0;
...@@ -657,11 +651,6 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo) ...@@ -657,11 +651,6 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
if (repo->got_indices) if (repo->got_indices)
return 0; return 0;
data = xmalloc(4096);
buffer.size = 4096;
buffer.posn = 0;
buffer.buffer = data;
if (walker->get_verbosely) if (walker->get_verbosely)
fprintf(stderr, "Getting pack list for %s\n", repo->base); fprintf(stderr, "Getting pack list for %s\n", repo->base);
...@@ -677,28 +666,27 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo) ...@@ -677,28 +666,27 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
if (start_active_slot(slot)) { if (start_active_slot(slot)) {
run_active_slot(slot); run_active_slot(slot);
if (results.curl_result != CURLE_OK) { if (results.curl_result != CURLE_OK) {
strbuf_release(&buffer);
if (missing_target(&results)) { if (missing_target(&results)) {
repo->got_indices = 1; repo->got_indices = 1;
free(buffer.buffer);
return 0; return 0;
} else { } else {
repo->got_indices = 0; repo->got_indices = 0;
free(buffer.buffer);
return error("%s", curl_errorstr); return error("%s", curl_errorstr);
} }
} }
} else { } else {
repo->got_indices = 0; repo->got_indices = 0;
free(buffer.buffer); strbuf_release(&buffer);
return error("Unable to start request"); return error("Unable to start request");
} }
data = buffer.buffer; data = buffer.buf;
while (i < buffer.posn) { while (i < buffer.len) {
switch (data[i]) { switch (data[i]) {
case 'P': case 'P':
i++; i++;
if (i + 52 <= buffer.posn && if (i + 52 <= buffer.len &&
!prefixcmp(data + i, " pack-") && !prefixcmp(data + i, " pack-") &&
!prefixcmp(data + i + 46, ".pack\n")) { !prefixcmp(data + i + 46, ".pack\n")) {
get_sha1_hex(data + i + 6, sha1); get_sha1_hex(data + i + 6, sha1);
...@@ -707,13 +695,13 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo) ...@@ -707,13 +695,13 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
break; break;
} }
default: default:
while (i < buffer.posn && data[i] != '\n') while (i < buffer.len && data[i] != '\n')
i++; i++;
} }
i++; i++;
} }
free(buffer.buffer); strbuf_release(&buffer);
repo->got_indices = 1; repo->got_indices = 1;
return 0; return 0;
} }
...@@ -945,17 +933,12 @@ static char *quote_ref_url(const char *base, const char *ref) ...@@ -945,17 +933,12 @@ static char *quote_ref_url(const char *base, const char *ref)
static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1) static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
{ {
char *url; char *url;
char hex[42]; struct strbuf buffer = STRBUF_INIT;
struct buffer buffer;
struct walker_data *data = walker->data; struct walker_data *data = walker->data;
const char *base = data->alt->base; const char *base = data->alt->base;
struct active_request_slot *slot; struct active_request_slot *slot;
struct slot_results results; struct slot_results results;
buffer.size = 41;
buffer.posn = 0;
buffer.buffer = hex;
hex[41] = '\0';
url = quote_ref_url(base, ref); url = quote_ref_url(base, ref);
slot = get_active_slot(); slot = get_active_slot();
...@@ -973,10 +956,10 @@ static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1) ...@@ -973,10 +956,10 @@ static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
return error("Unable to start request"); return error("Unable to start request");
} }
if (buffer.posn != 41) strbuf_rtrim(&buffer);
if (buffer.len != 40)
return 1; return 1;
hex[40] = '\0'; return get_sha1_hex(buffer.buf, sha1);
return get_sha1_hex(hex, sha1);
} }
static void cleanup(struct walker *walker) static void cleanup(struct walker *walker)
......
...@@ -34,31 +34,25 @@ size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, ...@@ -34,31 +34,25 @@ size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
struct buffer *buffer) struct buffer *buffer)
{ {
size_t size = eltsize * nmemb; size_t size = eltsize * nmemb;
if (size > buffer->size - buffer->posn) if (size > buffer->buf.len - buffer->posn)
size = buffer->size - buffer->posn; size = buffer->buf.len - buffer->posn;
memcpy(ptr, (char *) buffer->buffer + buffer->posn, size); memcpy(ptr, buffer->buf.buf + buffer->posn, size);
buffer->posn += size; buffer->posn += size;
return size; return size;
} }
size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t fwrite_buffer(const void *ptr, size_t eltsize,
size_t nmemb, struct buffer *buffer) size_t nmemb, struct strbuf *buffer)
{ {
size_t size = eltsize * nmemb; size_t size = eltsize * nmemb;
if (size > buffer->size - buffer->posn) { strbuf_add(buffer, ptr, size);
buffer->size = buffer->size * 3 / 2;
if (buffer->size < buffer->posn + size)
buffer->size = buffer->posn + size;
buffer->buffer = xrealloc(buffer->buffer, buffer->size);
}
memcpy((char *) buffer->buffer + buffer->posn, ptr, size);
buffer->posn += size;
data_received++; data_received++;
return size; return size;
} }
size_t fwrite_null(const void *ptr, size_t eltsize, size_t fwrite_null(const void *ptr, size_t eltsize,
size_t nmemb, struct buffer *buffer) size_t nmemb, struct strbuf *buffer)
{ {
data_received++; data_received++;
return eltsize * nmemb; return eltsize * nmemb;
...@@ -507,8 +501,8 @@ void run_active_slot(struct active_request_slot *slot) ...@@ -507,8 +501,8 @@ void run_active_slot(struct active_request_slot *slot)
static void closedown_active_slot(struct active_request_slot *slot) static void closedown_active_slot(struct active_request_slot *slot)
{ {
active_requests--; active_requests--;
slot->in_use = 0; slot->in_use = 0;
} }
void release_active_slot(struct active_request_slot *slot) void release_active_slot(struct active_request_slot *slot)
...@@ -529,7 +523,7 @@ void release_active_slot(struct active_request_slot *slot) ...@@ -529,7 +523,7 @@ void release_active_slot(struct active_request_slot *slot)
static void finish_active_slot(struct active_request_slot *slot) static void finish_active_slot(struct active_request_slot *slot)
{ {
closedown_active_slot(slot); closedown_active_slot(slot);
curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code); curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
if (slot->finished != NULL) if (slot->finished != NULL)
(*slot->finished) = 1; (*slot->finished) = 1;
...@@ -540,10 +534,10 @@ static void finish_active_slot(struct active_request_slot *slot) ...@@ -540,10 +534,10 @@ static void finish_active_slot(struct active_request_slot *slot)
slot->results->http_code = slot->http_code; slot->results->http_code = slot->http_code;
} }
/* Run callback if appropriate */ /* Run callback if appropriate */
if (slot->callback_func != NULL) { if (slot->callback_func != NULL) {
slot->callback_func(slot->callback_data); slot->callback_func(slot->callback_data);
} }
} }
void finish_all_active_slots(void) void finish_all_active_slots(void)
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include "strbuf.h"
#if LIBCURL_VERSION_NUM >= 0x071000 #if LIBCURL_VERSION_NUM >= 0x071000
#define USE_CURL_MULTI #define USE_CURL_MULTI
#define DEFAULT_MAX_REQUESTS 5 #define DEFAULT_MAX_REQUESTS 5
...@@ -48,18 +50,17 @@ struct active_request_slot ...@@ -48,18 +50,17 @@ struct active_request_slot
struct buffer struct buffer
{ {
size_t posn; struct strbuf buf;
size_t size; size_t posn;
void *buffer;
}; };
/* Curl request read/write callbacks */ /* Curl request read/write callbacks */
extern size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, extern size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
struct buffer *buffer); struct buffer *buffer);
extern size_t fwrite_buffer(const void *ptr, size_t eltsize, extern size_t fwrite_buffer(const void *ptr, size_t eltsize,
size_t nmemb, struct buffer *buffer); size_t nmemb, struct strbuf *buffer);
extern size_t fwrite_null(const void *ptr, size_t eltsize, extern size_t fwrite_null(const void *ptr, size_t eltsize,
size_t nmemb, struct buffer *buffer); size_t nmemb, struct strbuf *buffer);
/* Slot lifecycle functions */ /* Slot lifecycle functions */
extern struct active_request_slot *get_active_slot(void); extern struct active_request_slot *get_active_slot(void);
......
...@@ -428,7 +428,7 @@ static int curl_transport_push(struct transport *transport, int refspec_nr, cons ...@@ -428,7 +428,7 @@ static int curl_transport_push(struct transport *transport, int refspec_nr, cons
static struct ref *get_refs_via_curl(struct transport *transport) static struct ref *get_refs_via_curl(struct transport *transport)
{ {
struct buffer buffer; struct strbuf buffer = STRBUF_INIT;
char *data, *start, *mid; char *data, *start, *mid;
char *ref_name; char *ref_name;
char *refs_url; char *refs_url;
...@@ -441,11 +441,6 @@ static struct ref *get_refs_via_curl(struct transport *transport) ...@@ -441,11 +441,6 @@ static struct ref *get_refs_via_curl(struct transport *transport)
struct ref *ref = NULL; struct ref *ref = NULL;
struct ref *last_ref = NULL; struct ref *last_ref = NULL;
data = xmalloc(4096);
buffer.size = 4096;
buffer.posn = 0;
buffer.buffer = data;
refs_url = xmalloc(strlen(transport->url) + 11); refs_url = xmalloc(strlen(transport->url) + 11);
sprintf(refs_url, "%s/info/refs", transport->url); sprintf(refs_url, "%s/info/refs", transport->url);
...@@ -464,27 +459,26 @@ static struct ref *get_refs_via_curl(struct transport *transport) ...@@ -464,27 +459,26 @@ static struct ref *get_refs_via_curl(struct transport *transport)
if (start_active_slot(slot)) { if (start_active_slot(slot)) {
run_active_slot(slot); run_active_slot(slot);
if (results.curl_result != CURLE_OK) { if (results.curl_result != CURLE_OK) {
strbuf_release(&buffer);
if (missing_target(&results)) { if (missing_target(&results)) {
free(buffer.buffer);
return NULL; return NULL;
} else { } else {
free(buffer.buffer);
error("%s", curl_errorstr); error("%s", curl_errorstr);
return NULL; return NULL;
} }
} }
} else { } else {
free(buffer.buffer); strbuf_release(&buffer);
error("Unable to start request"); error("Unable to start request");
return NULL; return NULL;
} }
http_cleanup(); http_cleanup();
data = buffer.buffer; data = buffer.buf;
start = NULL; start = NULL;
mid = data; mid = data;
while (i < buffer.posn) { while (i < buffer.len) {
if (!start) if (!start)
start = &data[i]; start = &data[i];
if (data[i] == '\t') if (data[i] == '\t')
...@@ -507,7 +501,7 @@ static struct ref *get_refs_via_curl(struct transport *transport) ...@@ -507,7 +501,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)
i++; i++;
} }
free(buffer.buffer); strbuf_release(&buffer);
return refs; return refs;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册