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 const struct git_hash_algo
*hash_algo
;
50 static struct options options
;
51 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
53 static int set_option(const char *name
, const char *value
)
55 if (!strcmp(name
, "verbosity")) {
57 int v
= strtol(value
, &end
, 10);
58 if (value
== end
|| *end
)
60 options
.verbosity
= v
;
63 else if (!strcmp(name
, "progress")) {
64 if (!strcmp(value
, "true"))
66 else if (!strcmp(value
, "false"))
72 else if (!strcmp(name
, "depth")) {
74 unsigned long v
= strtoul(value
, &end
, 10);
75 if (value
== end
|| *end
)
80 else if (!strcmp(name
, "deepen-since")) {
81 options
.deepen_since
= xstrdup(value
);
84 else if (!strcmp(name
, "deepen-not")) {
85 string_list_append(&options
.deepen_not
, value
);
88 else if (!strcmp(name
, "deepen-relative")) {
89 if (!strcmp(value
, "true"))
90 options
.deepen_relative
= 1;
91 else if (!strcmp(value
, "false"))
92 options
.deepen_relative
= 0;
97 else if (!strcmp(name
, "followtags")) {
98 if (!strcmp(value
, "true"))
99 options
.followtags
= 1;
100 else if (!strcmp(value
, "false"))
101 options
.followtags
= 0;
106 else if (!strcmp(name
, "dry-run")) {
107 if (!strcmp(value
, "true"))
109 else if (!strcmp(value
, "false"))
115 else if (!strcmp(name
, "check-connectivity")) {
116 if (!strcmp(value
, "true"))
117 options
.check_self_contained_and_connected
= 1;
118 else if (!strcmp(value
, "false"))
119 options
.check_self_contained_and_connected
= 0;
124 else if (!strcmp(name
, "cas")) {
125 struct strbuf val
= STRBUF_INIT
;
126 strbuf_addstr(&val
, "--force-with-lease=");
128 strbuf_addstr(&val
, value
);
129 else if (unquote_c_style(&val
, value
, NULL
))
131 string_list_append(&cas_options
, val
.buf
);
132 strbuf_release(&val
);
134 } else if (!strcmp(name
, "cloning")) {
135 if (!strcmp(value
, "true"))
137 else if (!strcmp(value
, "false"))
142 } else if (!strcmp(name
, "update-shallow")) {
143 if (!strcmp(value
, "true"))
144 options
.update_shallow
= 1;
145 else if (!strcmp(value
, "false"))
146 options
.update_shallow
= 0;
150 } else if (!strcmp(name
, "pushcert")) {
151 if (!strcmp(value
, "true"))
152 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
153 else if (!strcmp(value
, "false"))
154 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
155 else if (!strcmp(value
, "if-asked"))
156 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
160 } else if (!strcmp(name
, "atomic")) {
161 if (!strcmp(value
, "true"))
163 else if (!strcmp(value
, "false"))
168 } else if (!strcmp(name
, "push-option")) {
170 string_list_append(&options
.push_options
, value
);
172 struct strbuf unquoted
= STRBUF_INIT
;
173 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
174 die(_("invalid quoting in push-option value: '%s'"), value
);
175 string_list_append_nodup(&options
.push_options
,
176 strbuf_detach(&unquoted
, NULL
));
180 #if LIBCURL_VERSION_NUM >= 0x070a08
181 } else if (!strcmp(name
, "family")) {
182 if (!strcmp(value
, "ipv4"))
183 git_curl_ipresolve
= CURL_IPRESOLVE_V4
;
184 else if (!strcmp(value
, "ipv6"))
185 git_curl_ipresolve
= CURL_IPRESOLVE_V6
;
186 else if (!strcmp(value
, "all"))
187 git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
191 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
192 } else if (!strcmp(name
, "from-promisor")) {
193 options
.from_promisor
= 1;
195 } else if (!strcmp(name
, "filter")) {
196 options
.filter
= xstrdup(value
);
198 } else if (!strcmp(name
, "object-format")) {
200 options
.object_format
= 1;
201 if (strcmp(value
, "true")) {
202 algo
= hash_algo_by_name(value
);
203 if (algo
== GIT_HASH_UNKNOWN
)
204 die("unknown object format '%s'", value
);
205 options
.hash_algo
= &hash_algos
[algo
];
209 return 1 /* unsupported */;
219 struct oid_array shallow
;
220 enum protocol_version version
;
221 unsigned proto_git
: 1;
223 static struct discovery
*last_discovery
;
225 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
227 struct ref
*list
= NULL
;
228 struct packet_reader reader
;
230 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
231 PACKET_READ_CHOMP_NEWLINE
|
232 PACKET_READ_GENTLE_ON_EOF
|
233 PACKET_READ_DIE_ON_ERR_PACKET
);
235 heads
->version
= discover_version(&reader
);
236 switch (heads
->version
) {
239 * Do nothing. This isn't a list of refs but rather a
240 * capability advertisement. Client would have run
241 * 'stateless-connect' so we'll dump this capability listing
242 * and let them request the refs themselves.
247 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
248 NULL
, &heads
->shallow
);
249 options
.hash_algo
= reader
.hash_algo
;
251 case protocol_unknown_version
:
252 BUG("unknown protocol version");
258 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
260 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
263 return the_hash_algo
;
265 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
266 if (algo
== GIT_HASH_UNKNOWN
)
268 return &hash_algos
[algo
];
271 static struct ref
*parse_info_refs(struct discovery
*heads
)
273 char *data
, *start
, *mid
;
277 struct ref
*refs
= NULL
;
278 struct ref
*ref
= NULL
;
279 struct ref
*last_ref
= NULL
;
281 options
.hash_algo
= detect_hash_algo(heads
);
282 if (!options
.hash_algo
)
283 die("%sinfo/refs not valid: could not determine hash algorithm; "
284 "is this a git repository?",
285 transport_anonymize_url(url
.buf
));
290 while (i
< heads
->len
) {
296 if (data
[i
] == '\n') {
297 if (mid
- start
!= options
.hash_algo
->hexsz
)
298 die(_("%sinfo/refs not valid: is this a git repository?"),
299 transport_anonymize_url(url
.buf
));
302 ref
= alloc_ref(ref_name
);
303 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
307 last_ref
->next
= ref
;
314 ref
= alloc_ref("HEAD");
315 if (!http_fetch_ref(url
.buf
, ref
) &&
316 !resolve_remote_symref(ref
, refs
)) {
326 static void free_discovery(struct discovery
*d
)
329 if (d
== last_discovery
)
330 last_discovery
= NULL
;
331 free(d
->shallow
.oid
);
339 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
345 * We only show text/plain parts, as other types are likely
346 * to be ugly to look at on the user's terminal.
348 if (strcmp(type
->buf
, "text/plain"))
351 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
359 eol
= strchrnul(p
, '\n');
360 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
366 static int get_protocol_http_header(enum protocol_version version
,
367 struct strbuf
*header
)
370 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
379 static void check_smart_http(struct discovery
*d
, const char *service
,
383 struct packet_reader reader
;
386 * If we don't see x-$service-advertisement, then it's not smart-http.
387 * But once we do, we commit to it and assume any other protocol
388 * violations are hard errors.
390 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
391 !skip_prefix(p
, service
, &p
) ||
392 strcmp(p
, "-advertisement"))
395 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
396 PACKET_READ_CHOMP_NEWLINE
|
397 PACKET_READ_DIE_ON_ERR_PACKET
);
398 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
399 die(_("invalid server response; expected service, got flush packet"));
401 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
403 * The header can include additional metadata lines, up
404 * until a packet flush marker. Ignore these now, but
405 * in the future we might start to scan them.
408 packet_reader_read(&reader
);
409 if (reader
.pktlen
<= 0) {
415 * v0 smart http; callers expect us to soak up the
416 * service and header packets
418 d
->buf
= reader
.src_buffer
;
419 d
->len
= reader
.src_len
;
422 } else if (!strcmp(reader
.line
, "version 2")) {
424 * v2 smart http; do not consume version packet, which will
425 * be handled elsewhere.
430 die(_("invalid server response; got '%s'"), reader
.line
);
434 static struct discovery
*discover_refs(const char *service
, int for_push
)
436 struct strbuf type
= STRBUF_INIT
;
437 struct strbuf charset
= STRBUF_INIT
;
438 struct strbuf buffer
= STRBUF_INIT
;
439 struct strbuf refs_url
= STRBUF_INIT
;
440 struct strbuf effective_url
= STRBUF_INIT
;
441 struct strbuf protocol_header
= STRBUF_INIT
;
442 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
443 struct discovery
*last
= last_discovery
;
444 int http_ret
, maybe_smart
= 0;
445 struct http_get_options http_options
;
446 enum protocol_version version
= get_protocol_version_config();
448 if (last
&& !strcmp(service
, last
->service
))
450 free_discovery(last
);
452 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
453 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
454 git_env_bool("GIT_SMART_HTTP", 1)) {
456 if (!strchr(url
.buf
, '?'))
457 strbuf_addch(&refs_url
, '?');
459 strbuf_addch(&refs_url
, '&');
460 strbuf_addf(&refs_url
, "service=%s", service
);
464 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
465 * to perform a push, then fallback to v0 since the client doesn't know
466 * how to push yet using v2.
468 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
469 version
= protocol_v0
;
471 /* Add the extra Git-Protocol header */
472 if (get_protocol_http_header(version
, &protocol_header
))
473 string_list_append(&extra_headers
, protocol_header
.buf
);
475 memset(&http_options
, 0, sizeof(http_options
));
476 http_options
.content_type
= &type
;
477 http_options
.charset
= &charset
;
478 http_options
.effective_url
= &effective_url
;
479 http_options
.base_url
= &url
;
480 http_options
.extra_headers
= &extra_headers
;
481 http_options
.initial_request
= 1;
482 http_options
.no_cache
= 1;
484 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
488 case HTTP_MISSING_TARGET
:
489 show_http_message(&type
, &charset
, &buffer
);
490 die(_("repository '%s' not found"),
491 transport_anonymize_url(url
.buf
));
493 show_http_message(&type
, &charset
, &buffer
);
494 die(_("Authentication failed for '%s'"),
495 transport_anonymize_url(url
.buf
));
497 show_http_message(&type
, &charset
, &buffer
);
498 die(_("unable to access '%s': %s"),
499 transport_anonymize_url(url
.buf
), curl_errorstr
);
502 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
503 char *u
= transport_anonymize_url(url
.buf
);
504 warning(_("redirecting to %s"), u
);
508 last
= xcalloc(1, sizeof(*last_discovery
));
509 last
->service
= xstrdup(service
);
510 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
511 last
->buf
= last
->buf_alloc
;
514 check_smart_http(last
, service
, &type
);
517 last
->refs
= parse_git_refs(last
, for_push
);
519 last
->refs
= parse_info_refs(last
);
521 strbuf_release(&refs_url
);
522 strbuf_release(&type
);
523 strbuf_release(&charset
);
524 strbuf_release(&effective_url
);
525 strbuf_release(&buffer
);
526 strbuf_release(&protocol_header
);
527 string_list_clear(&extra_headers
, 0);
528 last_discovery
= last
;
532 static struct ref
*get_refs(int for_push
)
534 struct discovery
*heads
;
537 heads
= discover_refs("git-receive-pack", for_push
);
539 heads
= discover_refs("git-upload-pack", for_push
);
544 static void output_refs(struct ref
*refs
)
547 if (options
.object_format
&& options
.hash_algo
) {
548 printf(":object-format %s\n", options
.hash_algo
->name
);
550 for (posn
= refs
; posn
; posn
= posn
->next
) {
552 printf("@%s %s\n", posn
->symref
, posn
->name
);
554 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
563 const char *service_name
;
565 char *hdr_content_type
;
567 char *protocol_header
;
575 unsigned gzip_request
: 1;
576 unsigned initial_buffer
: 1;
579 * Whenever a pkt-line is read into buf, append the 4 characters
580 * denoting its length before appending the payload.
582 unsigned write_line_lengths
: 1;
585 * Used by rpc_out; initialize to 0. This is true if a flush has been
586 * read, but the corresponding line length (if write_line_lengths is
587 * true) and EOF have not been sent to libcurl. Since each flush marks
588 * the end of a request, each flush must be completely sent before any
589 * further reading occurs.
591 unsigned flush_read_but_not_sent
: 1;
595 * Appends the result of reading from rpc->out to the string represented by
596 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
597 * enough space, 0 otherwise.
599 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
600 * hexadecimal string before appending the result described above.
602 * Writes the total number of bytes appended into appended.
604 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
606 enum packet_read_status
*status
) {
611 if (rpc
->write_line_lengths
) {
612 left
= rpc
->alloc
- rpc
->len
- 4;
613 buf
= rpc
->buf
+ rpc
->len
+ 4;
615 left
= rpc
->alloc
- rpc
->len
;
616 buf
= rpc
->buf
+ rpc
->len
;
619 if (left
< LARGE_PACKET_MAX
)
622 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
623 left
, &pktlen_raw
, options
);
624 if (*status
!= PACKET_READ_EOF
) {
625 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
626 rpc
->len
+= *appended
;
629 if (rpc
->write_line_lengths
) {
631 case PACKET_READ_EOF
:
632 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
633 die(_("shouldn't have EOF when not gentle on EOF"));
635 case PACKET_READ_NORMAL
:
636 set_packet_header(buf
- 4, *appended
);
638 case PACKET_READ_DELIM
:
639 memcpy(buf
- 4, "0001", 4);
641 case PACKET_READ_FLUSH
:
642 memcpy(buf
- 4, "0000", 4);
644 case PACKET_READ_RESPONSE_END
:
645 die(_("remote server sent stateless separator"));
652 static size_t rpc_out(void *ptr
, size_t eltsize
,
653 size_t nmemb
, void *buffer_
)
655 size_t max
= eltsize
* nmemb
;
656 struct rpc_state
*rpc
= buffer_
;
657 size_t avail
= rpc
->len
- rpc
->pos
;
658 enum packet_read_status status
;
661 rpc
->initial_buffer
= 0;
664 if (!rpc
->flush_read_but_not_sent
) {
665 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
666 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
667 if (status
== PACKET_READ_FLUSH
)
668 rpc
->flush_read_but_not_sent
= 1;
671 * If flush_read_but_not_sent is true, we have already read one
672 * full request but have not fully sent it + EOF, which is why
673 * we need to refrain from reading.
676 if (rpc
->flush_read_but_not_sent
) {
679 * The line length either does not need to be sent at
680 * all or has already been completely sent. Now we can
681 * return 0, indicating EOF, meaning that the flush has
684 rpc
->flush_read_but_not_sent
= 0;
688 * If avail is non-zero, the line length for the flush still
689 * hasn't been fully sent. Proceed with sending the line
696 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
701 #ifndef NO_CURL_IOCTL
702 static curlioerr
rpc_ioctl(CURL
*handle
, int cmd
, void *clientp
)
704 struct rpc_state
*rpc
= clientp
;
710 case CURLIOCMD_RESTARTREAD
:
711 if (rpc
->initial_buffer
) {
715 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
716 return CURLIOE_FAILRESTART
;
719 return CURLIOE_UNKNOWNCMD
;
724 struct check_pktline_state
{
730 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
733 if (!state
->remaining
) {
734 int digits_remaining
= 4 - state
->len_filled
;
735 if (digits_remaining
> size
)
736 digits_remaining
= size
;
737 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
738 state
->len_filled
+= digits_remaining
;
739 ptr
+= digits_remaining
;
740 size
-= digits_remaining
;
742 if (state
->len_filled
== 4) {
743 state
->remaining
= packet_length(state
->len_buf
);
744 if (state
->remaining
< 0) {
745 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
746 } else if (state
->remaining
== 2) {
747 die(_("remote-curl: unexpected response end packet"));
748 } else if (state
->remaining
< 4) {
749 state
->remaining
= 0;
751 state
->remaining
-= 4;
753 state
->len_filled
= 0;
757 if (state
->remaining
) {
758 int remaining
= state
->remaining
;
759 if (remaining
> size
)
763 state
->remaining
-= remaining
;
769 struct rpc_state
*rpc
;
770 struct active_request_slot
*slot
;
772 struct check_pktline_state pktline_state
;
776 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
779 static size_t rpc_in(char *ptr
, size_t eltsize
,
780 size_t nmemb
, void *buffer_
)
782 size_t size
= eltsize
* nmemb
;
783 struct rpc_in_data
*data
= buffer_
;
786 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
787 &response_code
) != CURLE_OK
)
789 if (response_code
>= 300)
792 data
->rpc
->any_written
= 1;
793 if (data
->check_pktline
)
794 check_pktline(&data
->pktline_state
, ptr
, size
);
795 write_or_die(data
->rpc
->in
, ptr
, size
);
799 static int run_slot(struct active_request_slot
*slot
,
800 struct slot_results
*results
)
803 struct slot_results results_buf
;
806 results
= &results_buf
;
808 err
= run_one_slot(slot
, results
);
810 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
811 struct strbuf msg
= STRBUF_INIT
;
812 if (results
->http_code
&& results
->http_code
!= 200)
813 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
814 if (results
->curl_result
!= CURLE_OK
) {
816 strbuf_addch(&msg
, ' ');
817 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
818 if (curl_errorstr
[0]) {
819 strbuf_addch(&msg
, ' ');
820 strbuf_addstr(&msg
, curl_errorstr
);
823 error(_("RPC failed; %s"), msg
.buf
);
824 strbuf_release(&msg
);
830 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
832 struct active_request_slot
*slot
;
833 struct curl_slist
*headers
= http_copy_default_headers();
834 struct strbuf buf
= STRBUF_INIT
;
837 slot
= get_active_slot();
839 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
840 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
842 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
843 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
844 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
845 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
846 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
847 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
848 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
849 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
850 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buf
);
852 err
= run_slot(slot
, results
);
854 curl_slist_free_all(headers
);
855 strbuf_release(&buf
);
859 static curl_off_t
xcurl_off_t(size_t len
)
861 uintmax_t size
= len
;
862 if (size
> maximum_signed_value_of_type(curl_off_t
))
863 die(_("cannot handle pushes this big"));
864 return (curl_off_t
)size
;
868 * If flush_received is true, do not attempt to read any more; just use what's
871 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
873 struct active_request_slot
*slot
;
874 struct curl_slist
*headers
= http_copy_default_headers();
875 int use_gzip
= rpc
->gzip_request
;
876 char *gzip_body
= NULL
;
877 size_t gzip_size
= 0;
878 int err
, large_request
= 0;
879 int needs_100_continue
= 0;
880 struct rpc_in_data rpc_in_data
;
882 /* Try to load the entire request, if we can fit it into the
883 * allocated buffer space we can use HTTP/1.0 and avoid the
884 * chunked encoding mess.
886 if (!flush_received
) {
889 enum packet_read_status status
;
891 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
896 if (status
== PACKET_READ_FLUSH
)
902 struct slot_results results
;
905 err
= probe_rpc(rpc
, &results
);
906 if (err
== HTTP_REAUTH
)
907 credential_fill(&http_auth
);
908 } while (err
== HTTP_REAUTH
);
912 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
913 needs_100_continue
= 1;
916 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
917 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
918 headers
= curl_slist_append(headers
, needs_100_continue
?
919 "Expect: 100-continue" : "Expect:");
921 /* Add the extra Git-Protocol header */
922 if (rpc
->protocol_header
)
923 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
926 slot
= get_active_slot();
928 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
929 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
930 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
931 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
934 /* The request body is large and the size cannot be predicted.
935 * We must use chunked encoding to send it.
937 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
938 rpc
->initial_buffer
= 1;
939 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
940 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
941 #ifndef NO_CURL_IOCTL
942 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLFUNCTION
, rpc_ioctl
);
943 curl_easy_setopt(slot
->curl
, CURLOPT_IOCTLDATA
, rpc
);
945 if (options
.verbosity
> 1) {
946 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
950 } else if (gzip_body
) {
952 * If we are looping to retry authentication, then the previous
953 * run will have set up the headers and gzip buffer already,
954 * and we just need to send it.
956 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
957 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
959 } else if (use_gzip
&& 1024 < rpc
->len
) {
960 /* The client backend isn't giving us compressed data so
961 * we can try to deflate it ourselves, this may save on
967 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
968 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
969 gzip_body
= xmalloc(gzip_size
);
971 stream
.next_in
= (unsigned char *)rpc
->buf
;
972 stream
.avail_in
= rpc
->len
;
973 stream
.next_out
= (unsigned char *)gzip_body
;
974 stream
.avail_out
= gzip_size
;
976 ret
= git_deflate(&stream
, Z_FINISH
);
977 if (ret
!= Z_STREAM_END
)
978 die(_("cannot deflate request; zlib deflate error %d"), ret
);
980 ret
= git_deflate_end_gently(&stream
);
982 die(_("cannot deflate request; zlib end error %d"), ret
);
984 gzip_size
= stream
.total_out
;
986 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
987 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
988 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
990 if (options
.verbosity
> 1) {
991 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
993 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
997 /* We know the complete request size in advance, use the
998 * more normal Content-Length approach.
1000 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1001 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1002 if (options
.verbosity
> 1) {
1003 fprintf(stderr
, "POST %s (%lu bytes)\n",
1004 rpc
->service_name
, (unsigned long)rpc
->len
);
1009 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1010 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1011 rpc_in_data
.rpc
= rpc
;
1012 rpc_in_data
.slot
= slot
;
1013 rpc_in_data
.check_pktline
= stateless_connect
;
1014 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1015 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &rpc_in_data
);
1016 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1019 rpc
->any_written
= 0;
1020 err
= run_slot(slot
, NULL
);
1021 if (err
== HTTP_REAUTH
&& !large_request
) {
1022 credential_fill(&http_auth
);
1028 if (!rpc
->any_written
)
1031 if (rpc_in_data
.pktline_state
.len_filled
)
1032 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1033 if (rpc_in_data
.pktline_state
.remaining
)
1034 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1036 if (stateless_connect
)
1037 packet_response_end(rpc
->in
);
1039 curl_slist_free_all(headers
);
1044 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1045 const char **client_argv
, const struct strbuf
*preamble
,
1046 struct strbuf
*rpc_result
)
1048 const char *svc
= rpc
->service_name
;
1049 struct strbuf buf
= STRBUF_INIT
;
1050 struct child_process client
= CHILD_PROCESS_INIT
;
1056 client
.argv
= client_argv
;
1057 if (start_command(&client
))
1059 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1061 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1063 rpc
->alloc
= http_post_buffer
;
1064 rpc
->buf
= xmalloc(rpc
->alloc
);
1065 rpc
->in
= client
.in
;
1066 rpc
->out
= client
.out
;
1068 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1069 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1071 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1072 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1074 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1075 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1077 if (get_protocol_http_header(heads
->version
, &buf
))
1078 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1080 rpc
->protocol_header
= NULL
;
1083 int n
= packet_read(rpc
->out
, NULL
, NULL
, rpc
->buf
, rpc
->alloc
, 0);
1088 err
|= post_rpc(rpc
, 0, 0);
1094 strbuf_read(rpc_result
, client
.out
, 0);
1098 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1105 err
|= finish_command(&client
);
1106 free(rpc
->service_url
);
1107 free(rpc
->hdr_content_type
);
1108 free(rpc
->hdr_accept
);
1109 free(rpc
->protocol_header
);
1111 strbuf_release(&buf
);
1115 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1117 struct walker
*walker
;
1121 ALLOC_ARRAY(targets
, nr_heads
);
1122 if (options
.depth
|| options
.deepen_since
)
1123 die(_("dumb http transport does not support shallow capabilities"));
1124 for (i
= 0; i
< nr_heads
; i
++)
1125 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1127 walker
= get_http_walker(url
.buf
);
1128 walker
->get_verbosely
= options
.verbosity
>= 3;
1129 walker
->get_progress
= options
.progress
;
1130 walker
->get_recover
= 0;
1131 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1132 walker_free(walker
);
1134 for (i
= 0; i
< nr_heads
; i
++)
1138 return ret
? error(_("fetch failed.")) : 0;
1141 static int fetch_git(struct discovery
*heads
,
1142 int nr_heads
, struct ref
**to_fetch
)
1144 struct rpc_state rpc
;
1145 struct strbuf preamble
= STRBUF_INIT
;
1147 struct strvec args
= STRVEC_INIT
;
1148 struct strbuf rpc_result
= STRBUF_INIT
;
1150 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1151 "--stdin", "--lock-pack", NULL
);
1152 if (options
.followtags
)
1153 strvec_push(&args
, "--include-tag");
1155 strvec_push(&args
, "--thin");
1156 if (options
.verbosity
>= 3)
1157 strvec_pushl(&args
, "-v", "-v", NULL
);
1158 if (options
.check_self_contained_and_connected
)
1159 strvec_push(&args
, "--check-self-contained-and-connected");
1160 if (options
.cloning
)
1161 strvec_push(&args
, "--cloning");
1162 if (options
.update_shallow
)
1163 strvec_push(&args
, "--update-shallow");
1164 if (!options
.progress
)
1165 strvec_push(&args
, "--no-progress");
1167 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1168 if (options
.deepen_since
)
1169 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1170 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1171 strvec_pushf(&args
, "--shallow-exclude=%s",
1172 options
.deepen_not
.items
[i
].string
);
1173 if (options
.deepen_relative
&& options
.depth
)
1174 strvec_push(&args
, "--deepen-relative");
1175 if (options
.from_promisor
)
1176 strvec_push(&args
, "--from-promisor");
1178 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1179 strvec_push(&args
, url
.buf
);
1181 for (i
= 0; i
< nr_heads
; i
++) {
1182 struct ref
*ref
= to_fetch
[i
];
1184 die(_("cannot fetch by sha1 over smart http"));
1185 packet_buf_write(&preamble
, "%s %s\n",
1186 oid_to_hex(&ref
->old_oid
), ref
->name
);
1188 packet_buf_flush(&preamble
);
1190 memset(&rpc
, 0, sizeof(rpc
));
1191 rpc
.service_name
= "git-upload-pack",
1192 rpc
.gzip_request
= 1;
1194 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1196 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1197 strbuf_release(&rpc_result
);
1198 strbuf_release(&preamble
);
1199 strvec_clear(&args
);
1203 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1205 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1207 return fetch_git(d
, nr_heads
, to_fetch
);
1209 return fetch_dumb(nr_heads
, to_fetch
);
1212 static void parse_fetch(struct strbuf
*buf
)
1214 struct ref
**to_fetch
= NULL
;
1215 struct ref
*list_head
= NULL
;
1216 struct ref
**list
= &list_head
;
1217 int alloc_heads
= 0, nr_heads
= 0;
1221 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1224 struct object_id old_oid
;
1227 if (parse_oid_hex(p
, &old_oid
, &q
))
1228 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1234 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1236 ref
= alloc_ref(name
);
1237 oidcpy(&ref
->old_oid
, &old_oid
);
1242 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1243 to_fetch
[nr_heads
++] = ref
;
1246 die(_("http transport does not support %s"), buf
->buf
);
1249 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1255 if (fetch(nr_heads
, to_fetch
))
1256 exit(128); /* error already reported */
1257 free_refs(list_head
);
1265 static int push_dav(int nr_spec
, const char **specs
)
1267 struct child_process child
= CHILD_PROCESS_INIT
;
1271 strvec_push(&child
.args
, "http-push");
1272 strvec_push(&child
.args
, "--helper-status");
1273 if (options
.dry_run
)
1274 strvec_push(&child
.args
, "--dry-run");
1275 if (options
.verbosity
> 1)
1276 strvec_push(&child
.args
, "--verbose");
1277 strvec_push(&child
.args
, url
.buf
);
1278 for (i
= 0; i
< nr_spec
; i
++)
1279 strvec_push(&child
.args
, specs
[i
]);
1281 if (run_command(&child
))
1282 die(_("git-http-push failed"));
1286 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1288 struct rpc_state rpc
;
1291 struct string_list_item
*cas_option
;
1292 struct strbuf preamble
= STRBUF_INIT
;
1293 struct strbuf rpc_result
= STRBUF_INIT
;
1296 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1300 strvec_push(&args
, "--thin");
1301 if (options
.dry_run
)
1302 strvec_push(&args
, "--dry-run");
1303 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1304 strvec_push(&args
, "--signed=yes");
1305 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1306 strvec_push(&args
, "--signed=if-asked");
1308 strvec_push(&args
, "--atomic");
1309 if (options
.verbosity
== 0)
1310 strvec_push(&args
, "--quiet");
1311 else if (options
.verbosity
> 1)
1312 strvec_push(&args
, "--verbose");
1313 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1314 strvec_pushf(&args
, "--push-option=%s",
1315 options
.push_options
.items
[i
].string
);
1316 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1317 for_each_string_list_item(cas_option
, &cas_options
)
1318 strvec_push(&args
, cas_option
->string
);
1319 strvec_push(&args
, url
.buf
);
1321 strvec_push(&args
, "--stdin");
1322 for (i
= 0; i
< nr_spec
; i
++)
1323 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1324 packet_buf_flush(&preamble
);
1326 memset(&rpc
, 0, sizeof(rpc
));
1327 rpc
.service_name
= "git-receive-pack",
1329 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1331 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1332 strbuf_release(&rpc_result
);
1333 strbuf_release(&preamble
);
1334 strvec_clear(&args
);
1338 static int push(int nr_spec
, const char **specs
)
1340 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1343 if (heads
->proto_git
)
1344 ret
= push_git(heads
, nr_spec
, specs
);
1346 ret
= push_dav(nr_spec
, specs
);
1347 free_discovery(heads
);
1351 static void parse_push(struct strbuf
*buf
)
1353 struct strvec specs
= STRVEC_INIT
;
1358 if (skip_prefix(buf
->buf
, "push ", &arg
))
1359 strvec_push(&specs
, arg
);
1361 die(_("http transport does not support %s"), buf
->buf
);
1364 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1370 ret
= push(specs
.nr
, specs
.v
);
1375 exit(128); /* error already reported */
1378 strvec_clear(&specs
);
1381 static int stateless_connect(const char *service_name
)
1383 struct discovery
*discover
;
1384 struct rpc_state rpc
;
1385 struct strbuf buf
= STRBUF_INIT
;
1388 * Run the info/refs request and see if the server supports protocol
1389 * v2. If and only if the server supports v2 can we successfully
1390 * establish a stateless connection, otherwise we need to tell the
1391 * client to fallback to using other transport helper functions to
1392 * complete their request.
1394 discover
= discover_refs(service_name
, 0);
1395 if (discover
->version
!= protocol_v2
) {
1396 printf("fallback\n");
1400 /* Stateless Connection established */
1405 rpc
.service_name
= service_name
;
1406 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1407 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1408 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1409 if (get_protocol_http_header(discover
->version
, &buf
)) {
1410 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1412 rpc
.protocol_header
= NULL
;
1413 strbuf_release(&buf
);
1415 rpc
.buf
= xmalloc(http_post_buffer
);
1416 rpc
.alloc
= http_post_buffer
;
1421 rpc
.any_written
= 0;
1422 rpc
.gzip_request
= 1;
1423 rpc
.initial_buffer
= 0;
1424 rpc
.write_line_lengths
= 1;
1425 rpc
.flush_read_but_not_sent
= 0;
1428 * Dump the capability listing that we got from the server earlier
1429 * during the info/refs request.
1431 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1433 /* Until we see EOF keep sending POSTs */
1436 enum packet_read_status status
;
1438 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1440 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1441 if (status
== PACKET_READ_EOF
)
1443 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1444 /* We would have an err here */
1446 /* Reset the buffer for next request */
1450 free(rpc
.service_url
);
1451 free(rpc
.hdr_content_type
);
1452 free(rpc
.hdr_accept
);
1453 free(rpc
.protocol_header
);
1455 strbuf_release(&buf
);
1460 int cmd_main(int argc
, const char **argv
)
1462 struct strbuf buf
= STRBUF_INIT
;
1465 setup_git_directory_gently(&nongit
);
1467 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1471 options
.verbosity
= 1;
1472 options
.progress
= !!isatty(2);
1474 string_list_init(&options
.deepen_not
, 1);
1475 string_list_init(&options
.push_options
, 1);
1478 * Just report "remote-curl" here (folding all the various aliases
1479 * ("git-remote-http", "git-remote-https", and etc.) here since they
1480 * are all just copies of the same actual executable.
1482 trace2_cmd_name("remote-curl");
1484 remote
= remote_get(argv
[1]);
1487 end_url_with_slash(&url
, argv
[2]);
1489 end_url_with_slash(&url
, remote
->url
[0]);
1492 http_init(remote
, url
.buf
, 0);
1497 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1499 error(_("remote-curl: error reading command stream from git"));
1504 if (starts_with(buf
.buf
, "fetch ")) {
1506 die(_("remote-curl: fetch attempted without a local repo"));
1509 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1510 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1511 output_refs(get_refs(for_push
));
1513 } else if (starts_with(buf
.buf
, "push ")) {
1516 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1517 char *value
= strchr(arg
, ' ');
1525 result
= set_option(arg
, value
);
1528 else if (result
< 0)
1529 printf("error invalid value\n");
1531 printf("unsupported\n");
1534 } else if (!strcmp(buf
.buf
, "capabilities")) {
1535 printf("stateless-connect\n");
1539 printf("check-connectivity\n");
1540 printf("object-format\n");
1543 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1544 if (!stateless_connect(arg
))
1547 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);