Commit b49c8f71aef574ce6606282a498627f5106220d5

Carlos Martín Nieto 2012-07-24T19:03:22

remote: use the same code to control git and http This allows us to add capabilitites to both at the same time, keeps them in sync and removes a lot of code. gitno_buffer now uses a callback to fill its buffer, allowing us to use the same interface for git and http (which uses callbacks).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
diff --git a/src/fetch.c b/src/fetch.c
index 4880772..0de0c62 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -79,7 +79,7 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
 {
 	const char *ptr = buf->data, *line_end = ptr;
 	git_pkt *pkt;
-	int pkt_type, error = 0;
+	int pkt_type, error = 0, ret;
 
 	do {
 		if (buf->offset > 0)
@@ -106,7 +106,7 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
 			return GIT_PKT_NAK;
 		}
 
-		if ((error = gitno_recv(buf)) < 0)
+		if ((ret = gitno_recv(buf)) < 0)
 			return -1;
 	} while (error);
 
@@ -122,7 +122,6 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
 
 static int store_common(git_transport *t)
 {
-	int done = 0;
 	git_pkt *pkt = NULL;
 	gitno_buffer *buf = &t->buffer;
 
@@ -219,12 +218,42 @@ int git_fetch_negotiate(git_remote *remote)
 
 		if (t->common.length > 0)
 			break;
+
+		if (i % 20 == 0 && t->rpc) {
+			git_pkt_ack *pkt;
+			unsigned int i;
+
+			if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
+				goto on_error;
+
+			git_vector_foreach(&t->common, i, pkt) {
+				git_pkt_buffer_have(&pkt->oid, &data);
+			}
+
+			if (git_buf_oom(&data))
+				goto on_error;
+		}
 	}
 
 	if (error < 0 && error != GIT_REVWALKOVER)
 		goto on_error;
 
 	/* Tell the other end that we're done negotiating */
+	if (t->rpc && t->common.length > 0) {
+		git_pkt_ack *pkt;
+		unsigned int i;
+
+		if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
+			goto on_error;
+
+		git_vector_foreach(&t->common, i, pkt) {
+			git_pkt_buffer_have(&pkt->oid, &data);
+		}
+
+		if (git_buf_oom(&data))
+			goto on_error;
+	}
+
 	git_pkt_buffer_done(&data);
 	if (t->negotiation_step(t, data.ptr, data.size) < 0)
 		goto on_error;
@@ -233,10 +262,26 @@ int git_fetch_negotiate(git_remote *remote)
 	git_revwalk_free(walk);
 
 	/* Now let's eat up whatever the server gives us */
-	pkt_type = recv_pkt(NULL, buf);
-	if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
-		giterr_set(GITERR_NET, "Unexpected pkt type");
-		return -1;
+	if (!t->caps.multi_ack) {
+		pkt_type = recv_pkt(NULL, buf);
+		if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
+			giterr_set(GITERR_NET, "Unexpected pkt type");
+			return -1;
+		}
+	} else {
+		git_pkt_ack *pkt;
+		do {
+			if (recv_pkt((git_pkt **)&pkt, buf) < 0)
+				return -1;
+
+			if (pkt->type == GIT_PKT_NAK ||
+			    (pkt->type == GIT_PKT_ACK && pkt->status != GIT_ACK_CONTINUE)) {
+				git__free(pkt);
+				break;
+			}
+
+			git__free(pkt);
+		} while (1);
 	}
 
 	return 0;
@@ -257,50 +302,39 @@ int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_st
 	if (t->own_logic)
 		return t->download_pack(t, remote->repo, bytes, stats);
 
