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>

Jeff King committed Oct 31, 2012 at 07:20 UTC df126e108b899da133a980e900df39dfe57fcd59
1 file changed +7 -7
remote-curl.c
+7 -7
@@ -413,6 +413,7 @@ static int post_rpc(struct rpc_state *rpc)
413 struct curl_slist *headers = NULL;
414 int use_gzip = rpc->gzip_request;
415 char *gzip_body = NULL;
416 + size_t gzip_size;
417 int err, large_request = 0;
418
419 /* Try to load the entire request, if we can fit it into the
@@ -478,19 +479,18 @@ retry:
479 * we can try to deflate it ourselves, this may save on.
480 * the transfer time.
481 */
481 - size_t size;
482 git_zstream stream;
483 int ret;
484
485 memset(&stream, 0, sizeof(stream));
486 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
487 - size = git_deflate_bound(&stream, rpc->len);
488 - gzip_body = xmalloc(size);
487 + gzip_size = git_deflate_bound(&stream, rpc->len);
488 + gzip_body = xmalloc(gzip_size);
489
490 stream.next_in = (unsigned char *)rpc->buf;
491 stream.avail_in = rpc->len;
492 stream.next_out = (unsigned char *)gzip_body;
493 - stream.avail_out = size;
493 + stream.avail_out = gzip_size;
494
495 ret = git_deflate(&stream, Z_FINISH);
496 if (ret != Z_STREAM_END)
@@ -500,16 +500,16 @@ retry:
500 if (ret != Z_OK)
501 die("cannot deflate request; zlib end error %d", ret);
502
503 - size = stream.total_out;
503 + gzip_size = stream.total_out;
504
505 headers = curl_slist_append(headers, "Content-Encoding: gzip");
506 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
507 - curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
507 + curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
508
509 if (options.verbosity > 1) {
510 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
511 rpc->service_name,
512 - (unsigned long)rpc->len, (unsigned long)size);
512 + (unsigned long)rpc->len, (unsigned long)gzip_size);
513 fflush(stderr);
514 }
515 } else {