remote-curl: hoist gzip buffer size to top of post_rpc

When we gzip the post data for a smart-http rpc request, we
compute the gzip body and its size inside the "use_gzip"
conditional. We keep track of the body after the conditional
ends, but not the size. Let's remember both, which will
enable us to retry failed gzip requests in a future patch.

Signed-off-by: Jeff King <peff@peff.net>
This commit is contained in:
Jeff King 2012-10-31 07:20:15 -04:00
parent 1960897ebc
commit df126e108b

View File

@ -413,6 +413,7 @@ static int post_rpc(struct rpc_state *rpc)
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
int use_gzip = rpc->gzip_request; int use_gzip = rpc->gzip_request;
char *gzip_body = NULL; char *gzip_body = NULL;
size_t gzip_size;
int err, large_request = 0; int err, large_request = 0;
/* Try to load the entire request, if we can fit it into the /* Try to load the entire request, if we can fit it into the
@ -478,19 +479,18 @@ retry:
* we can try to deflate it ourselves, this may save on. * we can try to deflate it ourselves, this may save on.
* the transfer time. * the transfer time.
*/ */
size_t size;
git_zstream stream; git_zstream stream;
int ret; int ret;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
size = git_deflate_bound(&stream, rpc->len); gzip_size = git_deflate_bound(&stream, rpc->len);
gzip_body = xmalloc(size); gzip_body = xmalloc(gzip_size);
stream.next_in = (unsigned char *)rpc->buf; stream.next_in = (unsigned char *)rpc->buf;
stream.avail_in = rpc->len; stream.avail_in = rpc->len;
stream.next_out = (unsigned char *)gzip_body; stream.next_out = (unsigned char *)gzip_body;
stream.avail_out = size; stream.avail_out = gzip_size;
ret = git_deflate(&stream, Z_FINISH); ret = git_deflate(&stream, Z_FINISH);
if (ret != Z_STREAM_END) if (ret != Z_STREAM_END)
@ -500,16 +500,16 @@ retry:
if (ret != Z_OK) if (ret != Z_OK)
die("cannot deflate request; zlib end error %d", ret); die("cannot deflate request; zlib end error %d", ret);
size = stream.total_out; gzip_size = stream.total_out;
headers = curl_slist_append(headers, "Content-Encoding: gzip"); headers = curl_slist_append(headers, "Content-Encoding: gzip");
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size); curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
if (options.verbosity > 1) { if (options.verbosity > 1) {
fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
rpc->service_name, rpc->service_name,
(unsigned long)rpc->len, (unsigned long)size); (unsigned long)rpc->len, (unsigned long)gzip_size);
fflush(stderr); fflush(stderr);
} }
} else { } else {