9 #include "run-command.h"
11 #include "string-list.h"
14 #include "credential.h"
15 #include "oid-array.h"
16 #include "send-pack.h"
19 #include "transport.h"
21 static struct remote
*remote
;
22 /* always ends with a trailing slash */
23 static struct strbuf url
= STRBUF_INIT
;
29 struct string_list deepen_not
;
30 struct string_list push_options
;
32 unsigned progress
: 1,
33 check_self_contained_and_connected
: 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
43 /* see documentation of corresponding flag in fetch-pack.h */
48 force_if_includes
: 1;
49 const struct git_hash_algo
*hash_algo
;
51 static struct options options
;
52 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
54 static int set_option(const char *name
, const char *value
)
56 if (!strcmp(name
, "verbosity")) {
58 int v
= strtol(value
, &end
, 10);
59 if (value
== end
|| *end
)
61 options
.verbosity
= v
;
64 else if (!strcmp(name
, "progress")) {
65 if (!strcmp(value
, "true"))
67 else if (!strcmp(value
, "false"))
73 else if (!strcmp(name
, "depth")) {
75 unsigned long v
= strtoul(value
, &end
, 10);
76 if (value
== end
|| *end
)
81 else if (!strcmp(name
, "deepen-since")) {
82 options
.deepen_since
= xstrdup(value
);
85 else if (!strcmp(name
, "deepen-not")) {
86 string_list_append(&options
.deepen_not
, value
);
89 else if (!strcmp(name
, "deepen-relative")) {
90 if (!strcmp(value
, "true"))
91 options
.deepen_relative
= 1;
92 else if (!strcmp(value
, "false"))
93 options
.deepen_relative
= 0;
98 else if (!strcmp(name
, "followtags")) {
99 if (!strcmp(value
, "true"))
100 options
.followtags
= 1;
101 else if (!strcmp(value
, "false"))
102 options
.followtags
= 0;
107 else if (!strcmp(name
, "dry-run")) {
108 if (!strcmp(value
, "true"))
110 else if (!strcmp(value
, "false"))
116 else if (!strcmp(name
, "check-connectivity")) {
117 if (!strcmp(value
, "true"))
118 options
.check_self_contained_and_connected
= 1;
119 else if (!strcmp(value
, "false"))
120 options
.check_self_contained_and_connected
= 0;
125 else if (!strcmp(name
, "cas")) {
126 struct strbuf val
= STRBUF_INIT
;
127 strbuf_addstr(&val
, "--force-with-lease=");
129 strbuf_addstr(&val
, value
);
130 else if (unquote_c_style(&val
, value
, NULL
))
132 string_list_append(&cas_options
, val
.buf
);
133 strbuf_release(&val
);
135 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
136 if (!strcmp(value
, "true"))
137 options
.force_if_includes
= 1;
138 else if (!strcmp(value
, "false"))
139 options
.force_if_includes
= 0;
143 } else if (!strcmp(name
, "cloning")) {
144 if (!strcmp(value
, "true"))
146 else if (!strcmp(value
, "false"))
151 } else if (!strcmp(name
, "update-shallow")) {
152 if (!strcmp(value
, "true"))
153 options
.update_shallow
= 1;
154 else if (!strcmp(value
, "false"))
155 options
.update_shallow
= 0;
159 } else if (!strcmp(name
, "pushcert")) {
160 if (!strcmp(value
, "true"))
161 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
162 else if (!strcmp(value
, "false"))
163 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
164 else if (!strcmp(value
, "if-asked"))
165 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
169 } else if (!strcmp(name
, "atomic")) {
170 if (!strcmp(value
, "true"))
172 else if (!strcmp(value
, "false"))
177 } else if (!strcmp(name
, "push-option")) {
179 string_list_append(&options
.push_options
, value
);
181 struct strbuf unquoted
= STRBUF_INIT
;
182 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
183 die(_("invalid quoting in push-option value: '%s'"), value
);
184 string_list_append_nodup(&options
.push_options
,
185 strbuf_detach(&unquoted
, NULL
));
188 } else if (!strcmp(name
, "family")) {
189 if (!strcmp(value
, "ipv4"))
190 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
191 else if (!strcmp(value
, "ipv6"))
192 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
193 else if (!strcmp(value
, "all"))
194 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
198 } else if (!strcmp(name
, "from-promisor")) {
199 options
.from_promisor
= 1;
201 } else if (!strcmp(name
, "filter")) {
202 options
.filter
= xstrdup(value
);
204 } else if (!strcmp(name
, "object-format")) {
206 options
.object_format
= 1;
207 if (strcmp(value
, "true")) {
208 algo
= hash_algo_by_name(value
);
209 if (algo
== GIT_HASH_UNKNOWN
)
210 die("unknown object format '%s'", value
);
211 options
.hash_algo
= &hash_algos
[algo
];
215 return 1 /* unsupported */;
225 struct oid_array shallow
;
226 enum protocol_version version
;
227 unsigned proto_git
: 1;
229 static struct discovery
*last_discovery
;
231 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
233 struct ref
*list
= NULL
;
234 struct packet_reader reader
;
236 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
237 PACKET_READ_CHOMP_NEWLINE
|
238 PACKET_READ_GENTLE_ON_EOF
|
239 PACKET_READ_DIE_ON_ERR_PACKET
);
241 heads
->version
= discover_version(&reader
);
242 switch (heads
->version
) {
245 * Do nothing. This isn't a list of refs but rather a
246 * capability advertisement. Client would have run
247 * 'stateless-connect' so we'll dump this capability listing
248 * and let them request the refs themselves.
253 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
254 NULL
, &heads
->shallow
);
255 options
.hash_algo
= reader
.hash_algo
;
257 case protocol_unknown_version
:
258 BUG("unknown protocol version");
264 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
266 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
269 return the_hash_algo
;
271 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
272 if (algo
== GIT_HASH_UNKNOWN
)
274 return &hash_algos
[algo
];
277 static struct ref
*parse_info_refs(struct discovery
*heads
)
279 char *data
, *start
, *mid
;
283 struct ref
*refs
= NULL
;
284 struct ref
*ref
= NULL
;
285 struct ref
*last_ref
= NULL
;
287 options
.hash_algo
= detect_hash_algo(heads
);
288 if (!options
.hash_algo
)
289 die("%sinfo/refs not valid: could not determine hash algorithm; "
290 "is this a git repository?",
291 transport_anonymize_url(url
.buf
));
296 while (i
< heads
->len
) {
302 if (data
[i
] == '\n') {
303 if (mid
- start
!= options
.hash_algo
->hexsz
)
304 die(_("%sinfo/refs not valid: is this a git repository?"),
305 transport_anonymize_url(url
.buf
));
308 ref
= alloc_ref(ref_name
);
309 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
313 last_ref
->next
= ref
;
320 ref
= alloc_ref("HEAD");
321 if (!http_fetch_ref(url
.buf
, ref
) &&
322 !resolve_remote_symref(ref
, refs
)) {
332 static void free_discovery(struct discovery
*d
)
335 if (d
== last_discovery
)
336 last_discovery
= NULL
;
337 free(d
->shallow
.oid
);
345 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
351 * We only show text/plain parts, as other types are likely
352 * to be ugly to look at on the user's terminal.
354 if (strcmp(type
->buf
, "text/plain"))
357 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
365 eol
= strchrnul(p
, '\n');
366 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
372 static int get_protocol_http_header(enum protocol_version version
,
373 struct strbuf
*header
)
376 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
385 static void check_smart_http(struct discovery
*d
, const char *service
,
389 struct packet_reader reader
;
392 * If we don't see x-$service-advertisement, then it's not smart-http.
393 * But once we do, we commit to it and assume any other protocol
394 * violations are hard errors.
396 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
397 !skip_prefix(p
, service
, &p
) ||
398 strcmp(p
, "-advertisement"))
401 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
402 PACKET_READ_CHOMP_NEWLINE
|
403 PACKET_READ_DIE_ON_ERR_PACKET
);
404 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
405 die(_("invalid server response; expected service, got flush packet"));
407 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
409 * The header can include additional metadata lines, up
410 * until a packet flush marker. Ignore these now, but
411 * in the future we might start to scan them.
414 packet_reader_read(&reader
);
415 if (reader
.pktlen
<= 0) {
421 * v0 smart http; callers expect us to soak up the
422 * service and header packets
424 d
->buf
= reader
.src_buffer
;
425 d
->len
= reader
.src_len
;
428 } else if (!strcmp(reader
.line
, "version 2")) {
430 * v2 smart http; do not consume version packet, which will
431 * be handled elsewhere.
436 die(_("invalid server response; got '%s'"), reader
.line
);
440 static struct discovery
*discover_refs(const char *service
, int for_push
)
442 struct strbuf type
= STRBUF_INIT
;
443 struct strbuf charset
= STRBUF_INIT
;
444 struct strbuf buffer
= STRBUF_INIT
;
445 struct strbuf refs_url
= STRBUF_INIT
;
446 struct strbuf effective_url
= STRBUF_INIT
;
447 struct strbuf protocol_header
= STRBUF_INIT
;
448 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
449 struct discovery
*last
= last_discovery
;
450 int http_ret
, maybe_smart
= 0;
451 struct http_get_options http_options
;
452 enum protocol_version version
= get_protocol_version_config();
454 if (last
&& !strcmp(service
, last
->service
))
456 free_discovery(last
);
458 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
459 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
460 git_env_bool("GIT_SMART_HTTP", 1)) {
462 if (!strchr(url
.buf
, '?'))
463 strbuf_addch(&refs_url
, '?');
465 strbuf_addch(&refs_url
, '&');
466 strbuf_addf(&refs_url
, "service=%s", service
);
470 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
471 * to perform a push, then fallback to v0 since the client doesn't know
472 * how to push yet using v2.
474 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
475 version
= protocol_v0
;
477 /* Add the extra Git-Protocol header */
478 if (get_protocol_http_header(version
, &protocol_header
))
479 string_list_append(&extra_headers
, protocol_header
.buf
);
481 memset(&http_options
, 0, sizeof(http_options
));
482 http_options
.content_type
= &type
;
483 http_options
.charset
= &charset
;
484 http_options
.effective_url
= &effective_url
;
485 http_options
.base_url
= &url
;
486 http_options
.extra_headers
= &extra_headers
;
487 http_options
.initial_request
= 1;
488 http_options
.no_cache
= 1;
490 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
494 case HTTP_MISSING_TARGET
:
495 show_http_message(&type
, &charset
, &buffer
);
496 die(_("repository '%s' not found"),
497 transport_anonymize_url(url
.buf
));
499 show_http_message(&type
, &charset
, &buffer
);
500 die(_("Authentication failed for '%s'"),
501 transport_anonymize_url(url
.buf
));
503 show_http_message(&type
, &charset
, &buffer
);
504 die(_("unable to access '%s': %s"),
505 transport_anonymize_url(url
.buf
), curl_errorstr
);
508 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
509 char *u
= transport_anonymize_url(url
.buf
);
510 warning(_("redirecting to %s"), u
);
514 last
= xcalloc(1, sizeof(*last_discovery
));
515 last
->service
= xstrdup(service
);
516 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
517 last
->buf
= last
->buf_alloc
;
520 check_smart_http(last
, service
, &type
);
523 last
->refs
= parse_git_refs(last
, for_push
);
525 last
->refs
= parse_info_refs(last
);
527 strbuf_release(&refs_url
);
528 strbuf_release(&type
);
529 strbuf_release(&charset
);
530 strbuf_release(&effective_url
);
531 strbuf_release(&buffer
);
532 strbuf_release(&protocol_header
);
533 string_list_clear(&extra_headers
, 0);
534 last_discovery
= last
;
538 static struct ref
*get_refs(int for_push
)
540 struct discovery
*heads
;
543 heads
= discover_refs("git-receive-pack", for_push
);
545 heads
= discover_refs("git-upload-pack", for_push
);
550 static void output_refs(struct ref
*refs
)
553 if (options
.object_format
&& options
.hash_algo
) {
554 printf(":object-format %s\n", options
.hash_algo
->name
);
555 repo_set_hash_algo(the_repository
,
556 hash_algo_by_ptr(options
.hash_algo
));
558 for (posn
= refs
; posn
; posn
= posn
->next
) {
560 printf("@%s %s\n", posn
->symref
, posn
->name
);
562 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
571 const char *service_name
;
573 char *hdr_content_type
;
575 char *protocol_header
;
583 unsigned gzip_request
: 1;
584 unsigned initial_buffer
: 1;
587 * Whenever a pkt-line is read into buf, append the 4 characters
588 * denoting its length before appending the payload.
590 unsigned write_line_lengths
: 1;
593 * Used by rpc_out; initialize to 0. This is true if a flush has been
594 * read, but the corresponding line length (if write_line_lengths is
595 * true) and EOF have not been sent to libcurl. Since each flush marks
596 * the end of a request, each flush must be completely sent before any
597 * further reading occurs.
599 unsigned flush_read_but_not_sent
: 1;
603 * Appends the result of reading from rpc->out to the string represented by
604 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
605 * enough space, 0 otherwise.
607 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
608 * hexadecimal string before appending the result described above.
610 * Writes the total number of bytes appended into appended.
612 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
614 enum packet_read_status
*status
) {
619 if (rpc
->write_line_lengths
) {
620 left
= rpc
->alloc
- rpc
->len
- 4;
621 buf
= rpc
->buf
+ rpc
->len
+ 4;
623 left
= rpc
->alloc
- rpc
->len
;
624 buf
= rpc
->buf
+ rpc
->len
;
627 if (left
< LARGE_PACKET_MAX
)
630 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
631 left
, &pktlen_raw
, options
);
632 if (*status
!= PACKET_READ_EOF
) {
633 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
634 rpc
->len
+= *appended
;
637 if (rpc
->write_line_lengths
) {
639 case PACKET_READ_EOF
:
640 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
641 die(_("shouldn't have EOF when not gentle on EOF"));
643 case PACKET_READ_NORMAL
:
644 set_packet_header(buf
- 4, *appended
);
646 case PACKET_READ_DELIM
:
647 memcpy(buf
- 4, "0001", 4);
649 case PACKET_READ_FLUSH
:
650 memcpy(buf
- 4, "0000", 4);
652 case PACKET_READ_RESPONSE_END
:
653 die(_("remote server sent unexpected response end packet"));
660 static size_t rpc_out(void *ptr
, size_t eltsize
,
661 size_t nmemb
, void *buffer_
)
663 size_t max
= eltsize
* nmemb
;
664 struct rpc_state
*rpc
= buffer_
;
665 size_t avail
= rpc
->len
- rpc
->pos
;
666 enum packet_read_status status
;
669 rpc
->initial_buffer
= 0;
672 if (!rpc
->flush_read_but_not_sent
) {
673 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
674 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
675 if (status
== PACKET_READ_FLUSH
)
676 rpc
->flush_read_but_not_sent
= 1;
679 * If flush_read_but_not_sent is true, we have already read one
680 * full request but have not fully sent it + EOF, which is why
681 * we need to refrain from reading.
684 if (rpc
->flush_read_but_not_sent
) {
687 * The line length either does not need to be sent at
688 * all or has already been completely sent. Now we can
689 * return 0, indicating EOF, meaning that the flush has
692 rpc
->flush_read_but_not_sent
= 0;
696 * If avail is non-zero, the line length for the flush still
697 * hasn't been fully sent. Proceed with sending the line
704 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
709 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
711 struct rpc_state
*rpc
= clientp
;
717 case CURLIOCMD_RESTARTREAD
:
718 if (rpc
->initial_buffer
) {
722 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
723 return CURLIOE_FAILRESTART
;
726 return CURLIOE_UNKNOWNCMD
;
730 struct check_pktline_state
{
736 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
739 if (!state
->remaining
) {
740 int digits_remaining
= 4 - state
->len_filled
;
741 if (digits_remaining
> size
)
742 digits_remaining
= size
;
743 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
744 state
->len_filled
+= digits_remaining
;
745 ptr
+= digits_remaining
;
746 size
-= digits_remaining
;
748 if (state
->len_filled
== 4) {
749 state
->remaining
= packet_length(state
->len_buf
);
750 if (state
->remaining
< 0) {
751 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
752 } else if (state
->remaining
== 2) {
753 die(_("remote-curl: unexpected response end packet"));
754 } else if (state
->remaining
< 4) {
755 state
->remaining
= 0;
757 state
->remaining
-= 4;
759 state
->len_filled
= 0;
763 if (state
->remaining
) {
764 int remaining
= state
->remaining
;
765 if (remaining
> size
)
769 state
->remaining
-= remaining
;
775 struct rpc_state
*rpc
;
776 struct active_request_slot
*slot
;
778 struct check_pktline_state pktline_state
;
782 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
785 static size_t rpc_in(char *ptr
, size_t eltsize
,
786 size_t nmemb
, void *buffer_
)
788 size_t size
= eltsize
* nmemb
;
789 struct rpc_in_data
*data
= buffer_
;
792 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
793 &response_code
) != CURLE_OK
)
795 if (response_code
>= 300)
798 data
->rpc
->any_written
= 1;
799 if (data
->check_pktline
)
800 check_pktline(&data
->pktline_state
, ptr
, size
);
801 write_or_die(data
->rpc
->in
, ptr
, size
);
805 static int run_slot(struct active_request_slot
*slot
,
806 struct slot_results
*results
)
809 struct slot_results results_buf
;
812 results
= &results_buf
;
814 err
= run_one_slot(slot
, results
);
816 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
817 struct strbuf msg
= STRBUF_INIT
;
818 if (results
->http_code
&& results
->http_code
!= 200)
819 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
820 if (results
->curl_result
!= CURLE_OK
) {
822 strbuf_addch(&msg
, ' ');
823 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
824 if (curl_errorstr
[0]) {
825 strbuf_addch(&msg
, ' ');
826 strbuf_addstr(&msg
, curl_errorstr
);
829 error(_("RPC failed; %s"), msg
.buf
);
830 strbuf_release(&msg
);
836 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
838 struct active_request_slot
*slot
;
839 struct curl_slist
*headers
= http_copy_default_headers();
840 struct strbuf buf
= STRBUF_INIT
;
843 slot
= get_active_slot();
845 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
846 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
848 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
849 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
850 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
851 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
852 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
853 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
854 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
855 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
856 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &buf
);
858 err
= run_slot(slot
, results
);
860 curl_slist_free_all(headers
);
861 strbuf_release(&buf
);
865 static curl_off_t
xcurl_off_t(size_t len
)
867 uintmax_t size
= len
;
868 if (size
> maximum_signed_value_of_type(curl_off_t
))
869 die(_("cannot handle pushes this big"));
870 return (curl_off_t
)size
;
874 * If flush_received is true, do not attempt to read any more; just use what's
877 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
879 struct active_request_slot
*slot
;
880 struct curl_slist
*headers
= http_copy_default_headers();
881 int use_gzip
= rpc
->gzip_request
;
882 char *gzip_body
= NULL
;
883 size_t gzip_size
= 0;
884 int err
, large_request
= 0;
885 int needs_100_continue
= 0;
886 struct rpc_in_data rpc_in_data
;
888 /* Try to load the entire request, if we can fit it into the
889 * allocated buffer space we can use HTTP/1.0 and avoid the
890 * chunked encoding mess.
892 if (!flush_received
) {
895 enum packet_read_status status
;
897 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
902 if (status
== PACKET_READ_FLUSH
)
908 struct slot_results results
;
911 err
= probe_rpc(rpc
, &results
);
912 if (err
== HTTP_REAUTH
)
913 credential_fill(&http_auth
);
914 } while (err
== HTTP_REAUTH
);
918 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
919 needs_100_continue
= 1;
922 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
923 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
924 headers
= curl_slist_append(headers
, needs_100_continue
?
925 "Expect: 100-continue" : "Expect:");
927 /* Add the extra Git-Protocol header */
928 if (rpc
->protocol_header
)
929 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
932 slot
= get_active_slot();
934 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
935 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
936 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
937 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
940 /* The request body is large and the size cannot be predicted.
941 * We must use chunked encoding to send it.
943 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
944 rpc
->initial_buffer
= 1;
945 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
946 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
947 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
948 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
949 if (options
.verbosity
> 1) {
950 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
954 } else if (gzip_body
) {
956 * If we are looping to retry authentication, then the previous
957 * run will have set up the headers and gzip buffer already,
958 * and we just need to send it.
960 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
961 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
963 } else if (use_gzip
&& 1024 < rpc
->len
) {
964 /* The client backend isn't giving us compressed data so
965 * we can try to deflate it ourselves, this may save on
971 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
972 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
973 gzip_body
= xmalloc(gzip_size
);
975 stream
.next_in
= (unsigned char *)rpc
->buf
;
976 stream
.avail_in
= rpc
->len
;
977 stream
.next_out
= (unsigned char *)gzip_body
;
978 stream
.avail_out
= gzip_size
;
980 ret
= git_deflate(&stream
, Z_FINISH
);
981 if (ret
!= Z_STREAM_END
)
982 die(_("cannot deflate request; zlib deflate error %d"), ret
);
984 ret
= git_deflate_end_gently(&stream
);
986 die(_("cannot deflate request; zlib end error %d"), ret
);
988 gzip_size
= stream
.total_out
;
990 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
991 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
992 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
994 if (options
.verbosity
> 1) {
995 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
997 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1001 /* We know the complete request size in advance, use the
1002 * more normal Content-Length approach.
1004 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1005 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1006 if (options
.verbosity
> 1) {
1007 fprintf(stderr
, "POST %s (%lu bytes)\n",
1008 rpc
->service_name
, (unsigned long)rpc
->len
);
1013 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1014 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1015 rpc_in_data
.rpc
= rpc
;
1016 rpc_in_data
.slot
= slot
;
1017 rpc_in_data
.check_pktline
= stateless_connect
;
1018 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1019 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &rpc_in_data
);
1020 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1023 rpc
->any_written
= 0;
1024 err
= run_slot(slot
, NULL
);
1025 if (err
== HTTP_REAUTH
&& !large_request
) {
1026 credential_fill(&http_auth
);
1032 if (!rpc
->any_written
)
1035 if (rpc_in_data
.pktline_state
.len_filled
)
1036 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1037 if (rpc_in_data
.pktline_state
.remaining
)
1038 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1040 if (stateless_connect
)
1041 packet_response_end(rpc
->in
);
1043 curl_slist_free_all(headers
);
1048 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1049 const char **client_argv
, const struct strbuf
*preamble
,
1050 struct strbuf
*rpc_result
)
1052 const char *svc
= rpc
->service_name
;
1053 struct strbuf buf
= STRBUF_INIT
;
1054 struct child_process client
= CHILD_PROCESS_INIT
;
1060 client
.argv
= client_argv
;
1061 if (start_command(&client
))
1063 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1065 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1067 rpc
->alloc
= http_post_buffer
;
1068 rpc
->buf
= xmalloc(rpc
->alloc
);
1069 rpc
->in
= client
.in
;
1070 rpc
->out
= client
.out
;
1072 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1073 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1075 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1076 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1078 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1079 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1081 if (get_protocol_http_header(heads
->version
, &buf
))
1082 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1084 rpc
->protocol_header
= NULL
;
1087 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
1092 err
|= post_rpc(rpc
, 0, 0);
1098 strbuf_read(rpc_result
, client
.out
, 0);
1102 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1109 err
|= finish_command(&client
);
1110 free(rpc
->service_url
);
1111 free(rpc
->hdr_content_type
);
1112 free(rpc
->hdr_accept
);
1113 free(rpc
->protocol_header
);
1115 strbuf_release(&buf
);
1119 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1121 struct walker
*walker
;
1125 ALLOC_ARRAY(targets
, nr_heads
);
1126 if (options
.depth
|| options
.deepen_since
)
1127 die(_("dumb http transport does not support shallow capabilities"));
1128 for (i
= 0; i
< nr_heads
; i
++)
1129 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1131 walker
= get_http_walker(url
.buf
);
1132 walker
->get_verbosely
= options
.verbosity
>= 3;
1133 walker
->get_progress
= options
.progress
;
1134 walker
->get_recover
= 0;
1135 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1136 walker_free(walker
);
1138 for (i
= 0; i
< nr_heads
; i
++)
1142 return ret
? error(_("fetch failed.")) : 0;
1145 static int fetch_git(struct discovery
*heads
,
1146 int nr_heads
, struct ref
**to_fetch
)
1148 struct rpc_state rpc
;
1149 struct strbuf preamble
= STRBUF_INIT
;
1151 struct strvec args
= STRVEC_INIT
;
1152 struct strbuf rpc_result
= STRBUF_INIT
;
1154 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1155 "--stdin", "--lock-pack", NULL
);
1156 if (options
.followtags
)
1157 strvec_push(&args
, "--include-tag");
1159 strvec_push(&args
, "--thin");
1160 if (options
.verbosity
>= 3)
1161 strvec_pushl(&args
, "-v", "-v", NULL
);
1162 if (options
.check_self_contained_and_connected
)
1163 strvec_push(&args
, "--check-self-contained-and-connected");
1164 if (options
.cloning
)
1165 strvec_push(&args
, "--cloning");
1166 if (options
.update_shallow
)
1167 strvec_push(&args
, "--update-shallow");
1168 if (!options
.progress
)
1169 strvec_push(&args
, "--no-progress");
1171 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1172 if (options
.deepen_since
)
1173 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1174 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1175 strvec_pushf(&args
, "--shallow-exclude=%s",
1176 options
.deepen_not
.items
[i
].string
);
1177 if (options
.deepen_relative
&& options
.depth
)
1178 strvec_push(&args
, "--deepen-relative");
1179 if (options
.from_promisor
)
1180 strvec_push(&args
, "--from-promisor");
1182 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1183 strvec_push(&args
, url
.buf
);
1185 for (i
= 0; i
< nr_heads
; i
++) {
1186 struct ref
*ref
= to_fetch
[i
];
1188 die(_("cannot fetch by sha1 over smart http"));
1189 packet_buf_write(&preamble
, "%s %s\n",
1190 oid_to_hex(&ref
->old_oid
), ref
->name
);
1192 packet_buf_flush(&preamble
);
1194 memset(&rpc
, 0, sizeof(rpc
));
1195 rpc
.service_name
= "git-upload-pack",
1196 rpc
.gzip_request
= 1;
1198 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1200 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1201 strbuf_release(&rpc_result
);
1202 strbuf_release(&preamble
);
1203 strvec_clear(&args
);
1207 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1209 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1211 return fetch_git(d
, nr_heads
, to_fetch
);
1213 return fetch_dumb(nr_heads
, to_fetch
);
1216 static void parse_fetch(struct strbuf
*buf
)
1218 struct ref
**to_fetch
= NULL
;
1219 struct ref
*list_head
= NULL
;
1220 struct ref
**list
= &list_head
;
1221 int alloc_heads
= 0, nr_heads
= 0;
1225 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1228 struct object_id old_oid
;
1231 if (parse_oid_hex(p
, &old_oid
, &q
))
1232 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1238 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1240 ref
= alloc_ref(name
);
1241 oidcpy(&ref
->old_oid
, &old_oid
);
1246 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1247 to_fetch
[nr_heads
++] = ref
;
1250 die(_("http transport does not support %s"), buf
->buf
);
1253 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1259 if (fetch(nr_heads
, to_fetch
))
1260 exit(128); /* error already reported */
1261 free_refs(list_head
);
1269 static int push_dav(int nr_spec
, const char **specs
)
1271 struct child_process child
= CHILD_PROCESS_INIT
;
1275 strvec_push(&child
.args
, "http-push");
1276 strvec_push(&child
.args
, "--helper-status");
1277 if (options
.dry_run
)
1278 strvec_push(&child
.args
, "--dry-run");
1279 if (options
.verbosity
> 1)
1280 strvec_push(&child
.args
, "--verbose");
1281 strvec_push(&child
.args
, url
.buf
);
1282 for (i
= 0; i
< nr_spec
; i
++)
1283 strvec_push(&child
.args
, specs
[i
]);
1285 if (run_command(&child
))
1286 die(_("git-http-push failed"));
1290 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1292 struct rpc_state rpc
;
1295 struct string_list_item
*cas_option
;
1296 struct strbuf preamble
= STRBUF_INIT
;
1297 struct strbuf rpc_result
= STRBUF_INIT
;
1300 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1304 strvec_push(&args
, "--thin");
1305 if (options
.dry_run
)
1306 strvec_push(&args
, "--dry-run");
1307 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1308 strvec_push(&args
, "--signed=yes");
1309 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1310 strvec_push(&args
, "--signed=if-asked");
1312 strvec_push(&args
, "--atomic");
1313 if (options
.verbosity
== 0)
1314 strvec_push(&args
, "--quiet");
1315 else if (options
.verbosity
> 1)
1316 strvec_push(&args
, "--verbose");
1317 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1318 strvec_pushf(&args
, "--push-option=%s",
1319 options
.push_options
.items
[i
].string
);
1320 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1321 for_each_string_list_item(cas_option
, &cas_options
)
1322 strvec_push(&args
, cas_option
->string
);
1323 strvec_push(&args
, url
.buf
);
1325 if (options
.force_if_includes
)
1326 strvec_push(&args
, "--force-if-includes");
1328 strvec_push(&args
, "--stdin");
1329 for (i
= 0; i
< nr_spec
; i
++)
1330 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1331 packet_buf_flush(&preamble
);
1333 memset(&rpc
, 0, sizeof(rpc
));
1334 rpc
.service_name
= "git-receive-pack",
1336 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1338 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1339 strbuf_release(&rpc_result
);
1340 strbuf_release(&preamble
);
1341 strvec_clear(&args
);
1345 static int push(int nr_spec
, const char **specs
)
1347 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1350 if (heads
->proto_git
)
1351 ret
= push_git(heads
, nr_spec
, specs
);
1353 ret
= push_dav(nr_spec
, specs
);
1354 free_discovery(heads
);
1358 static void parse_push(struct strbuf
*buf
)
1360 struct strvec specs
= STRVEC_INIT
;
1365 if (skip_prefix(buf
->buf
, "push ", &arg
))
1366 strvec_push(&specs
, arg
);
1368 die(_("http transport does not support %s"), buf
->buf
);
1371 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1377 ret
= push(specs
.nr
, specs
.v
);
1382 exit(128); /* error already reported */
1385 strvec_clear(&specs
);
1388 static int stateless_connect(const char *service_name
)
1390 struct discovery
*discover
;
1391 struct rpc_state rpc
;
1392 struct strbuf buf
= STRBUF_INIT
;
1395 * Run the info/refs request and see if the server supports protocol
1396 * v2. If and only if the server supports v2 can we successfully
1397 * establish a stateless connection, otherwise we need to tell the
1398 * client to fallback to using other transport helper functions to
1399 * complete their request.
1401 discover
= discover_refs(service_name
, 0);
1402 if (discover
->version
!= protocol_v2
) {
1403 printf("fallback\n");
1407 /* Stateless Connection established */
1412 rpc
.service_name
= service_name
;
1413 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1414 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1415 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1416 if (get_protocol_http_header(discover
->version
, &buf
)) {
1417 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1419 rpc
.protocol_header
= NULL
;
1420 strbuf_release(&buf
);
1422 rpc
.buf
= xmalloc(http_post_buffer
);
1423 rpc
.alloc
= http_post_buffer
;
1428 rpc
.any_written
= 0;
1429 rpc
.gzip_request
= 1;
1430 rpc
.initial_buffer
= 0;
1431 rpc
.write_line_lengths
= 1;
1432 rpc
.flush_read_but_not_sent
= 0;
1435 * Dump the capability listing that we got from the server earlier
1436 * during the info/refs request.
1438 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1440 /* Until we see EOF keep sending POSTs */
1443 enum packet_read_status status
;
1445 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1447 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1448 if (status
== PACKET_READ_EOF
)
1450 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1451 /* We would have an err here */
1453 /* Reset the buffer for next request */
1457 free(rpc
.service_url
);
1458 free(rpc
.hdr_content_type
);
1459 free(rpc
.hdr_accept
);
1460 free(rpc
.protocol_header
);
1462 strbuf_release(&buf
);
1467 int cmd_main(int argc
, const char **argv
)
1469 struct strbuf buf
= STRBUF_INIT
;
1472 setup_git_directory_gently(&nongit
);
1474 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1478 options
.verbosity
= 1;
1479 options
.progress
= !!isatty(2);
1481 string_list_init(&options
.deepen_not
, 1);
1482 string_list_init(&options
.push_options
, 1);
1485 * Just report "remote-curl" here (folding all the various aliases
1486 * ("git-remote-http", "git-remote-https", and etc.) here since they
1487 * are all just copies of the same actual executable.
1489 trace2_cmd_name("remote-curl");
1491 remote
= remote_get(argv
[1]);
1494 end_url_with_slash(&url
, argv
[2]);
1496 end_url_with_slash(&url
, remote
->url
[0]);
1499 http_init(remote
, url
.buf
, 0);
1504 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1506 error(_("remote-curl: error reading command stream from git"));
1511 if (starts_with(buf
.buf
, "fetch ")) {
1513 die(_("remote-curl: fetch attempted without a local repo"));
1516 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1517 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1518 output_refs(get_refs(for_push
));
1520 } else if (starts_with(buf
.buf
, "push ")) {
1523 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1524 char *value
= strchr(arg
, ' ');
1532 result
= set_option(arg
, value
);
1535 else if (result
< 0)
1536 printf("error invalid value\n");
1538 printf("unsupported\n");
1541 } else if (!strcmp(buf
.buf
, "capabilities")) {
1542 printf("stateless-connect\n");
1546 printf("check-connectivity\n");
1547 printf("object-format\n");
1550 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1551 if (!stateless_connect(arg
))
1554 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);