-	return git_fetch__download_pack(NULL, 0, t, remote->repo, bytes, stats);
+	return git_fetch__download_pack(t, remote->repo, bytes, stats);
 
 }
 
 /* Receiving data from a socket and storing it is pretty much the same for git and HTTP */
 int git_fetch__download_pack(
-	const char *buffered,
-	size_t buffered_size,
 	git_transport *t,
 	git_repository *repo,
 	git_off_t *bytes,
 	git_indexer_stats *stats)
 {
 	int recvd;
-	char buff[1024];
-	gitno_buffer buf;
 	git_buf path = GIT_BUF_INIT;
+	gitno_buffer *buf = &t->buffer;
 	git_indexer_stream *idx = NULL;
 
-	gitno_buffer_setup(t, &buf, buff, sizeof(buff));
-
-	if (buffered && memcmp(buffered, "PACK", strlen("PACK"))) {
-		giterr_set(GITERR_NET, "The pack doesn't start with the signature");
-		return -1;
-	}
-
 	if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
 		return -1;
 
 	if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
 		goto on_error;
 
+	git_buf_free(&path);
 	memset(stats, 0, sizeof(git_indexer_stats));
-	if (buffered && git_indexer_stream_add(idx, buffered, buffered_size, stats) < 0)
-		goto on_error;
-
-	*bytes = buffered_size;
+	*bytes = 0;
 
 	do {
-		if (git_indexer_stream_add(idx, buf.data, buf.offset, stats) < 0)
+		if (git_indexer_stream_add(idx, buf->data, buf->offset, stats) < 0)
 			goto on_error;
 
-		gitno_consume_n(&buf, buf.offset);
-		if ((recvd = gitno_recv(&buf)) < 0)
+		gitno_consume_n(buf, buf->offset);
+
+		if ((recvd = gitno_recv(buf)) < 0)
 			goto on_error;
 
 		*bytes += recvd;
diff --git a/src/fetch.h b/src/fetch.h
index a7f1265..87bb43b 100644
--- a/src/fetch.h
+++ b/src/fetch.h
@@ -12,8 +12,7 @@
 int git_fetch_negotiate(git_remote *remote);
 int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats);
 
-int git_fetch__download_pack(const char *buffered, size_t buffered_size, git_transport *t,
-			     git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
+int git_fetch__download_pack(git_transport *t, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
 int git_fetch_setup_walk(git_revwalk **out, git_repository *repo);
 
 #endif
diff --git a/src/netops.c b/src/netops.c
index b369e51..918ca1d 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -61,7 +61,7 @@ static int ssl_set_error(gitno_ssl *ssl, int error)
 }
 #endif
 
-void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
+void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
 {
 	memset(buf, 0x0, sizeof(gitno_buffer));
 	memset(data, 0x0, len);
@@ -73,6 +73,19 @@ void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigne
 	if (t->encrypt)
 		buf->ssl = &t->ssl;
 #endif
+
+	buf->recv = recv;
+	buf->cb_data = cb_data;
+}
+
+void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
+{
+	gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
+}
+
+int gitno_recv(gitno_buffer *buf)
+{
+	return buf->recv(buf);
 }
 
 #ifdef GIT_SSL
@@ -91,7 +104,7 @@ static int ssl_recv(gitno_ssl *ssl, void *data, size_t len)
 }
 #endif
 
-int gitno_recv(gitno_buffer *buf)
+int gitno__recv(gitno_buffer *buf)
 {
 	int ret;
 
diff --git a/src/netops.h b/src/netops.h
index e2c2b81..dded55b 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -18,10 +18,14 @@ struct gitno_buffer {
 #ifdef GIT_SSL
 	struct gitno_ssl *ssl;
 #endif
+	int (*recv)(gitno_buffer *buffer);
+	void *cb_data;
 };
 
 void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len);
+void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data);
 int gitno_recv(gitno_buffer *buf);
+int gitno__recv(gitno_buffer *buf);
 
 void gitno_consume(gitno_buffer *buf, const char *ptr);
 void gitno_consume_n(gitno_buffer *buf, size_t cons);
diff --git a/src/pkt.c b/src/pkt.c
index e60e30d..8c916ff 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -42,15 +42,29 @@ static int flush_pkt(git_pkt **out)
 /* the rest of the line will be useful for multi_ack */
 static int ack_pkt(git_pkt **out, const char *line, size_t len)
 {
-	git_pkt *pkt;
+	git_pkt_ack *pkt;
 	GIT_UNUSED(line);
 	GIT_UNUSED(len);
 
-	pkt = git__malloc(sizeof(git_pkt));
+	pkt = git__calloc(1, sizeof(git_pkt_ack));
 	GITERR_CHECK_ALLOC(pkt);
 
 	pkt->type = GIT_PKT_ACK;
-	*out = pkt;
+	line += 3;
+	len -= 3;
+
+	if (len >= GIT_OID_HEXSZ) {
+		git_oid_fromstr(&pkt->oid, line + 1);
+		line += GIT_OID_HEXSZ + 1;
+		len -= GIT_OID_HEXSZ + 1;
+	}
+
+	if (len >= 7) {
+		if (!git__prefixcmp(line + 1, "continue"))
+			pkt->status = GIT_ACK_CONTINUE;
+	}
+
+	*out = (git_pkt *) pkt;
 
 	return 0;
 }
