9 #include "run-command.h"
11 #include "string-list.h"
13 #include "argv-array.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. */
46 const struct git_hash_algo
*hash_algo
;
48 static struct options options
;
49 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
51 static int set_option(const char *name
, const char *value
)
53 if (!strcmp(name
, "verbosity")) {
55 int v
= strtol(value
, &end
, 10);
56 if (value
== end
|| *end
)
58 options
.verbosity
= v
;
61 else if (!strcmp(name
, "progress")) {
62 if (!strcmp(value
, "true"))
64 else if (!strcmp(value
, "false"))
70 else if (!strcmp(name
, "depth")) {
72 unsigned long v
= strtoul(value
, &end
, 10);
73 if (value
== end
|| *end
)
78 else if (!strcmp(name
, "deepen-since")) {
79 options
.deepen_since
= xstrdup(value
);
82 else if (!strcmp(name
, "deepen-not")) {
83 string_list_append(&options
.deepen_not
, value
);
86 else if (!strcmp(name
, "deepen-relative")) {
87 if (!strcmp(value
, "true"))
88 options
.deepen_relative
= 1;
89 else if (!strcmp(value
, "false"))
90 options
.deepen_relative
= 0;
95 else if (!strcmp(name
, "followtags")) {
96 if (!strcmp(value
, "true"))
97 options
.followtags
= 1;
98 else if (!strcmp(value
, "false"))
99 options
.followtags
= 0;
104 else if (!strcmp(name
, "dry-run")) {
105 if (!strcmp(value
, "true"))
107 else if (!strcmp(value
, "false"))
113 else if (!strcmp(name
, "check-connectivity")) {
114 if (!strcmp(value
, "true"))
115 options
.check_self_contained_and_connected
= 1;
116 else if (!strcmp(value
, "false"))
117 options
.check_self_contained_and_connected
= 0;
122 else if (!strcmp(name
, "cas")) {
123 struct strbuf val
= STRBUF_INIT
;
124 strbuf_addstr(&val
, "--force-with-lease=");
126 strbuf_addstr(&val
, value
);
127 else if (unquote_c_style(&val
, value
, NULL
))
129 string_list_append(&cas_options
, val
.buf
);
130 strbuf_release(&val
);
132 } else if (!strcmp(name
, "cloning")) {
133 if (!strcmp(value
, "true"))
135 else if (!strcmp(value
, "false"))
140 } else if (!strcmp(name
, "update-shallow")) {
141 if (!strcmp(value
, "true"))
142 options
.update_shallow
= 1;
143 else if (!strcmp(value
, "false"))
144 options
.update_shallow
= 0;
148 } else if (!strcmp(name
, "pushcert")) {
149 if (!strcmp(value
, "true"))
150 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
151 else if (!strcmp(value
, "false"))
152 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
153 else if (!strcmp(value
, "if-asked"))
154 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
158 } else if (!strcmp(name
, "atomic")) {
159 if (!strcmp(value
, "true"))
161 else if (!strcmp(value
, "false"))
166 } else if (!strcmp(name
, "push-option")) {
168 string_list_append(&options
.push_options
, value
);
170 struct strbuf unquoted
= STRBUF_INIT
;
171 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
172 die(_("invalid quoting in push-option value: '%s'"), value
);
173 string_list_append_nodup(&options
.push_options
,
174 strbuf_detach(&unquoted
, NULL
));
178 #if LIBCURL_VERSION_NUM >= 0x070a08
179 } else if (!strcmp(name
, "family")) {
180 if (!strcmp(value
, "ipv4"))
181 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
182 else if (!strcmp(value
, "ipv6"))
183 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
184 else if (!strcmp(value
, "all"))
185 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
189 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
190 } else if (!strcmp(name
, "from-promisor")) {
191 options
.from_promisor
= 1;
193 } else if (!strcmp(name
, "no-dependents")) {
194 options
.no_dependents
= 1;
196 } else if (!strcmp(name
, "filter")) {
197 options
.filter
= xstrdup(value
);
199 } else if (!strcmp(name
, "object-format")) {
201 options
.object_format
= 1;
202 if (strcmp(value
, "true")) {
203 algo
= hash_algo_by_name(value
);
204 if (algo
== GIT_HASH_UNKNOWN
)
205 die("unknown object format '%s'", value
);
206 options
.hash_algo
= &hash_algos
[algo
];
210 return 1 /* unsupported */;
220 struct oid_array shallow
;
221 enum protocol_version version
;
222 unsigned proto_git
: 1;
224 static struct discovery
*last_discovery
;
226 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
228 struct ref
*list
= NULL
;
229 struct packet_reader reader
;
231 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
232 PACKET_READ_CHOMP_NEWLINE
|
233 PACKET_READ_GENTLE_ON_EOF
|
234 PACKET_READ_DIE_ON_ERR_PACKET
);
236 heads
->version
= discover_version(&reader
);
237 switch (heads
->version
) {
240 * Do nothing. This isn't a list of refs but rather a
241 * capability advertisement. Client would have run
242 * 'stateless-connect' so we'll dump this capability listing
243 * and let them request the refs themselves.
248 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
249 NULL
, &heads
->shallow
);
250 options
.hash_algo
= reader
.hash_algo
;
252 case protocol_unknown_version
:
253 BUG("unknown protocol version");
259 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
261 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
264 return the_hash_algo
;
266 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
267 if (algo
== GIT_HASH_UNKNOWN
)
269 return &hash_algos
[algo
];
272 static struct ref
*parse_info_refs(struct discovery
*heads
)
274 char *data
, *start
, *mid
;
278 struct ref
*refs
= NULL
;
279 struct ref
*ref
= NULL
;
280 struct ref
*last_ref
= NULL
;
282 options
.hash_algo
= detect_hash_algo(heads
);
283 if (!options
.hash_algo
)
284 die("%sinfo/refs not valid: could not determine hash algorithm; "
285 "is this a git repository?",
286 transport_anonymize_url(url
.buf
));
291 while (i
< heads
->len
) {
297 if (data
[i
] == '\n') {
298 if (mid
- start
!= options
.hash_algo
->hexsz
)
299 die(_("%sinfo/refs not valid: is this a git repository?"),
300 transport_anonymize_url(url
.buf
));
303 ref
= alloc_ref(ref_name
);
304 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
308 last_ref
->next
= ref
;
315 ref
= alloc_ref("HEAD");
316 if (!http_fetch_ref(url
.buf
, ref
) &&
317 !resolve_remote_symref(ref
, refs
)) {
327 static void free_discovery(struct discovery
*d
)
330 if (d
== last_discovery
)
331 last_discovery
= NULL
;
332 free(d
->shallow
.oid
);
340 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
346 * We only show text/plain parts, as other types are likely
347 * to be ugly to look at on the user's terminal.
349 if (strcmp(type
->buf
, "text/plain"))
352 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
360 eol
= strchrnul(p
, '\n');
361 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
367 static int get_protocol_http_header(enum protocol_version version
,
368 struct strbuf
*header
)
371 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
380 static void check_smart_http(struct discovery
*d
, const char *service
,
384 struct packet_reader reader
;
387 * If we don't see x-$service-advertisement, then it's not smart-http.
388 * But once we do, we commit to it and assume any other protocol
389 * violations are hard errors.
391 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
392 !skip_prefix(p
, service
, &p
) ||
393 strcmp(p
, "-advertisement"))
396 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
397 PACKET_READ_CHOMP_NEWLINE
|
398 PACKET_READ_DIE_ON_ERR_PACKET
);
399 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
400 die(_("invalid server response; expected service, got flush packet"));
402 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
404 * The header can include additional metadata lines, up
405 * until a packet flush marker. Ignore these now, but
406 * in the future we might start to scan them.
409 packet_reader_read(&reader
);
410 if (reader
.pktlen
<= 0) {
416 * v0 smart http; callers expect us to soak up the
417 * service and header packets
419 d
->buf
= reader
.src_buffer
;
420 d
->len
= reader
.src_len
;
423 } else if (!strcmp(reader
.line
, "version 2")) {
425 * v2 smart http; do not consume version packet, which will
426 * be handled elsewhere.
431 die(_("invalid server response; got '%s'"), reader
.line
);
435 static struct discovery
*discover_refs(const char *service
, int for_push
)
437 struct strbuf type
= STRBUF_INIT
;
438 struct strbuf charset
= STRBUF_INIT
;
439 struct strbuf buffer
= STRBUF_INIT
;
440 struct strbuf refs_url
= STRBUF_INIT
;
441 struct strbuf effective_url
= STRBUF_INIT
;
442 struct strbuf protocol_header
= STRBUF_INIT
;
443 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
444 struct discovery
*last
= last_discovery
;
445 int http_ret
, maybe_smart
= 0;
446 struct http_get_options http_options
;
447 enum protocol_version version
= get_protocol_version_config();
449 if (last
&& !strcmp(service
, last
->service
))
451 free_discovery(last
);
453 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
454 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
455 git_env_bool("GIT_SMART_HTTP", 1)) {
457 if (!strchr(url
.buf
, '?'))
458 strbuf_addch(&refs_url
, '?');
460 strbuf_addch(&refs_url
, '&');
461 strbuf_addf(&refs_url
, "service=%s", service
);
465 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
466 * to perform a push, then fallback to v0 since the client doesn't know
467 * how to push yet using v2.
469 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
470 version
= protocol_v0
;
472 /* Add the extra Git-Protocol header */
473 if (get_protocol_http_header(version
, &protocol_header
))
474 string_list_append(&extra_headers
, protocol_header
.buf
);
476 memset(&http_options
, 0, sizeof(http_options
));
477 http_options
.content_type
= &type
;
478 http_options
.charset
= &charset
;
479 http_options
.effective_url
= &effective_url
;
480 http_options
.base_url
= &url
;
481 http_options
.extra_headers
= &extra_headers
;
482 http_options
.initial_request
= 1;
483 http_options
.no_cache
= 1;
485 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
489 case HTTP_MISSING_TARGET
:
490 show_http_message(&type
, &charset
, &buffer
);
491 die(_("repository '%s' not found"),
492 transport_anonymize_url(url
.buf
));
494 show_http_message(&type
, &charset
, &buffer
);
495 die(_("Authentication failed for '%s'"),
496 transport_anonymize_url(url
.buf
));
498 show_http_message(&type
, &charset
, &buffer
);
499 die(_("unable to access '%s': %s"),
500 transport_anonymize_url(url
.buf
), curl_errorstr
);
503 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
504 char *u
= transport_anonymize_url(url
.buf
);
505 warning(_("redirecting to %s"), u
);
509 last
= xcalloc(1, sizeof(*last_discovery
));
510 last
->service
= xstrdup(service
);
511 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
512 last
->buf
= last
->buf_alloc
;
515 check_smart_http(last
, service
, &type
);
518 last
->refs
= parse_git_refs(last
, for_push
);
520 last
->refs
= parse_info_refs(last
);
522 strbuf_release(&refs_url
);
523 strbuf_release(&type
);
524 strbuf_release(&charset
);
525 strbuf_release(&effective_url
);
526 strbuf_release(&buffer
);
527 strbuf_release(&protocol_header
);
528 string_list_clear(&extra_headers
, 0);
529 last_discovery
= last
;
533 static struct ref
*get_refs(int for_push
)
535 struct discovery
*heads
;
538 heads
= discover_refs("git-receive-pack", for_push
);
540 heads
= discover_refs("git-upload-pack", for_push
);
545 static void output_refs(struct ref
*refs
)
548 if (options
.object_format
&& options
.hash_algo
) {
549 printf(":object-format %s\n", options
.hash_algo
->name
);
551 for (posn
= refs
; posn
; posn
= posn
->next
) {
553 printf("@%s %s\n", posn
->symref
, posn
->name
);
555 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
564 const char *service_name
;
566 char *hdr_content_type
;
568 char *protocol_header
;
576 unsigned gzip_request
: 1;
577 unsigned initial_buffer
: 1;
580 * Whenever a pkt-line is read into buf, append the 4 characters
581 * denoting its length before appending the payload.
583 unsigned write_line_lengths
: 1;
586 * Used by rpc_out; initialize to 0. This is true if a flush has been
587 * read, but the corresponding line length (if write_line_lengths is
588 * true) and EOF have not been sent to libcurl. Since each flush marks
589 * the end of a request, each flush must be completely sent before any
590 * further reading occurs.
592 unsigned flush_read_but_not_sent
: 1;
596 * Appends the result of reading from rpc->out to the string represented by
597 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
598 * enough space, 0 otherwise.
600 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
601 * hexadecimal string before appending the result described above.
603 * Writes the total number of bytes appended into appended.
605 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
607 enum packet_read_status
*status
) {
612 if (rpc
->write_line_lengths
) {
613 left
= rpc
->alloc
- rpc
->len
- 4;
614 buf
= rpc
->buf
+ rpc
->len
+ 4;
616 left
= rpc
->alloc
- rpc
->len
;
617 buf
= rpc
->buf
+ rpc
->len
;
620 if (left
< LARGE_PACKET_MAX
)
623 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
624 left
, &pktlen_raw
, options
);
625 if (*status
!= PACKET_READ_EOF
) {
626 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
627 rpc
->len
+= *appended
;
630 if (rpc
->write_line_lengths
) {
632 case PACKET_READ_EOF
:
633 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
634 die(_("shouldn't have EOF when not gentle on EOF"));
636 case PACKET_READ_NORMAL
:
637 set_packet_header(buf
- 4, *appended
);
639 case PACKET_READ_DELIM
:
640 memcpy(buf
- 4, "0001", 4);
642 case PACKET_READ_FLUSH
:
643 memcpy(buf
- 4, "0000", 4);
645 case PACKET_READ_RESPONSE_END
:
646 die(_("remote server sent stateless separator"));
653 static size_t rpc_out(void *ptr
, size_t eltsize
,
654 size_t nmemb
, void *buffer_
)
656 size_t max
= eltsize
* nmemb
;
657 struct rpc_state
*rpc
= buffer_
;
658 size_t avail
= rpc
->len
- rpc
->pos
;
659 enum packet_read_status status
;
662 rpc
->initial_buffer
= 0;
665 if (!rpc
->flush_read_but_not_sent
) {
666 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
667 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
668 if (status
== PACKET_READ_FLUSH
)
669 rpc
->flush_read_but_not_sent
= 1;
672 * If flush_read_but_not_sent is true, we have already read one
673 * full request but have not fully sent it + EOF, which is why
674 * we need to refrain from reading.
677 if (rpc
->flush_read_but_not_sent
) {
680 * The line length either does not need to be sent at
681 * all or has already been completely sent. Now we can
682 * return 0, indicating EOF, meaning that the flush has
685 rpc
->flush_read_but_not_sent
= 0;
689 * If avail is non-zero, the line length for the flush still
690 * hasn't been fully sent. Proceed with sending the line
697 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
702 #ifndef NO_CURL_IOCTL
703 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
705 struct rpc_state
*rpc
= clientp
;
711 case CURLIOCMD_RESTARTREAD
:
712 if (rpc
->initial_buffer
) {
716 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
717 return CURLIOE_FAILRESTART
;
720 return CURLIOE_UNKNOWNCMD
;
725 struct check_pktline_state
{
731 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
734 if (!state
->remaining
) {
735 int digits_remaining
= 4 - state
->len_filled
;
736 if (digits_remaining
> size
)
737 digits_remaining
= size
;
738 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
739 state
->len_filled
+= digits_remaining
;
740 ptr
+= digits_remaining
;
741 size
-= digits_remaining
;
743 if (state
->len_filled
== 4) {
744 state
->remaining
= packet_length(state
->len_buf
);
745 if (state
->remaining
< 0) {
746 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
747 } else if (state
->remaining
== 2) {
748 die(_("remote-curl: unexpected response end packet"));
749 } else if (state
->remaining
< 4) {
750 state
->remaining
= 0;
752 state
->remaining
-= 4;
754 state
->len_filled
= 0;
758 if (state
->remaining
) {
759 int remaining
= state
->remaining
;
760 if (remaining
> size
)
764 state
->remaining
-= remaining
;
770 struct rpc_state
*rpc
;
771 struct active_request_slot
*slot
;
773 struct check_pktline_state pktline_state
;
777 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
780 static size_t rpc_in(char *ptr
, size_t eltsize
,
781 size_t nmemb
, void *buffer_
)
783 size_t size
= eltsize
* nmemb
;
784 struct rpc_in_data
*data
= buffer_
;
787 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
788 &response_code
) != CURLE_OK
)
790 if (response_code
>= 300)
793 data
->rpc
->any_written
= 1;
794 if (data
->check_pktline
)
795 check_pktline(&data
->pktline_state
, ptr
, size
);
796 write_or_die(data
->rpc
->in
, ptr
, size
);
800 static int run_slot(struct active_request_slot
*slot
,
801 struct slot_results
*results
)
804 struct slot_results results_buf
;
807 results
= &results_buf
;
809 err
= run_one_slot(slot
, results
);
811 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
812 struct strbuf msg
= STRBUF_INIT
;
813 if (results
->http_code
&& results
->http_code
!= 200)
814 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
815 if (results
->curl_result
!= CURLE_OK
) {
817 strbuf_addch(&msg
, ' ');
818 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
819 if (curl_errorstr
[0]) {
820 strbuf_addch(&msg
, ' ');
821 strbuf_addstr(&msg
, curl_errorstr
);
824 error(_("RPC failed; %s"), msg
.buf
);
825 strbuf_release(&msg
);
831 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
833 struct active_request_slot
*slot
;
834 struct curl_slist
*headers
= http_copy_default_headers();
835 struct strbuf buf
= STRBUF_INIT
;
838 slot
= get_active_slot();
840 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
841 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
843 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
844 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
845 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
846 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
847 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
848 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
849 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
850 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
851 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
853 err
= run_slot(slot
, results
);
855 curl_slist_free_all(headers
);
856 strbuf_release(&buf
);
860 static curl_off_t
xcurl_off_t(size_t len
)
862 uintmax_t size
= len
;
863 if (size
> maximum_signed_value_of_type(curl_off_t
))
864 die(_("cannot handle pushes this big"));
865 return (curl_off_t
)size
;
869 * If flush_received is true, do not attempt to read any more; just use what's
872 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
874 struct active_request_slot
*slot
;
875 struct curl_slist
*headers
= http_copy_default_headers();
876 int use_gzip
= rpc
->gzip_request
;
877 char *gzip_body
= NULL
;
878 size_t gzip_size
= 0;
879 int err
, large_request
= 0;
880 int needs_100_continue
= 0;
881 struct rpc_in_data rpc_in_data
;
883 /* Try to load the entire request, if we can fit it into the
884 * allocated buffer space we can use HTTP/1.0 and avoid the
885 * chunked encoding mess.
887 if (!flush_received
) {
890 enum packet_read_status status
;
892 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
897 if (status
== PACKET_READ_FLUSH
)
903 struct slot_results results
;
906 err
= probe_rpc(rpc
, &results
);
907 if (err
== HTTP_REAUTH
)
908 credential_fill(&http_auth
);
909 } while (err
== HTTP_REAUTH
);
913 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
914 needs_100_continue
= 1;
917 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
918 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
919 headers
= curl_slist_append(headers
, needs_100_continue
?
920 "Expect: 100-continue" : "Expect:");
922 /* Add the extra Git-Protocol header */
923 if (rpc
->protocol_header
)
924 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
927 slot
= get_active_slot();
929 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
930 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
931 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
932 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
935 /* The request body is large and the size cannot be predicted.
936 * We must use chunked encoding to send it.
938 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
939 rpc
->initial_buffer
= 1;
940 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
941 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
942 #ifndef NO_CURL_IOCTL
943 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
944 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
946 if (options
.verbosity
> 1) {
947 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
951 } else if (gzip_body
) {
953 * If we are looping to retry authentication, then the previous
954 * run will have set up the headers and gzip buffer already,
955 * and we just need to send it.
957 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
958 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
960 } else if (use_gzip
&& 1024 < rpc
->len
) {
961 /* The client backend isn't giving us compressed data so
962 * we can try to deflate it ourselves, this may save on
968 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
969 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
970 gzip_body
= xmalloc(gzip_size
);
972 stream
.next_in
= (unsigned char *)rpc
->buf
;
973 stream
.avail_in
= rpc
->len
;
974 stream
.next_out
= (unsigned char *)gzip_body
;
975 stream
.avail_out
= gzip_size
;
977 ret
= git_deflate(&stream
, Z_FINISH
);
978 if (ret
!= Z_STREAM_END
)
979 die(_("cannot deflate request; zlib deflate error %d"), ret
);
981 ret
= git_deflate_end_gently(&stream
);
983 die(_("cannot deflate request; zlib end error %d"), ret
);
985 gzip_size
= stream
.total_out
;
987 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
988 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
989 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
991 if (options
.verbosity
> 1) {
992 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
994 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
998 /* We know the complete request size in advance, use the
999 * more normal Content-Length approach.
1001 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1002 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1003 if (options
.verbosity
> 1) {
1004 fprintf(stderr
, "POST %s (%lu bytes)\n",
1005 rpc
->service_name
, (unsigned long)rpc
->len
);
1010 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1011 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1012 rpc_in_data
.rpc
= rpc
;
1013 rpc_in_data
.slot
= slot
;
1014 rpc_in_data
.check_pktline
= stateless_connect
;
1015 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1016 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
1017 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1020 rpc
->any_written
= 0;
1021 err
= run_slot(slot
, NULL
);
1022 if (err
== HTTP_REAUTH
&& !large_request
) {
1023 credential_fill(&http_auth
);
1029 if (!rpc
->any_written
)
1032 if (rpc_in_data
.pktline_state
.len_filled
)
1033 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1034 if (rpc_in_data
.pktline_state
.remaining
)
1035 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1037 if (stateless_connect
)
1038 packet_response_end(rpc
->in
);
1040 curl_slist_free_all(headers
);
1045 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1046 const char **client_argv
, const struct strbuf
*preamble
,
1047 struct strbuf
*rpc_result
)
1049 const char *svc
= rpc
->service_name
;
1050 struct strbuf buf
= STRBUF_INIT
;
1051 struct child_process client
= CHILD_PROCESS_INIT
;
1057 client
.argv
= client_argv
;
1058 if (start_command(&client
))
1060 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1062 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1064 rpc
->alloc
= http_post_buffer
;
1065 rpc
->buf
= xmalloc(rpc
->alloc
);
1066 rpc
->in
= client
.in
;
1067 rpc
->out
= client
.out
;
1069 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1070 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1072 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1073 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1075 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1076 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1078 if (get_protocol_http_header(heads
->version
, &buf
))
1079 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1081 rpc
->protocol_header
= NULL
;
1084 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
1089 err
|= post_rpc(rpc
, 0, 0);
1095 strbuf_read(rpc_result
, client
.out
, 0);
1099 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1106 err
|= finish_command(&client
);
1107 free(rpc
->service_url
);
1108 free(rpc
->hdr_content_type
);
1109 free(rpc
->hdr_accept
);
1110 free(rpc
->protocol_header
);
1112 strbuf_release(&buf
);
1116 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1118 struct walker
*walker
;
1122 ALLOC_ARRAY(targets
, nr_heads
);
1123 if (options
.depth
|| options
.deepen_since
)
1124 die(_("dumb http transport does not support shallow capabilities"));
1125 for (i
= 0; i
< nr_heads
; i
++)
1126 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1128 walker
= get_http_walker(url
.buf
);
1129 walker
->get_verbosely
= options
.verbosity
>= 3;
1130 walker
->get_progress
= options
.progress
;
1131 walker
->get_recover
= 0;
1132 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1133 walker_free(walker
);
1135 for (i
= 0; i
< nr_heads
; i
++)
1139 return ret
? error(_("fetch failed.")) : 0;
1142 static int fetch_git(struct discovery
*heads
,
1143 int nr_heads
, struct ref
**to_fetch
)
1145 struct rpc_state rpc
;
1146 struct strbuf preamble
= STRBUF_INIT
;
1148 struct argv_array args
= ARGV_ARRAY_INIT
;
1149 struct strbuf rpc_result
= STRBUF_INIT
;
1151 argv_array_pushl(&args
, "fetch-pack", "--stateless-rpc",
1152 "--stdin", "--lock-pack", NULL
);
1153 if (options
.followtags
)
1154 argv_array_push(&args
, "--include-tag");
1156 argv_array_push(&args
, "--thin");
1157 if (options
.verbosity
>= 3)
1158 argv_array_pushl(&args
, "-v", "-v", NULL
);
1159 if (options
.check_self_contained_and_connected
)
1160 argv_array_push(&args
, "--check-self-contained-and-connected");
1161 if (options
.cloning
)
1162 argv_array_push(&args
, "--cloning");
1163 if (options
.update_shallow
)
1164 argv_array_push(&args
, "--update-shallow");
1165 if (!options
.progress
)
1166 argv_array_push(&args
, "--no-progress");
1168 argv_array_pushf(&args
, "--depth=%lu", options
.depth
);
1169 if (options
.deepen_since
)
1170 argv_array_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1171 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1172 argv_array_pushf(&args
, "--shallow-exclude=%s",
1173 options
.deepen_not
.items
[i
].string
);
1174 if (options
.deepen_relative
&& options
.depth
)
1175 argv_array_push(&args
, "--deepen-relative");
1176 if (options
.from_promisor
)
1177 argv_array_push(&args
, "--from-promisor");
1178 if (options
.no_dependents
)
1179 argv_array_push(&args
, "--no-dependents");
1181 argv_array_pushf(&args
, "--filter=%s", options
.filter
);
1182 argv_array_push(&args
, url
.buf
);
1184 for (i
= 0; i
< nr_heads
; i
++) {
1185 struct ref
*ref
= to_fetch
[i
];
1187 die(_("cannot fetch by sha1 over smart http"));
1188 packet_buf_write(&preamble
, "%s %s\n",
1189 oid_to_hex(&ref
->old_oid
), ref
->name
);
1191 packet_buf_flush(&preamble
);
1193 memset(&rpc
, 0, sizeof(rpc
));
1194 rpc
.service_name
= "git-upload-pack",
1195 rpc
.gzip_request
= 1;
1197 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1199 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1200 strbuf_release(&rpc_result
);
1201 strbuf_release(&preamble
);
1202 argv_array_clear(&args
);
1206 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1208 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1210 return fetch_git(d
, nr_heads
, to_fetch
);
1212 return fetch_dumb(nr_heads
, to_fetch
);
1215 static void parse_fetch(struct strbuf
*buf
)
1217 struct ref
**to_fetch
= NULL
;
1218 struct ref
*list_head
= NULL
;
1219 struct ref
**list
= &list_head
;
1220 int alloc_heads
= 0, nr_heads
= 0;
1224 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1227 struct object_id old_oid
;
1230 if (parse_oid_hex(p
, &old_oid
, &q
))
1231 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1237 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1239 ref
= alloc_ref(name
);
1240 oidcpy(&ref
->old_oid
, &old_oid
);
1245 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1246 to_fetch
[nr_heads
++] = ref
;
1249 die(_("http transport does not support %s"), buf
->buf
);
1252 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1258 if (fetch(nr_heads
, to_fetch
))
1259 exit(128); /* error already reported */
1260 free_refs(list_head
);
1268 static int push_dav(int nr_spec
, const char **specs
)
1270 struct child_process child
= CHILD_PROCESS_INIT
;
1274 argv_array_push(&child
.args
, "http-push");
1275 argv_array_push(&child
.args
, "--helper-status");
1276 if (options
.dry_run
)
1277 argv_array_push(&child
.args
, "--dry-run");
1278 if (options
.verbosity
> 1)
1279 argv_array_push(&child
.args
, "--verbose");
1280 argv_array_push(&child
.args
, url
.buf
);
1281 for (i
= 0; i
< nr_spec
; i
++)
1282 argv_array_push(&child
.args
, specs
[i
]);
1284 if (run_command(&child
))
1285 die(_("git-http-push failed"));
1289 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1291 struct rpc_state rpc
;
1293 struct argv_array args
;
1294 struct string_list_item
*cas_option
;
1295 struct strbuf preamble
= STRBUF_INIT
;
1296 struct strbuf rpc_result
= STRBUF_INIT
;
1298 argv_array_init(&args
);
1299 argv_array_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1303 argv_array_push(&args
, "--thin");
1304 if (options
.dry_run
)
1305 argv_array_push(&args
, "--dry-run");
1306 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1307 argv_array_push(&args
, "--signed=yes");
1308 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1309 argv_array_push(&args
, "--signed=if-asked");
1311 argv_array_push(&args
, "--atomic");
1312 if (options
.verbosity
== 0)
1313 argv_array_push(&args
, "--quiet");
1314 else if (options
.verbosity
> 1)
1315 argv_array_push(&args
, "--verbose");
1316 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1317 argv_array_pushf(&args
, "--push-option=%s",
1318 options
.push_options
.items
[i
].string
);
1319 argv_array_push(&args
, options
.progress
? "--progress" : "--no-progress");
1320 for_each_string_list_item(cas_option
, &cas_options
)
1321 argv_array_push(&args
, cas_option
->string
);
1322 argv_array_push(&args
, url
.buf
);
1324 argv_array_push(&args
, "--stdin");
1325 for (i
= 0; i
< nr_spec
; i
++)
1326 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1327 packet_buf_flush(&preamble
);
1329 memset(&rpc
, 0, sizeof(rpc
));
1330 rpc
.service_name
= "git-receive-pack",
1332 err
= rpc_service(&rpc
, heads
, args
.argv
, &preamble
, &rpc_result
);
1334 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1335 strbuf_release(&rpc_result
);
1336 strbuf_release(&preamble
);
1337 argv_array_clear(&args
);
1341 static int push(int nr_spec
, const char **specs
)
1343 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1346 if (heads
->proto_git
)
1347 ret
= push_git(heads
, nr_spec
, specs
);
1349 ret
= push_dav(nr_spec
, specs
);
1350 free_discovery(heads
);
1354 static void parse_push(struct strbuf
*buf
)
1356 struct argv_array specs
= ARGV_ARRAY_INIT
;
1361 if (skip_prefix(buf
->buf
, "push ", &arg
))
1362 argv_array_push(&specs
, arg
);
1364 die(_("http transport does not support %s"), buf
->buf
);
1367 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1373 ret
= push(specs
.argc
, specs
.argv
);
1378 exit(128); /* error already reported */
1381 argv_array_clear(&specs
);
1384 static int stateless_connect(const char *service_name
)
1386 struct discovery
*discover
;
1387 struct rpc_state rpc
;
1388 struct strbuf buf
= STRBUF_INIT
;
1391 * Run the info/refs request and see if the server supports protocol
1392 * v2. If and only if the server supports v2 can we successfully
1393 * establish a stateless connection, otherwise we need to tell the
1394 * client to fallback to using other transport helper functions to
1395 * complete their request.
1397 discover
= discover_refs(service_name
, 0);
1398 if (discover
->version
!= protocol_v2
) {
1399 printf("fallback\n");
1403 /* Stateless Connection established */
1408 rpc
.service_name
= service_name
;
1409 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1410 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1411 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1412 if (get_protocol_http_header(discover
->version
, &buf
)) {
1413 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1415 rpc
.protocol_header
= NULL
;
1416 strbuf_release(&buf
);
1418 rpc
.buf
= xmalloc(http_post_buffer
);
1419 rpc
.alloc
= http_post_buffer
;
1424 rpc
.any_written
= 0;
1425 rpc
.gzip_request
= 1;
1426 rpc
.initial_buffer
= 0;
1427 rpc
.write_line_lengths
= 1;
1428 rpc
.flush_read_but_not_sent
= 0;
1431 * Dump the capability listing that we got from the server earlier
1432 * during the info/refs request.
1434 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1436 /* Until we see EOF keep sending POSTs */
1439 enum packet_read_status status
;
1441 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1443 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1444 if (status
== PACKET_READ_EOF
)
1446 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1447 /* We would have an err here */
1449 /* Reset the buffer for next request */
1453 free(rpc
.service_url
);
1454 free(rpc
.hdr_content_type
);
1455 free(rpc
.hdr_accept
);
1456 free(rpc
.protocol_header
);
1458 strbuf_release(&buf
);
1463 int cmd_main(int argc
, const char **argv
)
1465 struct strbuf buf
= STRBUF_INIT
;
1468 setup_git_directory_gently(&nongit
);
1470 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1474 options
.verbosity
= 1;
1475 options
.progress
= !!isatty(2);
1477 string_list_init(&options
.deepen_not
, 1);
1478 string_list_init(&options
.push_options
, 1);
1481 * Just report "remote-curl" here (folding all the various aliases
1482 * ("git-remote-http", "git-remote-https", and etc.) here since they
1483 * are all just copies of the same actual executable.
1485 trace2_cmd_name("remote-curl");
1487 remote
= remote_get(argv
[1]);
1490 end_url_with_slash(&url
, argv
[2]);
1492 end_url_with_slash(&url
, remote
->url
[0]);
1495 http_init(remote
, url
.buf
, 0);
1500 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1502 error(_("remote-curl: error reading command stream from git"));
1507 if (starts_with(buf
.buf
, "fetch ")) {
1509 die(_("remote-curl: fetch attempted without a local repo"));
1512 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1513 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1514 output_refs(get_refs(for_push
));
1516 } else if (starts_with(buf
.buf
, "push ")) {
1519 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1520 char *value
= strchr(arg
, ' ');
1528 result
= set_option(arg
, value
);
1531 else if (result
< 0)
1532 printf("error invalid value\n");
1534 printf("unsupported\n");
1537 } else if (!strcmp(buf
.buf
, "capabilities")) {
1538 printf("stateless-connect\n");
1542 printf("check-connectivity\n");
1543 printf("object-format\n");
1546 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1547 if (!stateless_connect(arg
))
1550 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);