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 */
49 force_if_includes
: 1;
50 const struct git_hash_algo
*hash_algo
;
52 static struct options options
;
53 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
55 static int set_option(const char *name
, const char *value
)
57 if (!strcmp(name
, "verbosity")) {
59 int v
= strtol(value
, &end
, 10);
60 if (value
== end
|| *end
)
62 options
.verbosity
= v
;
65 else if (!strcmp(name
, "progress")) {
66 if (!strcmp(value
, "true"))
68 else if (!strcmp(value
, "false"))
74 else if (!strcmp(name
, "depth")) {
76 unsigned long v
= strtoul(value
, &end
, 10);
77 if (value
== end
|| *end
)
82 else if (!strcmp(name
, "deepen-since")) {
83 options
.deepen_since
= xstrdup(value
);
86 else if (!strcmp(name
, "deepen-not")) {
87 string_list_append(&options
.deepen_not
, value
);
90 else if (!strcmp(name
, "deepen-relative")) {
91 if (!strcmp(value
, "true"))
92 options
.deepen_relative
= 1;
93 else if (!strcmp(value
, "false"))
94 options
.deepen_relative
= 0;
99 else if (!strcmp(name
, "followtags")) {
100 if (!strcmp(value
, "true"))
101 options
.followtags
= 1;
102 else if (!strcmp(value
, "false"))
103 options
.followtags
= 0;
108 else if (!strcmp(name
, "dry-run")) {
109 if (!strcmp(value
, "true"))
111 else if (!strcmp(value
, "false"))
117 else if (!strcmp(name
, "check-connectivity")) {
118 if (!strcmp(value
, "true"))
119 options
.check_self_contained_and_connected
= 1;
120 else if (!strcmp(value
, "false"))
121 options
.check_self_contained_and_connected
= 0;
126 else if (!strcmp(name
, "cas")) {
127 struct strbuf val
= STRBUF_INIT
;
128 strbuf_addstr(&val
, "--force-with-lease=");
130 strbuf_addstr(&val
, value
);
131 else if (unquote_c_style(&val
, value
, NULL
))
133 string_list_append(&cas_options
, val
.buf
);
134 strbuf_release(&val
);
136 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
137 if (!strcmp(value
, "true"))
138 options
.force_if_includes
= 1;
139 else if (!strcmp(value
, "false"))
140 options
.force_if_includes
= 0;
144 } else if (!strcmp(name
, "cloning")) {
145 if (!strcmp(value
, "true"))
147 else if (!strcmp(value
, "false"))
152 } else if (!strcmp(name
, "update-shallow")) {
153 if (!strcmp(value
, "true"))
154 options
.update_shallow
= 1;
155 else if (!strcmp(value
, "false"))
156 options
.update_shallow
= 0;
160 } else if (!strcmp(name
, "pushcert")) {
161 if (!strcmp(value
, "true"))
162 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
163 else if (!strcmp(value
, "false"))
164 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
165 else if (!strcmp(value
, "if-asked"))
166 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
170 } else if (!strcmp(name
, "atomic")) {
171 if (!strcmp(value
, "true"))
173 else if (!strcmp(value
, "false"))
178 } else if (!strcmp(name
, "push-option")) {
180 string_list_append(&options
.push_options
, value
);
182 struct strbuf unquoted
= STRBUF_INIT
;
183 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
184 die(_("invalid quoting in push-option value: '%s'"), value
);
185 string_list_append_nodup(&options
.push_options
,
186 strbuf_detach(&unquoted
, NULL
));
189 } else if (!strcmp(name
, "family")) {
190 if (!strcmp(value
, "ipv4"))
191 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
192 else if (!strcmp(value
, "ipv6"))
193 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
194 else if (!strcmp(value
, "all"))
195 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
199 } else if (!strcmp(name
, "from-promisor")) {
200 options
.from_promisor
= 1;
202 } else if (!strcmp(name
, "refetch")) {
205 } else if (!strcmp(name
, "filter")) {
206 options
.filter
= xstrdup(value
);
208 } else if (!strcmp(name
, "object-format")) {
210 options
.object_format
= 1;
211 if (strcmp(value
, "true")) {
212 algo
= hash_algo_by_name(value
);
213 if (algo
== GIT_HASH_UNKNOWN
)
214 die("unknown object format '%s'", value
);
215 options
.hash_algo
= &hash_algos
[algo
];
219 return 1 /* unsupported */;
229 struct oid_array shallow
;
230 enum protocol_version version
;
231 unsigned proto_git
: 1;
233 static struct discovery
*last_discovery
;
235 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
237 struct ref
*list
= NULL
;
238 struct packet_reader reader
;
240 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
241 PACKET_READ_CHOMP_NEWLINE
|
242 PACKET_READ_GENTLE_ON_EOF
|
243 PACKET_READ_DIE_ON_ERR_PACKET
);
245 heads
->version
= discover_version(&reader
);
246 switch (heads
->version
) {
249 * Do nothing. This isn't a list of refs but rather a
250 * capability advertisement. Client would have run
251 * 'stateless-connect' so we'll dump this capability listing
252 * and let them request the refs themselves.
257 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
258 NULL
, &heads
->shallow
);
259 options
.hash_algo
= reader
.hash_algo
;
261 case protocol_unknown_version
:
262 BUG("unknown protocol version");
268 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
270 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
273 return the_hash_algo
;
275 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
276 if (algo
== GIT_HASH_UNKNOWN
)
278 return &hash_algos
[algo
];
281 static struct ref
*parse_info_refs(struct discovery
*heads
)
283 char *data
, *start
, *mid
;
287 struct ref
*refs
= NULL
;
288 struct ref
*ref
= NULL
;
289 struct ref
*last_ref
= NULL
;
291 options
.hash_algo
= detect_hash_algo(heads
);
292 if (!options
.hash_algo
)
293 die("%sinfo/refs not valid: could not determine hash algorithm; "
294 "is this a git repository?",
295 transport_anonymize_url(url
.buf
));
300 while (i
< heads
->len
) {
306 if (data
[i
] == '\n') {
307 if (mid
- start
!= options
.hash_algo
->hexsz
)
308 die(_("%sinfo/refs not valid: is this a git repository?"),
309 transport_anonymize_url(url
.buf
));
312 ref
= alloc_ref(ref_name
);
313 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
317 last_ref
->next
= ref
;
324 ref
= alloc_ref("HEAD");
325 if (!http_fetch_ref(url
.buf
, ref
) &&
326 !resolve_remote_symref(ref
, refs
)) {
336 static void free_discovery(struct discovery
*d
)
339 if (d
== last_discovery
)
340 last_discovery
= NULL
;
341 free(d
->shallow
.oid
);
349 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
355 * We only show text/plain parts, as other types are likely
356 * to be ugly to look at on the user's terminal.
358 if (strcmp(type
->buf
, "text/plain"))
361 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
369 eol
= strchrnul(p
, '\n');
370 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
376 static int get_protocol_http_header(enum protocol_version version
,
377 struct strbuf
*header
)
380 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
389 static void check_smart_http(struct discovery
*d
, const char *service
,
393 struct packet_reader reader
;
396 * If we don't see x-$service-advertisement, then it's not smart-http.
397 * But once we do, we commit to it and assume any other protocol
398 * violations are hard errors.
400 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
401 !skip_prefix(p
, service
, &p
) ||
402 strcmp(p
, "-advertisement"))
405 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
406 PACKET_READ_CHOMP_NEWLINE
|
407 PACKET_READ_DIE_ON_ERR_PACKET
);
408 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
409 die(_("invalid server response; expected service, got flush packet"));
411 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
413 * The header can include additional metadata lines, up
414 * until a packet flush marker. Ignore these now, but
415 * in the future we might start to scan them.
418 packet_reader_read(&reader
);
419 if (reader
.pktlen
<= 0) {
425 * v0 smart http; callers expect us to soak up the
426 * service and header packets
428 d
->buf
= reader
.src_buffer
;
429 d
->len
= reader
.src_len
;
432 } else if (!strcmp(reader
.line
, "version 2")) {
434 * v2 smart http; do not consume version packet, which will
435 * be handled elsewhere.
440 die(_("invalid server response; got '%s'"), reader
.line
);
444 static struct discovery
*discover_refs(const char *service
, int for_push
)
446 struct strbuf type
= STRBUF_INIT
;
447 struct strbuf charset
= STRBUF_INIT
;
448 struct strbuf buffer
= STRBUF_INIT
;
449 struct strbuf refs_url
= STRBUF_INIT
;
450 struct strbuf effective_url
= STRBUF_INIT
;
451 struct strbuf protocol_header
= STRBUF_INIT
;
452 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
453 struct discovery
*last
= last_discovery
;
454 int http_ret
, maybe_smart
= 0;
455 struct http_get_options http_options
;
456 enum protocol_version version
= get_protocol_version_config();
458 if (last
&& !strcmp(service
, last
->service
))
460 free_discovery(last
);
462 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
463 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
464 git_env_bool("GIT_SMART_HTTP", 1)) {
466 if (!strchr(url
.buf
, '?'))
467 strbuf_addch(&refs_url
, '?');
469 strbuf_addch(&refs_url
, '&');
470 strbuf_addf(&refs_url
, "service=%s", service
);
474 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
475 * to perform a push, then fallback to v0 since the client doesn't know
476 * how to push yet using v2.
478 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
479 version
= protocol_v0
;
481 /* Add the extra Git-Protocol header */
482 if (get_protocol_http_header(version
, &protocol_header
))
483 string_list_append(&extra_headers
, protocol_header
.buf
);
485 memset(&http_options
, 0, sizeof(http_options
));
486 http_options
.content_type
= &type
;
487 http_options
.charset
= &charset
;
488 http_options
.effective_url
= &effective_url
;
489 http_options
.base_url
= &url
;
490 http_options
.extra_headers
= &extra_headers
;
491 http_options
.initial_request
= 1;
492 http_options
.no_cache
= 1;
494 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
498 case HTTP_MISSING_TARGET
:
499 show_http_message(&type
, &charset
, &buffer
);
500 die(_("repository '%s' not found"),
501 transport_anonymize_url(url
.buf
));
503 show_http_message(&type
, &charset
, &buffer
);
504 die(_("Authentication failed for '%s'"),
505 transport_anonymize_url(url
.buf
));
506 case HTTP_NOMATCHPUBLICKEY
:
507 show_http_message(&type
, &charset
, &buffer
);
508 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
509 transport_anonymize_url(url
.buf
), curl_errorstr
);
511 show_http_message(&type
, &charset
, &buffer
);
512 die(_("unable to access '%s': %s"),
513 transport_anonymize_url(url
.buf
), curl_errorstr
);
516 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
517 char *u
= transport_anonymize_url(url
.buf
);
518 warning(_("redirecting to %s"), u
);
522 last
= xcalloc(1, sizeof(*last_discovery
));
523 last
->service
= xstrdup(service
);
524 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
525 last
->buf
= last
->buf_alloc
;
528 check_smart_http(last
, service
, &type
);
531 last
->refs
= parse_git_refs(last
, for_push
);
533 last
->refs
= parse_info_refs(last
);
535 strbuf_release(&refs_url
);
536 strbuf_release(&type
);
537 strbuf_release(&charset
);
538 strbuf_release(&effective_url
);
539 strbuf_release(&buffer
);
540 strbuf_release(&protocol_header
);
541 string_list_clear(&extra_headers
, 0);
542 last_discovery
= last
;
546 static struct ref
*get_refs(int for_push
)
548 struct discovery
*heads
;
551 heads
= discover_refs("git-receive-pack", for_push
);
553 heads
= discover_refs("git-upload-pack", for_push
);
558 static void output_refs(struct ref
*refs
)
561 if (options
.object_format
&& options
.hash_algo
) {
562 printf(":object-format %s\n", options
.hash_algo
->name
);
563 repo_set_hash_algo(the_repository
,
564 hash_algo_by_ptr(options
.hash_algo
));
566 for (posn
= refs
; posn
; posn
= posn
->next
) {
568 printf("@%s %s\n", posn
->symref
, posn
->name
);
570 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
579 const char *service_name
;
581 char *hdr_content_type
;
583 char *protocol_header
;
591 unsigned gzip_request
: 1;
592 unsigned initial_buffer
: 1;
595 * Whenever a pkt-line is read into buf, append the 4 characters
596 * denoting its length before appending the payload.
598 unsigned write_line_lengths
: 1;
601 * Used by rpc_out; initialize to 0. This is true if a flush has been
602 * read, but the corresponding line length (if write_line_lengths is
603 * true) and EOF have not been sent to libcurl. Since each flush marks
604 * the end of a request, each flush must be completely sent before any
605 * further reading occurs.
607 unsigned flush_read_but_not_sent
: 1;
611 * Appends the result of reading from rpc->out to the string represented by
612 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
613 * enough space, 0 otherwise.
615 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
616 * hexadecimal string before appending the result described above.
618 * Writes the total number of bytes appended into appended.
620 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
622 enum packet_read_status
*status
) {
627 if (rpc
->write_line_lengths
) {
628 left
= rpc
->alloc
- rpc
->len
- 4;
629 buf
= rpc
->buf
+ rpc
->len
+ 4;
631 left
= rpc
->alloc
- rpc
->len
;
632 buf
= rpc
->buf
+ rpc
->len
;
635 if (left
< LARGE_PACKET_MAX
)
638 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
639 left
, &pktlen_raw
, options
);
640 if (*status
!= PACKET_READ_EOF
) {
641 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
642 rpc
->len
+= *appended
;
645 if (rpc
->write_line_lengths
) {
647 case PACKET_READ_EOF
:
648 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
649 die(_("shouldn't have EOF when not gentle on EOF"));
651 case PACKET_READ_NORMAL
:
652 set_packet_header(buf
- 4, *appended
);
654 case PACKET_READ_DELIM
:
655 memcpy(buf
- 4, "0001", 4);
657 case PACKET_READ_FLUSH
:
658 memcpy(buf
- 4, "0000", 4);
660 case PACKET_READ_RESPONSE_END
:
661 die(_("remote server sent unexpected response end packet"));
668 static size_t rpc_out(void *ptr
, size_t eltsize
,
669 size_t nmemb
, void *buffer_
)
671 size_t max
= eltsize
* nmemb
;
672 struct rpc_state
*rpc
= buffer_
;
673 size_t avail
= rpc
->len
- rpc
->pos
;
674 enum packet_read_status status
;
677 rpc
->initial_buffer
= 0;
680 if (!rpc
->flush_read_but_not_sent
) {
681 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
682 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
683 if (status
== PACKET_READ_FLUSH
)
684 rpc
->flush_read_but_not_sent
= 1;
687 * If flush_read_but_not_sent is true, we have already read one
688 * full request but have not fully sent it + EOF, which is why
689 * we need to refrain from reading.
692 if (rpc
->flush_read_but_not_sent
) {
695 * The line length either does not need to be sent at
696 * all or has already been completely sent. Now we can
697 * return 0, indicating EOF, meaning that the flush has
700 rpc
->flush_read_but_not_sent
= 0;
704 * If avail is non-zero, the line length for the flush still
705 * hasn't been fully sent. Proceed with sending the line
712 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
717 static int rpc_seek(void *clientp
, curl_off_t offset
, int origin
)
719 struct rpc_state
*rpc
= clientp
;
721 if (origin
!= SEEK_SET
)
722 BUG("rpc_seek only handles SEEK_SET, not %d", origin
);
724 if (rpc
->initial_buffer
) {
725 if (offset
< 0 || offset
> rpc
->len
) {
726 error("curl seek would be outside of rpc buffer");
727 return CURL_SEEKFUNC_FAIL
;
730 return CURL_SEEKFUNC_OK
;
732 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
733 return CURL_SEEKFUNC_FAIL
;
736 struct check_pktline_state
{
742 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
745 if (!state
->remaining
) {
746 int digits_remaining
= 4 - state
->len_filled
;
747 if (digits_remaining
> size
)
748 digits_remaining
= size
;
749 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
750 state
->len_filled
+= digits_remaining
;
751 ptr
+= digits_remaining
;
752 size
-= digits_remaining
;
754 if (state
->len_filled
== 4) {
755 state
->remaining
= packet_length(state
->len_buf
);
756 if (state
->remaining
< 0) {
757 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
758 } else if (state
->remaining
== 2) {
759 die(_("remote-curl: unexpected response end packet"));
760 } else if (state
->remaining
< 4) {
761 state
->remaining
= 0;
763 state
->remaining
-= 4;
765 state
->len_filled
= 0;
769 if (state
->remaining
) {
770 int remaining
= state
->remaining
;
771 if (remaining
> size
)
775 state
->remaining
-= remaining
;
781 struct rpc_state
*rpc
;
782 struct active_request_slot
*slot
;
784 struct check_pktline_state pktline_state
;
788 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
791 static size_t rpc_in(char *ptr
, size_t eltsize
,
792 size_t nmemb
, void *buffer_
)
794 size_t size
= eltsize
* nmemb
;
795 struct rpc_in_data
*data
= buffer_
;
798 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
799 &response_code
) != CURLE_OK
)
801 if (response_code
>= 300)
804 data
->rpc
->any_written
= 1;
805 if (data
->check_pktline
)
806 check_pktline(&data
->pktline_state
, ptr
, size
);
807 write_or_die(data
->rpc
->in
, ptr
, size
);
811 static int run_slot(struct active_request_slot
*slot
,
812 struct slot_results
*results
)
815 struct slot_results results_buf
;
818 results
= &results_buf
;
820 err
= run_one_slot(slot
, results
);
822 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
823 struct strbuf msg
= STRBUF_INIT
;
824 if (results
->http_code
&& results
->http_code
!= 200)
825 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
826 if (results
->curl_result
!= CURLE_OK
) {
828 strbuf_addch(&msg
, ' ');
829 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
830 if (curl_errorstr
[0]) {
831 strbuf_addch(&msg
, ' ');
832 strbuf_addstr(&msg
, curl_errorstr
);
835 error(_("RPC failed; %s"), msg
.buf
);
836 strbuf_release(&msg
);
842 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
844 struct active_request_slot
*slot
;
845 struct curl_slist
*headers
= http_copy_default_headers();
846 struct strbuf buf
= STRBUF_INIT
;
849 slot
= get_active_slot();
851 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
852 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
854 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
855 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
856 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
857 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
858 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
859 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
860 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
861 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
862 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &buf
);
864 err
= run_slot(slot
, results
);
866 curl_slist_free_all(headers
);
867 strbuf_release(&buf
);
871 static curl_off_t
xcurl_off_t(size_t len
)
873 uintmax_t size
= len
;
874 if (size
> maximum_signed_value_of_type(curl_off_t
))
875 die(_("cannot handle pushes this big"));
876 return (curl_off_t
)size
;
880 * If flush_received is true, do not attempt to read any more; just use what's
883 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
885 struct active_request_slot
*slot
;
886 struct curl_slist
*headers
= http_copy_default_headers();
887 int use_gzip
= rpc
->gzip_request
;
888 char *gzip_body
= NULL
;
889 size_t gzip_size
= 0;
890 int err
, large_request
= 0;
891 int needs_100_continue
= 0;
892 struct rpc_in_data rpc_in_data
;
894 /* Try to load the entire request, if we can fit it into the
895 * allocated buffer space we can use HTTP/1.0 and avoid the
896 * chunked encoding mess.
898 if (!flush_received
) {
901 enum packet_read_status status
;
903 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
908 if (status
== PACKET_READ_FLUSH
)
914 struct slot_results results
;
917 err
= probe_rpc(rpc
, &results
);
918 if (err
== HTTP_REAUTH
)
919 credential_fill(&http_auth
);
920 } while (err
== HTTP_REAUTH
);
924 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
925 needs_100_continue
= 1;
928 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
929 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
930 headers
= curl_slist_append(headers
, needs_100_continue
?
931 "Expect: 100-continue" : "Expect:");
933 /* Add the extra Git-Protocol header */
934 if (rpc
->protocol_header
)
935 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
938 slot
= get_active_slot();
940 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
941 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
942 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
943 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
946 /* The request body is large and the size cannot be predicted.
947 * We must use chunked encoding to send it.
949 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
950 rpc
->initial_buffer
= 1;
951 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
952 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
953 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKFUNCTION
, rpc_seek
);
954 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKDATA
, rpc
);
955 if (options
.verbosity
> 1) {
956 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
960 } else if (gzip_body
) {
962 * If we are looping to retry authentication, then the previous
963 * run will have set up the headers and gzip buffer already,
964 * and we just need to send it.
966 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
967 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
969 } else if (use_gzip
&& 1024 < rpc
->len
) {
970 /* The client backend isn't giving us compressed data so
971 * we can try to deflate it ourselves, this may save on
977 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
978 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
979 gzip_body
= xmalloc(gzip_size
);
981 stream
.next_in
= (unsigned char *)rpc
->buf
;
982 stream
.avail_in
= rpc
->len
;
983 stream
.next_out
= (unsigned char *)gzip_body
;
984 stream
.avail_out
= gzip_size
;
986 ret
= git_deflate(&stream
, Z_FINISH
);
987 if (ret
!= Z_STREAM_END
)
988 die(_("cannot deflate request; zlib deflate error %d"), ret
);
990 ret
= git_deflate_end_gently(&stream
);
992 die(_("cannot deflate request; zlib end error %d"), ret
);
994 gzip_size
= stream
.total_out
;
996 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
997 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
998 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
1000 if (options
.verbosity
> 1) {
1001 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
1003 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1007 /* We know the complete request size in advance, use the
1008 * more normal Content-Length approach.
1010 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1011 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1012 if (options
.verbosity
> 1) {
1013 fprintf(stderr
, "POST %s (%lu bytes)\n",
1014 rpc
->service_name
, (unsigned long)rpc
->len
);
1019 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1020 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1021 rpc_in_data
.rpc
= rpc
;
1022 rpc_in_data
.slot
= slot
;
1023 rpc_in_data
.check_pktline
= stateless_connect
;
1024 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1025 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &rpc_in_data
);
1026 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1029 rpc
->any_written
= 0;
1030 err
= run_slot(slot
, NULL
);
1031 if (err
== HTTP_REAUTH
&& !large_request
) {
1032 credential_fill(&http_auth
);
1038 if (!rpc
->any_written
)
1041 if (rpc_in_data
.pktline_state
.len_filled
)
1042 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1043 if (rpc_in_data
.pktline_state
.remaining
)
1044 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1046 if (stateless_connect
)
1047 packet_response_end(rpc
->in
);
1049 curl_slist_free_all(headers
);
1054 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1055 const char **client_argv
, const struct strbuf
*preamble
,
1056 struct strbuf
*rpc_result
)
1058 const char *svc
= rpc
->service_name
;
1059 struct strbuf buf
= STRBUF_INIT
;
1060 struct child_process client
= CHILD_PROCESS_INIT
;
1066 strvec_pushv(&client
.args
, client_argv
);
1067 if (start_command(&client
))
1069 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1071 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1073 rpc
->alloc
= http_post_buffer
;
1074 rpc
->buf
= xmalloc(rpc
->alloc
);
1075 rpc
->in
= client
.in
;
1076 rpc
->out
= client
.out
;
1078 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1079 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1081 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1082 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1084 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1085 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1087 if (get_protocol_http_header(heads
->version
, &buf
))
1088 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1090 rpc
->protocol_header
= NULL
;
1093 int n
= packet_read(rpc
->out
, rpc
->buf
, rpc
->alloc
, 0);
1098 err
|= post_rpc(rpc
, 0, 0);
1104 strbuf_read(rpc_result
, client
.out
, 0);
1108 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1115 err
|= finish_command(&client
);
1116 free(rpc
->service_url
);
1117 free(rpc
->hdr_content_type
);
1118 free(rpc
->hdr_accept
);
1119 free(rpc
->protocol_header
);
1121 strbuf_release(&buf
);
1125 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1127 struct walker
*walker
;
1131 ALLOC_ARRAY(targets
, nr_heads
);
1132 if (options
.depth
|| options
.deepen_since
)
1133 die(_("dumb http transport does not support shallow capabilities"));
1134 for (i
= 0; i
< nr_heads
; i
++)
1135 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1137 walker
= get_http_walker(url
.buf
);
1138 walker
->get_verbosely
= options
.verbosity
>= 3;
1139 walker
->get_progress
= options
.progress
;
1140 walker
->get_recover
= 0;
1141 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1142 walker_free(walker
);
1144 for (i
= 0; i
< nr_heads
; i
++)
1148 return ret
? error(_("fetch failed.")) : 0;
1151 static int fetch_git(struct discovery
*heads
,
1152 int nr_heads
, struct ref
**to_fetch
)
1154 struct rpc_state rpc
;
1155 struct strbuf preamble
= STRBUF_INIT
;
1157 struct strvec args
= STRVEC_INIT
;
1158 struct strbuf rpc_result
= STRBUF_INIT
;
1160 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1161 "--stdin", "--lock-pack", NULL
);
1162 if (options
.followtags
)
1163 strvec_push(&args
, "--include-tag");
1165 strvec_push(&args
, "--thin");
1166 if (options
.verbosity
>= 3)
1167 strvec_pushl(&args
, "-v", "-v", NULL
);
1168 if (options
.check_self_contained_and_connected
)
1169 strvec_push(&args
, "--check-self-contained-and-connected");
1170 if (options
.cloning
)
1171 strvec_push(&args
, "--cloning");
1172 if (options
.update_shallow
)
1173 strvec_push(&args
, "--update-shallow");
1174 if (!options
.progress
)
1175 strvec_push(&args
, "--no-progress");
1177 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1178 if (options
.deepen_since
)
1179 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1180 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1181 strvec_pushf(&args
, "--shallow-exclude=%s",
1182 options
.deepen_not
.items
[i
].string
);
1183 if (options
.deepen_relative
&& options
.depth
)
1184 strvec_push(&args
, "--deepen-relative");
1185 if (options
.from_promisor
)
1186 strvec_push(&args
, "--from-promisor");
1187 if (options
.refetch
)
1188 strvec_push(&args
, "--refetch");
1190 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1191 strvec_push(&args
, url
.buf
);
1193 for (i
= 0; i
< nr_heads
; i
++) {
1194 struct ref
*ref
= to_fetch
[i
];
1196 die(_("cannot fetch by sha1 over smart http"));
1197 packet_buf_write(&preamble
, "%s %s\n",
1198 oid_to_hex(&ref
->old_oid
), ref
->name
);
1200 packet_buf_flush(&preamble
);
1202 memset(&rpc
, 0, sizeof(rpc
));
1203 rpc
.service_name
= "git-upload-pack",
1204 rpc
.gzip_request
= 1;
1206 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1208 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1209 strbuf_release(&rpc_result
);
1210 strbuf_release(&preamble
);
1211 strvec_clear(&args
);
1215 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1217 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1219 return fetch_git(d
, nr_heads
, to_fetch
);
1221 return fetch_dumb(nr_heads
, to_fetch
);
1224 static void parse_fetch(struct strbuf
*buf
)
1226 struct ref
**to_fetch
= NULL
;
1227 struct ref
*list_head
= NULL
;
1228 struct ref
**list
= &list_head
;
1229 int alloc_heads
= 0, nr_heads
= 0;
1233 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1236 struct object_id old_oid
;
1239 if (parse_oid_hex(p
, &old_oid
, &q
))
1240 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1246 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1248 ref
= alloc_ref(name
);
1249 oidcpy(&ref
->old_oid
, &old_oid
);
1254 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1255 to_fetch
[nr_heads
++] = ref
;
1258 die(_("http transport does not support %s"), buf
->buf
);
1261 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1267 if (fetch(nr_heads
, to_fetch
))
1268 exit(128); /* error already reported */
1269 free_refs(list_head
);
1277 static int push_dav(int nr_spec
, const char **specs
)
1279 struct child_process child
= CHILD_PROCESS_INIT
;
1283 strvec_push(&child
.args
, "http-push");
1284 strvec_push(&child
.args
, "--helper-status");
1285 if (options
.dry_run
)
1286 strvec_push(&child
.args
, "--dry-run");
1287 if (options
.verbosity
> 1)
1288 strvec_push(&child
.args
, "--verbose");
1289 strvec_push(&child
.args
, url
.buf
);
1290 for (i
= 0; i
< nr_spec
; i
++)
1291 strvec_push(&child
.args
, specs
[i
]);
1293 if (run_command(&child
))
1294 die(_("git-http-push failed"));
1298 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1300 struct rpc_state rpc
;
1303 struct string_list_item
*cas_option
;
1304 struct strbuf preamble
= STRBUF_INIT
;
1305 struct strbuf rpc_result
= STRBUF_INIT
;
1308 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1312 strvec_push(&args
, "--thin");
1313 if (options
.dry_run
)
1314 strvec_push(&args
, "--dry-run");
1315 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1316 strvec_push(&args
, "--signed=yes");
1317 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1318 strvec_push(&args
, "--signed=if-asked");
1320 strvec_push(&args
, "--atomic");
1321 if (options
.verbosity
== 0)
1322 strvec_push(&args
, "--quiet");
1323 else if (options
.verbosity
> 1)
1324 strvec_push(&args
, "--verbose");
1325 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1326 strvec_pushf(&args
, "--push-option=%s",
1327 options
.push_options
.items
[i
].string
);
1328 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1329 for_each_string_list_item(cas_option
, &cas_options
)
1330 strvec_push(&args
, cas_option
->string
);
1331 strvec_push(&args
, url
.buf
);
1333 if (options
.force_if_includes
)
1334 strvec_push(&args
, "--force-if-includes");
1336 strvec_push(&args
, "--stdin");
1337 for (i
= 0; i
< nr_spec
; i
++)
1338 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1339 packet_buf_flush(&preamble
);
1341 memset(&rpc
, 0, sizeof(rpc
));
1342 rpc
.service_name
= "git-receive-pack",
1344 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1346 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1347 strbuf_release(&rpc_result
);
1348 strbuf_release(&preamble
);
1349 strvec_clear(&args
);
1353 static int push(int nr_spec
, const char **specs
)
1355 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1358 if (heads
->proto_git
)
1359 ret
= push_git(heads
, nr_spec
, specs
);
1361 ret
= push_dav(nr_spec
, specs
);
1362 free_discovery(heads
);
1366 static void parse_push(struct strbuf
*buf
)
1368 struct strvec specs
= STRVEC_INIT
;
1373 if (skip_prefix(buf
->buf
, "push ", &arg
))
1374 strvec_push(&specs
, arg
);
1376 die(_("http transport does not support %s"), buf
->buf
);
1379 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1385 ret
= push(specs
.nr
, specs
.v
);
1390 exit(128); /* error already reported */
1393 strvec_clear(&specs
);
1396 static int stateless_connect(const char *service_name
)
1398 struct discovery
*discover
;
1399 struct rpc_state rpc
;
1400 struct strbuf buf
= STRBUF_INIT
;
1403 * Run the info/refs request and see if the server supports protocol
1404 * v2. If and only if the server supports v2 can we successfully
1405 * establish a stateless connection, otherwise we need to tell the
1406 * client to fallback to using other transport helper functions to
1407 * complete their request.
1409 discover
= discover_refs(service_name
, 0);
1410 if (discover
->version
!= protocol_v2
) {
1411 printf("fallback\n");
1415 /* Stateless Connection established */
1420 rpc
.service_name
= service_name
;
1421 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1422 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1423 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1424 if (get_protocol_http_header(discover
->version
, &buf
)) {
1425 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1427 rpc
.protocol_header
= NULL
;
1428 strbuf_release(&buf
);
1430 rpc
.buf
= xmalloc(http_post_buffer
);
1431 rpc
.alloc
= http_post_buffer
;
1436 rpc
.any_written
= 0;
1437 rpc
.gzip_request
= 1;
1438 rpc
.initial_buffer
= 0;
1439 rpc
.write_line_lengths
= 1;
1440 rpc
.flush_read_but_not_sent
= 0;
1443 * Dump the capability listing that we got from the server earlier
1444 * during the info/refs request.
1446 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1448 /* Until we see EOF keep sending POSTs */
1451 enum packet_read_status status
;
1453 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1455 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1456 if (status
== PACKET_READ_EOF
)
1458 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1459 /* We would have an err here */
1461 /* Reset the buffer for next request */
1465 free(rpc
.service_url
);
1466 free(rpc
.hdr_content_type
);
1467 free(rpc
.hdr_accept
);
1468 free(rpc
.protocol_header
);
1470 strbuf_release(&buf
);
1475 int cmd_main(int argc
, const char **argv
)
1477 struct strbuf buf
= STRBUF_INIT
;
1481 setup_git_directory_gently(&nongit
);
1483 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1487 options
.verbosity
= 1;
1488 options
.progress
= !!isatty(2);
1490 string_list_init_dup(&options
.deepen_not
);
1491 string_list_init_dup(&options
.push_options
);
1494 * Just report "remote-curl" here (folding all the various aliases
1495 * ("git-remote-http", "git-remote-https", and etc.) here since they
1496 * are all just copies of the same actual executable.
1498 trace2_cmd_name("remote-curl");
1500 remote
= remote_get(argv
[1]);
1503 end_url_with_slash(&url
, argv
[2]);
1505 end_url_with_slash(&url
, remote
->url
[0]);
1508 http_init(remote
, url
.buf
, 0);
1513 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1515 error(_("remote-curl: error reading command stream from git"));
1520 if (starts_with(buf
.buf
, "fetch ")) {
1522 die(_("remote-curl: fetch attempted without a local repo"));
1525 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1526 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1527 output_refs(get_refs(for_push
));
1529 } else if (starts_with(buf
.buf
, "push ")) {
1532 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1533 char *value
= strchr(arg
, ' ');
1541 result
= set_option(arg
, value
);
1544 else if (result
< 0)
1545 printf("error invalid value\n");
1547 printf("unsupported\n");
1550 } else if (!strcmp(buf
.buf
, "capabilities")) {
1551 printf("stateless-connect\n");
1555 printf("check-connectivity\n");
1556 printf("object-format\n");
1559 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1560 if (!stateless_connect(arg
))
1563 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);
1572 strbuf_release(&buf
);