1 #include "git-compat-util.h"
11 #include "run-command.h"
13 #include "string-list.h"
16 #include "credential.h"
17 #include "oid-array.h"
18 #include "send-pack.h"
21 #include "transport.h"
23 static struct remote
*remote
;
24 /* always ends with a trailing slash */
25 static struct strbuf url
= STRBUF_INIT
;
31 struct string_list deepen_not
;
32 struct string_list push_options
;
34 unsigned progress
: 1,
35 check_self_contained_and_connected
: 1,
41 /* One of the SEND_PACK_PUSH_CERT_* constants. */
45 /* see documentation of corresponding flag in fetch-pack.h */
51 force_if_includes
: 1;
52 const struct git_hash_algo
*hash_algo
;
54 static struct options options
;
55 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
57 static int set_option(const char *name
, const char *value
)
59 if (!strcmp(name
, "verbosity")) {
61 int v
= strtol(value
, &end
, 10);
62 if (value
== end
|| *end
)
64 options
.verbosity
= v
;
67 else if (!strcmp(name
, "progress")) {
68 if (!strcmp(value
, "true"))
70 else if (!strcmp(value
, "false"))
76 else if (!strcmp(name
, "depth")) {
78 unsigned long v
= strtoul(value
, &end
, 10);
79 if (value
== end
|| *end
)
84 else if (!strcmp(name
, "deepen-since")) {
85 options
.deepen_since
= xstrdup(value
);
88 else if (!strcmp(name
, "deepen-not")) {
89 string_list_append(&options
.deepen_not
, value
);
92 else if (!strcmp(name
, "deepen-relative")) {
93 if (!strcmp(value
, "true"))
94 options
.deepen_relative
= 1;
95 else if (!strcmp(value
, "false"))
96 options
.deepen_relative
= 0;
101 else if (!strcmp(name
, "followtags")) {
102 if (!strcmp(value
, "true"))
103 options
.followtags
= 1;
104 else if (!strcmp(value
, "false"))
105 options
.followtags
= 0;
110 else if (!strcmp(name
, "dry-run")) {
111 if (!strcmp(value
, "true"))
113 else if (!strcmp(value
, "false"))
119 else if (!strcmp(name
, "check-connectivity")) {
120 if (!strcmp(value
, "true"))
121 options
.check_self_contained_and_connected
= 1;
122 else if (!strcmp(value
, "false"))
123 options
.check_self_contained_and_connected
= 0;
128 else if (!strcmp(name
, "cas")) {
129 struct strbuf val
= STRBUF_INIT
;
130 strbuf_addstr(&val
, "--force-with-lease=");
132 strbuf_addstr(&val
, value
);
133 else if (unquote_c_style(&val
, value
, NULL
))
135 string_list_append(&cas_options
, val
.buf
);
136 strbuf_release(&val
);
138 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
139 if (!strcmp(value
, "true"))
140 options
.force_if_includes
= 1;
141 else if (!strcmp(value
, "false"))
142 options
.force_if_includes
= 0;
146 } else if (!strcmp(name
, "cloning")) {
147 if (!strcmp(value
, "true"))
149 else if (!strcmp(value
, "false"))
154 } else if (!strcmp(name
, "update-shallow")) {
155 if (!strcmp(value
, "true"))
156 options
.update_shallow
= 1;
157 else if (!strcmp(value
, "false"))
158 options
.update_shallow
= 0;
162 } else if (!strcmp(name
, "pushcert")) {
163 if (!strcmp(value
, "true"))
164 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
165 else if (!strcmp(value
, "false"))
166 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
167 else if (!strcmp(value
, "if-asked"))
168 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
172 } else if (!strcmp(name
, "atomic")) {
173 if (!strcmp(value
, "true"))
175 else if (!strcmp(value
, "false"))
180 } else if (!strcmp(name
, "push-option")) {
182 string_list_append(&options
.push_options
, value
);
184 struct strbuf unquoted
= STRBUF_INIT
;
185 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
186 die(_("invalid quoting in push-option value: '%s'"), value
);
187 string_list_append_nodup(&options
.push_options
,
188 strbuf_detach(&unquoted
, NULL
));
191 } else if (!strcmp(name
, "family")) {
192 if (!strcmp(value
, "ipv4"))
193 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
194 else if (!strcmp(value
, "ipv6"))
195 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
196 else if (!strcmp(value
, "all"))
197 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
201 } else if (!strcmp(name
, "from-promisor")) {
202 options
.from_promisor
= 1;
204 } else if (!strcmp(name
, "refetch")) {
207 } else if (!strcmp(name
, "filter")) {
208 options
.filter
= xstrdup(value
);
210 } else if (!strcmp(name
, "object-format")) {
212 options
.object_format
= 1;
213 if (strcmp(value
, "true")) {
214 algo
= hash_algo_by_name(value
);
215 if (algo
== GIT_HASH_UNKNOWN
)
216 die("unknown object format '%s'", value
);
217 options
.hash_algo
= &hash_algos
[algo
];
221 return 1 /* unsupported */;
231 struct oid_array shallow
;
232 enum protocol_version version
;
233 unsigned proto_git
: 1;
235 static struct discovery
*last_discovery
;
237 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
239 struct ref
*list
= NULL
;
240 struct packet_reader reader
;
242 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
243 PACKET_READ_CHOMP_NEWLINE
|
244 PACKET_READ_GENTLE_ON_EOF
|
245 PACKET_READ_DIE_ON_ERR_PACKET
);
247 heads
->version
= discover_version(&reader
);
248 switch (heads
->version
) {
251 * Do nothing. This isn't a list of refs but rather a
252 * capability advertisement. Client would have run
253 * 'stateless-connect' so we'll dump this capability listing
254 * and let them request the refs themselves.
259 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
260 NULL
, &heads
->shallow
);
261 options
.hash_algo
= reader
.hash_algo
;
263 case protocol_unknown_version
:
264 BUG("unknown protocol version");
270 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
272 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
275 return the_hash_algo
;
277 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
278 if (algo
== GIT_HASH_UNKNOWN
)
280 return &hash_algos
[algo
];
283 static struct ref
*parse_info_refs(struct discovery
*heads
)
285 char *data
, *start
, *mid
;
289 struct ref
*refs
= NULL
;
290 struct ref
*ref
= NULL
;
291 struct ref
*last_ref
= NULL
;
293 options
.hash_algo
= detect_hash_algo(heads
);
294 if (!options
.hash_algo
)
295 die("%sinfo/refs not valid: could not determine hash algorithm; "
296 "is this a git repository?",
297 transport_anonymize_url(url
.buf
));
302 while (i
< heads
->len
) {
308 if (data
[i
] == '\n') {
309 if (mid
- start
!= options
.hash_algo
->hexsz
)
310 die(_("%sinfo/refs not valid: is this a git repository?"),
311 transport_anonymize_url(url
.buf
));
314 ref
= alloc_ref(ref_name
);
315 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
319 last_ref
->next
= ref
;
326 ref
= alloc_ref("HEAD");
327 if (!http_fetch_ref(url
.buf
, ref
) &&
328 !resolve_remote_symref(ref
, refs
)) {
338 static void free_discovery(struct discovery
*d
)
341 if (d
== last_discovery
)
342 last_discovery
= NULL
;
343 free(d
->shallow
.oid
);
351 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
357 * We only show text/plain parts, as other types are likely
358 * to be ugly to look at on the user's terminal.
360 if (strcmp(type
->buf
, "text/plain"))
363 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
371 eol
= strchrnul(p
, '\n');
372 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
378 static int get_protocol_http_header(enum protocol_version version
,
379 struct strbuf
*header
)
382 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
391 static void check_smart_http(struct discovery
*d
, const char *service
,
395 struct packet_reader reader
;
398 * If we don't see x-$service-advertisement, then it's not smart-http.
399 * But once we do, we commit to it and assume any other protocol
400 * violations are hard errors.
402 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
403 !skip_prefix(p
, service
, &p
) ||
404 strcmp(p
, "-advertisement"))
407 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
408 PACKET_READ_CHOMP_NEWLINE
|
409 PACKET_READ_DIE_ON_ERR_PACKET
);
410 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
411 die(_("invalid server response; expected service, got flush packet"));
413 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
415 * The header can include additional metadata lines, up
416 * until a packet flush marker. Ignore these now, but
417 * in the future we might start to scan them.
420 packet_reader_read(&reader
);
421 if (reader
.pktlen
<= 0) {
427 * v0 smart http; callers expect us to soak up the
428 * service and header packets
430 d
->buf
= reader
.src_buffer
;
431 d
->len
= reader
.src_len
;
434 } else if (!strcmp(reader
.line
, "version 2")) {
436 * v2 smart http; do not consume version packet, which will
437 * be handled elsewhere.
442 die(_("invalid server response; got '%s'"), reader
.line
);
446 static struct discovery
*discover_refs(const char *service
, int for_push
)
448 struct strbuf type
= STRBUF_INIT
;
449 struct strbuf charset
= STRBUF_INIT
;
450 struct strbuf buffer
= STRBUF_INIT
;
451 struct strbuf refs_url
= STRBUF_INIT
;
452 struct strbuf effective_url
= STRBUF_INIT
;
453 struct strbuf protocol_header
= STRBUF_INIT
;
454 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
455 struct discovery
*last
= last_discovery
;
456 int http_ret
, maybe_smart
= 0;
457 struct http_get_options http_options
;
458 enum protocol_version version
= get_protocol_version_config();
460 if (last
&& !strcmp(service
, last
->service
))
462 free_discovery(last
);
464 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
465 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
466 git_env_bool("GIT_SMART_HTTP", 1)) {
468 if (!strchr(url
.buf
, '?'))
469 strbuf_addch(&refs_url
, '?');
471 strbuf_addch(&refs_url
, '&');
472 strbuf_addf(&refs_url
, "service=%s", service
);
476 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
477 * to perform any operation that doesn't involve upload-pack (i.e., a
478 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
479 * to do anything else (like push or remote archive) via v2.
481 if (version
== protocol_v2
&& strcmp("git-upload-pack", service
))
482 version
= protocol_v0
;
484 /* Add the extra Git-Protocol header */
485 if (get_protocol_http_header(version
, &protocol_header
))
486 string_list_append(&extra_headers
, protocol_header
.buf
);
488 memset(&http_options
, 0, sizeof(http_options
));
489 http_options
.content_type
= &type
;
490 http_options
.charset
= &charset
;
491 http_options
.effective_url
= &effective_url
;
492 http_options
.base_url
= &url
;
493 http_options
.extra_headers
= &extra_headers
;
494 http_options
.initial_request
= 1;
495 http_options
.no_cache
= 1;
497 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
501 case HTTP_MISSING_TARGET
:
502 show_http_message(&type
, &charset
, &buffer
);
503 die(_("repository '%s' not found"),
504 transport_anonymize_url(url
.buf
));
506 show_http_message(&type
, &charset
, &buffer
);
507 die(_("Authentication failed for '%s'"),
508 transport_anonymize_url(url
.buf
));
509 case HTTP_NOMATCHPUBLICKEY
:
510 show_http_message(&type
, &charset
, &buffer
);
511 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
512 transport_anonymize_url(url
.buf
), curl_errorstr
);
514 show_http_message(&type
, &charset
, &buffer
);
515 die(_("unable to access '%s': %s"),
516 transport_anonymize_url(url
.buf
), curl_errorstr
);
519 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
520 char *u
= transport_anonymize_url(url
.buf
);
521 warning(_("redirecting to %s"), u
);
525 last
= xcalloc(1, sizeof(*last_discovery
));
526 last
->service
= xstrdup(service
);
527 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
528 last
->buf
= last
->buf_alloc
;
531 check_smart_http(last
, service
, &type
);
534 last
->refs
= parse_git_refs(last
, for_push
);
536 last
->refs
= parse_info_refs(last
);
538 strbuf_release(&refs_url
);
539 strbuf_release(&type
);
540 strbuf_release(&charset
);
541 strbuf_release(&effective_url
);
542 strbuf_release(&buffer
);
543 strbuf_release(&protocol_header
);
544 string_list_clear(&extra_headers
, 0);
545 last_discovery
= last
;
549 static struct ref
*get_refs(int for_push
)
551 struct discovery
*heads
;
554 heads
= discover_refs("git-receive-pack", for_push
);
556 heads
= discover_refs("git-upload-pack", for_push
);
561 static void output_refs(struct ref
*refs
)
564 if (options
.object_format
&& options
.hash_algo
) {
565 printf(":object-format %s\n", options
.hash_algo
->name
);
566 repo_set_hash_algo(the_repository
,
567 hash_algo_by_ptr(options
.hash_algo
));
569 for (posn
= refs
; posn
; posn
= posn
->next
) {
571 printf("@%s %s\n", posn
->symref
, posn
->name
);
573 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
582 const char *service_name
;
584 char *hdr_content_type
;
586 char *hdr_accept_language
;
587 char *protocol_header
;
595 unsigned gzip_request
: 1;
596 unsigned initial_buffer
: 1;
599 * Whenever a pkt-line is read into buf, append the 4 characters
600 * denoting its length before appending the payload.
602 unsigned write_line_lengths
: 1;
605 * Used by rpc_out; initialize to 0. This is true if a flush has been
606 * read, but the corresponding line length (if write_line_lengths is
607 * true) and EOF have not been sent to libcurl. Since each flush marks
608 * the end of a request, each flush must be completely sent before any
609 * further reading occurs.
611 unsigned flush_read_but_not_sent
: 1;
614 #define RPC_STATE_INIT { 0 }
617 * Appends the result of reading from rpc->out to the string represented by
618 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
619 * enough space, 0 otherwise.
621 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
622 * hexadecimal string before appending the result described above.
624 * Writes the total number of bytes appended into appended.
626 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
628 enum packet_read_status
*status
) {
633 if (rpc
->write_line_lengths
) {
634 left
= rpc
->alloc
- rpc
->len
- 4;
635 buf
= rpc
->buf
+ rpc
->len
+ 4;
637 left
= rpc
->alloc
- rpc
->len
;
638 buf
= rpc
->buf
+ rpc
->len
;
641 if (left
< LARGE_PACKET_MAX
)
644 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
645 left
, &pktlen_raw
, options
);
646 if (*status
!= PACKET_READ_EOF
) {
647 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
648 rpc
->len
+= *appended
;
651 if (rpc
->write_line_lengths
) {
653 case PACKET_READ_EOF
:
654 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
655 die(_("shouldn't have EOF when not gentle on EOF"));
657 case PACKET_READ_NORMAL
:
658 set_packet_header(buf
- 4, *appended
);
660 case PACKET_READ_DELIM
:
661 memcpy(buf
- 4, "0001", 4);
663 case PACKET_READ_FLUSH
:
664 memcpy(buf
- 4, "0000", 4);
666 case PACKET_READ_RESPONSE_END
:
667 die(_("remote server sent unexpected response end packet"));
674 static size_t rpc_out(void *ptr
, size_t eltsize
,
675 size_t nmemb
, void *buffer_
)
677 size_t max
= eltsize
* nmemb
;
678 struct rpc_state
*rpc
= buffer_
;
679 size_t avail
= rpc
->len
- rpc
->pos
;
680 enum packet_read_status status
;
683 rpc
->initial_buffer
= 0;
686 if (!rpc
->flush_read_but_not_sent
) {
687 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
688 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
689 if (status
== PACKET_READ_FLUSH
)
690 rpc
->flush_read_but_not_sent
= 1;
693 * If flush_read_but_not_sent is true, we have already read one
694 * full request but have not fully sent it + EOF, which is why
695 * we need to refrain from reading.
698 if (rpc
->flush_read_but_not_sent
) {
701 * The line length either does not need to be sent at
702 * all or has already been completely sent. Now we can
703 * return 0, indicating EOF, meaning that the flush has
706 rpc
->flush_read_but_not_sent
= 0;
710 * If avail is non-zero, the line length for the flush still
711 * hasn't been fully sent. Proceed with sending the line
718 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
723 static int rpc_seek(void *clientp
, curl_off_t offset
, int origin
)
725 struct rpc_state
*rpc
= clientp
;
727 if (origin
!= SEEK_SET
)
728 BUG("rpc_seek only handles SEEK_SET, not %d", origin
);
730 if (rpc
->initial_buffer
) {
731 if (offset
< 0 || offset
> rpc
->len
) {
732 error("curl seek would be outside of rpc buffer");
733 return CURL_SEEKFUNC_FAIL
;
736 return CURL_SEEKFUNC_OK
;
738 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
739 return CURL_SEEKFUNC_FAIL
;
742 struct check_pktline_state
{
748 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
751 if (!state
->remaining
) {
752 int digits_remaining
= 4 - state
->len_filled
;
753 if (digits_remaining
> size
)
754 digits_remaining
= size
;
755 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
756 state
->len_filled
+= digits_remaining
;
757 ptr
+= digits_remaining
;
758 size
-= digits_remaining
;
760 if (state
->len_filled
== 4) {
761 state
->remaining
= packet_length(state
->len_buf
);
762 if (state
->remaining
< 0) {
763 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
764 } else if (state
->remaining
== 2) {
765 die(_("remote-curl: unexpected response end packet"));
766 } else if (state
->remaining
< 4) {
767 state
->remaining
= 0;
769 state
->remaining
-= 4;
771 state
->len_filled
= 0;
775 if (state
->remaining
) {
776 int remaining
= state
->remaining
;
777 if (remaining
> size
)
781 state
->remaining
-= remaining
;
787 struct rpc_state
*rpc
;
788 struct active_request_slot
*slot
;
790 struct check_pktline_state pktline_state
;
794 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
797 static size_t rpc_in(char *ptr
, size_t eltsize
,
798 size_t nmemb
, void *buffer_
)
800 size_t size
= eltsize
* nmemb
;
801 struct rpc_in_data
*data
= buffer_
;
804 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
805 &response_code
) != CURLE_OK
)
807 if (response_code
>= 300)
810 data
->rpc
->any_written
= 1;
811 if (data
->check_pktline
)
812 check_pktline(&data
->pktline_state
, ptr
, size
);
813 write_or_die(data
->rpc
->in
, ptr
, size
);
817 static int run_slot(struct active_request_slot
*slot
,
818 struct slot_results
*results
)
821 struct slot_results results_buf
;
824 results
= &results_buf
;
826 err
= run_one_slot(slot
, results
);
828 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
829 struct strbuf msg
= STRBUF_INIT
;
830 if (results
->http_code
&& results
->http_code
!= 200)
831 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
832 if (results
->curl_result
!= CURLE_OK
) {
834 strbuf_addch(&msg
, ' ');
835 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
836 if (curl_errorstr
[0]) {
837 strbuf_addch(&msg
, ' ');
838 strbuf_addstr(&msg
, curl_errorstr
);
841 error(_("RPC failed; %s"), msg
.buf
);
842 strbuf_release(&msg
);
848 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
850 struct active_request_slot
*slot
;
851 struct curl_slist
*headers
= http_copy_default_headers();
852 struct strbuf buf
= STRBUF_INIT
;
855 slot
= get_active_slot();
857 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
858 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
860 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
861 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
862 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
863 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
864 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
865 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
866 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
867 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
868 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &buf
);
870 err
= run_slot(slot
, results
);
872 curl_slist_free_all(headers
);
873 strbuf_release(&buf
);
877 static curl_off_t
xcurl_off_t(size_t len
)
879 uintmax_t size
= len
;
880 if (size
> maximum_signed_value_of_type(curl_off_t
))
881 die(_("cannot handle pushes this big"));
882 return (curl_off_t
)size
;
886 * If flush_received is true, do not attempt to read any more; just use what's
889 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
891 struct active_request_slot
*slot
;
892 struct curl_slist
*headers
= http_copy_default_headers();
893 int use_gzip
= rpc
->gzip_request
;
894 char *gzip_body
= NULL
;
895 size_t gzip_size
= 0;
896 int err
, large_request
= 0;
897 int needs_100_continue
= 0;
898 struct rpc_in_data rpc_in_data
;
900 /* Try to load the entire request, if we can fit it into the
901 * allocated buffer space we can use HTTP/1.0 and avoid the
902 * chunked encoding mess.
904 if (!flush_received
) {
907 enum packet_read_status status
;
909 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
914 if (status
== PACKET_READ_FLUSH
)
920 struct slot_results results
;
923 err
= probe_rpc(rpc
, &results
);
924 if (err
== HTTP_REAUTH
)
925 credential_fill(&http_auth
);
926 } while (err
== HTTP_REAUTH
);
930 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
931 needs_100_continue
= 1;
934 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
935 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
936 headers
= curl_slist_append(headers
, needs_100_continue
?
937 "Expect: 100-continue" : "Expect:");
939 /* Add Accept-Language header */
940 if (rpc
->hdr_accept_language
)
941 headers
= curl_slist_append(headers
, rpc
->hdr_accept_language
);
943 /* Add the extra Git-Protocol header */
944 if (rpc
->protocol_header
)
945 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
948 slot
= get_active_slot();
950 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
951 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
952 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
953 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
956 /* The request body is large and the size cannot be predicted.
957 * We must use chunked encoding to send it.
959 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
960 rpc
->initial_buffer
= 1;
961 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
962 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
963 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKFUNCTION
, rpc_seek
);
964 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKDATA
, rpc
);
965 if (options
.verbosity
> 1) {
966 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
970 } else if (gzip_body
) {
972 * If we are looping to retry authentication, then the previous
973 * run will have set up the headers and gzip buffer already,
974 * and we just need to send it.
976 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
977 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
979 } else if (use_gzip
&& 1024 < rpc
->len
) {
980 /* The client backend isn't giving us compressed data so
981 * we can try to deflate it ourselves, this may save on
987 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
988 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
989 gzip_body
= xmalloc(gzip_size
);
991 stream
.next_in
= (unsigned char *)rpc
->buf
;
992 stream
.avail_in
= rpc
->len
;
993 stream
.next_out
= (unsigned char *)gzip_body
;
994 stream
.avail_out
= gzip_size
;
996 ret
= git_deflate(&stream
, Z_FINISH
);
997 if (ret
!= Z_STREAM_END
)
998 die(_("cannot deflate request; zlib deflate error %d"), ret
);
1000 ret
= git_deflate_end_gently(&stream
);
1002 die(_("cannot deflate request; zlib end error %d"), ret
);
1004 gzip_size
= stream
.total_out
;
1006 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
1007 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
1008 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
1010 if (options
.verbosity
> 1) {
1011 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
1013 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1017 /* We know the complete request size in advance, use the
1018 * more normal Content-Length approach.
1020 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1021 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1022 if (options
.verbosity
> 1) {
1023 fprintf(stderr
, "POST %s (%lu bytes)\n",
1024 rpc
->service_name
, (unsigned long)rpc
->len
);
1029 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1030 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1031 rpc_in_data
.rpc
= rpc
;
1032 rpc_in_data
.slot
= slot
;
1033 rpc_in_data
.check_pktline
= stateless_connect
;
1034 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1035 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &rpc_in_data
);
1036 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1039 rpc
->any_written
= 0;
1040 err
= run_slot(slot
, NULL
);
1041 if (err
== HTTP_REAUTH
&& !large_request
) {
1042 credential_fill(&http_auth
);
1048 if (!rpc
->any_written
)
1051 if (rpc_in_data
.pktline_state
.len_filled
)
1052 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1053 if (rpc_in_data
.pktline_state
.remaining
)
1054 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1056 if (stateless_connect
)
1057 packet_response_end(rpc
->in
);
1059 curl_slist_free_all(headers
);
1064 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1065 const char **client_argv
, const struct strbuf
*preamble
,
1066 struct strbuf
*rpc_result
)
1068 const char *svc
= rpc
->service_name
;
1069 struct strbuf buf
= STRBUF_INIT
;
1070 struct child_process client
= CHILD_PROCESS_INIT
;
1076 strvec_pushv(&client
.args
, client_argv
);
1077 if (start_command(&client
))
1079 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1081 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1083 rpc
->alloc
= http_post_buffer
;
1084 rpc
->buf
= xmalloc(rpc
->alloc
);
1085 rpc
->in
= client
.in
;
1086 rpc
->out
= client
.out
;
1088 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1089 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1091 rpc
->hdr_accept_language
= xstrdup_or_null(http_get_accept_language_header());
1093 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1094 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1096 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1097 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1099 if (get_protocol_http_header(heads
->version
, &buf
))
1100 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1102 rpc
->protocol_header
= NULL
;
1105 int n
= packet_read(rpc
->out
, rpc
->buf
, rpc
->alloc
, 0);
1110 err
|= post_rpc(rpc
, 0, 0);
1116 strbuf_read(rpc_result
, client
.out
, 0);
1120 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1127 err
|= finish_command(&client
);
1128 free(rpc
->service_url
);
1129 free(rpc
->hdr_content_type
);
1130 free(rpc
->hdr_accept
);
1131 free(rpc
->hdr_accept_language
);
1132 free(rpc
->protocol_header
);
1134 strbuf_release(&buf
);
1138 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1140 struct walker
*walker
;
1144 ALLOC_ARRAY(targets
, nr_heads
);
1145 if (options
.depth
|| options
.deepen_since
)
1146 die(_("dumb http transport does not support shallow capabilities"));
1147 for (i
= 0; i
< nr_heads
; i
++)
1148 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1150 walker
= get_http_walker(url
.buf
);
1151 walker
->get_verbosely
= options
.verbosity
>= 3;
1152 walker
->get_progress
= options
.progress
;
1153 walker
->get_recover
= 0;
1154 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1155 walker_free(walker
);
1157 for (i
= 0; i
< nr_heads
; i
++)
1161 return ret
? error(_("fetch failed.")) : 0;
1164 static int fetch_git(struct discovery
*heads
,
1165 int nr_heads
, struct ref
**to_fetch
)
1167 struct rpc_state rpc
= RPC_STATE_INIT
;
1168 struct strbuf preamble
= STRBUF_INIT
;
1170 struct strvec args
= STRVEC_INIT
;
1171 struct strbuf rpc_result
= STRBUF_INIT
;
1173 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1174 "--stdin", "--lock-pack", NULL
);
1175 if (options
.followtags
)
1176 strvec_push(&args
, "--include-tag");
1178 strvec_push(&args
, "--thin");
1179 if (options
.verbosity
>= 3)
1180 strvec_pushl(&args
, "-v", "-v", NULL
);
1181 if (options
.check_self_contained_and_connected
)
1182 strvec_push(&args
, "--check-self-contained-and-connected");
1183 if (options
.cloning
)
1184 strvec_push(&args
, "--cloning");
1185 if (options
.update_shallow
)
1186 strvec_push(&args
, "--update-shallow");
1187 if (!options
.progress
)
1188 strvec_push(&args
, "--no-progress");
1190 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1191 if (options
.deepen_since
)
1192 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1193 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1194 strvec_pushf(&args
, "--shallow-exclude=%s",
1195 options
.deepen_not
.items
[i
].string
);
1196 if (options
.deepen_relative
&& options
.depth
)
1197 strvec_push(&args
, "--deepen-relative");
1198 if (options
.from_promisor
)
1199 strvec_push(&args
, "--from-promisor");
1200 if (options
.refetch
)
1201 strvec_push(&args
, "--refetch");
1203 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1204 strvec_push(&args
, url
.buf
);
1206 for (i
= 0; i
< nr_heads
; i
++) {
1207 struct ref
*ref
= to_fetch
[i
];
1209 die(_("cannot fetch by sha1 over smart http"));
1210 packet_buf_write(&preamble
, "%s %s\n",
1211 oid_to_hex(&ref
->old_oid
), ref
->name
);
1213 packet_buf_flush(&preamble
);
1215 memset(&rpc
, 0, sizeof(rpc
));
1216 rpc
.service_name
= "git-upload-pack",
1217 rpc
.gzip_request
= 1;
1219 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1221 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1222 strbuf_release(&rpc_result
);
1223 strbuf_release(&preamble
);
1224 strvec_clear(&args
);
1228 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1230 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1232 return fetch_git(d
, nr_heads
, to_fetch
);
1234 return fetch_dumb(nr_heads
, to_fetch
);
1237 static void parse_fetch(struct strbuf
*buf
)
1239 struct ref
**to_fetch
= NULL
;
1240 struct ref
*list_head
= NULL
;
1241 struct ref
**list
= &list_head
;
1242 int alloc_heads
= 0, nr_heads
= 0;
1246 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1249 struct object_id old_oid
;
1252 if (parse_oid_hex(p
, &old_oid
, &q
))
1253 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1259 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1261 ref
= alloc_ref(name
);
1262 oidcpy(&ref
->old_oid
, &old_oid
);
1267 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1268 to_fetch
[nr_heads
++] = ref
;
1271 die(_("http transport does not support %s"), buf
->buf
);
1274 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1280 if (fetch(nr_heads
, to_fetch
))
1281 exit(128); /* error already reported */
1282 free_refs(list_head
);
1290 static void parse_get(const char *arg
)
1292 struct strbuf url
= STRBUF_INIT
;
1293 struct strbuf path
= STRBUF_INIT
;
1296 space
= strchr(arg
, ' ');
1299 die(_("protocol error: expected '<url> <path>', missing space"));
1301 strbuf_add(&url
, arg
, space
- arg
);
1302 strbuf_addstr(&path
, space
+ 1);
1304 if (http_get_file(url
.buf
, path
.buf
, NULL
))
1305 die(_("failed to download file at URL '%s'"), url
.buf
);
1307 strbuf_release(&url
);
1308 strbuf_release(&path
);
1313 static int push_dav(int nr_spec
, const char **specs
)
1315 struct child_process child
= CHILD_PROCESS_INIT
;
1319 strvec_push(&child
.args
, "http-push");
1320 strvec_push(&child
.args
, "--helper-status");
1321 if (options
.dry_run
)
1322 strvec_push(&child
.args
, "--dry-run");
1323 if (options
.verbosity
> 1)
1324 strvec_push(&child
.args
, "--verbose");
1325 strvec_push(&child
.args
, url
.buf
);
1326 for (i
= 0; i
< nr_spec
; i
++)
1327 strvec_push(&child
.args
, specs
[i
]);
1329 if (run_command(&child
))
1330 die(_("git-http-push failed"));
1334 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1336 struct rpc_state rpc
= RPC_STATE_INIT
;
1339 struct string_list_item
*cas_option
;
1340 struct strbuf preamble
= STRBUF_INIT
;
1341 struct strbuf rpc_result
= STRBUF_INIT
;
1344 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1348 strvec_push(&args
, "--thin");
1349 if (options
.dry_run
)
1350 strvec_push(&args
, "--dry-run");
1351 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1352 strvec_push(&args
, "--signed=yes");
1353 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1354 strvec_push(&args
, "--signed=if-asked");
1356 strvec_push(&args
, "--atomic");
1357 if (options
.verbosity
== 0)
1358 strvec_push(&args
, "--quiet");
1359 else if (options
.verbosity
> 1)
1360 strvec_push(&args
, "--verbose");
1361 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1362 strvec_pushf(&args
, "--push-option=%s",
1363 options
.push_options
.items
[i
].string
);
1364 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1365 for_each_string_list_item(cas_option
, &cas_options
)
1366 strvec_push(&args
, cas_option
->string
);
1367 strvec_push(&args
, url
.buf
);
1369 if (options
.force_if_includes
)
1370 strvec_push(&args
, "--force-if-includes");
1372 strvec_push(&args
, "--stdin");
1373 for (i
= 0; i
< nr_spec
; i
++)
1374 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1375 packet_buf_flush(&preamble
);
1377 memset(&rpc
, 0, sizeof(rpc
));
1378 rpc
.service_name
= "git-receive-pack",
1380 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1382 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1383 strbuf_release(&rpc_result
);
1384 strbuf_release(&preamble
);
1385 strvec_clear(&args
);
1389 static int push(int nr_spec
, const char **specs
)
1391 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1394 if (heads
->proto_git
)
1395 ret
= push_git(heads
, nr_spec
, specs
);
1397 ret
= push_dav(nr_spec
, specs
);
1398 free_discovery(heads
);
1402 static void parse_push(struct strbuf
*buf
)
1404 struct strvec specs
= STRVEC_INIT
;
1409 if (skip_prefix(buf
->buf
, "push ", &arg
))
1410 strvec_push(&specs
, arg
);
1412 die(_("http transport does not support %s"), buf
->buf
);
1415 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1421 ret
= push(specs
.nr
, specs
.v
);
1426 exit(128); /* error already reported */
1429 strvec_clear(&specs
);
1432 static int stateless_connect(const char *service_name
)
1434 struct discovery
*discover
;
1435 struct rpc_state rpc
= RPC_STATE_INIT
;
1436 struct strbuf buf
= STRBUF_INIT
;
1437 const char *accept_language
;
1440 * Run the info/refs request and see if the server supports protocol
1441 * v2. If and only if the server supports v2 can we successfully
1442 * establish a stateless connection, otherwise we need to tell the
1443 * client to fallback to using other transport helper functions to
1444 * complete their request.
1446 discover
= discover_refs(service_name
, 0);
1447 if (discover
->version
!= protocol_v2
) {
1448 printf("fallback\n");
1452 /* Stateless Connection established */
1456 accept_language
= http_get_accept_language_header();
1457 if (accept_language
)
1458 rpc
.hdr_accept_language
= xstrfmt("%s", accept_language
);
1460 rpc
.service_name
= service_name
;
1461 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1462 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1463 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1464 if (get_protocol_http_header(discover
->version
, &buf
)) {
1465 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1467 rpc
.protocol_header
= NULL
;
1468 strbuf_release(&buf
);
1470 rpc
.buf
= xmalloc(http_post_buffer
);
1471 rpc
.alloc
= http_post_buffer
;
1476 rpc
.any_written
= 0;
1477 rpc
.gzip_request
= 1;
1478 rpc
.initial_buffer
= 0;
1479 rpc
.write_line_lengths
= 1;
1480 rpc
.flush_read_but_not_sent
= 0;
1483 * Dump the capability listing that we got from the server earlier
1484 * during the info/refs request.
1486 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1488 /* Until we see EOF keep sending POSTs */
1491 enum packet_read_status status
;
1493 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1495 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1496 if (status
== PACKET_READ_EOF
)
1498 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1499 /* We would have an err here */
1501 /* Reset the buffer for next request */
1505 free(rpc
.service_url
);
1506 free(rpc
.hdr_content_type
);
1507 free(rpc
.hdr_accept
);
1508 free(rpc
.hdr_accept_language
);
1509 free(rpc
.protocol_header
);
1511 strbuf_release(&buf
);
1516 int cmd_main(int argc
, const char **argv
)
1518 struct strbuf buf
= STRBUF_INIT
;
1522 setup_git_directory_gently(&nongit
);
1524 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1528 options
.verbosity
= 1;
1529 options
.progress
= !!isatty(2);
1531 string_list_init_dup(&options
.deepen_not
);
1532 string_list_init_dup(&options
.push_options
);
1535 * Just report "remote-curl" here (folding all the various aliases
1536 * ("git-remote-http", "git-remote-https", and etc.) here since they
1537 * are all just copies of the same actual executable.
1539 trace2_cmd_name("remote-curl");
1541 remote
= remote_get(argv
[1]);
1544 end_url_with_slash(&url
, argv
[2]);
1546 end_url_with_slash(&url
, remote
->url
[0]);
1549 http_init(remote
, url
.buf
, 0);
1554 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1556 error(_("remote-curl: error reading command stream from git"));
1561 if (starts_with(buf
.buf
, "fetch ")) {
1563 die(_("remote-curl: fetch attempted without a local repo"));
1566 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1567 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1568 output_refs(get_refs(for_push
));
1570 } else if (starts_with(buf
.buf
, "push ")) {
1573 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1574 char *value
= strchr(arg
, ' ');
1582 result
= set_option(arg
, value
);
1585 else if (result
< 0)
1586 printf("error invalid value\n");
1588 printf("unsupported\n");
1591 } else if (skip_prefix(buf
.buf
, "get ", &arg
)) {
1595 } else if (!strcmp(buf
.buf
, "capabilities")) {
1596 printf("stateless-connect\n");
1601 printf("check-connectivity\n");
1602 printf("object-format\n");
1605 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1606 if (!stateless_connect(arg
))
1609 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);
1618 strbuf_release(&buf
);