9 #include "run-command.h"
11 #include "string-list.h"
14 #include "credential.h"
15 #include "oid-array.h"
16 #include "send-pack.h"
19 #include "transport.h"
21 static struct remote
*remote
;
22 /* always ends with a trailing slash */
23 static struct strbuf url
= STRBUF_INIT
;
29 struct string_list deepen_not
;
30 struct string_list push_options
;
32 unsigned progress
: 1,
33 check_self_contained_and_connected
: 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
43 /* see documentation of corresponding flag in fetch-pack.h */
48 force_if_includes
: 1;
49 const struct git_hash_algo
*hash_algo
;
51 static struct options options
;
52 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
54 static int set_option(const char *name
, const char *value
)
56 if (!strcmp(name
, "verbosity")) {
58 int v
= strtol(value
, &end
, 10);
59 if (value
== end
|| *end
)
61 options
.verbosity
= v
;
64 else if (!strcmp(name
, "progress")) {
65 if (!strcmp(value
, "true"))
67 else if (!strcmp(value
, "false"))
73 else if (!strcmp(name
, "depth")) {
75 unsigned long v
= strtoul(value
, &end
, 10);
76 if (value
== end
|| *end
)
81 else if (!strcmp(name
, "deepen-since")) {
82 options
.deepen_since
= xstrdup(value
);
85 else if (!strcmp(name
, "deepen-not")) {
86 string_list_append(&options
.deepen_not
, value
);
89 else if (!strcmp(name
, "deepen-relative")) {
90 if (!strcmp(value
, "true"))
91 options
.deepen_relative
= 1;
92 else if (!strcmp(value
, "false"))
93 options
.deepen_relative
= 0;
98 else if (!strcmp(name
, "followtags")) {
99 if (!strcmp(value
, "true"))
100 options
.followtags
= 1;
101 else if (!strcmp(value
, "false"))
102 options
.followtags
= 0;
107 else if (!strcmp(name
, "dry-run")) {
108 if (!strcmp(value
, "true"))
110 else if (!strcmp(value
, "false"))
116 else if (!strcmp(name
, "check-connectivity")) {
117 if (!strcmp(value
, "true"))
118 options
.check_self_contained_and_connected
= 1;
119 else if (!strcmp(value
, "false"))
120 options
.check_self_contained_and_connected
= 0;
125 else if (!strcmp(name
, "cas")) {
126 struct strbuf val
= STRBUF_INIT
;
127 strbuf_addstr(&val
, "--force-with-lease=");
129 strbuf_addstr(&val
, value
);
130 else if (unquote_c_style(&val
, value
, NULL
))
132 string_list_append(&cas_options
, val
.buf
);
133 strbuf_release(&val
);
135 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
136 if (!strcmp(value
, "true"))
137 options
.force_if_includes
= 1;
138 else if (!strcmp(value
, "false"))
139 options
.force_if_includes
= 0;
143 } else if (!strcmp(name
, "cloning")) {
144 if (!strcmp(value
, "true"))
146 else if (!strcmp(value
, "false"))
151 } else if (!strcmp(name
, "update-shallow")) {
152 if (!strcmp(value
, "true"))
153 options
.update_shallow
= 1;
154 else if (!strcmp(value
, "false"))
155 options
.update_shallow
= 0;
159 } else if (!strcmp(name
, "pushcert")) {
160 if (!strcmp(value
, "true"))
161 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
162 else if (!strcmp(value
, "false"))
163 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
164 else if (!strcmp(value
, "if-asked"))
165 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
169 } else if (!strcmp(name
, "atomic")) {
170 if (!strcmp(value
, "true"))
172 else if (!strcmp(value
, "false"))
177 } else if (!strcmp(name
, "push-option")) {
179 string_list_append(&options
.push_options
, value
);
181 struct strbuf unquoted
= STRBUF_INIT
;
182 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
183 die(_("invalid quoting in push-option value: '%s'"), value
);
184 string_list_append_nodup(&options
.push_options
,
185 strbuf_detach(&unquoted
, NULL
));
189 #if LIBCURL_VERSION_NUM >= 0x070a08
190 } else if (!strcmp(name
, "family")) {
191 if (!strcmp(value
, "ipv4"))
192 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
193 else if (!strcmp(value
, "ipv6"))
194 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
195 else if (!strcmp(value
, "all"))
196 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
200 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
201 } else if (!strcmp(name
, "from-promisor")) {
202 options
.from_promisor
= 1;
204 } else if (!strcmp(name
, "filter")) {
205 options
.filter
= xstrdup(value
);
207 } else if (!strcmp(name
, "object-format")) {
209 options
.object_format
= 1;
210 if (strcmp(value
, "true")) {
211 algo
= hash_algo_by_name(value
);
212 if (algo
== GIT_HASH_UNKNOWN
)
213 die("unknown object format '%s'", value
);
214 options
.hash_algo
= &hash_algos
[algo
];
218 return 1 /* unsupported */;
228 struct oid_array shallow
;
229 enum protocol_version version
;
230 unsigned proto_git
: 1;
232 static struct discovery
*last_discovery
;
234 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
236 struct ref
*list
= NULL
;
237 struct packet_reader reader
;
239 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
240 PACKET_READ_CHOMP_NEWLINE
|
241 PACKET_READ_GENTLE_ON_EOF
|
242 PACKET_READ_DIE_ON_ERR_PACKET
);
244 heads
->version
= discover_version(&reader
);
245 switch (heads
->version
) {
248 * Do nothing. This isn't a list of refs but rather a
249 * capability advertisement. Client would have run
250 * 'stateless-connect' so we'll dump this capability listing
251 * and let them request the refs themselves.
256 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
257 NULL
, &heads
->shallow
);
258 options
.hash_algo
= reader
.hash_algo
;
260 case protocol_unknown_version
:
261 BUG("unknown protocol version");
267 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
269 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
272 return the_hash_algo
;
274 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
275 if (algo
== GIT_HASH_UNKNOWN
)
277 return &hash_algos
[algo
];
280 static struct ref
*parse_info_refs(struct discovery
*heads
)
282 char *data
, *start
, *mid
;
286 struct ref
*refs
= NULL
;
287 struct ref
*ref
= NULL
;
288 struct ref
*last_ref
= NULL
;
290 options
.hash_algo
= detect_hash_algo(heads
);
291 if (!options
.hash_algo
)
292 die("%sinfo/refs not valid: could not determine hash algorithm; "
293 "is this a git repository?",
294 transport_anonymize_url(url
.buf
));
299 while (i
< heads
->len
) {
305 if (data
[i
] == '\n') {
306 if (mid
- start
!= options
.hash_algo
->hexsz
)
307 die(_("%sinfo/refs not valid: is this a git repository?"),
308 transport_anonymize_url(url
.buf
));
311 ref
= alloc_ref(ref_name
);
312 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
316 last_ref
->next
= ref
;
323 ref
= alloc_ref("HEAD");
324 if (!http_fetch_ref(url
.buf
, ref
) &&
325 !resolve_remote_symref(ref
, refs
)) {
335 static void free_discovery(struct discovery
*d
)
338 if (d
== last_discovery
)
339 last_discovery
= NULL
;
340 free(d
->shallow
.oid
);
348 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
354 * We only show text/plain parts, as other types are likely
355 * to be ugly to look at on the user's terminal.
357 if (strcmp(type
->buf
, "text/plain"))
360 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
368 eol
= strchrnul(p
, '\n');
369 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
375 static int get_protocol_http_header(enum protocol_version version
,
376 struct strbuf
*header
)
379 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
388 static void check_smart_http(struct discovery
*d
, const char *service
,
392 struct packet_reader reader
;
395 * If we don't see x-$service-advertisement, then it's not smart-http.
396 * But once we do, we commit to it and assume any other protocol
397 * violations are hard errors.
399 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
400 !skip_prefix(p
, service
, &p
) ||
401 strcmp(p
, "-advertisement"))
404 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
405 PACKET_READ_CHOMP_NEWLINE
|
406 PACKET_READ_DIE_ON_ERR_PACKET
);
407 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
408 die(_("invalid server response; expected service, got flush packet"));
410 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
412 * The header can include additional metadata lines, up
413 * until a packet flush marker. Ignore these now, but
414 * in the future we might start to scan them.
417 packet_reader_read(&reader
);
418 if (reader
.pktlen
<= 0) {
424 * v0 smart http; callers expect us to soak up the
425 * service and header packets
427 d
->buf
= reader
.src_buffer
;
428 d
->len
= reader
.src_len
;
431 } else if (!strcmp(reader
.line
, "version 2")) {
433 * v2 smart http; do not consume version packet, which will
434 * be handled elsewhere.
439 die(_("invalid server response; got '%s'"), reader
.line
);
443 static struct discovery
*discover_refs(const char *service
, int for_push
)
445 struct strbuf type
= STRBUF_INIT
;
446 struct strbuf charset
= STRBUF_INIT
;
447 struct strbuf buffer
= STRBUF_INIT
;
448 struct strbuf refs_url
= STRBUF_INIT
;
449 struct strbuf effective_url
= STRBUF_INIT
;
450 struct strbuf protocol_header
= STRBUF_INIT
;
451 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
452 struct discovery
*last
= last_discovery
;
453 int http_ret
, maybe_smart
= 0;
454 struct http_get_options http_options
;
455 enum protocol_version version
= get_protocol_version_config();
457 if (last
&& !strcmp(service
, last
->service
))
459 free_discovery(last
);
461 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
462 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
463 git_env_bool("GIT_SMART_HTTP", 1)) {
465 if (!strchr(url
.buf
, '?'))
466 strbuf_addch(&refs_url
, '?');
468 strbuf_addch(&refs_url
, '&');
469 strbuf_addf(&refs_url
, "service=%s", service
);
473 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
474 * to perform a push, then fallback to v0 since the client doesn't know
475 * how to push yet using v2.
477 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
478 version
= protocol_v0
;
480 /* Add the extra Git-Protocol header */
481 if (get_protocol_http_header(version
, &protocol_header
))
482 string_list_append(&extra_headers
, protocol_header
.buf
);
484 memset(&http_options
, 0, sizeof(http_options
));
485 http_options
.content_type
= &type
;
486 http_options
.charset
= &charset
;
487 http_options
.effective_url
= &effective_url
;
488 http_options
.base_url
= &url
;
489 http_options
.extra_headers
= &extra_headers
;
490 http_options
.initial_request
= 1;
491 http_options
.no_cache
= 1;
493 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
497 case HTTP_MISSING_TARGET
:
498 show_http_message(&type
, &charset
, &buffer
);
499 die(_("repository '%s' not found"),
500 transport_anonymize_url(url
.buf
));
502 show_http_message(&type
, &charset
, &buffer
);
503 die(_("Authentication failed for '%s'"),
504 transport_anonymize_url(url
.buf
));
506 show_http_message(&type
, &charset
, &buffer
);
507 die(_("unable to access '%s': %s"),
508 transport_anonymize_url(url
.buf
), curl_errorstr
);
511 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
512 char *u
= transport_anonymize_url(url
.buf
);
513 warning(_("redirecting to %s"), u
);
517 last
= xcalloc(1, sizeof(*last_discovery
));
518 last
->service
= xstrdup(service
);
519 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
520 last
->buf
= last
->buf_alloc
;
523 check_smart_http(last
, service
, &type
);
526 last
->refs
= parse_git_refs(last
, for_push
);
528 last
->refs
= parse_info_refs(last
);
530 strbuf_release(&refs_url
);
531 strbuf_release(&type
);
532 strbuf_release(&charset
);
533 strbuf_release(&effective_url
);
534 strbuf_release(&buffer
);
535 strbuf_release(&protocol_header
);
536 string_list_clear(&extra_headers
, 0);
537 last_discovery
= last
;
541 static struct ref
*get_refs(int for_push
)
543 struct discovery
*heads
;
546 heads
= discover_refs("git-receive-pack", for_push
);
548 heads
= discover_refs("git-upload-pack", for_push
);
553 static void output_refs(struct ref
*refs
)
556 if (options
.object_format
&& options
.hash_algo
) {
557 printf(":object-format %s\n", options
.hash_algo
->name
);
559 for (posn
= refs
; posn
; posn
= posn
->next
) {
561 printf("@%s %s\n", posn
->symref
, posn
->name
);
563 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
572 const char *service_name
;
574 char *hdr_content_type
;
576 char *protocol_header
;
584 unsigned gzip_request
: 1;
585 unsigned initial_buffer
: 1;
588 * Whenever a pkt-line is read into buf, append the 4 characters
589 * denoting its length before appending the payload.
591 unsigned write_line_lengths
: 1;
594 * Used by rpc_out; initialize to 0. This is true if a flush has been
595 * read, but the corresponding line length (if write_line_lengths is
596 * true) and EOF have not been sent to libcurl. Since each flush marks
597 * the end of a request, each flush must be completely sent before any
598 * further reading occurs.
600 unsigned flush_read_but_not_sent
: 1;
604 * Appends the result of reading from rpc->out to the string represented by
605 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
606 * enough space, 0 otherwise.
608 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
609 * hexadecimal string before appending the result described above.
611 * Writes the total number of bytes appended into appended.
613 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
615 enum packet_read_status
*status
) {
620 if (rpc
->write_line_lengths
) {
621 left
= rpc
->alloc
- rpc
->len
- 4;
622 buf
= rpc
->buf
+ rpc
->len
+ 4;
624 left
= rpc
->alloc
- rpc
->len
;
625 buf
= rpc
->buf
+ rpc
->len
;
628 if (left
< LARGE_PACKET_MAX
)
631 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
632 left
, &pktlen_raw
, options
);
633 if (*status
!= PACKET_READ_EOF
) {
634 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
635 rpc
->len
+= *appended
;
638 if (rpc
->write_line_lengths
) {
640 case PACKET_READ_EOF
:
641 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
642 die(_("shouldn't have EOF when not gentle on EOF"));
644 case PACKET_READ_NORMAL
:
645 set_packet_header(buf
- 4, *appended
);
647 case PACKET_READ_DELIM
:
648 memcpy(buf
- 4, "0001", 4);
650 case PACKET_READ_FLUSH
:
651 memcpy(buf
- 4, "0000", 4);
653 case PACKET_READ_RESPONSE_END
:
654 die(_("remote server sent stateless separator"));
661 static size_t rpc_out(void *ptr
, size_t eltsize
,
662 size_t nmemb
, void *buffer_
)
664 size_t max
= eltsize
* nmemb
;
665 struct rpc_state
*rpc
= buffer_
;
666 size_t avail
= rpc
->len
- rpc
->pos
;
667 enum packet_read_status status
;
670 rpc
->initial_buffer
= 0;
673 if (!rpc
->flush_read_but_not_sent
) {
674 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
675 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
676 if (status
== PACKET_READ_FLUSH
)
677 rpc
->flush_read_but_not_sent
= 1;
680 * If flush_read_but_not_sent is true, we have already read one
681 * full request but have not fully sent it + EOF, which is why
682 * we need to refrain from reading.
685 if (rpc
->flush_read_but_not_sent
) {
688 * The line length either does not need to be sent at
689 * all or has already been completely sent. Now we can
690 * return 0, indicating EOF, meaning that the flush has
693 rpc
->flush_read_but_not_sent
= 0;
697 * If avail is non-zero, the line length for the flush still
698 * hasn't been fully sent. Proceed with sending the line
705 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
711 static int rpc_seek(void *clientp
, curl_off_t offset
, int origin
)
713 struct rpc_state
*rpc
= clientp
;
715 if (origin
!= SEEK_SET
)
716 BUG("rpc_seek only handles SEEK_SET, not %d", origin
);
718 if (rpc
->initial_buffer
) {
719 if (offset
< 0 || offset
> rpc
->len
) {
720 error("curl seek would be outside of rpc buffer");
721 return CURL_SEEKFUNC_FAIL
;
724 return CURL_SEEKFUNC_OK
;
726 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
727 return CURL_SEEKFUNC_FAIL
;
731 struct check_pktline_state
{
737 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
740 if (!state
->remaining
) {
741 int digits_remaining
= 4 - state
->len_filled
;
742 if (digits_remaining
> size
)
743 digits_remaining
= size
;
744 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
745 state
->len_filled
+= digits_remaining
;
746 ptr
+= digits_remaining
;
747 size
-= digits_remaining
;
749 if (state
->len_filled
== 4) {
750 state
->remaining
= packet_length(state
->len_buf
);
751 if (state
->remaining
< 0) {
752 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
753 } else if (state
->remaining
== 2) {
754 die(_("remote-curl: unexpected response end packet"));
755 } else if (state
->remaining
< 4) {
756 state
->remaining
= 0;
758 state
->remaining
-= 4;
760 state
->len_filled
= 0;
764 if (state
->remaining
) {
765 int remaining
= state
->remaining
;
766 if (remaining
> size
)
770 state
->remaining
-= remaining
;
776 struct rpc_state
*rpc
;
777 struct active_request_slot
*slot
;
779 struct check_pktline_state pktline_state
;
783 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
786 static size_t rpc_in(char *ptr
, size_t eltsize
,
787 size_t nmemb
, void *buffer_
)
789 size_t size
= eltsize
* nmemb
;
790 struct rpc_in_data
*data
= buffer_
;
793 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
794 &response_code
) != CURLE_OK
)
796 if (response_code
>= 300)
799 data
->rpc
->any_written
= 1;
800 if (data
->check_pktline
)
801 check_pktline(&data
->pktline_state
, ptr
, size
);
802 write_or_die(data
->rpc
->in
, ptr
, size
);
806 static int run_slot(struct active_request_slot
*slot
,
807 struct slot_results
*results
)
810 struct slot_results results_buf
;
813 results
= &results_buf
;
815 err
= run_one_slot(slot
, results
);
817 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
818 struct strbuf msg
= STRBUF_INIT
;
819 if (results
->http_code
&& results
->http_code
!= 200)
820 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
821 if (results
->curl_result
!= CURLE_OK
) {
823 strbuf_addch(&msg
, ' ');
824 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
825 if (curl_errorstr
[0]) {
826 strbuf_addch(&msg
, ' ');
827 strbuf_addstr(&msg
, curl_errorstr
);
830 error(_("RPC failed; %s"), msg
.buf
);
831 strbuf_release(&msg
);
837 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
839 struct active_request_slot
*slot
;
840 struct curl_slist
*headers
= http_copy_default_headers();
841 struct strbuf buf
= STRBUF_INIT
;
844 slot
= get_active_slot();
846 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
847 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
849 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
850 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
851 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
852 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
853 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
854 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
855 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
856 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
857 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
859 err
= run_slot(slot
, results
);
861 curl_slist_free_all(headers
);
862 strbuf_release(&buf
);
866 static curl_off_t
xcurl_off_t(size_t len
)
868 uintmax_t size
= len
;
869 if (size
> maximum_signed_value_of_type(curl_off_t
))
870 die(_("cannot handle pushes this big"));
871 return (curl_off_t
)size
;
875 * If flush_received is true, do not attempt to read any more; just use what's
878 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
880 struct active_request_slot
*slot
;
881 struct curl_slist
*headers
= http_copy_default_headers();
882 int use_gzip
= rpc
->gzip_request
;
883 char *gzip_body
= NULL
;
884 size_t gzip_size
= 0;
885 int err
, large_request
= 0;
886 int needs_100_continue
= 0;
887 struct rpc_in_data rpc_in_data
;
889 /* Try to load the entire request, if we can fit it into the
890 * allocated buffer space we can use HTTP/1.0 and avoid the
891 * chunked encoding mess.
893 if (!flush_received
) {
896 enum packet_read_status status
;
898 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
903 if (status
== PACKET_READ_FLUSH
)
909 struct slot_results results
;
912 err
= probe_rpc(rpc
, &results
);
913 if (err
== HTTP_REAUTH
)
914 credential_fill(&http_auth
);
915 } while (err
== HTTP_REAUTH
);
919 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
920 needs_100_continue
= 1;
923 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
924 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
925 headers
= curl_slist_append(headers
, needs_100_continue
?
926 "Expect: 100-continue" : "Expect:");
928 /* Add the extra Git-Protocol header */
929 if (rpc
->protocol_header
)
930 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
933 slot
= get_active_slot();
935 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
936 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
937 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
938 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
941 /* The request body is large and the size cannot be predicted.
942 * We must use chunked encoding to send it.
944 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
945 rpc
->initial_buffer
= 1;
946 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
947 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
949 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKFUNCTION
, rpc_seek
);
950 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKDATA
, rpc
);
952 if (options
.verbosity
> 1) {
953 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
957 } else if (gzip_body
) {
959 * If we are looping to retry authentication, then the previous
960 * run will have set up the headers and gzip buffer already,
961 * and we just need to send it.
963 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
964 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
966 } else if (use_gzip
&& 1024 < rpc
->len
) {
967 /* The client backend isn't giving us compressed data so
968 * we can try to deflate it ourselves, this may save on
974 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
975 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
976 gzip_body
= xmalloc(gzip_size
);
978 stream
.next_in
= (unsigned char *)rpc
->buf
;
979 stream
.avail_in
= rpc
->len
;
980 stream
.next_out
= (unsigned char *)gzip_body
;
981 stream
.avail_out
= gzip_size
;
983 ret
= git_deflate(&stream
, Z_FINISH
);
984 if (ret
!= Z_STREAM_END
)
985 die(_("cannot deflate request; zlib deflate error %d"), ret
);
987 ret
= git_deflate_end_gently(&stream
);
989 die(_("cannot deflate request; zlib end error %d"), ret
);
991 gzip_size
= stream
.total_out
;
993 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
994 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
995 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
997 if (options
.verbosity
> 1) {
998 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
1000 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1004 /* We know the complete request size in advance, use the
1005 * more normal Content-Length approach.
1007 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1008 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1009 if (options
.verbosity
> 1) {
1010 fprintf(stderr
, "POST %s (%lu bytes)\n",
1011 rpc
->service_name
, (unsigned long)rpc
->len
);
1016 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1017 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1018 rpc_in_data
.rpc
= rpc
;
1019 rpc_in_data
.slot
= slot
;
1020 rpc_in_data
.check_pktline
= stateless_connect
;
1021 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1022 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
1023 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1026 rpc
->any_written
= 0;
1027 err
= run_slot(slot
, NULL
);
1028 if (err
== HTTP_REAUTH
&& !large_request
) {
1029 credential_fill(&http_auth
);
1035 if (!rpc
->any_written
)
1038 if (rpc_in_data
.pktline_state
.len_filled
)
1039 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1040 if (rpc_in_data
.pktline_state
.remaining
)
1041 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1043 if (stateless_connect
)
1044 packet_response_end(rpc
->in
);
1046 curl_slist_free_all(headers
);
1051 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1052 const char **client_argv
, const struct strbuf
*preamble
,
1053 struct strbuf
*rpc_result
)
1055 const char *svc
= rpc
->service_name
;
1056 struct strbuf buf
= STRBUF_INIT
;
1057 struct child_process client
= CHILD_PROCESS_INIT
;
1063 client
.argv
= client_argv
;
1064 if (start_command(&client
))
1066 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1068 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1070 rpc
->alloc
= http_post_buffer
;
1071 rpc
->buf
= xmalloc(rpc
->alloc
);
1072 rpc
->in
= client
.in
;
1073 rpc
->out
= client
.out
;
1075 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1076 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1078 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1079 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1081 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1082 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1084 if (get_protocol_http_header(heads
->version
, &buf
))
1085 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1087 rpc
->protocol_header
= NULL
;
1090 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
1095 err
|= post_rpc(rpc
, 0, 0);
1101 strbuf_read(rpc_result
, client
.out
, 0);
1105 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1112 err
|= finish_command(&client
);
1113 free(rpc
->service_url
);
1114 free(rpc
->hdr_content_type
);
1115 free(rpc
->hdr_accept
);
1116 free(rpc
->protocol_header
);
1118 strbuf_release(&buf
);
1122 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1124 struct walker
*walker
;
1128 ALLOC_ARRAY(targets
, nr_heads
);
1129 if (options
.depth
|| options
.deepen_since
)
1130 die(_("dumb http transport does not support shallow capabilities"));
1131 for (i
= 0; i
< nr_heads
; i
++)
1132 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1134 walker
= get_http_walker(url
.buf
);
1135 walker
->get_verbosely
= options
.verbosity
>= 3;
1136 walker
->get_progress
= options
.progress
;
1137 walker
->get_recover
= 0;
1138 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1139 walker_free(walker
);
1141 for (i
= 0; i
< nr_heads
; i
++)
1145 return ret
? error(_("fetch failed.")) : 0;
1148 static int fetch_git(struct discovery
*heads
,
1149 int nr_heads
, struct ref
**to_fetch
)
1151 struct rpc_state rpc
;
1152 struct strbuf preamble
= STRBUF_INIT
;
1154 struct strvec args
= STRVEC_INIT
;
1155 struct strbuf rpc_result
= STRBUF_INIT
;
1157 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1158 "--stdin", "--lock-pack", NULL
);
1159 if (options
.followtags
)
1160 strvec_push(&args
, "--include-tag");
1162 strvec_push(&args
, "--thin");
1163 if (options
.verbosity
>= 3)
1164 strvec_pushl(&args
, "-v", "-v", NULL
);
1165 if (options
.check_self_contained_and_connected
)
1166 strvec_push(&args
, "--check-self-contained-and-connected");
1167 if (options
.cloning
)
1168 strvec_push(&args
, "--cloning");
1169 if (options
.update_shallow
)
1170 strvec_push(&args
, "--update-shallow");
1171 if (!options
.progress
)
1172 strvec_push(&args
, "--no-progress");
1174 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1175 if (options
.deepen_since
)
1176 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1177 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1178 strvec_pushf(&args
, "--shallow-exclude=%s",
1179 options
.deepen_not
.items
[i
].string
);
1180 if (options
.deepen_relative
&& options
.depth
)
1181 strvec_push(&args
, "--deepen-relative");
1182 if (options
.from_promisor
)
1183 strvec_push(&args
, "--from-promisor");
1185 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1186 strvec_push(&args
, url
.buf
);
1188 for (i
= 0; i
< nr_heads
; i
++) {
1189 struct ref
*ref
= to_fetch
[i
];
1191 die(_("cannot fetch by sha1 over smart http"));
1192 packet_buf_write(&preamble
, "%s %s\n",
1193 oid_to_hex(&ref
->old_oid
), ref
->name
);
1195 packet_buf_flush(&preamble
);
1197 memset(&rpc
, 0, sizeof(rpc
));
1198 rpc
.service_name
= "git-upload-pack",
1199 rpc
.gzip_request
= 1;
1201 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1203 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1204 strbuf_release(&rpc_result
);
1205 strbuf_release(&preamble
);
1206 strvec_clear(&args
);
1210 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1212 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1214 return fetch_git(d
, nr_heads
, to_fetch
);
1216 return fetch_dumb(nr_heads
, to_fetch
);
1219 static void parse_fetch(struct strbuf
*buf
)
1221 struct ref
**to_fetch
= NULL
;
1222 struct ref
*list_head
= NULL
;
1223 struct ref
**list
= &list_head
;
1224 int alloc_heads
= 0, nr_heads
= 0;
1228 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1231 struct object_id old_oid
;
1234 if (parse_oid_hex(p
, &old_oid
, &q
))
1235 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1241 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1243 ref
= alloc_ref(name
);
1244 oidcpy(&ref
->old_oid
, &old_oid
);
1249 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1250 to_fetch
[nr_heads
++] = ref
;
1253 die(_("http transport does not support %s"), buf
->buf
);
1256 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1262 if (fetch(nr_heads
, to_fetch
))
1263 exit(128); /* error already reported */
1264 free_refs(list_head
);
1272 static int push_dav(int nr_spec
, const char **specs
)
1274 struct child_process child
= CHILD_PROCESS_INIT
;
1278 strvec_push(&child
.args
, "http-push");
1279 strvec_push(&child
.args
, "--helper-status");
1280 if (options
.dry_run
)
1281 strvec_push(&child
.args
, "--dry-run");
1282 if (options
.verbosity
> 1)
1283 strvec_push(&child
.args
, "--verbose");
1284 strvec_push(&child
.args
, url
.buf
);
1285 for (i
= 0; i
< nr_spec
; i
++)
1286 strvec_push(&child
.args
, specs
[i
]);
1288 if (run_command(&child
))
1289 die(_("git-http-push failed"));
1293 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1295 struct rpc_state rpc
;
1298 struct string_list_item
*cas_option
;
1299 struct strbuf preamble
= STRBUF_INIT
;
1300 struct strbuf rpc_result
= STRBUF_INIT
;
1303 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1307 strvec_push(&args
, "--thin");
1308 if (options
.dry_run
)
1309 strvec_push(&args
, "--dry-run");
1310 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1311 strvec_push(&args
, "--signed=yes");
1312 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1313 strvec_push(&args
, "--signed=if-asked");
1315 strvec_push(&args
, "--atomic");
1316 if (options
.verbosity
== 0)
1317 strvec_push(&args
, "--quiet");
1318 else if (options
.verbosity
> 1)
1319 strvec_push(&args
, "--verbose");
1320 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1321 strvec_pushf(&args
, "--push-option=%s",
1322 options
.push_options
.items
[i
].string
);
1323 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1324 for_each_string_list_item(cas_option
, &cas_options
)
1325 strvec_push(&args
, cas_option
->string
);
1326 strvec_push(&args
, url
.buf
);
1328 if (options
.force_if_includes
)
1329 strvec_push(&args
, "--force-if-includes");
1331 strvec_push(&args
, "--stdin");
1332 for (i
= 0; i
< nr_spec
; i
++)
1333 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1334 packet_buf_flush(&preamble
);
1336 memset(&rpc
, 0, sizeof(rpc
));
1337 rpc
.service_name
= "git-receive-pack",
1339 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1341 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1342 strbuf_release(&rpc_result
);
1343 strbuf_release(&preamble
);
1344 strvec_clear(&args
);
1348 static int push(int nr_spec
, const char **specs
)
1350 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1353 if (heads
->proto_git
)
1354 ret
= push_git(heads
, nr_spec
, specs
);
1356 ret
= push_dav(nr_spec
, specs
);
1357 free_discovery(heads
);
1361 static void parse_push(struct strbuf
*buf
)
1363 struct strvec specs
= STRVEC_INIT
;
1368 if (skip_prefix(buf
->buf
, "push ", &arg
))
1369 strvec_push(&specs
, arg
);
1371 die(_("http transport does not support %s"), buf
->buf
);
1374 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1380 ret
= push(specs
.nr
, specs
.v
);
1385 exit(128); /* error already reported */
1388 strvec_clear(&specs
);
1391 static int stateless_connect(const char *service_name
)
1393 struct discovery
*discover
;
1394 struct rpc_state rpc
;
1395 struct strbuf buf
= STRBUF_INIT
;
1398 * Run the info/refs request and see if the server supports protocol
1399 * v2. If and only if the server supports v2 can we successfully
1400 * establish a stateless connection, otherwise we need to tell the
1401 * client to fallback to using other transport helper functions to
1402 * complete their request.
1404 discover
= discover_refs(service_name
, 0);
1405 if (discover
->version
!= protocol_v2
) {
1406 printf("fallback\n");
1410 /* Stateless Connection established */
1415 rpc
.service_name
= service_name
;
1416 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1417 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1418 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1419 if (get_protocol_http_header(discover
->version
, &buf
)) {
1420 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1422 rpc
.protocol_header
= NULL
;
1423 strbuf_release(&buf
);
1425 rpc
.buf
= xmalloc(http_post_buffer
);
1426 rpc
.alloc
= http_post_buffer
;
1431 rpc
.any_written
= 0;
1432 rpc
.gzip_request
= 1;
1433 rpc
.initial_buffer
= 0;
1434 rpc
.write_line_lengths
= 1;
1435 rpc
.flush_read_but_not_sent
= 0;
1438 * Dump the capability listing that we got from the server earlier
1439 * during the info/refs request.
1441 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1443 /* Until we see EOF keep sending POSTs */
1446 enum packet_read_status status
;
1448 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1450 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1451 if (status
== PACKET_READ_EOF
)
1453 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1454 /* We would have an err here */
1456 /* Reset the buffer for next request */
1460 free(rpc
.service_url
);
1461 free(rpc
.hdr_content_type
);
1462 free(rpc
.hdr_accept
);
1463 free(rpc
.protocol_header
);
1465 strbuf_release(&buf
);
1470 int cmd_main(int argc
, const char **argv
)
1472 struct strbuf buf
= STRBUF_INIT
;
1475 setup_git_directory_gently(&nongit
);
1477 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1481 options
.verbosity
= 1;
1482 options
.progress
= !!isatty(2);
1484 string_list_init(&options
.deepen_not
, 1);
1485 string_list_init(&options
.push_options
, 1);
1488 * Just report "remote-curl" here (folding all the various aliases
1489 * ("git-remote-http", "git-remote-https", and etc.) here since they
1490 * are all just copies of the same actual executable.
1492 trace2_cmd_name("remote-curl");
1494 remote
= remote_get(argv
[1]);
1497 end_url_with_slash(&url
, argv
[2]);
1499 end_url_with_slash(&url
, remote
->url
[0]);
1502 http_init(remote
, url
.buf
, 0);
1507 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1509 error(_("remote-curl: error reading command stream from git"));
1514 if (starts_with(buf
.buf
, "fetch ")) {
1516 die(_("remote-curl: fetch attempted without a local repo"));
1519 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1520 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1521 output_refs(get_refs(for_push
));
1523 } else if (starts_with(buf
.buf
, "push ")) {
1526 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1527 char *value
= strchr(arg
, ' ');
1535 result
= set_option(arg
, value
);
1538 else if (result
< 0)
1539 printf("error invalid value\n");
1541 printf("unsupported\n");
1544 } else if (!strcmp(buf
.buf
, "capabilities")) {
1545 printf("stateless-connect\n");
1549 printf("check-connectivity\n");
1550 printf("object-format\n");
1553 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1554 if (!stateless_connect(arg
))
1557 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);