remote-curl: refactor reading into rpc_state's buf
Currently, whenever remote-curl reads pkt-lines from its response file descriptor, only the payload is written to its buf, not the 4 characters denoting the length. A future patch will require the ability to also write those 4 characters, so in preparation for that, refactor this read into its own function. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Feb 21, 2019 at 12:24 UTC
78ad91728d859a3ddb6e86218e86f16cd3489d0a
1 file changed
+24
-9
remote-curl.c
+24
-9
@@ -520,6 +520,25 @@ struct rpc_state {
520
unsigned initial_buffer : 1;
521
};
522
523
+/*
524
+ * Appends the result of reading from rpc->out to the string represented by
525
+ * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
526
+ * enough space, 0 otherwise.
527
+ *
528
+ * Writes the number of bytes appended into appended.
529
+ */
530
+static int rpc_read_from_out(struct rpc_state *rpc, size_t *appended) {
531
+ size_t left = rpc->alloc - rpc->len;
532
+ char *buf = rpc->buf + rpc->len;
533
+
534
+ if (left < LARGE_PACKET_MAX)
535
+ return 0;
536
+
537
+ *appended = packet_read(rpc->out, NULL, NULL, buf, left, 0);
538
+ rpc->len += *appended;
539
+ return 1;
540
+}
541
+
542
static size_t rpc_out(void *ptr, size_t eltsize,
543
size_t nmemb, void *buffer_)
544
{
@@ -529,11 +548,12 @@ static size_t rpc_out(void *ptr, size_t eltsize,
548
549
if (!avail) {
550
rpc->initial_buffer = 0;
532
- avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
551
+ rpc->len = 0;
552
+ if (!rpc_read_from_out(rpc, &avail))
553
+ BUG("The entire rpc->buf should be larger than LARGE_PACKET_DATA_MAX");
554
if (!avail)
555
return 0;
556
rpc->pos = 0;
536
- rpc->len = avail;
557
}
558
559
if (max < avail)
@@ -677,20 +697,15 @@ static int post_rpc(struct rpc_state *rpc)
697
* chunked encoding mess.
698
*/
699
while (1) {
680
- size_t left = rpc->alloc - rpc->len;
681
- char *buf = rpc->buf + rpc->len;
682
- int n;
700
+ size_t n;
701
684
- if (left < LARGE_PACKET_MAX) {
702
+ if (!rpc_read_from_out(rpc, &n)) {
703
large_request = 1;
704
use_gzip = 0;
705
break;
706
}
689
-
690
- n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
707
if (!n)
708
break;
693
- rpc->len += n;
709
}
710
711
if (large_request) {