diff --git a/src/protocol.c b/src/protocol.c
index 6b38617..d8512fd 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -56,3 +56,35 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
 
 	return 0;
 }
+
+int git_protocol_detect_caps(git_pkt_ref *pkt, git_transport_caps *caps)
+{
+	const char *ptr;
+
+	/* No refs or capabilites, odd but not a problem */
+	if (pkt == NULL || pkt->capabilities == NULL)
+		return 0;
+
+	ptr = pkt->capabilities;
+	while (ptr != NULL && *ptr != '\0') {
+		if (*ptr == ' ')
+			ptr++;
+
+		if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
+			caps->common = caps->ofs_delta = 1;
+			ptr += strlen(GIT_CAP_OFS_DELTA);
+			continue;
+		}
+
+		if(!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
+			caps->common = caps->multi_ack = 1;
+			ptr += strlen(GIT_CAP_MULTI_ACK);
+			continue;
+		}
+
+		/* We don't know this capability, so skip it */
+		ptr = strchr(ptr, ' ');
+	}
+
+	return 0;
+}
diff --git a/src/protocol.h b/src/protocol.h
index a6c3e07..9d53480 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -9,6 +9,7 @@
 
 #include "transport.h"
 #include "buffer.h"
+#include "pkt.h"
 
 typedef struct {
 	git_transport *transport;
@@ -19,5 +20,6 @@ typedef struct {
 } git_protocol;
 
 int git_protocol_store_refs(git_protocol *p, const char *data, size_t len);
+int git_protocol_detect_caps(git_pkt_ref *pkt, git_transport_caps *caps);
 
 #endif
diff --git a/src/transport.h b/src/transport.h
index 3ef36f4..0adc037 100644
--- a/src/transport.h
+++ b/src/transport.h
@@ -74,7 +74,8 @@ struct git_transport {
 		connected : 1,
 		check_cert: 1,
 		encrypt : 1,
-		own_logic: 1; /* transitional */
+		own_logic: 1, /* transitional */
+		rpc: 1; /* git-speak for the HTTP transport */
 #ifdef GIT_SSL
 	struct gitno_ssl ssl;
 #endif
diff --git a/src/transports/git.c b/src/transports/git.c
index f5cdfe7..ccd9755 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -156,42 +156,6 @@ static int store_refs(transport_git *t)
 	}
 }
 
-static int detect_caps(transport_git *t)
-{
-	git_vector *refs = &t->refs;
-	git_pkt_ref *pkt;
-	git_transport_caps *caps = &t->parent.caps;
-	const char *ptr;
-
-	pkt = git_vector_get(refs, 0);
-	/* No refs or capabilites, odd but not a problem */
-	if (pkt == NULL || pkt->capabilities == NULL)
-		return 0;
-
-	ptr = pkt->capabilities;
-	while (ptr != NULL && *ptr != '\0') {
-		if (*ptr == ' ')
-			ptr++;
-
-		if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
-			caps->common = caps->ofs_delta = 1;
-			ptr += strlen(GIT_CAP_OFS_DELTA);
-			continue;
-		}
-
-		if(!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
-			caps->common = caps->multi_ack = 1;
-			ptr += strlen(GIT_CAP_MULTI_ACK);
-			continue;
-		}
-
-		/* We don't know this capability, so skip it */
-		ptr = strchr(ptr, ' ');
-	}
-
-	return 0;
-}
-
 /*
  * Since this is a network connection, we need to parse and store the
  * pkt-lines at this stage and keep them there.
@@ -219,7 +183,7 @@ static int git_connect(git_transport *transport, int direction)
 	if (store_refs(t) < 0)
 		goto cleanup;
 
-	if (detect_caps(t) < 0)
+	if (git_protocol_detect_caps(git_vector_get(&t->refs, 0), &transport->caps) < 0)
 		goto cleanup;
 
 	return 0;
diff --git a/src/transports/http.c b/src/transports/http.c
index 3d99839..25db802 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -31,7 +31,7 @@ typedef struct {
 	git_transport parent;
 	git_protocol proto;
 	git_vector refs;
-	git_vector common;
+	http_parser_settings settings;
 	git_buf buf;
 	git_remote_head **heads;
 	int error;
@@ -46,7 +46,7 @@ typedef struct {
 	char *host;
 	char *port;
 	char *service;
-	git_transport_caps caps;
+	char buffer[4096];
 #ifdef GIT_WIN32
 	WSADATA wsd;
 #endif
@@ -210,6 +210,7 @@ static int on_message_complete(http_parser *parser)
 
 static int store_refs(transport_http *t)
 {
+	git_transport *transport = (git_transport *) t;
 	http_parser_settings settings;
 	char buffer[1024];
 	gitno_buffer buf;
@@ -241,7 +242,7 @@ static int store_refs(transport_http *t)
 		gitno_consume_n(&buf, parsed);
 
 		if (ret == 0 || t->transfer_finished)
-			return 0;
+			break;
 	}
 
 	pkt = git_vector_get(&t->refs, 0);
@@ -249,9 +250,14 @@ static int store_refs(transport_http *t)
 		giterr_set(GITERR_NET, "Invalid HTTP response");
 		return t->error = -1;
 	} else {
+		/* Remove the comment and flush pkts */
+		git_vector_remove(&t->refs, 0);
 		git_vector_remove(&t->refs, 0);
 	}
 
