1 #include "git-compat-util.h"
3 #include "environment.h"
11 #include "run-command.h"
13 #include "string-list.h"
15 #include "credential.h"
16 #include "oid-array.h"
17 #include "send-pack.h"
22 #include "transport.h"
24 #include "write-or-die.h"
26 static struct remote
*remote
;
27 /* always ends with a trailing slash */
28 static struct strbuf url
= STRBUF_INIT
;
34 struct string_list deepen_not
;
35 struct string_list push_options
;
37 unsigned progress
: 1,
38 check_self_contained_and_connected
: 1,
44 /* One of the SEND_PACK_PUSH_CERT_* constants. */
48 /* see documentation of corresponding flag in fetch-pack.h */
54 force_if_includes
: 1;
55 const struct git_hash_algo
*hash_algo
;
57 static struct options options
;
58 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
60 static int set_option(const char *name
, const char *value
)
62 if (!strcmp(name
, "verbosity")) {
64 int v
= strtol(value
, &end
, 10);
65 if (value
== end
|| *end
)
67 options
.verbosity
= v
;
70 else if (!strcmp(name
, "progress")) {
71 if (!strcmp(value
, "true"))
73 else if (!strcmp(value
, "false"))
79 else if (!strcmp(name
, "depth")) {
81 unsigned long v
= strtoul(value
, &end
, 10);
82 if (value
== end
|| *end
)
87 else if (!strcmp(name
, "deepen-since")) {
88 options
.deepen_since
= xstrdup(value
);
91 else if (!strcmp(name
, "deepen-not")) {
92 string_list_append(&options
.deepen_not
, value
);
95 else if (!strcmp(name
, "deepen-relative")) {
96 if (!strcmp(value
, "true"))
97 options
.deepen_relative
= 1;
98 else if (!strcmp(value
, "false"))
99 options
.deepen_relative
= 0;
104 else if (!strcmp(name
, "followtags")) {
105 if (!strcmp(value
, "true"))
106 options
.followtags
= 1;
107 else if (!strcmp(value
, "false"))
108 options
.followtags
= 0;
113 else if (!strcmp(name
, "dry-run")) {
114 if (!strcmp(value
, "true"))
116 else if (!strcmp(value
, "false"))
122 else if (!strcmp(name
, "check-connectivity")) {
123 if (!strcmp(value
, "true"))
124 options
.check_self_contained_and_connected
= 1;
125 else if (!strcmp(value
, "false"))
126 options
.check_self_contained_and_connected
= 0;
131 else if (!strcmp(name
, "cas")) {
132 struct strbuf val
= STRBUF_INIT
;
133 strbuf_addstr(&val
, "--force-with-lease=");
135 strbuf_addstr(&val
, value
);
136 else if (unquote_c_style(&val
, value
, NULL
))
138 string_list_append(&cas_options
, val
.buf
);
139 strbuf_release(&val
);
141 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
142 if (!strcmp(value
, "true"))
143 options
.force_if_includes
= 1;
144 else if (!strcmp(value
, "false"))
145 options
.force_if_includes
= 0;
149 } else if (!strcmp(name
, "cloning")) {
150 if (!strcmp(value
, "true"))
152 else if (!strcmp(value
, "false"))
157 } else if (!strcmp(name
, "update-shallow")) {
158 if (!strcmp(value
, "true"))
159 options
.update_shallow
= 1;
160 else if (!strcmp(value
, "false"))
161 options
.update_shallow
= 0;
165 } else if (!strcmp(name
, "pushcert")) {
166 if (!strcmp(value
, "true"))
167 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
168 else if (!strcmp(value
, "false"))
169 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
170 else if (!strcmp(value
, "if-asked"))
171 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
175 } else if (!strcmp(name
, "atomic")) {
176 if (!strcmp(value
, "true"))
178 else if (!strcmp(value
, "false"))
183 } else if (!strcmp(name
, "push-option")) {
185 string_list_append(&options
.push_options
, value
);
187 struct strbuf unquoted
= STRBUF_INIT
;
188 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
189 die(_("invalid quoting in push-option value: '%s'"), value
);
190 string_list_append_nodup(&options
.push_options
,
191 strbuf_detach(&unquoted
, NULL
));
194 } else if (!strcmp(name
, "family")) {
195 if (!strcmp(value
, "ipv4"))
196 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
197 else if (!strcmp(value
, "ipv6"))
198 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
199 else if (!strcmp(value
, "all"))
200 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
204 } else if (!strcmp(name
, "from-promisor")) {
205 options
.from_promisor
= 1;
207 } else if (!strcmp(name
, "refetch")) {
210 } else if (!strcmp(name
, "filter")) {
211 options
.filter
= xstrdup(value
);
213 } else if (!strcmp(name
, "object-format")) {
215 options
.object_format
= 1;
216 if (strcmp(value
, "true")) {
217 algo
= hash_algo_by_name(value
);
218 if (algo
== GIT_HASH_UNKNOWN
)
219 die("unknown object format '%s'", value
);
220 options
.hash_algo
= &hash_algos
[algo
];
224 return 1 /* unsupported */;
234 struct oid_array shallow
;
235 enum protocol_version version
;
236 unsigned proto_git
: 1;
238 static struct discovery
*last_discovery
;
240 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
242 struct ref
*list
= NULL
;
243 struct packet_reader reader
;
245 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
246 PACKET_READ_CHOMP_NEWLINE
|
247 PACKET_READ_GENTLE_ON_EOF
|
248 PACKET_READ_DIE_ON_ERR_PACKET
);
250 heads
->version
= discover_version(&reader
);
251 switch (heads
->version
) {
254 * Do nothing. This isn't a list of refs but rather a
255 * capability advertisement. Client would have run
256 * 'stateless-connect' so we'll dump this capability listing
257 * and let them request the refs themselves.
262 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
263 NULL
, &heads
->shallow
);
264 options
.hash_algo
= reader
.hash_algo
;
266 case protocol_unknown_version
:
267 BUG("unknown protocol version");
273 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
275 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
278 return the_hash_algo
;
280 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
281 if (algo
== GIT_HASH_UNKNOWN
)
283 return &hash_algos
[algo
];
286 static struct ref
*parse_info_refs(struct discovery
*heads
)
288 char *data
, *start
, *mid
;
292 struct ref
*refs
= NULL
;
293 struct ref
*ref
= NULL
;
294 struct ref
*last_ref
= NULL
;
296 options
.hash_algo
= detect_hash_algo(heads
);
297 if (!options
.hash_algo
)
298 die("%sinfo/refs not valid: could not determine hash algorithm; "
299 "is this a git repository?",
300 transport_anonymize_url(url
.buf
));
305 while (i
< heads
->len
) {
311 if (data
[i
] == '\n') {
312 if (mid
- start
!= options
.hash_algo
->hexsz
)
313 die(_("%sinfo/refs not valid: is this a git repository?"),
314 transport_anonymize_url(url
.buf
));
317 ref
= alloc_ref(ref_name
);
318 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
322 last_ref
->next
= ref
;
329 ref
= alloc_ref("HEAD");
330 if (!http_fetch_ref(url
.buf
, ref
) &&
331 !resolve_remote_symref(ref
, refs
)) {
341 static void free_discovery(struct discovery
*d
)
344 if (d
== last_discovery
)
345 last_discovery
= NULL
;
346 free(d
->shallow
.oid
);
354 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
360 * We only show text/plain parts, as other types are likely
361 * to be ugly to look at on the user's terminal.
363 if (strcmp(type
->buf
, "text/plain"))
366 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
374 eol
= strchrnul(p
, '\n');
375 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
381 static int get_protocol_http_header(enum protocol_version version
,
382 struct strbuf
*header
)
385 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
394 static void check_smart_http(struct discovery
*d
, const char *service
,
398 struct packet_reader reader
;
401 * If we don't see x-$service-advertisement, then it's not smart-http.
402 * But once we do, we commit to it and assume any other protocol
403 * violations are hard errors.
405 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
406 !skip_prefix(p
, service
, &p
) ||
407 strcmp(p
, "-advertisement"))
410 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
411 PACKET_READ_CHOMP_NEWLINE
|
412 PACKET_READ_DIE_ON_ERR_PACKET
);
413 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
414 die(_("invalid server response; expected service, got flush packet"));
416 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
418 * The header can include additional metadata lines, up
419 * until a packet flush marker. Ignore these now, but
420 * in the future we might start to scan them.
423 packet_reader_read(&reader
);
424 if (reader
.pktlen
<= 0) {
430 * v0 smart http; callers expect us to soak up the
431 * service and header packets
433 d
->buf
= reader
.src_buffer
;
434 d
->len
= reader
.src_len
;
437 } else if (!strcmp(reader
.line
, "version 2")) {
439 * v2 smart http; do not consume version packet, which will
440 * be handled elsewhere.
445 die(_("invalid server response; got '%s'"), reader
.line
);
449 static struct discovery
*discover_refs(const char *service
, int for_push
)
451 struct strbuf type
= STRBUF_INIT
;
452 struct strbuf charset
= STRBUF_INIT
;
453 struct strbuf buffer
= STRBUF_INIT
;
454 struct strbuf refs_url
= STRBUF_INIT
;
455 struct strbuf effective_url
= STRBUF_INIT
;
456 struct strbuf protocol_header
= STRBUF_INIT
;
457 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
458 struct discovery
*last
= last_discovery
;
459 int http_ret
, maybe_smart
= 0;
460 struct http_get_options http_options
;
461 enum protocol_version version
= get_protocol_version_config();
463 if (last
&& !strcmp(service
, last
->service
))
465 free_discovery(last
);
467 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
468 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
469 git_env_bool("GIT_SMART_HTTP", 1)) {
471 if (!strchr(url
.buf
, '?'))
472 strbuf_addch(&refs_url
, '?');
474 strbuf_addch(&refs_url
, '&');
475 strbuf_addf(&refs_url
, "service=%s", service
);
479 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
480 * to perform any operation that doesn't involve upload-pack (i.e., a
481 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
482 * to do anything else (like push or remote archive) via v2.
484 if (version
== protocol_v2
&& strcmp("git-upload-pack", service
))
485 version
= protocol_v0
;
487 /* Add the extra Git-Protocol header */
488 if (get_protocol_http_header(version
, &protocol_header
))
489 string_list_append(&extra_headers
, protocol_header
.buf
);
491 memset(&http_options
, 0, sizeof(http_options
));
492 http_options
.content_type
= &type
;
493 http_options
.charset
= &charset
;
494 http_options
.effective_url
= &effective_url
;
495 http_options
.base_url
= &url
;
496 http_options
.extra_headers
= &extra_headers
;
497 http_options
.initial_request
= 1;
498 http_options
.no_cache
= 1;
500 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
504 case HTTP_MISSING_TARGET
:
505 show_http_message(&type
, &charset
, &buffer
);
506 die(_("repository '%s' not found"),
507 transport_anonymize_url(url
.buf
));
509 show_http_message(&type
, &charset
, &buffer
);
510 die(_("Authentication failed for '%s'"),
511 transport_anonymize_url(url
.buf
));
512 case HTTP_NOMATCHPUBLICKEY
:
513 show_http_message(&type
, &charset
, &buffer
);
514 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
515 transport_anonymize_url(url
.buf
), curl_errorstr
);
517 show_http_message(&type
, &charset
, &buffer
);
518 die(_("unable to access '%s': %s"),
519 transport_anonymize_url(url
.buf
), curl_errorstr
);
522 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
523 char *u
= transport_anonymize_url(url
.buf
);
524 warning(_("redirecting to %s"), u
);
528 last
= xcalloc(1, sizeof(*last_discovery
));
529 last
->service
= xstrdup(service
);
530 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
531 last
->buf
= last
->buf_alloc
;
534 check_smart_http(last
, service
, &type
);
537 last
->refs
= parse_git_refs(last
, for_push
);
539 last
->refs
= parse_info_refs(last
);
541 strbuf_release(&refs_url
);
542 strbuf_release(&type
);
543 strbuf_release(&charset
);
544 strbuf_release(&effective_url
);
545 strbuf_release(&buffer
);
546 strbuf_release(&protocol_header
);
547 string_list_clear(&extra_headers
, 0);
548 last_discovery
= last
;
552 static struct ref
*get_refs(int for_push
)
554 struct discovery
*heads
;
557 heads
= discover_refs("git-receive-pack", for_push
);
559 heads
= discover_refs("git-upload-pack", for_push
);
564 static void output_refs(struct ref
*refs
)
567 if (options
.object_format
&& options
.hash_algo
) {
568 printf(":object-format %s\n", options
.hash_algo
->name
);
569 repo_set_hash_algo(the_repository
,
570 hash_algo_by_ptr(options
.hash_algo
));
572 for (posn
= refs
; posn
; posn
= posn
->next
) {
574 printf("@%s %s\n", posn
->symref
, posn
->name
);
576 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
585 const char *service_name
;
587 char *hdr_content_type
;
589 char *hdr_accept_language
;
590 char *protocol_header
;
598 unsigned gzip_request
: 1;
599 unsigned initial_buffer
: 1;
602 * Whenever a pkt-line is read into buf, append the 4 characters
603 * denoting its length before appending the payload.
605 unsigned write_line_lengths
: 1;
608 * Used by rpc_out; initialize to 0. This is true if a flush has been
609 * read, but the corresponding line length (if write_line_lengths is
610 * true) and EOF have not been sent to libcurl. Since each flush marks
611 * the end of a request, each flush must be completely sent before any
612 * further reading occurs.
614 unsigned flush_read_but_not_sent
: 1;
617 #define RPC_STATE_INIT { 0 }
620 * Appends the result of reading from rpc->out to the string represented by
621 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
622 * enough space, 0 otherwise.
624 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
625 * hexadecimal string before appending the result described above.
627 * Writes the total number of bytes appended into appended.
629 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
631 enum packet_read_status
*status
) {
636 if (rpc
->write_line_lengths
) {
637 left
= rpc
->alloc
- rpc
->len
- 4;
638 buf
= rpc
->buf
+ rpc
->len
+ 4;
640 left
= rpc
->alloc
- rpc
->len
;
641 buf
= rpc
->buf
+ rpc
->len
;
644 if (left
< LARGE_PACKET_MAX
)
647 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
648 left
, &pktlen_raw
, options
);
649 if (*status
!= PACKET_READ_EOF
) {
650 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
651 rpc
->len
+= *appended
;
654 if (rpc
->write_line_lengths
) {
656 case PACKET_READ_EOF
:
657 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
658 die(_("shouldn't have EOF when not gentle on EOF"));
660 case PACKET_READ_NORMAL
:
661 set_packet_header(buf
- 4, *appended
);
663 case PACKET_READ_DELIM
:
664 memcpy(buf
- 4, "0001", 4);
666 case PACKET_READ_FLUSH
:
667 memcpy(buf
- 4, "0000", 4);
669 case PACKET_READ_RESPONSE_END
:
670 die(_("remote server sent unexpected response end packet"));
677 static size_t rpc_out(void *ptr
, size_t eltsize
,
678 size_t nmemb
, void *buffer_
)
680 size_t max
= eltsize
* nmemb
;
681 struct rpc_state
*rpc
= buffer_
;
682 size_t avail
= rpc
->len
- rpc
->pos
;
683 enum packet_read_status status
;
686 rpc
->initial_buffer
= 0;
689 if (!rpc
->flush_read_but_not_sent
) {
690 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
691 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
692 if (status
== PACKET_READ_FLUSH
)
693 rpc
->flush_read_but_not_sent
= 1;
696 * If flush_read_but_not_sent is true, we have already read one
697 * full request but have not fully sent it + EOF, which is why
698 * we need to refrain from reading.
701 if (rpc
->flush_read_but_not_sent
) {
704 * The line length either does not need to be sent at
705 * all or has already been completely sent. Now we can
706 * return 0, indicating EOF, meaning that the flush has
709 rpc
->flush_read_but_not_sent
= 0;
713 * If avail is non-zero, the line length for the flush still
714 * hasn't been fully sent. Proceed with sending the line
721 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
726 static int rpc_seek(void *clientp
, curl_off_t offset
, int origin
)
728 struct rpc_state
*rpc
= clientp
;
730 if (origin
!= SEEK_SET
)
731 BUG("rpc_seek only handles SEEK_SET, not %d", origin
);
733 if (rpc
->initial_buffer
) {
734 if (offset
< 0 || offset
> rpc
->len
) {
735 error("curl seek would be outside of rpc buffer");
736 return CURL_SEEKFUNC_FAIL
;
739 return CURL_SEEKFUNC_OK
;
741 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
742 return CURL_SEEKFUNC_FAIL
;
745 struct check_pktline_state
{
751 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
754 if (!state
->remaining
) {
755 int digits_remaining
= 4 - state
->len_filled
;
756 if (digits_remaining
> size
)
757 digits_remaining
= size
;
758 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
759 state
->len_filled
+= digits_remaining
;
760 ptr
+= digits_remaining
;
761 size
-= digits_remaining
;
763 if (state
->len_filled
== 4) {
764 state
->remaining
= packet_length(state
->len_buf
,
765 sizeof(state
->len_buf
));
766 if (state
->remaining
< 0) {
767 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
768 } else if (state
->remaining
== 2) {
769 die(_("remote-curl: unexpected response end packet"));
770 } else if (state
->remaining
< 4) {
771 state
->remaining
= 0;
773 state
->remaining
-= 4;
775 state
->len_filled
= 0;
779 if (state
->remaining
) {
780 int remaining
= state
->remaining
;
781 if (remaining
> size
)
785 state
->remaining
-= remaining
;
791 struct rpc_state
*rpc
;
792 struct active_request_slot
*slot
;
794 struct check_pktline_state pktline_state
;
798 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
801 static size_t rpc_in(char *ptr
, size_t eltsize
,
802 size_t nmemb
, void *buffer_
)
804 size_t size
= eltsize
* nmemb
;
805 struct rpc_in_data
*data
= buffer_
;
808 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
809 &response_code
) != CURLE_OK
)
811 if (response_code
>= 300)
814 data
->rpc
->any_written
= 1;
815 if (data
->check_pktline
)
816 check_pktline(&data
->pktline_state
, ptr
, size
);
817 write_or_die(data
->rpc
->in
, ptr
, size
);
821 static int run_slot(struct active_request_slot
*slot
,
822 struct slot_results
*results
)
825 struct slot_results results_buf
;
828 results
= &results_buf
;
830 err
= run_one_slot(slot
, results
);
832 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
833 struct strbuf msg
= STRBUF_INIT
;
834 if (results
->http_code
&& results
->http_code
!= 200)
835 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
836 if (results
->curl_result
!= CURLE_OK
) {
838 strbuf_addch(&msg
, ' ');
839 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
840 if (curl_errorstr
[0]) {
841 strbuf_addch(&msg
, ' ');
842 strbuf_addstr(&msg
, curl_errorstr
);
845 error(_("RPC failed; %s"), msg
.buf
);
846 strbuf_release(&msg
);
852 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
854 struct active_request_slot
*slot
;
855 struct curl_slist
*headers
= http_copy_default_headers();
856 struct strbuf buf
= STRBUF_INIT
;
859 slot
= get_active_slot();
861 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
862 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
864 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
865 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
866 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
867 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
868 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
869 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
870 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
871 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
872 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &buf
);
874 err
= run_slot(slot
, results
);
876 curl_slist_free_all(headers
);
877 strbuf_release(&buf
);
881 static curl_off_t
xcurl_off_t(size_t len
)
883 uintmax_t size
= len
;
884 if (size
> maximum_signed_value_of_type(curl_off_t
))
885 die(_("cannot handle pushes this big"));
886 return (curl_off_t
)size
;
890 * If flush_received is true, do not attempt to read any more; just use what's
893 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
895 struct active_request_slot
*slot
;
896 struct curl_slist
*headers
= http_copy_default_headers();
897 int use_gzip
= rpc
->gzip_request
;
898 char *gzip_body
= NULL
;
899 size_t gzip_size
= 0;
900 int err
, large_request
= 0;
901 int needs_100_continue
= 0;
902 struct rpc_in_data rpc_in_data
;
904 /* Try to load the entire request, if we can fit it into the
905 * allocated buffer space we can use HTTP/1.0 and avoid the
906 * chunked encoding mess.
908 if (!flush_received
) {
911 enum packet_read_status status
;
913 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
918 if (status
== PACKET_READ_FLUSH
)
924 struct slot_results results
;
927 err
= probe_rpc(rpc
, &results
);
928 if (err
== HTTP_REAUTH
)
929 credential_fill(&http_auth
);
930 } while (err
== HTTP_REAUTH
);
934 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
935 needs_100_continue
= 1;
938 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
939 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
940 headers
= curl_slist_append(headers
, needs_100_continue
?
941 "Expect: 100-continue" : "Expect:");
943 /* Add Accept-Language header */
944 if (rpc
->hdr_accept_language
)
945 headers
= curl_slist_append(headers
, rpc
->hdr_accept_language
);
947 /* Add the extra Git-Protocol header */
948 if (rpc
->protocol_header
)
949 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
952 slot
= get_active_slot();
954 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
955 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
956 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
957 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
960 /* The request body is large and the size cannot be predicted.
961 * We must use chunked encoding to send it.
963 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
964 rpc
->initial_buffer
= 1;
965 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
966 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
967 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKFUNCTION
, rpc_seek
);
968 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKDATA
, rpc
);
969 if (options
.verbosity
> 1) {
970 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
974 } else if (gzip_body
) {
976 * If we are looping to retry authentication, then the previous
977 * run will have set up the headers and gzip buffer already,
978 * and we just need to send it.
980 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
981 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
983 } else if (use_gzip
&& 1024 < rpc
->len
) {
984 /* The client backend isn't giving us compressed data so
985 * we can try to deflate it ourselves, this may save on
991 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
992 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
993 gzip_body
= xmalloc(gzip_size
);
995 stream
.next_in
= (unsigned char *)rpc
->buf
;
996 stream
.avail_in
= rpc
->len
;
997 stream
.next_out
= (unsigned char *)gzip_body
;
998 stream
.avail_out
= gzip_size
;
1000 ret
= git_deflate(&stream
, Z_FINISH
);
1001 if (ret
!= Z_STREAM_END
)
1002 die(_("cannot deflate request; zlib deflate error %d"), ret
);
1004 ret
= git_deflate_end_gently(&stream
);
1006 die(_("cannot deflate request; zlib end error %d"), ret
);
1008 gzip_size
= stream
.total_out
;
1010 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
1011 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
1012 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
1014 if (options
.verbosity
> 1) {
1015 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
1017 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1021 /* We know the complete request size in advance, use the
1022 * more normal Content-Length approach.
1024 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1025 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1026 if (options
.verbosity
> 1) {
1027 fprintf(stderr
, "POST %s (%lu bytes)\n",
1028 rpc
->service_name
, (unsigned long)rpc
->len
);
1033 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1034 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1035 rpc_in_data
.rpc
= rpc
;
1036 rpc_in_data
.slot
= slot
;
1037 rpc_in_data
.check_pktline
= stateless_connect
;
1038 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1039 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &rpc_in_data
);
1040 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1043 rpc
->any_written
= 0;
1044 err
= run_slot(slot
, NULL
);
1045 if (err
== HTTP_REAUTH
&& !large_request
) {
1046 credential_fill(&http_auth
);
1052 if (!rpc
->any_written
)
1055 if (rpc_in_data
.pktline_state
.len_filled
)
1056 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1057 if (rpc_in_data
.pktline_state
.remaining
)
1058 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1060 if (stateless_connect
)
1061 packet_response_end(rpc
->in
);
1063 curl_slist_free_all(headers
);
1068 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1069 const char **client_argv
, const struct strbuf
*preamble
,
1070 struct strbuf
*rpc_result
)
1072 const char *svc
= rpc
->service_name
;
1073 struct strbuf buf
= STRBUF_INIT
;
1074 struct child_process client
= CHILD_PROCESS_INIT
;
1080 strvec_pushv(&client
.args
, client_argv
);
1081 if (start_command(&client
))
1083 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1085 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1087 rpc
->alloc
= http_post_buffer
;
1088 rpc
->buf
= xmalloc(rpc
->alloc
);
1089 rpc
->in
= client
.in
;
1090 rpc
->out
= client
.out
;
1092 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1093 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1095 rpc
->hdr_accept_language
= xstrdup_or_null(http_get_accept_language_header());
1097 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1098 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1100 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1101 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1103 if (get_protocol_http_header(heads
->version
, &buf
))
1104 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1106 rpc
->protocol_header
= NULL
;
1109 int n
= packet_read(rpc
->out
, rpc
->buf
, rpc
->alloc
, 0);
1114 err
|= post_rpc(rpc
, 0, 0);
1120 strbuf_read(rpc_result
, client
.out
, 0);
1124 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1131 err
|= finish_command(&client
);
1132 free(rpc
->service_url
);
1133 free(rpc
->hdr_content_type
);
1134 free(rpc
->hdr_accept
);
1135 free(rpc
->hdr_accept_language
);
1136 free(rpc
->protocol_header
);
1138 strbuf_release(&buf
);
1142 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1144 struct walker
*walker
;
1148 ALLOC_ARRAY(targets
, nr_heads
);
1149 if (options
.depth
|| options
.deepen_since
)
1150 die(_("dumb http transport does not support shallow capabilities"));
1151 for (i
= 0; i
< nr_heads
; i
++)
1152 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1154 walker
= get_http_walker(url
.buf
);
1155 walker
->get_verbosely
= options
.verbosity
>= 3;
1156 walker
->get_progress
= options
.progress
;
1157 walker
->get_recover
= 0;
1158 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1159 walker_free(walker
);
1161 for (i
= 0; i
< nr_heads
; i
++)
1165 return ret
? error(_("fetch failed.")) : 0;
1168 static int fetch_git(struct discovery
*heads
,
1169 int nr_heads
, struct ref
**to_fetch
)
1171 struct rpc_state rpc
= RPC_STATE_INIT
;
1172 struct strbuf preamble
= STRBUF_INIT
;
1174 struct strvec args
= STRVEC_INIT
;
1175 struct strbuf rpc_result
= STRBUF_INIT
;
1177 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1178 "--stdin", "--lock-pack", NULL
);
1179 if (options
.followtags
)
1180 strvec_push(&args
, "--include-tag");
1182 strvec_push(&args
, "--thin");
1183 if (options
.verbosity
>= 3)
1184 strvec_pushl(&args
, "-v", "-v", NULL
);
1185 if (options
.check_self_contained_and_connected
)
1186 strvec_push(&args
, "--check-self-contained-and-connected");
1187 if (options
.cloning
)
1188 strvec_push(&args
, "--cloning");
1189 if (options
.update_shallow
)
1190 strvec_push(&args
, "--update-shallow");
1191 if (!options
.progress
)
1192 strvec_push(&args
, "--no-progress");
1194 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1195 if (options
.deepen_since
)
1196 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1197 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1198 strvec_pushf(&args
, "--shallow-exclude=%s",
1199 options
.deepen_not
.items
[i
].string
);
1200 if (options
.deepen_relative
&& options
.depth
)
1201 strvec_push(&args
, "--deepen-relative");
1202 if (options
.from_promisor
)
1203 strvec_push(&args
, "--from-promisor");
1204 if (options
.refetch
)
1205 strvec_push(&args
, "--refetch");
1207 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1208 strvec_push(&args
, url
.buf
);
1210 for (i
= 0; i
< nr_heads
; i
++) {
1211 struct ref
*ref
= to_fetch
[i
];
1213 die(_("cannot fetch by sha1 over smart http"));
1214 packet_buf_write(&preamble
, "%s %s\n",
1215 oid_to_hex(&ref
->old_oid
), ref
->name
);
1217 packet_buf_flush(&preamble
);
1219 memset(&rpc
, 0, sizeof(rpc
));
1220 rpc
.service_name
= "git-upload-pack",
1221 rpc
.gzip_request
= 1;
1223 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1225 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1226 strbuf_release(&rpc_result
);
1227 strbuf_release(&preamble
);
1228 strvec_clear(&args
);
1232 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1234 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1236 return fetch_git(d
, nr_heads
, to_fetch
);
1238 return fetch_dumb(nr_heads
, to_fetch
);
1241 static void parse_fetch(struct strbuf
*buf
)
1243 struct ref
**to_fetch
= NULL
;
1244 struct ref
*list_head
= NULL
;
1245 struct ref
**list
= &list_head
;
1246 int alloc_heads
= 0, nr_heads
= 0;
1250 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1253 struct object_id old_oid
;
1256 if (parse_oid_hex(p
, &old_oid
, &q
))
1257 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1263 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1265 ref
= alloc_ref(name
);
1266 oidcpy(&ref
->old_oid
, &old_oid
);
1271 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1272 to_fetch
[nr_heads
++] = ref
;
1275 die(_("http transport does not support %s"), buf
->buf
);
1278 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1284 if (fetch(nr_heads
, to_fetch
))
1285 exit(128); /* error already reported */
1286 free_refs(list_head
);
1294 static void parse_get(const char *arg
)
1296 struct strbuf url
= STRBUF_INIT
;
1297 struct strbuf path
= STRBUF_INIT
;
1300 space
= strchr(arg
, ' ');
1303 die(_("protocol error: expected '<url> <path>', missing space"));
1305 strbuf_add(&url
, arg
, space
- arg
);
1306 strbuf_addstr(&path
, space
+ 1);
1308 if (http_get_file(url
.buf
, path
.buf
, NULL
))
1309 die(_("failed to download file at URL '%s'"), url
.buf
);
1311 strbuf_release(&url
);
1312 strbuf_release(&path
);
1317 static int push_dav(int nr_spec
, const char **specs
)
1319 struct child_process child
= CHILD_PROCESS_INIT
;
1323 strvec_push(&child
.args
, "http-push");
1324 strvec_push(&child
.args
, "--helper-status");
1325 if (options
.dry_run
)
1326 strvec_push(&child
.args
, "--dry-run");
1327 if (options
.verbosity
> 1)
1328 strvec_push(&child
.args
, "--verbose");
1329 strvec_push(&child
.args
, url
.buf
);
1330 for (i
= 0; i
< nr_spec
; i
++)
1331 strvec_push(&child
.args
, specs
[i
]);
1333 if (run_command(&child
))
1334 die(_("git-http-push failed"));
1338 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1340 struct rpc_state rpc
= RPC_STATE_INIT
;
1343 struct string_list_item
*cas_option
;
1344 struct strbuf preamble
= STRBUF_INIT
;
1345 struct strbuf rpc_result
= STRBUF_INIT
;
1348 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1352 strvec_push(&args
, "--thin");
1353 if (options
.dry_run
)
1354 strvec_push(&args
, "--dry-run");
1355 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1356 strvec_push(&args
, "--signed=yes");
1357 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1358 strvec_push(&args
, "--signed=if-asked");
1360 strvec_push(&args
, "--atomic");
1361 if (options
.verbosity
== 0)
1362 strvec_push(&args
, "--quiet");
1363 else if (options
.verbosity
> 1)
1364 strvec_push(&args
, "--verbose");
1365 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1366 strvec_pushf(&args
, "--push-option=%s",
1367 options
.push_options
.items
[i
].string
);
1368 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1369 for_each_string_list_item(cas_option
, &cas_options
)
1370 strvec_push(&args
, cas_option
->string
);
1371 strvec_push(&args
, url
.buf
);
1373 if (options
.force_if_includes
)
1374 strvec_push(&args
, "--force-if-includes");
1376 strvec_push(&args
, "--stdin");
1377 for (i
= 0; i
< nr_spec
; i
++)
1378 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1379 packet_buf_flush(&preamble
);
1381 memset(&rpc
, 0, sizeof(rpc
));
1382 rpc
.service_name
= "git-receive-pack",
1384 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1386 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1387 strbuf_release(&rpc_result
);
1388 strbuf_release(&preamble
);
1389 strvec_clear(&args
);
1393 static int push(int nr_spec
, const char **specs
)
1395 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1398 if (heads
->proto_git
)
1399 ret
= push_git(heads
, nr_spec
, specs
);
1401 ret
= push_dav(nr_spec
, specs
);
1402 free_discovery(heads
);
1406 static void parse_push(struct strbuf
*buf
)
1408 struct strvec specs
= STRVEC_INIT
;
1413 if (skip_prefix(buf
->buf
, "push ", &arg
))
1414 strvec_push(&specs
, arg
);
1416 die(_("http transport does not support %s"), buf
->buf
);
1419 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1425 ret
= push(specs
.nr
, specs
.v
);
1430 exit(128); /* error already reported */
1433 strvec_clear(&specs
);
1436 static int stateless_connect(const char *service_name
)
1438 struct discovery
*discover
;
1439 struct rpc_state rpc
= RPC_STATE_INIT
;
1440 struct strbuf buf
= STRBUF_INIT
;
1441 const char *accept_language
;
1444 * Run the info/refs request and see if the server supports protocol
1445 * v2. If and only if the server supports v2 can we successfully
1446 * establish a stateless connection, otherwise we need to tell the
1447 * client to fallback to using other transport helper functions to
1448 * complete their request.
1450 * The "git-upload-archive" service is a read-only operation. Fallback
1451 * to use "git-upload-pack" service to discover protocol version.
1453 if (!strcmp(service_name
, "git-upload-archive"))
1454 discover
= discover_refs("git-upload-pack", 0);
1456 discover
= discover_refs(service_name
, 0);
1457 if (discover
->version
!= protocol_v2
) {
1458 printf("fallback\n");
1462 /* Stateless Connection established */
1466 accept_language
= http_get_accept_language_header();
1467 if (accept_language
)
1468 rpc
.hdr_accept_language
= xstrfmt("%s", accept_language
);
1470 rpc
.service_name
= service_name
;
1471 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1472 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1473 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1474 if (get_protocol_http_header(discover
->version
, &buf
)) {
1475 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1477 rpc
.protocol_header
= NULL
;
1478 strbuf_release(&buf
);
1480 rpc
.buf
= xmalloc(http_post_buffer
);
1481 rpc
.alloc
= http_post_buffer
;
1486 rpc
.any_written
= 0;
1487 rpc
.gzip_request
= 1;
1488 rpc
.initial_buffer
= 0;
1489 rpc
.write_line_lengths
= 1;
1490 rpc
.flush_read_but_not_sent
= 0;
1493 * Dump the capability listing that we got from the server earlier
1494 * during the info/refs request. This does not work with the
1495 * "git-upload-archive" service.
1497 if (strcmp(service_name
, "git-upload-archive"))
1498 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1500 /* Until we see EOF keep sending POSTs */
1503 enum packet_read_status status
;
1505 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1507 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1508 if (status
== PACKET_READ_EOF
)
1510 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1511 /* We would have an err here */
1513 /* Reset the buffer for next request */
1517 free(rpc
.service_url
);
1518 free(rpc
.hdr_content_type
);
1519 free(rpc
.hdr_accept
);
1520 free(rpc
.hdr_accept_language
);
1521 free(rpc
.protocol_header
);
1523 strbuf_release(&buf
);
1528 int cmd_main(int argc
, const char **argv
)
1530 struct strbuf buf
= STRBUF_INIT
;
1534 setup_git_directory_gently(&nongit
);
1536 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1540 options
.verbosity
= 1;
1541 options
.progress
= !!isatty(2);
1543 string_list_init_dup(&options
.deepen_not
);
1544 string_list_init_dup(&options
.push_options
);
1547 * Just report "remote-curl" here (folding all the various aliases
1548 * ("git-remote-http", "git-remote-https", and etc.) here since they
1549 * are all just copies of the same actual executable.
1551 trace2_cmd_name("remote-curl");
1553 remote
= remote_get(argv
[1]);
1556 end_url_with_slash(&url
, argv
[2]);
1558 end_url_with_slash(&url
, remote
->url
[0]);
1561 http_init(remote
, url
.buf
, 0);
1566 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1568 error(_("remote-curl: error reading command stream from git"));
1573 if (starts_with(buf
.buf
, "fetch ")) {
1575 setup_git_directory_gently(&nongit
);
1577 die(_("remote-curl: fetch attempted without a local repo"));
1581 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1582 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1583 output_refs(get_refs(for_push
));
1585 } else if (starts_with(buf
.buf
, "push ")) {
1588 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1589 char *value
= strchr(arg
, ' ');
1597 result
= set_option(arg
, value
);
1600 else if (result
< 0)
1601 printf("error invalid value\n");
1603 printf("unsupported\n");
1606 } else if (skip_prefix(buf
.buf
, "get ", &arg
)) {
1610 } else if (!strcmp(buf
.buf
, "capabilities")) {
1611 printf("stateless-connect\n");
1616 printf("check-connectivity\n");
1617 printf("object-format\n");
1620 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1621 if (!stateless_connect(arg
))
1624 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);
1633 strbuf_release(&buf
);