1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
4 #include "environment.h"
12 #include "run-command.h"
14 #include "string-list.h"
16 #include "credential.h"
17 #include "oid-array.h"
18 #include "send-pack.h"
23 #include "transport.h"
25 #include "write-or-die.h"
27 static struct remote
*remote
;
28 /* always ends with a trailing slash */
29 static struct strbuf url
= STRBUF_INIT
;
35 struct string_list deepen_not
;
36 struct string_list push_options
;
38 unsigned progress
: 1,
39 check_self_contained_and_connected
: 1,
45 /* One of the SEND_PACK_PUSH_CERT_* constants. */
49 /* see documentation of corresponding flag in fetch-pack.h */
55 force_if_includes
: 1;
56 const struct git_hash_algo
*hash_algo
;
58 static struct options options
;
59 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
61 static int set_option(const char *name
, const char *value
)
63 if (!strcmp(name
, "verbosity")) {
65 int v
= strtol(value
, &end
, 10);
66 if (value
== end
|| *end
)
68 options
.verbosity
= v
;
71 else if (!strcmp(name
, "progress")) {
72 if (!strcmp(value
, "true"))
74 else if (!strcmp(value
, "false"))
80 else if (!strcmp(name
, "depth")) {
82 unsigned long v
= strtoul(value
, &end
, 10);
83 if (value
== end
|| *end
)
88 else if (!strcmp(name
, "deepen-since")) {
89 options
.deepen_since
= xstrdup(value
);
92 else if (!strcmp(name
, "deepen-not")) {
93 string_list_append(&options
.deepen_not
, value
);
96 else if (!strcmp(name
, "deepen-relative")) {
97 if (!strcmp(value
, "true"))
98 options
.deepen_relative
= 1;
99 else if (!strcmp(value
, "false"))
100 options
.deepen_relative
= 0;
105 else if (!strcmp(name
, "followtags")) {
106 if (!strcmp(value
, "true"))
107 options
.followtags
= 1;
108 else if (!strcmp(value
, "false"))
109 options
.followtags
= 0;
114 else if (!strcmp(name
, "dry-run")) {
115 if (!strcmp(value
, "true"))
117 else if (!strcmp(value
, "false"))
123 else if (!strcmp(name
, "check-connectivity")) {
124 if (!strcmp(value
, "true"))
125 options
.check_self_contained_and_connected
= 1;
126 else if (!strcmp(value
, "false"))
127 options
.check_self_contained_and_connected
= 0;
132 else if (!strcmp(name
, "cas")) {
133 struct strbuf val
= STRBUF_INIT
;
134 strbuf_addstr(&val
, "--force-with-lease=");
136 strbuf_addstr(&val
, value
);
137 else if (unquote_c_style(&val
, value
, NULL
))
139 string_list_append(&cas_options
, val
.buf
);
140 strbuf_release(&val
);
142 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
143 if (!strcmp(value
, "true"))
144 options
.force_if_includes
= 1;
145 else if (!strcmp(value
, "false"))
146 options
.force_if_includes
= 0;
150 } else if (!strcmp(name
, "cloning")) {
151 if (!strcmp(value
, "true"))
153 else if (!strcmp(value
, "false"))
158 } else if (!strcmp(name
, "update-shallow")) {
159 if (!strcmp(value
, "true"))
160 options
.update_shallow
= 1;
161 else if (!strcmp(value
, "false"))
162 options
.update_shallow
= 0;
166 } else if (!strcmp(name
, "pushcert")) {
167 if (!strcmp(value
, "true"))
168 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
169 else if (!strcmp(value
, "false"))
170 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
171 else if (!strcmp(value
, "if-asked"))
172 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
176 } else if (!strcmp(name
, "atomic")) {
177 if (!strcmp(value
, "true"))
179 else if (!strcmp(value
, "false"))
184 } else if (!strcmp(name
, "push-option")) {
186 string_list_append(&options
.push_options
, value
);
188 struct strbuf unquoted
= STRBUF_INIT
;
189 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
190 die(_("invalid quoting in push-option value: '%s'"), value
);
191 string_list_append_nodup(&options
.push_options
,
192 strbuf_detach(&unquoted
, NULL
));
195 } else if (!strcmp(name
, "family")) {
196 if (!strcmp(value
, "ipv4"))
197 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
198 else if (!strcmp(value
, "ipv6"))
199 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
200 else if (!strcmp(value
, "all"))
201 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
205 } else if (!strcmp(name
, "from-promisor")) {
206 options
.from_promisor
= 1;
208 } else if (!strcmp(name
, "refetch")) {
211 } else if (!strcmp(name
, "filter")) {
212 options
.filter
= xstrdup(value
);
214 } else if (!strcmp(name
, "object-format")) {
215 options
.object_format
= 1;
216 if (strcmp(value
, "true"))
217 die(_("unknown value for object-format: %s"), value
);
220 return 1 /* unsupported */;
230 struct oid_array shallow
;
231 enum protocol_version version
;
232 unsigned proto_git
: 1;
234 static struct discovery
*last_discovery
;
236 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
238 struct ref
*list
= NULL
;
239 struct packet_reader reader
;
241 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
242 PACKET_READ_CHOMP_NEWLINE
|
243 PACKET_READ_GENTLE_ON_EOF
|
244 PACKET_READ_DIE_ON_ERR_PACKET
);
246 heads
->version
= discover_version(&reader
);
247 switch (heads
->version
) {
250 * Do nothing. This isn't a list of refs but rather a
251 * capability advertisement. Client would have run
252 * 'stateless-connect' so we'll dump this capability listing
253 * and let them request the refs themselves.
258 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
259 NULL
, &heads
->shallow
);
260 options
.hash_algo
= reader
.hash_algo
;
262 case protocol_unknown_version
:
263 BUG("unknown protocol version");
269 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
271 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
274 return the_hash_algo
;
276 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
277 if (algo
== GIT_HASH_UNKNOWN
)
279 return &hash_algos
[algo
];
282 static struct ref
*parse_info_refs(struct discovery
*heads
)
284 char *data
, *start
, *mid
;
288 struct ref
*refs
= NULL
;
289 struct ref
*ref
= NULL
;
290 struct ref
*last_ref
= NULL
;
292 options
.hash_algo
= detect_hash_algo(heads
);
293 if (!options
.hash_algo
)
294 die("%sinfo/refs not valid: could not determine hash algorithm; "
295 "is this a git repository?",
296 transport_anonymize_url(url
.buf
));
301 while (i
< heads
->len
) {
307 if (data
[i
] == '\n') {
308 if (mid
- start
!= options
.hash_algo
->hexsz
)
309 die(_("%sinfo/refs not valid: is this a git repository?"),
310 transport_anonymize_url(url
.buf
));
313 ref
= alloc_ref(ref_name
);
314 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
318 last_ref
->next
= ref
;
325 ref
= alloc_ref("HEAD");
326 if (!http_fetch_ref(url
.buf
, ref
) &&
327 !resolve_remote_symref(ref
, refs
)) {
337 static void free_discovery(struct discovery
*d
)
340 if (d
== last_discovery
)
341 last_discovery
= NULL
;
342 free(d
->shallow
.oid
);
350 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
356 * We only show text/plain parts, as other types are likely
357 * to be ugly to look at on the user's terminal.
359 if (strcmp(type
->buf
, "text/plain"))
362 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
370 eol
= strchrnul(p
, '\n');
371 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
377 static int get_protocol_http_header(enum protocol_version version
,
378 struct strbuf
*header
)
381 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
390 static void check_smart_http(struct discovery
*d
, const char *service
,
394 struct packet_reader reader
;
397 * If we don't see x-$service-advertisement, then it's not smart-http.
398 * But once we do, we commit to it and assume any other protocol
399 * violations are hard errors.
401 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
402 !skip_prefix(p
, service
, &p
) ||
403 strcmp(p
, "-advertisement"))
406 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
407 PACKET_READ_CHOMP_NEWLINE
|
408 PACKET_READ_DIE_ON_ERR_PACKET
);
409 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
410 die(_("invalid server response; expected service, got flush packet"));
412 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
414 * The header can include additional metadata lines, up
415 * until a packet flush marker. Ignore these now, but
416 * in the future we might start to scan them.
419 packet_reader_read(&reader
);
420 if (reader
.pktlen
<= 0) {
426 * v0 smart http; callers expect us to soak up the
427 * service and header packets
429 d
->buf
= reader
.src_buffer
;
430 d
->len
= reader
.src_len
;
433 } else if (!strcmp(reader
.line
, "version 2")) {
435 * v2 smart http; do not consume version packet, which will
436 * be handled elsewhere.
441 die(_("invalid server response; got '%s'"), reader
.line
);
445 static struct discovery
*discover_refs(const char *service
, int for_push
)
447 struct strbuf type
= STRBUF_INIT
;
448 struct strbuf charset
= STRBUF_INIT
;
449 struct strbuf buffer
= STRBUF_INIT
;
450 struct strbuf refs_url
= STRBUF_INIT
;
451 struct strbuf effective_url
= STRBUF_INIT
;
452 struct strbuf protocol_header
= STRBUF_INIT
;
453 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
454 struct discovery
*last
= last_discovery
;
455 int http_ret
, maybe_smart
= 0;
456 struct http_get_options http_options
;
457 enum protocol_version version
= get_protocol_version_config();
459 if (last
&& !strcmp(service
, last
->service
))
461 free_discovery(last
);
463 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
464 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
465 git_env_bool("GIT_SMART_HTTP", 1)) {
467 if (!strchr(url
.buf
, '?'))
468 strbuf_addch(&refs_url
, '?');
470 strbuf_addch(&refs_url
, '&');
471 strbuf_addf(&refs_url
, "service=%s", service
);
475 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
476 * to perform any operation that doesn't involve upload-pack (i.e., a
477 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
478 * to do anything else (like push or remote archive) via v2.
480 if (version
== protocol_v2
&& strcmp("git-upload-pack", service
))
481 version
= protocol_v0
;
483 /* Add the extra Git-Protocol header */
484 if (get_protocol_http_header(version
, &protocol_header
))
485 string_list_append(&extra_headers
, protocol_header
.buf
);
487 memset(&http_options
, 0, sizeof(http_options
));
488 http_options
.content_type
= &type
;
489 http_options
.charset
= &charset
;
490 http_options
.effective_url
= &effective_url
;
491 http_options
.base_url
= &url
;
492 http_options
.extra_headers
= &extra_headers
;
493 http_options
.initial_request
= 1;
494 http_options
.no_cache
= 1;
496 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
500 case HTTP_MISSING_TARGET
:
501 show_http_message(&type
, &charset
, &buffer
);
502 die(_("repository '%s' not found"),
503 transport_anonymize_url(url
.buf
));
505 show_http_message(&type
, &charset
, &buffer
);
506 die(_("Authentication failed for '%s'"),
507 transport_anonymize_url(url
.buf
));
508 case HTTP_NOMATCHPUBLICKEY
:
509 show_http_message(&type
, &charset
, &buffer
);
510 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
511 transport_anonymize_url(url
.buf
), curl_errorstr
);
513 show_http_message(&type
, &charset
, &buffer
);
514 die(_("unable to access '%s': %s"),
515 transport_anonymize_url(url
.buf
), curl_errorstr
);
518 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
519 char *u
= transport_anonymize_url(url
.buf
);
520 warning(_("redirecting to %s"), u
);
524 last
= xcalloc(1, sizeof(*last_discovery
));
525 last
->service
= xstrdup(service
);
526 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
527 last
->buf
= last
->buf_alloc
;
530 check_smart_http(last
, service
, &type
);
533 last
->refs
= parse_git_refs(last
, for_push
);
535 last
->refs
= parse_info_refs(last
);
537 strbuf_release(&refs_url
);
538 strbuf_release(&type
);
539 strbuf_release(&charset
);
540 strbuf_release(&effective_url
);
541 strbuf_release(&buffer
);
542 strbuf_release(&protocol_header
);
543 string_list_clear(&extra_headers
, 0);
544 last_discovery
= last
;
548 static struct ref
*get_refs(int for_push
)
550 struct discovery
*heads
;
553 heads
= discover_refs("git-receive-pack", for_push
);
555 heads
= discover_refs("git-upload-pack", for_push
);
560 static void output_refs(struct ref
*refs
)
563 if (options
.object_format
&& options
.hash_algo
) {
564 printf(":object-format %s\n", options
.hash_algo
->name
);
565 repo_set_hash_algo(the_repository
,
566 hash_algo_by_ptr(options
.hash_algo
));
568 for (posn
= refs
; posn
; posn
= posn
->next
) {
570 printf("@%s %s\n", posn
->symref
, posn
->name
);
572 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
581 const char *service_name
;
583 char *hdr_content_type
;
585 char *hdr_accept_language
;
586 char *protocol_header
;
594 unsigned gzip_request
: 1;
595 unsigned initial_buffer
: 1;
598 * Whenever a pkt-line is read into buf, append the 4 characters
599 * denoting its length before appending the payload.
601 unsigned write_line_lengths
: 1;
604 * Used by rpc_out; initialize to 0. This is true if a flush has been
605 * read, but the corresponding line length (if write_line_lengths is
606 * true) and EOF have not been sent to libcurl. Since each flush marks
607 * the end of a request, each flush must be completely sent before any
608 * further reading occurs.
610 unsigned flush_read_but_not_sent
: 1;
613 #define RPC_STATE_INIT { 0 }
616 * Appends the result of reading from rpc->out to the string represented by
617 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
618 * enough space, 0 otherwise.
620 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
621 * hexadecimal string before appending the result described above.
623 * Writes the total number of bytes appended into appended.
625 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
627 enum packet_read_status
*status
) {
632 if (rpc
->write_line_lengths
) {
633 left
= rpc
->alloc
- rpc
->len
- 4;
634 buf
= rpc
->buf
+ rpc
->len
+ 4;
636 left
= rpc
->alloc
- rpc
->len
;
637 buf
= rpc
->buf
+ rpc
->len
;
640 if (left
< LARGE_PACKET_MAX
)
643 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
644 left
, &pktlen_raw
, options
);
645 if (*status
!= PACKET_READ_EOF
) {
646 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
647 rpc
->len
+= *appended
;
650 if (rpc
->write_line_lengths
) {
652 case PACKET_READ_EOF
:
653 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
654 die(_("shouldn't have EOF when not gentle on EOF"));
656 case PACKET_READ_NORMAL
:
657 set_packet_header(buf
- 4, *appended
);
659 case PACKET_READ_DELIM
:
660 memcpy(buf
- 4, "0001", 4);
662 case PACKET_READ_FLUSH
:
663 memcpy(buf
- 4, "0000", 4);
665 case PACKET_READ_RESPONSE_END
:
666 die(_("remote server sent unexpected response end packet"));
673 static size_t rpc_out(void *ptr
, size_t eltsize
,
674 size_t nmemb
, void *buffer_
)
676 size_t max
= eltsize
* nmemb
;
677 struct rpc_state
*rpc
= buffer_
;
678 size_t avail
= rpc
->len
- rpc
->pos
;
679 enum packet_read_status status
;
682 rpc
->initial_buffer
= 0;
685 if (!rpc
->flush_read_but_not_sent
) {
686 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
687 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
688 if (status
== PACKET_READ_FLUSH
)
689 rpc
->flush_read_but_not_sent
= 1;
692 * If flush_read_but_not_sent is true, we have already read one
693 * full request but have not fully sent it + EOF, which is why
694 * we need to refrain from reading.
697 if (rpc
->flush_read_but_not_sent
) {
700 * The line length either does not need to be sent at
701 * all or has already been completely sent. Now we can
702 * return 0, indicating EOF, meaning that the flush has
705 rpc
->flush_read_but_not_sent
= 0;
709 * If avail is non-zero, the line length for the flush still
710 * hasn't been fully sent. Proceed with sending the line
717 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
722 static int rpc_seek(void *clientp
, curl_off_t offset
, int origin
)
724 struct rpc_state
*rpc
= clientp
;
726 if (origin
!= SEEK_SET
)
727 BUG("rpc_seek only handles SEEK_SET, not %d", origin
);
729 if (rpc
->initial_buffer
) {
730 if (offset
< 0 || offset
> rpc
->len
) {
731 error("curl seek would be outside of rpc buffer");
732 return CURL_SEEKFUNC_FAIL
;
735 return CURL_SEEKFUNC_OK
;
737 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
738 return CURL_SEEKFUNC_FAIL
;
741 struct check_pktline_state
{
747 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
750 if (!state
->remaining
) {
751 int digits_remaining
= 4 - state
->len_filled
;
752 if (digits_remaining
> size
)
753 digits_remaining
= size
;
754 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
755 state
->len_filled
+= digits_remaining
;
756 ptr
+= digits_remaining
;
757 size
-= digits_remaining
;
759 if (state
->len_filled
== 4) {
760 state
->remaining
= packet_length(state
->len_buf
,
761 sizeof(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
= NULL
;
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
, 0);
926 } while (err
== HTTP_REAUTH
);
930 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
|| http_auth
.authtype
)
931 needs_100_continue
= 1;
935 headers
= http_copy_default_headers();
936 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
937 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
938 headers
= curl_slist_append(headers
, needs_100_continue
?
939 "Expect: 100-continue" : "Expect:");
941 headers
= http_append_auth_header(&http_auth
, headers
);
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
);
951 slot
= get_active_slot();
953 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
954 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
955 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
956 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
959 /* The request body is large and the size cannot be predicted.
960 * We must use chunked encoding to send it.
962 #ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
963 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
965 rpc
->initial_buffer
= 1;
966 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
967 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
968 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKFUNCTION
, rpc_seek
);
969 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKDATA
, rpc
);
970 if (options
.verbosity
> 1) {
971 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
975 } else if (gzip_body
) {
977 * If we are looping to retry authentication, then the previous
978 * run will have set up the headers and gzip buffer already,
979 * and we just need to send it.
981 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
982 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
984 } else if (use_gzip
&& 1024 < rpc
->len
) {
985 /* The client backend isn't giving us compressed data so
986 * we can try to deflate it ourselves, this may save on
992 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
993 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
994 gzip_body
= xmalloc(gzip_size
);
996 stream
.next_in
= (unsigned char *)rpc
->buf
;
997 stream
.avail_in
= rpc
->len
;
998 stream
.next_out
= (unsigned char *)gzip_body
;
999 stream
.avail_out
= gzip_size
;
1001 ret
= git_deflate(&stream
, Z_FINISH
);
1002 if (ret
!= Z_STREAM_END
)
1003 die(_("cannot deflate request; zlib deflate error %d"), ret
);
1005 ret
= git_deflate_end_gently(&stream
);
1007 die(_("cannot deflate request; zlib end error %d"), ret
);
1009 gzip_size
= stream
.total_out
;
1011 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
1012 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
1013 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
1015 if (options
.verbosity
> 1) {
1016 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
1018 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1022 /* We know the complete request size in advance, use the
1023 * more normal Content-Length approach.
1025 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1026 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1027 if (options
.verbosity
> 1) {
1028 fprintf(stderr
, "POST %s (%lu bytes)\n",
1029 rpc
->service_name
, (unsigned long)rpc
->len
);
1034 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1035 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1036 rpc_in_data
.rpc
= rpc
;
1037 rpc_in_data
.slot
= slot
;
1038 rpc_in_data
.check_pktline
= stateless_connect
;
1039 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1040 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &rpc_in_data
);
1041 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1044 rpc
->any_written
= 0;
1045 err
= run_slot(slot
, NULL
);
1046 if (err
== HTTP_REAUTH
&& !large_request
) {
1047 credential_fill(&http_auth
, 0);
1048 curl_slist_free_all(headers
);
1054 if (!rpc
->any_written
)
1057 if (rpc_in_data
.pktline_state
.len_filled
)
1058 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1059 if (rpc_in_data
.pktline_state
.remaining
)
1060 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1062 if (stateless_connect
)
1063 packet_response_end(rpc
->in
);
1065 curl_slist_free_all(headers
);
1070 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1071 const char **client_argv
, const struct strbuf
*preamble
,
1072 struct strbuf
*rpc_result
)
1074 const char *svc
= rpc
->service_name
;
1075 struct strbuf buf
= STRBUF_INIT
;
1076 struct child_process client
= CHILD_PROCESS_INIT
;
1082 strvec_pushv(&client
.args
, client_argv
);
1083 if (start_command(&client
))
1085 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1087 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1089 rpc
->alloc
= http_post_buffer
;
1090 rpc
->buf
= xmalloc(rpc
->alloc
);
1091 rpc
->in
= client
.in
;
1092 rpc
->out
= client
.out
;
1094 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1095 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1097 rpc
->hdr_accept_language
= xstrdup_or_null(http_get_accept_language_header());
1099 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1100 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1102 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1103 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1105 if (get_protocol_http_header(heads
->version
, &buf
))
1106 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1108 rpc
->protocol_header
= NULL
;
1111 int n
= packet_read(rpc
->out
, rpc
->buf
, rpc
->alloc
, 0);
1116 err
|= post_rpc(rpc
, 0, 0);
1122 strbuf_read(rpc_result
, client
.out
, 0);
1126 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1133 err
|= finish_command(&client
);
1134 free(rpc
->service_url
);
1135 free(rpc
->hdr_content_type
);
1136 free(rpc
->hdr_accept
);
1137 free(rpc
->hdr_accept_language
);
1138 free(rpc
->protocol_header
);
1140 strbuf_release(&buf
);
1144 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1146 struct walker
*walker
;
1150 ALLOC_ARRAY(targets
, nr_heads
);
1151 if (options
.depth
|| options
.deepen_since
)
1152 die(_("dumb http transport does not support shallow capabilities"));
1153 for (i
= 0; i
< nr_heads
; i
++)
1154 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1156 walker
= get_http_walker(url
.buf
);
1157 walker
->get_verbosely
= options
.verbosity
>= 3;
1158 walker
->get_progress
= options
.progress
;
1159 walker
->get_recover
= 0;
1160 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1161 walker_free(walker
);
1163 for (i
= 0; i
< nr_heads
; i
++)
1167 return ret
? error(_("fetch failed.")) : 0;
1170 static int fetch_git(struct discovery
*heads
,
1171 int nr_heads
, struct ref
**to_fetch
)
1173 struct rpc_state rpc
= RPC_STATE_INIT
;
1174 struct strbuf preamble
= STRBUF_INIT
;
1176 struct strvec args
= STRVEC_INIT
;
1177 struct strbuf rpc_result
= STRBUF_INIT
;
1179 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1180 "--stdin", "--lock-pack", NULL
);
1181 if (options
.followtags
)
1182 strvec_push(&args
, "--include-tag");
1184 strvec_push(&args
, "--thin");
1185 if (options
.verbosity
>= 3)
1186 strvec_pushl(&args
, "-v", "-v", NULL
);
1187 if (options
.check_self_contained_and_connected
)
1188 strvec_push(&args
, "--check-self-contained-and-connected");
1189 if (options
.cloning
)
1190 strvec_push(&args
, "--cloning");
1191 if (options
.update_shallow
)
1192 strvec_push(&args
, "--update-shallow");
1193 if (!options
.progress
)
1194 strvec_push(&args
, "--no-progress");
1196 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1197 if (options
.deepen_since
)
1198 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1199 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1200 strvec_pushf(&args
, "--shallow-exclude=%s",
1201 options
.deepen_not
.items
[i
].string
);
1202 if (options
.deepen_relative
&& options
.depth
)
1203 strvec_push(&args
, "--deepen-relative");
1204 if (options
.from_promisor
)
1205 strvec_push(&args
, "--from-promisor");
1206 if (options
.refetch
)
1207 strvec_push(&args
, "--refetch");
1209 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1210 strvec_push(&args
, url
.buf
);
1212 for (i
= 0; i
< nr_heads
; i
++) {
1213 struct ref
*ref
= to_fetch
[i
];
1215 die(_("cannot fetch by sha1 over smart http"));
1216 packet_buf_write(&preamble
, "%s %s\n",
1217 oid_to_hex(&ref
->old_oid
), ref
->name
);
1219 packet_buf_flush(&preamble
);
1221 memset(&rpc
, 0, sizeof(rpc
));
1222 rpc
.service_name
= "git-upload-pack",
1223 rpc
.gzip_request
= 1;
1225 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1227 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1228 strbuf_release(&rpc_result
);
1229 strbuf_release(&preamble
);
1230 strvec_clear(&args
);
1234 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1236 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1238 return fetch_git(d
, nr_heads
, to_fetch
);
1240 return fetch_dumb(nr_heads
, to_fetch
);
1243 static void parse_fetch(struct strbuf
*buf
)
1245 struct ref
**to_fetch
= NULL
;
1246 struct ref
*list_head
= NULL
;
1247 struct ref
**list
= &list_head
;
1248 int alloc_heads
= 0, nr_heads
= 0;
1252 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1255 struct object_id old_oid
;
1258 if (parse_oid_hex(p
, &old_oid
, &q
))
1259 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1265 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1267 ref
= alloc_ref(name
);
1268 oidcpy(&ref
->old_oid
, &old_oid
);
1273 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1274 to_fetch
[nr_heads
++] = ref
;
1277 die(_("http transport does not support %s"), buf
->buf
);
1280 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1286 if (fetch(nr_heads
, to_fetch
))
1287 exit(128); /* error already reported */
1288 free_refs(list_head
);
1296 static void parse_get(const char *arg
)
1298 struct strbuf url
= STRBUF_INIT
;
1299 struct strbuf path
= STRBUF_INIT
;
1302 space
= strchr(arg
, ' ');
1305 die(_("protocol error: expected '<url> <path>', missing space"));
1307 strbuf_add(&url
, arg
, space
- arg
);
1308 strbuf_addstr(&path
, space
+ 1);
1310 if (http_get_file(url
.buf
, path
.buf
, NULL
))
1311 die(_("failed to download file at URL '%s'"), url
.buf
);
1313 strbuf_release(&url
);
1314 strbuf_release(&path
);
1319 static int push_dav(int nr_spec
, const char **specs
)
1321 struct child_process child
= CHILD_PROCESS_INIT
;
1325 strvec_push(&child
.args
, "http-push");
1326 strvec_push(&child
.args
, "--helper-status");
1327 if (options
.dry_run
)
1328 strvec_push(&child
.args
, "--dry-run");
1329 if (options
.verbosity
> 1)
1330 strvec_push(&child
.args
, "--verbose");
1331 strvec_push(&child
.args
, url
.buf
);
1332 for (i
= 0; i
< nr_spec
; i
++)
1333 strvec_push(&child
.args
, specs
[i
]);
1335 if (run_command(&child
))
1336 die(_("git-http-push failed"));
1340 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1342 struct rpc_state rpc
= RPC_STATE_INIT
;
1345 struct string_list_item
*cas_option
;
1346 struct strbuf preamble
= STRBUF_INIT
;
1347 struct strbuf rpc_result
= STRBUF_INIT
;
1350 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1354 strvec_push(&args
, "--thin");
1355 if (options
.dry_run
)
1356 strvec_push(&args
, "--dry-run");
1357 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1358 strvec_push(&args
, "--signed=yes");
1359 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1360 strvec_push(&args
, "--signed=if-asked");
1362 strvec_push(&args
, "--atomic");
1363 if (options
.verbosity
== 0)
1364 strvec_push(&args
, "--quiet");
1365 else if (options
.verbosity
> 1)
1366 strvec_push(&args
, "--verbose");
1367 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1368 strvec_pushf(&args
, "--push-option=%s",
1369 options
.push_options
.items
[i
].string
);
1370 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1371 for_each_string_list_item(cas_option
, &cas_options
)
1372 strvec_push(&args
, cas_option
->string
);
1373 strvec_push(&args
, url
.buf
);
1375 if (options
.force_if_includes
)
1376 strvec_push(&args
, "--force-if-includes");
1378 strvec_push(&args
, "--stdin");
1379 for (i
= 0; i
< nr_spec
; i
++)
1380 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1381 packet_buf_flush(&preamble
);
1383 memset(&rpc
, 0, sizeof(rpc
));
1384 rpc
.service_name
= "git-receive-pack",
1386 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1388 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1389 strbuf_release(&rpc_result
);
1390 strbuf_release(&preamble
);
1391 strvec_clear(&args
);
1395 static int push(int nr_spec
, const char **specs
)
1397 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1400 if (heads
->proto_git
)
1401 ret
= push_git(heads
, nr_spec
, specs
);
1403 ret
= push_dav(nr_spec
, specs
);
1404 free_discovery(heads
);
1408 static void parse_push(struct strbuf
*buf
)
1410 struct strvec specs
= STRVEC_INIT
;
1415 if (skip_prefix(buf
->buf
, "push ", &arg
))
1416 strvec_push(&specs
, arg
);
1418 die(_("http transport does not support %s"), buf
->buf
);
1421 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1427 ret
= push(specs
.nr
, specs
.v
);
1432 exit(128); /* error already reported */
1435 strvec_clear(&specs
);
1438 static int stateless_connect(const char *service_name
)
1440 struct discovery
*discover
;
1441 struct rpc_state rpc
= RPC_STATE_INIT
;
1442 struct strbuf buf
= STRBUF_INIT
;
1443 const char *accept_language
;
1446 * Run the info/refs request and see if the server supports protocol
1447 * v2. If and only if the server supports v2 can we successfully
1448 * establish a stateless connection, otherwise we need to tell the
1449 * client to fallback to using other transport helper functions to
1450 * complete their request.
1452 * The "git-upload-archive" service is a read-only operation. Fallback
1453 * to use "git-upload-pack" service to discover protocol version.
1455 if (!strcmp(service_name
, "git-upload-archive"))
1456 discover
= discover_refs("git-upload-pack", 0);
1458 discover
= discover_refs(service_name
, 0);
1459 if (discover
->version
!= protocol_v2
) {
1460 printf("fallback\n");
1464 /* Stateless Connection established */
1468 accept_language
= http_get_accept_language_header();
1469 if (accept_language
)
1470 rpc
.hdr_accept_language
= xstrfmt("%s", accept_language
);
1472 rpc
.service_name
= service_name
;
1473 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1474 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1475 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1476 if (get_protocol_http_header(discover
->version
, &buf
)) {
1477 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1479 rpc
.protocol_header
= NULL
;
1480 strbuf_release(&buf
);
1482 rpc
.buf
= xmalloc(http_post_buffer
);
1483 rpc
.alloc
= http_post_buffer
;
1488 rpc
.any_written
= 0;
1489 rpc
.gzip_request
= 1;
1490 rpc
.initial_buffer
= 0;
1491 rpc
.write_line_lengths
= 1;
1492 rpc
.flush_read_but_not_sent
= 0;
1495 * Dump the capability listing that we got from the server earlier
1496 * during the info/refs request. This does not work with the
1497 * "git-upload-archive" service.
1499 if (strcmp(service_name
, "git-upload-archive"))
1500 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1502 /* Until we see EOF keep sending POSTs */
1505 enum packet_read_status status
;
1507 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1509 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1510 if (status
== PACKET_READ_EOF
)
1512 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1513 /* We would have an err here */
1515 /* Reset the buffer for next request */
1519 free(rpc
.service_url
);
1520 free(rpc
.hdr_content_type
);
1521 free(rpc
.hdr_accept
);
1522 free(rpc
.hdr_accept_language
);
1523 free(rpc
.protocol_header
);
1525 strbuf_release(&buf
);
1530 int cmd_main(int argc
, const char **argv
)
1532 struct strbuf buf
= STRBUF_INIT
;
1536 setup_git_directory_gently(&nongit
);
1538 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1542 options
.verbosity
= 1;
1543 options
.progress
= !!isatty(2);
1545 string_list_init_dup(&options
.deepen_not
);
1546 string_list_init_dup(&options
.push_options
);
1549 * Just report "remote-curl" here (folding all the various aliases
1550 * ("git-remote-http", "git-remote-https", and etc.) here since they
1551 * are all just copies of the same actual executable.
1553 trace2_cmd_name("remote-curl");
1555 remote
= remote_get(argv
[1]);
1558 end_url_with_slash(&url
, argv
[2]);
1560 end_url_with_slash(&url
, remote
->url
[0]);
1563 http_init(remote
, url
.buf
, 0);
1568 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1570 error(_("remote-curl: error reading command stream from git"));
1575 if (starts_with(buf
.buf
, "fetch ")) {
1577 setup_git_directory_gently(&nongit
);
1579 die(_("remote-curl: fetch attempted without a local repo"));
1583 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1584 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1585 output_refs(get_refs(for_push
));
1587 } else if (starts_with(buf
.buf
, "push ")) {
1590 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1591 char *value
= strchr(arg
, ' ');
1599 result
= set_option(arg
, value
);
1602 else if (result
< 0)
1603 printf("error invalid value\n");
1605 printf("unsupported\n");
1608 } else if (skip_prefix(buf
.buf
, "get ", &arg
)) {
1612 } else if (!strcmp(buf
.buf
, "capabilities")) {
1613 printf("stateless-connect\n");
1618 printf("check-connectivity\n");
1619 printf("object-format\n");
1622 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1623 if (!stateless_connect(arg
))
1626 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);
1635 strbuf_release(&buf
);