+	if (git_protocol_detect_caps(git_vector_get(&t->refs, 0), &transport->caps) < 0)
+		return t->error = -1;
+
 	return 0;
 }
 
@@ -333,278 +339,86 @@ static int http_ls(git_transport *transport, git_headlist_cb list_cb, void *opaq
 	return 0;
 }
 
-static int on_body_parse_response(http_parser *parser, const char *str, size_t len)
+static int on_body_fill_buffer(http_parser *parser, const char *str, size_t len)
 {
+	git_transport *transport = (git_transport *) parser->data;
 	transport_http *t = (transport_http *) parser->data;
-	git_buf *buf = &t->buf;
-	git_vector *common = &t->common;
-	int error;
-	const char *line_end, *ptr;
-
-	if (len == 0) { /* EOF */
-		if (git_buf_len(buf) != 0) {
-			giterr_set(GITERR_NET, "Unexpected EOF");
-			return t->error = -1;
-		} else {
-			return 0;
-		}
-	}
-
-	git_buf_put(buf, str, len);
-	ptr = buf->ptr;
-	while (1) {
-		git_pkt *pkt;
-
-		if (git_buf_len(buf) == 0)
-			return 0;
-
-		error = git_pkt_parse_line(&pkt, ptr, &line_end, git_buf_len(buf));
-		if (error == GIT_EBUFS) {
-			return 0; /* Ask for more */
-		}
-		if (error < 0)
-			return t->error = -1;
+	gitno_buffer *buf = &transport->buffer;
 
-		git_buf_consume(buf, line_end);
-
-		if (pkt->type == GIT_PKT_PACK) {
-			git__free(pkt);
-			t->pack_ready = 1;
-			return 0;
-		}
-
-		if (pkt->type == GIT_PKT_NAK) {
-			git__free(pkt);
-			return 0;
-		}
-
-		if (pkt->type != GIT_PKT_ACK) {
-			git__free(pkt);
-			continue;
-		}
-
-		if (git_vector_insert(common, pkt) < 0)
-			return -1;
+	if (buf->len - buf->offset < len) {
+		giterr_set(GITERR_NET, "Can't fit data in the buffer");
+		return t->error = -1;
 	}
 
-	return error;
-
-}
+	memcpy(buf->data + buf->offset, str, len);
+	buf->offset += len;
 
-static int parse_response(transport_http *t)
-{
-	int ret = 0;
-	http_parser_settings settings;
-	char buffer[1024];
-	gitno_buffer buf;
-
-	http_parser_init(&t->parser, HTTP_RESPONSE);
-	t->parser.data = t;
-	t->transfer_finished = 0;
-	memset(&settings, 0x0, sizeof(http_parser_settings));
-	settings.on_header_field = on_header_field;
-	settings.on_header_value = on_header_value;
-	settings.on_headers_complete = on_headers_complete;
-	settings.on_body = on_body_parse_response;
-	settings.on_message_complete = on_message_complete;
-
-	gitno_buffer_setup((git_transport *)t, &buf, buffer, sizeof(buffer));
-
-	while(1) {
-		size_t parsed;
-
-		if ((ret = gitno_recv(&buf)) < 0)
-			return -1;
-
-		parsed = http_parser_execute(&t->parser, &settings, buf.data, buf.offset);
-		/* Both should happen at the same time */
-		if (parsed != buf.offset || t->error < 0)
-			return t->error;
-
-		gitno_consume_n(&buf, parsed);
-
-		if (ret == 0 || t->transfer_finished || t->pack_ready) {
-			return 0;
-		}
-	}
-
-	return ret;
+	return 0;
 }
 
-static int http_negotiate_fetch(git_transport *transport, git_repository *repo, const git_vector *wants)
+static int http_recv_cb(gitno_buffer *buf)
 {
+	git_transport *transport = (git_transport *) buf->cb_data;
 	transport_http *t = (transport_http *) transport;
-	int ret;
-	unsigned int i;
-	char buff[128];
-	gitno_buffer buf;
-	git_revwalk *walk = NULL;
-	git_oid oid;
-	git_pkt_ack *pkt;
-	git_vector *common = &t->common;
-	git_buf request = GIT_BUF_INIT, data = GIT_BUF_INIT;
+	size_t parsed, old_len;
+	gitno_buffer inner;
+	char buffer[2048];
+	int error;
 
-	gitno_buffer_setup(transport, &buf, buff, sizeof(buff));
+	if (t->transfer_finished)
+		return 0;
 
-	if (git_vector_init(common, 16, NULL) < 0)
-		return -1;
+	gitno_buffer_setup(transport, &inner, buffer, sizeof(buffer));
 
-	if (git_fetch_setup_walk(&walk, repo) < 0)
+	if ((error = gitno_recv(&inner)) < 0)
 		return -1;
 
-	do {
-		if ((ret = do_connect(t, t->host, t->port)) < 0)
-			goto cleanup;
-
-		if ((ret = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0)
-			goto cleanup;
-
-		/* We need to send these on each connection */
-		git_vector_foreach (common, i, pkt) {
-			if ((ret = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
-				goto cleanup;
-		}
-
-		i = 0;
-		while ((i < 20) && ((ret = git_revwalk_next(&oid, walk)) == 0)) {
-			if ((ret = git_pkt_buffer_have(&oid, &data)) < 0)
-				goto cleanup;
-
-			i++;
-		}
-
-		git_pkt_buffer_done(&data);
-
-		if ((ret = gen_request(&request, t->path, t->host, "POST", "upload-pack", data.size, 0)) < 0)
-			goto cleanup;
-
-		if ((ret = gitno_send(transport, request.ptr, request.size, 0)) < 0)
-			goto cleanup;
-
-		if ((ret = gitno_send(transport, data.ptr, data.size, 0)) < 0)
-			goto cleanup;
-
-		git_buf_clear(&request);
-		git_buf_clear(&data);
-
-		if (ret < 0 || i >= 256)
-			break;
+	old_len = buf->offset;
+	parsed = http_parser_execute(&t->parser, &t->settings, inner.data, inner.offset);
+	if (t->error < 0)
+		return t->error;
 
-		if ((ret = parse_response(t)) < 0)
-			goto cleanup;
-
-		if (t->pack_ready) {
-			ret = 0;
-			goto cleanup;
-		}
-
-	} while(1);
-
-cleanup:
-	git_buf_free(&request);
-	git_buf_free(&data);
-	git_revwalk_free(walk);
-	return ret;
+	return buf->offset - old_len;
 }
 
-typedef struct {
-	git_indexer_stream *idx;
-	git_indexer_stats *stats;
-	transport_http *transport;
-} download_pack_cbdata;
-
-static int on_message_complete_download_pack(http_parser *parser)
-{
-	download_pack_cbdata *data = (download_pack_cbdata *) parser->data;
-
-	data->transport->transfer_finished = 1;
-
-	return 0;
-}
-static int on_body_download_pack(http_parser *parser, const char *str, size_t len)
-{
-	download_pack_cbdata *data = (download_pack_cbdata *) parser->data;
-	transport_http *t = data->transport;
-	git_indexer_stream *idx = data->idx;
-	git_indexer_stats *stats = data->stats;
-
-	return t->error = git_indexer_stream_add(idx, str, len, stats);
-}
-
-/*
- * As the server is probably using Transfer-Encoding: chunked, we have
- * to use the HTTP parser to download the pack instead of giving it to
- * the simple downloader. Furthermore, we're using keep-alive
- * connections, so the simple downloader would just hang.
- */
-static int http_download_pack(git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats)
+static int http_negotiation_step(struct git_transport *transport, void *data, size_t len)
 {
 	transport_http *t = (transport_http *) transport;
-	git_buf *oldbuf = &t->buf;
-	int recvd;
-	http_parser_settings settings;
-	char buffer[1024];
-	gitno_buffer buf;
-	git_buf path = GIT_BUF_INIT;
-	git_indexer_stream *idx = NULL;
-	download_pack_cbdata data;
-
-	gitno_buffer_setup(transport, &buf, buffer, sizeof(buffer));
-
-	if (memcmp(oldbuf->ptr, "PACK", strlen("PACK"))) {
-		giterr_set(GITERR_NET, "The pack doesn't start with a pack signature");
-		return -1;
-	}
-
-	if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
-		return -1;
+	git_buf request = GIT_BUF_INIT;
+	int ret;
 
-	if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
+	/* First, send the data as a HTTP POST request */
+	if ((ret = do_connect(t, t->host, t->port)) < 0)
 		return -1;
 
-	/*
-	 * This is part of the previous response, so we don't want to
-	 * re-init the parser, just set these two callbacks.
-	 */
-	memset(stats, 0, sizeof(git_indexer_stats));
-	data.stats = stats;
-	data.idx = idx;
-	data.transport = t;
-	t->parser.data = &data;
-	t->transfer_finished = 0;
-	memset(&settings, 0x0, sizeof(settings));
-	settings.on_message_complete = on_message_complete_download_pack;
-	settings.on_body = on_body_download_pack;
-	*bytes = git_buf_len(oldbuf);
-
-	if (git_indexer_stream_add(idx, git_buf_cstr(oldbuf), git_buf_len(oldbuf), stats) < 0)
+	if ((ret = gen_request(&request, t->path, t->host, "POST", "upload-pack", len, 0)) < 0)
 		goto on_error;
 
-	gitno_buffer_setup(transport, &buf, buffer, sizeof(buffer));
-
-	do {
-		size_t parsed;
+	if ((ret = gitno_send(transport, request.ptr, request.size, 0)) < 0)
+		goto on_error;
 
-		if ((recvd = gitno_recv(&buf)) < 0)
-			goto on_error;
+	if ((ret = gitno_send(transport, data, len, 0)) < 0)
+		goto on_error;
 
-		parsed = http_parser_execute(&t->parser, &settings, buf.data, buf.offset);
-		if (parsed != buf.offset || t->error < 0)
-			goto on_error;
+	git_buf_free(&request);
 
-		*bytes += recvd;
-		gitno_consume_n(&buf, parsed);
-	} while (recvd > 0 && !t->transfer_finished);
+	/* Then we need to set up the buffer to grab data from the HTTP response */
+	http_parser_init(&t->parser, HTTP_RESPONSE);
+	t->parser.data = t;
+	t->transfer_finished = 0;
+	memset(&t->settings, 0x0, sizeof(http_parser_settings));
+	t->settings.on_header_field = on_header_field;
+	t->settings.on_header_value = on_header_value;
+	t->settings.on_headers_complete = on_headers_complete;
+	t->settings.on_body = on_body_fill_buffer;
+	t->settings.on_message_complete = on_message_complete;
 
-	if (git_indexer_stream_finalize(idx, stats) < 0)
-		goto on_error;
+	gitno_buffer_setup_callback(transport, &transport->buffer, t->buffer, sizeof(t->buffer), http_recv_cb, t);
 
-	git_indexer_stream_free(idx);
 	return 0;
 
 on_error:
-	git_indexer_stream_free(idx);
-	git_buf_free(&path);
+	git_buf_free(&request);
 	return -1;
 }
 
@@ -628,7 +442,7 @@ static void http_free(git_transport *transport)
 {
 	transport_http *t = (transport_http *) transport;
 	git_vector *refs = &t->refs;
-	git_vector *common = &t->common;
+	git_vector *common = &transport->common;
 	unsigned int i;
 	git_pkt *p;
 
@@ -668,13 +482,12 @@ int git_transport_http(git_transport **out)
 
 	memset(t, 0x0, sizeof(transport_http));
 
-	t->parent.own_logic = 1;
 	t->parent.connect = http_connect;
 	t->parent.ls = http_ls;
-	t->parent.negotiate_fetch = http_negotiate_fetch;
-	t->parent.download_pack = http_download_pack;
+	t->parent.negotiation_step = http_negotiation_step;
 	t->parent.close = http_close;
 	t->parent.free = http_free;
+	t->parent.rpc = 1;
 	t->proto.refs = &t->refs;
 	t->proto.transport = (git_transport *) t;