2 #include "git-curl-compat.h"
10 #include "run-command.h"
12 #include "string-list.h"
15 #include "credential.h"
16 #include "oid-array.h"
17 #include "send-pack.h"
20 #include "transport.h"
22 static struct remote
*remote
;
23 /* always ends with a trailing slash */
24 static struct strbuf url
= STRBUF_INIT
;
30 struct string_list deepen_not
;
31 struct string_list push_options
;
33 unsigned progress
: 1,
34 check_self_contained_and_connected
: 1,
40 /* One of the SEND_PACK_PUSH_CERT_* constants. */
44 /* see documentation of corresponding flag in fetch-pack.h */
50 force_if_includes
: 1;
51 const struct git_hash_algo
*hash_algo
;
53 static struct options options
;
54 static struct string_list cas_options
= STRING_LIST_INIT_DUP
;
56 static int set_option(const char *name
, const char *value
)
58 if (!strcmp(name
, "verbosity")) {
60 int v
= strtol(value
, &end
, 10);
61 if (value
== end
|| *end
)
63 options
.verbosity
= v
;
66 else if (!strcmp(name
, "progress")) {
67 if (!strcmp(value
, "true"))
69 else if (!strcmp(value
, "false"))
75 else if (!strcmp(name
, "depth")) {
77 unsigned long v
= strtoul(value
, &end
, 10);
78 if (value
== end
|| *end
)
83 else if (!strcmp(name
, "deepen-since")) {
84 options
.deepen_since
= xstrdup(value
);
87 else if (!strcmp(name
, "deepen-not")) {
88 string_list_append(&options
.deepen_not
, value
);
91 else if (!strcmp(name
, "deepen-relative")) {
92 if (!strcmp(value
, "true"))
93 options
.deepen_relative
= 1;
94 else if (!strcmp(value
, "false"))
95 options
.deepen_relative
= 0;
100 else if (!strcmp(name
, "followtags")) {
101 if (!strcmp(value
, "true"))
102 options
.followtags
= 1;
103 else if (!strcmp(value
, "false"))
104 options
.followtags
= 0;
109 else if (!strcmp(name
, "dry-run")) {
110 if (!strcmp(value
, "true"))
112 else if (!strcmp(value
, "false"))
118 else if (!strcmp(name
, "check-connectivity")) {
119 if (!strcmp(value
, "true"))
120 options
.check_self_contained_and_connected
= 1;
121 else if (!strcmp(value
, "false"))
122 options
.check_self_contained_and_connected
= 0;
127 else if (!strcmp(name
, "cas")) {
128 struct strbuf val
= STRBUF_INIT
;
129 strbuf_addstr(&val
, "--force-with-lease=");
131 strbuf_addstr(&val
, value
);
132 else if (unquote_c_style(&val
, value
, NULL
))
134 string_list_append(&cas_options
, val
.buf
);
135 strbuf_release(&val
);
137 } else if (!strcmp(name
, TRANS_OPT_FORCE_IF_INCLUDES
)) {
138 if (!strcmp(value
, "true"))
139 options
.force_if_includes
= 1;
140 else if (!strcmp(value
, "false"))
141 options
.force_if_includes
= 0;
145 } else if (!strcmp(name
, "cloning")) {
146 if (!strcmp(value
, "true"))
148 else if (!strcmp(value
, "false"))
153 } else if (!strcmp(name
, "update-shallow")) {
154 if (!strcmp(value
, "true"))
155 options
.update_shallow
= 1;
156 else if (!strcmp(value
, "false"))
157 options
.update_shallow
= 0;
161 } else if (!strcmp(name
, "pushcert")) {
162 if (!strcmp(value
, "true"))
163 options
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
164 else if (!strcmp(value
, "false"))
165 options
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
166 else if (!strcmp(value
, "if-asked"))
167 options
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
171 } else if (!strcmp(name
, "atomic")) {
172 if (!strcmp(value
, "true"))
174 else if (!strcmp(value
, "false"))
179 } else if (!strcmp(name
, "push-option")) {
181 string_list_append(&options
.push_options
, value
);
183 struct strbuf unquoted
= STRBUF_INIT
;
184 if (unquote_c_style(&unquoted
, value
, NULL
) < 0)
185 die(_("invalid quoting in push-option value: '%s'"), value
);
186 string_list_append_nodup(&options
.push_options
,
187 strbuf_detach(&unquoted
, NULL
));
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 } else if (!strcmp(name
, "from-promisor")) {
201 options
.from_promisor
= 1;
203 } else if (!strcmp(name
, "refetch")) {
206 } else if (!strcmp(name
, "filter")) {
207 options
.filter
= xstrdup(value
);
209 } else if (!strcmp(name
, "object-format")) {
211 options
.object_format
= 1;
212 if (strcmp(value
, "true")) {
213 algo
= hash_algo_by_name(value
);
214 if (algo
== GIT_HASH_UNKNOWN
)
215 die("unknown object format '%s'", value
);
216 options
.hash_algo
= &hash_algos
[algo
];
220 return 1 /* unsupported */;
230 struct oid_array shallow
;
231 enum protocol_version version
;
232 unsigned proto_git
: 1;
234 static struct discovery
*last_discovery
;
236 static struct ref
*parse_git_refs(struct discovery
*heads
, int for_push
)
238 struct ref
*list
= NULL
;
239 struct packet_reader reader
;
241 packet_reader_init(&reader
, -1, heads
->buf
, heads
->len
,
242 PACKET_READ_CHOMP_NEWLINE
|
243 PACKET_READ_GENTLE_ON_EOF
|
244 PACKET_READ_DIE_ON_ERR_PACKET
);
246 heads
->version
= discover_version(&reader
);
247 switch (heads
->version
) {
250 * Do nothing. This isn't a list of refs but rather a
251 * capability advertisement. Client would have run
252 * 'stateless-connect' so we'll dump this capability listing
253 * and let them request the refs themselves.
258 get_remote_heads(&reader
, &list
, for_push
? REF_NORMAL
: 0,
259 NULL
, &heads
->shallow
);
260 options
.hash_algo
= reader
.hash_algo
;
262 case protocol_unknown_version
:
263 BUG("unknown protocol version");
269 static const struct git_hash_algo
*detect_hash_algo(struct discovery
*heads
)
271 const char *p
= memchr(heads
->buf
, '\t', heads
->len
);
274 return the_hash_algo
;
276 algo
= hash_algo_by_length((p
- heads
->buf
) / 2);
277 if (algo
== GIT_HASH_UNKNOWN
)
279 return &hash_algos
[algo
];
282 static struct ref
*parse_info_refs(struct discovery
*heads
)
284 char *data
, *start
, *mid
;
288 struct ref
*refs
= NULL
;
289 struct ref
*ref
= NULL
;
290 struct ref
*last_ref
= NULL
;
292 options
.hash_algo
= detect_hash_algo(heads
);
293 if (!options
.hash_algo
)
294 die("%sinfo/refs not valid: could not determine hash algorithm; "
295 "is this a git repository?",
296 transport_anonymize_url(url
.buf
));
301 while (i
< heads
->len
) {
307 if (data
[i
] == '\n') {
308 if (mid
- start
!= options
.hash_algo
->hexsz
)
309 die(_("%sinfo/refs not valid: is this a git repository?"),
310 transport_anonymize_url(url
.buf
));
313 ref
= alloc_ref(ref_name
);
314 get_oid_hex_algop(start
, &ref
->old_oid
, options
.hash_algo
);
318 last_ref
->next
= ref
;
325 ref
= alloc_ref("HEAD");
326 if (!http_fetch_ref(url
.buf
, ref
) &&
327 !resolve_remote_symref(ref
, refs
)) {
337 static void free_discovery(struct discovery
*d
)
340 if (d
== last_discovery
)
341 last_discovery
= NULL
;
342 free(d
->shallow
.oid
);
350 static int show_http_message(struct strbuf
*type
, struct strbuf
*charset
,
356 * We only show text/plain parts, as other types are likely
357 * to be ugly to look at on the user's terminal.
359 if (strcmp(type
->buf
, "text/plain"))
362 strbuf_reencode(msg
, charset
->buf
, get_log_output_encoding());
370 eol
= strchrnul(p
, '\n');
371 fprintf(stderr
, "remote: %.*s\n", (int)(eol
- p
), p
);
377 static int get_protocol_http_header(enum protocol_version version
,
378 struct strbuf
*header
)
381 strbuf_addf(header
, GIT_PROTOCOL_HEADER
": version=%d",
390 static void check_smart_http(struct discovery
*d
, const char *service
,
394 struct packet_reader reader
;
397 * If we don't see x-$service-advertisement, then it's not smart-http.
398 * But once we do, we commit to it and assume any other protocol
399 * violations are hard errors.
401 if (!skip_prefix(type
->buf
, "application/x-", &p
) ||
402 !skip_prefix(p
, service
, &p
) ||
403 strcmp(p
, "-advertisement"))
406 packet_reader_init(&reader
, -1, d
->buf
, d
->len
,
407 PACKET_READ_CHOMP_NEWLINE
|
408 PACKET_READ_DIE_ON_ERR_PACKET
);
409 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
410 die(_("invalid server response; expected service, got flush packet"));
412 if (skip_prefix(reader
.line
, "# service=", &p
) && !strcmp(p
, service
)) {
414 * The header can include additional metadata lines, up
415 * until a packet flush marker. Ignore these now, but
416 * in the future we might start to scan them.
419 packet_reader_read(&reader
);
420 if (reader
.pktlen
<= 0) {
426 * v0 smart http; callers expect us to soak up the
427 * service and header packets
429 d
->buf
= reader
.src_buffer
;
430 d
->len
= reader
.src_len
;
433 } else if (!strcmp(reader
.line
, "version 2")) {
435 * v2 smart http; do not consume version packet, which will
436 * be handled elsewhere.
441 die(_("invalid server response; got '%s'"), reader
.line
);
445 static struct discovery
*discover_refs(const char *service
, int for_push
)
447 struct strbuf type
= STRBUF_INIT
;
448 struct strbuf charset
= STRBUF_INIT
;
449 struct strbuf buffer
= STRBUF_INIT
;
450 struct strbuf refs_url
= STRBUF_INIT
;
451 struct strbuf effective_url
= STRBUF_INIT
;
452 struct strbuf protocol_header
= STRBUF_INIT
;
453 struct string_list extra_headers
= STRING_LIST_INIT_DUP
;
454 struct discovery
*last
= last_discovery
;
455 int http_ret
, maybe_smart
= 0;
456 struct http_get_options http_options
;
457 enum protocol_version version
= get_protocol_version_config();
459 if (last
&& !strcmp(service
, last
->service
))
461 free_discovery(last
);
463 strbuf_addf(&refs_url
, "%sinfo/refs", url
.buf
);
464 if ((starts_with(url
.buf
, "http://") || starts_with(url
.buf
, "https://")) &&
465 git_env_bool("GIT_SMART_HTTP", 1)) {
467 if (!strchr(url
.buf
, '?'))
468 strbuf_addch(&refs_url
, '?');
470 strbuf_addch(&refs_url
, '&');
471 strbuf_addf(&refs_url
, "service=%s", service
);
475 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
476 * to perform a push, then fallback to v0 since the client doesn't know
477 * how to push yet using v2.
479 if (version
== protocol_v2
&& !strcmp("git-receive-pack", service
))
480 version
= protocol_v0
;
482 /* Add the extra Git-Protocol header */
483 if (get_protocol_http_header(version
, &protocol_header
))
484 string_list_append(&extra_headers
, protocol_header
.buf
);
486 memset(&http_options
, 0, sizeof(http_options
));
487 http_options
.content_type
= &type
;
488 http_options
.charset
= &charset
;
489 http_options
.effective_url
= &effective_url
;
490 http_options
.base_url
= &url
;
491 http_options
.extra_headers
= &extra_headers
;
492 http_options
.initial_request
= 1;
493 http_options
.no_cache
= 1;
495 http_ret
= http_get_strbuf(refs_url
.buf
, &buffer
, &http_options
);
499 case HTTP_MISSING_TARGET
:
500 show_http_message(&type
, &charset
, &buffer
);
501 die(_("repository '%s' not found"),
502 transport_anonymize_url(url
.buf
));
504 show_http_message(&type
, &charset
, &buffer
);
505 die(_("Authentication failed for '%s'"),
506 transport_anonymize_url(url
.buf
));
507 case HTTP_NOMATCHPUBLICKEY
:
508 show_http_message(&type
, &charset
, &buffer
);
509 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
510 transport_anonymize_url(url
.buf
), curl_errorstr
);
512 show_http_message(&type
, &charset
, &buffer
);
513 die(_("unable to access '%s': %s"),
514 transport_anonymize_url(url
.buf
), curl_errorstr
);
517 if (options
.verbosity
&& !starts_with(refs_url
.buf
, url
.buf
)) {
518 char *u
= transport_anonymize_url(url
.buf
);
519 warning(_("redirecting to %s"), u
);
523 last
= xcalloc(1, sizeof(*last_discovery
));
524 last
->service
= xstrdup(service
);
525 last
->buf_alloc
= strbuf_detach(&buffer
, &last
->len
);
526 last
->buf
= last
->buf_alloc
;
529 check_smart_http(last
, service
, &type
);
532 last
->refs
= parse_git_refs(last
, for_push
);
534 last
->refs
= parse_info_refs(last
);
536 strbuf_release(&refs_url
);
537 strbuf_release(&type
);
538 strbuf_release(&charset
);
539 strbuf_release(&effective_url
);
540 strbuf_release(&buffer
);
541 strbuf_release(&protocol_header
);
542 string_list_clear(&extra_headers
, 0);
543 last_discovery
= last
;
547 static struct ref
*get_refs(int for_push
)
549 struct discovery
*heads
;
552 heads
= discover_refs("git-receive-pack", for_push
);
554 heads
= discover_refs("git-upload-pack", for_push
);
559 static void output_refs(struct ref
*refs
)
562 if (options
.object_format
&& options
.hash_algo
) {
563 printf(":object-format %s\n", options
.hash_algo
->name
);
564 repo_set_hash_algo(the_repository
,
565 hash_algo_by_ptr(options
.hash_algo
));
567 for (posn
= refs
; posn
; posn
= posn
->next
) {
569 printf("@%s %s\n", posn
->symref
, posn
->name
);
571 printf("%s %s\n", hash_to_hex_algop(posn
->old_oid
.hash
,
580 const char *service_name
;
582 char *hdr_content_type
;
584 char *hdr_accept_language
;
585 char *protocol_header
;
593 unsigned gzip_request
: 1;
594 unsigned initial_buffer
: 1;
597 * Whenever a pkt-line is read into buf, append the 4 characters
598 * denoting its length before appending the payload.
600 unsigned write_line_lengths
: 1;
603 * Used by rpc_out; initialize to 0. This is true if a flush has been
604 * read, but the corresponding line length (if write_line_lengths is
605 * true) and EOF have not been sent to libcurl. Since each flush marks
606 * the end of a request, each flush must be completely sent before any
607 * further reading occurs.
609 unsigned flush_read_but_not_sent
: 1;
612 #define RPC_STATE_INIT { 0 }
615 * Appends the result of reading from rpc->out to the string represented by
616 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
617 * enough space, 0 otherwise.
619 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
620 * hexadecimal string before appending the result described above.
622 * Writes the total number of bytes appended into appended.
624 static int rpc_read_from_out(struct rpc_state
*rpc
, int options
,
626 enum packet_read_status
*status
) {
631 if (rpc
->write_line_lengths
) {
632 left
= rpc
->alloc
- rpc
->len
- 4;
633 buf
= rpc
->buf
+ rpc
->len
+ 4;
635 left
= rpc
->alloc
- rpc
->len
;
636 buf
= rpc
->buf
+ rpc
->len
;
639 if (left
< LARGE_PACKET_MAX
)
642 *status
= packet_read_with_status(rpc
->out
, NULL
, NULL
, buf
,
643 left
, &pktlen_raw
, options
);
644 if (*status
!= PACKET_READ_EOF
) {
645 *appended
= pktlen_raw
+ (rpc
->write_line_lengths
? 4 : 0);
646 rpc
->len
+= *appended
;
649 if (rpc
->write_line_lengths
) {
651 case PACKET_READ_EOF
:
652 if (!(options
& PACKET_READ_GENTLE_ON_EOF
))
653 die(_("shouldn't have EOF when not gentle on EOF"));
655 case PACKET_READ_NORMAL
:
656 set_packet_header(buf
- 4, *appended
);
658 case PACKET_READ_DELIM
:
659 memcpy(buf
- 4, "0001", 4);
661 case PACKET_READ_FLUSH
:
662 memcpy(buf
- 4, "0000", 4);
664 case PACKET_READ_RESPONSE_END
:
665 die(_("remote server sent unexpected response end packet"));
672 static size_t rpc_out(void *ptr
, size_t eltsize
,
673 size_t nmemb
, void *buffer_
)
675 size_t max
= eltsize
* nmemb
;
676 struct rpc_state
*rpc
= buffer_
;
677 size_t avail
= rpc
->len
- rpc
->pos
;
678 enum packet_read_status status
;
681 rpc
->initial_buffer
= 0;
684 if (!rpc
->flush_read_but_not_sent
) {
685 if (!rpc_read_from_out(rpc
, 0, &avail
, &status
))
686 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
687 if (status
== PACKET_READ_FLUSH
)
688 rpc
->flush_read_but_not_sent
= 1;
691 * If flush_read_but_not_sent is true, we have already read one
692 * full request but have not fully sent it + EOF, which is why
693 * we need to refrain from reading.
696 if (rpc
->flush_read_but_not_sent
) {
699 * The line length either does not need to be sent at
700 * all or has already been completely sent. Now we can
701 * return 0, indicating EOF, meaning that the flush has
704 rpc
->flush_read_but_not_sent
= 0;
708 * If avail is non-zero, the line length for the flush still
709 * hasn't been fully sent. Proceed with sending the line
716 memcpy(ptr
, rpc
->buf
+ rpc
->pos
, avail
);
721 static int rpc_seek(void *clientp
, curl_off_t offset
, int origin
)
723 struct rpc_state
*rpc
= clientp
;
725 if (origin
!= SEEK_SET
)
726 BUG("rpc_seek only handles SEEK_SET, not %d", origin
);
728 if (rpc
->initial_buffer
) {
729 if (offset
< 0 || offset
> rpc
->len
) {
730 error("curl seek would be outside of rpc buffer");
731 return CURL_SEEKFUNC_FAIL
;
734 return CURL_SEEKFUNC_OK
;
736 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
737 return CURL_SEEKFUNC_FAIL
;
740 struct check_pktline_state
{
746 static void check_pktline(struct check_pktline_state
*state
, const char *ptr
, size_t size
)
749 if (!state
->remaining
) {
750 int digits_remaining
= 4 - state
->len_filled
;
751 if (digits_remaining
> size
)
752 digits_remaining
= size
;
753 memcpy(&state
->len_buf
[state
->len_filled
], ptr
, digits_remaining
);
754 state
->len_filled
+= digits_remaining
;
755 ptr
+= digits_remaining
;
756 size
-= digits_remaining
;
758 if (state
->len_filled
== 4) {
759 state
->remaining
= packet_length(state
->len_buf
);
760 if (state
->remaining
< 0) {
761 die(_("remote-curl: bad line length character: %.4s"), state
->len_buf
);
762 } else if (state
->remaining
== 2) {
763 die(_("remote-curl: unexpected response end packet"));
764 } else if (state
->remaining
< 4) {
765 state
->remaining
= 0;
767 state
->remaining
-= 4;
769 state
->len_filled
= 0;
773 if (state
->remaining
) {
774 int remaining
= state
->remaining
;
775 if (remaining
> size
)
779 state
->remaining
-= remaining
;
785 struct rpc_state
*rpc
;
786 struct active_request_slot
*slot
;
788 struct check_pktline_state pktline_state
;
792 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
795 static size_t rpc_in(char *ptr
, size_t eltsize
,
796 size_t nmemb
, void *buffer_
)
798 size_t size
= eltsize
* nmemb
;
799 struct rpc_in_data
*data
= buffer_
;
802 if (curl_easy_getinfo(data
->slot
->curl
, CURLINFO_RESPONSE_CODE
,
803 &response_code
) != CURLE_OK
)
805 if (response_code
>= 300)
808 data
->rpc
->any_written
= 1;
809 if (data
->check_pktline
)
810 check_pktline(&data
->pktline_state
, ptr
, size
);
811 write_or_die(data
->rpc
->in
, ptr
, size
);
815 static int run_slot(struct active_request_slot
*slot
,
816 struct slot_results
*results
)
819 struct slot_results results_buf
;
822 results
= &results_buf
;
824 err
= run_one_slot(slot
, results
);
826 if (err
!= HTTP_OK
&& err
!= HTTP_REAUTH
) {
827 struct strbuf msg
= STRBUF_INIT
;
828 if (results
->http_code
&& results
->http_code
!= 200)
829 strbuf_addf(&msg
, "HTTP %ld", results
->http_code
);
830 if (results
->curl_result
!= CURLE_OK
) {
832 strbuf_addch(&msg
, ' ');
833 strbuf_addf(&msg
, "curl %d", results
->curl_result
);
834 if (curl_errorstr
[0]) {
835 strbuf_addch(&msg
, ' ');
836 strbuf_addstr(&msg
, curl_errorstr
);
839 error(_("RPC failed; %s"), msg
.buf
);
840 strbuf_release(&msg
);
846 static int probe_rpc(struct rpc_state
*rpc
, struct slot_results
*results
)
848 struct active_request_slot
*slot
;
849 struct curl_slist
*headers
= http_copy_default_headers();
850 struct strbuf buf
= STRBUF_INIT
;
853 slot
= get_active_slot();
855 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
856 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
858 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
859 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
860 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
861 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, NULL
);
862 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, "0000");
863 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, 4);
864 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
865 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
866 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &buf
);
868 err
= run_slot(slot
, results
);
870 curl_slist_free_all(headers
);
871 strbuf_release(&buf
);
875 static curl_off_t
xcurl_off_t(size_t len
)
877 uintmax_t size
= len
;
878 if (size
> maximum_signed_value_of_type(curl_off_t
))
879 die(_("cannot handle pushes this big"));
880 return (curl_off_t
)size
;
884 * If flush_received is true, do not attempt to read any more; just use what's
887 static int post_rpc(struct rpc_state
*rpc
, int stateless_connect
, int flush_received
)
889 struct active_request_slot
*slot
;
890 struct curl_slist
*headers
= http_copy_default_headers();
891 int use_gzip
= rpc
->gzip_request
;
892 char *gzip_body
= NULL
;
893 size_t gzip_size
= 0;
894 int err
, large_request
= 0;
895 int needs_100_continue
= 0;
896 struct rpc_in_data rpc_in_data
;
898 /* Try to load the entire request, if we can fit it into the
899 * allocated buffer space we can use HTTP/1.0 and avoid the
900 * chunked encoding mess.
902 if (!flush_received
) {
905 enum packet_read_status status
;
907 if (!rpc_read_from_out(rpc
, 0, &n
, &status
)) {
912 if (status
== PACKET_READ_FLUSH
)
918 struct slot_results results
;
921 err
= probe_rpc(rpc
, &results
);
922 if (err
== HTTP_REAUTH
)
923 credential_fill(&http_auth
);
924 } while (err
== HTTP_REAUTH
);
928 if (results
.auth_avail
& CURLAUTH_GSSNEGOTIATE
)
929 needs_100_continue
= 1;
932 headers
= curl_slist_append(headers
, rpc
->hdr_content_type
);
933 headers
= curl_slist_append(headers
, rpc
->hdr_accept
);
934 headers
= curl_slist_append(headers
, needs_100_continue
?
935 "Expect: 100-continue" : "Expect:");
937 /* Add Accept-Language header */
938 if (rpc
->hdr_accept_language
)
939 headers
= curl_slist_append(headers
, rpc
->hdr_accept_language
);
941 /* Add the extra Git-Protocol header */
942 if (rpc
->protocol_header
)
943 headers
= curl_slist_append(headers
, rpc
->protocol_header
);
946 slot
= get_active_slot();
948 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
949 curl_easy_setopt(slot
->curl
, CURLOPT_POST
, 1);
950 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, rpc
->service_url
);
951 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
954 /* The request body is large and the size cannot be predicted.
955 * We must use chunked encoding to send it.
957 #ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
958 headers
= curl_slist_append(headers
, "Transfer-Encoding: chunked");
960 rpc
->initial_buffer
= 1;
961 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, rpc_out
);
962 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, rpc
);
963 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKFUNCTION
, rpc_seek
);
964 curl_easy_setopt(slot
->curl
, CURLOPT_SEEKDATA
, rpc
);
965 if (options
.verbosity
> 1) {
966 fprintf(stderr
, "POST %s (chunked)\n", rpc
->service_name
);
970 } else if (gzip_body
) {
972 * If we are looping to retry authentication, then the previous
973 * run will have set up the headers and gzip buffer already,
974 * and we just need to send it.
976 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
977 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
979 } else if (use_gzip
&& 1024 < rpc
->len
) {
980 /* The client backend isn't giving us compressed data so
981 * we can try to deflate it ourselves, this may save on
987 git_deflate_init_gzip(&stream
, Z_BEST_COMPRESSION
);
988 gzip_size
= git_deflate_bound(&stream
, rpc
->len
);
989 gzip_body
= xmalloc(gzip_size
);
991 stream
.next_in
= (unsigned char *)rpc
->buf
;
992 stream
.avail_in
= rpc
->len
;
993 stream
.next_out
= (unsigned char *)gzip_body
;
994 stream
.avail_out
= gzip_size
;
996 ret
= git_deflate(&stream
, Z_FINISH
);
997 if (ret
!= Z_STREAM_END
)
998 die(_("cannot deflate request; zlib deflate error %d"), ret
);
1000 ret
= git_deflate_end_gently(&stream
);
1002 die(_("cannot deflate request; zlib end error %d"), ret
);
1004 gzip_size
= stream
.total_out
;
1006 headers
= curl_slist_append(headers
, "Content-Encoding: gzip");
1007 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, gzip_body
);
1008 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(gzip_size
));
1010 if (options
.verbosity
> 1) {
1011 fprintf(stderr
, "POST %s (gzip %lu to %lu bytes)\n",
1013 (unsigned long)rpc
->len
, (unsigned long)gzip_size
);
1017 /* We know the complete request size in advance, use the
1018 * more normal Content-Length approach.
1020 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, rpc
->buf
);
1021 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE_LARGE
, xcurl_off_t(rpc
->len
));
1022 if (options
.verbosity
> 1) {
1023 fprintf(stderr
, "POST %s (%lu bytes)\n",
1024 rpc
->service_name
, (unsigned long)rpc
->len
);
1029 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1030 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, rpc_in
);
1031 rpc_in_data
.rpc
= rpc
;
1032 rpc_in_data
.slot
= slot
;
1033 rpc_in_data
.check_pktline
= stateless_connect
;
1034 memset(&rpc_in_data
.pktline_state
, 0, sizeof(rpc_in_data
.pktline_state
));
1035 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &rpc_in_data
);
1036 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1039 rpc
->any_written
= 0;
1040 err
= run_slot(slot
, NULL
);
1041 if (err
== HTTP_REAUTH
&& !large_request
) {
1042 credential_fill(&http_auth
);
1048 if (!rpc
->any_written
)
1051 if (rpc_in_data
.pktline_state
.len_filled
)
1052 err
= error(_("%d bytes of length header were received"), rpc_in_data
.pktline_state
.len_filled
);
1053 if (rpc_in_data
.pktline_state
.remaining
)
1054 err
= error(_("%d bytes of body are still expected"), rpc_in_data
.pktline_state
.remaining
);
1056 if (stateless_connect
)
1057 packet_response_end(rpc
->in
);
1059 curl_slist_free_all(headers
);
1064 static int rpc_service(struct rpc_state
*rpc
, struct discovery
*heads
,
1065 const char **client_argv
, const struct strbuf
*preamble
,
1066 struct strbuf
*rpc_result
)
1068 const char *svc
= rpc
->service_name
;
1069 struct strbuf buf
= STRBUF_INIT
;
1070 struct child_process client
= CHILD_PROCESS_INIT
;
1076 strvec_pushv(&client
.args
, client_argv
);
1077 if (start_command(&client
))
1079 write_or_die(client
.in
, preamble
->buf
, preamble
->len
);
1081 write_or_die(client
.in
, heads
->buf
, heads
->len
);
1083 rpc
->alloc
= http_post_buffer
;
1084 rpc
->buf
= xmalloc(rpc
->alloc
);
1085 rpc
->in
= client
.in
;
1086 rpc
->out
= client
.out
;
1088 strbuf_addf(&buf
, "%s%s", url
.buf
, svc
);
1089 rpc
->service_url
= strbuf_detach(&buf
, NULL
);
1091 rpc
->hdr_accept_language
= xstrdup_or_null(http_get_accept_language_header());
1093 strbuf_addf(&buf
, "Content-Type: application/x-%s-request", svc
);
1094 rpc
->hdr_content_type
= strbuf_detach(&buf
, NULL
);
1096 strbuf_addf(&buf
, "Accept: application/x-%s-result", svc
);
1097 rpc
->hdr_accept
= strbuf_detach(&buf
, NULL
);
1099 if (get_protocol_http_header(heads
->version
, &buf
))
1100 rpc
->protocol_header
= strbuf_detach(&buf
, NULL
);
1102 rpc
->protocol_header
= NULL
;
1105 int n
= packet_read(rpc
->out
, rpc
->buf
, rpc
->alloc
, 0);
1110 err
|= post_rpc(rpc
, 0, 0);
1116 strbuf_read(rpc_result
, client
.out
, 0);
1120 if (xread(client
.out
, buf
, sizeof(buf
)) <= 0)
1127 err
|= finish_command(&client
);
1128 free(rpc
->service_url
);
1129 free(rpc
->hdr_content_type
);
1130 free(rpc
->hdr_accept
);
1131 free(rpc
->hdr_accept_language
);
1132 free(rpc
->protocol_header
);
1134 strbuf_release(&buf
);
1138 static int fetch_dumb(int nr_heads
, struct ref
**to_fetch
)
1140 struct walker
*walker
;
1144 ALLOC_ARRAY(targets
, nr_heads
);
1145 if (options
.depth
|| options
.deepen_since
)
1146 die(_("dumb http transport does not support shallow capabilities"));
1147 for (i
= 0; i
< nr_heads
; i
++)
1148 targets
[i
] = xstrdup(oid_to_hex(&to_fetch
[i
]->old_oid
));
1150 walker
= get_http_walker(url
.buf
);
1151 walker
->get_verbosely
= options
.verbosity
>= 3;
1152 walker
->get_progress
= options
.progress
;
1153 walker
->get_recover
= 0;
1154 ret
= walker_fetch(walker
, nr_heads
, targets
, NULL
, NULL
);
1155 walker_free(walker
);
1157 for (i
= 0; i
< nr_heads
; i
++)
1161 return ret
? error(_("fetch failed.")) : 0;
1164 static int fetch_git(struct discovery
*heads
,
1165 int nr_heads
, struct ref
**to_fetch
)
1167 struct rpc_state rpc
= RPC_STATE_INIT
;
1168 struct strbuf preamble
= STRBUF_INIT
;
1170 struct strvec args
= STRVEC_INIT
;
1171 struct strbuf rpc_result
= STRBUF_INIT
;
1173 strvec_pushl(&args
, "fetch-pack", "--stateless-rpc",
1174 "--stdin", "--lock-pack", NULL
);
1175 if (options
.followtags
)
1176 strvec_push(&args
, "--include-tag");
1178 strvec_push(&args
, "--thin");
1179 if (options
.verbosity
>= 3)
1180 strvec_pushl(&args
, "-v", "-v", NULL
);
1181 if (options
.check_self_contained_and_connected
)
1182 strvec_push(&args
, "--check-self-contained-and-connected");
1183 if (options
.cloning
)
1184 strvec_push(&args
, "--cloning");
1185 if (options
.update_shallow
)
1186 strvec_push(&args
, "--update-shallow");
1187 if (!options
.progress
)
1188 strvec_push(&args
, "--no-progress");
1190 strvec_pushf(&args
, "--depth=%lu", options
.depth
);
1191 if (options
.deepen_since
)
1192 strvec_pushf(&args
, "--shallow-since=%s", options
.deepen_since
);
1193 for (i
= 0; i
< options
.deepen_not
.nr
; i
++)
1194 strvec_pushf(&args
, "--shallow-exclude=%s",
1195 options
.deepen_not
.items
[i
].string
);
1196 if (options
.deepen_relative
&& options
.depth
)
1197 strvec_push(&args
, "--deepen-relative");
1198 if (options
.from_promisor
)
1199 strvec_push(&args
, "--from-promisor");
1200 if (options
.refetch
)
1201 strvec_push(&args
, "--refetch");
1203 strvec_pushf(&args
, "--filter=%s", options
.filter
);
1204 strvec_push(&args
, url
.buf
);
1206 for (i
= 0; i
< nr_heads
; i
++) {
1207 struct ref
*ref
= to_fetch
[i
];
1209 die(_("cannot fetch by sha1 over smart http"));
1210 packet_buf_write(&preamble
, "%s %s\n",
1211 oid_to_hex(&ref
->old_oid
), ref
->name
);
1213 packet_buf_flush(&preamble
);
1215 memset(&rpc
, 0, sizeof(rpc
));
1216 rpc
.service_name
= "git-upload-pack",
1217 rpc
.gzip_request
= 1;
1219 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1221 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1222 strbuf_release(&rpc_result
);
1223 strbuf_release(&preamble
);
1224 strvec_clear(&args
);
1228 static int fetch(int nr_heads
, struct ref
**to_fetch
)
1230 struct discovery
*d
= discover_refs("git-upload-pack", 0);
1232 return fetch_git(d
, nr_heads
, to_fetch
);
1234 return fetch_dumb(nr_heads
, to_fetch
);
1237 static void parse_fetch(struct strbuf
*buf
)
1239 struct ref
**to_fetch
= NULL
;
1240 struct ref
*list_head
= NULL
;
1241 struct ref
**list
= &list_head
;
1242 int alloc_heads
= 0, nr_heads
= 0;
1246 if (skip_prefix(buf
->buf
, "fetch ", &p
)) {
1249 struct object_id old_oid
;
1252 if (parse_oid_hex(p
, &old_oid
, &q
))
1253 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1259 die(_("protocol error: expected sha/ref, got '%s'"), p
);
1261 ref
= alloc_ref(name
);
1262 oidcpy(&ref
->old_oid
, &old_oid
);
1267 ALLOC_GROW(to_fetch
, nr_heads
+ 1, alloc_heads
);
1268 to_fetch
[nr_heads
++] = ref
;
1271 die(_("http transport does not support %s"), buf
->buf
);
1274 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1280 if (fetch(nr_heads
, to_fetch
))
1281 exit(128); /* error already reported */
1282 free_refs(list_head
);
1290 static void parse_get(const char *arg
)
1292 struct strbuf url
= STRBUF_INIT
;
1293 struct strbuf path
= STRBUF_INIT
;
1296 space
= strchr(arg
, ' ');
1299 die(_("protocol error: expected '<url> <path>', missing space"));
1301 strbuf_add(&url
, arg
, space
- arg
);
1302 strbuf_addstr(&path
, space
+ 1);
1304 if (http_get_file(url
.buf
, path
.buf
, NULL
))
1305 die(_("failed to download file at URL '%s'"), url
.buf
);
1307 strbuf_release(&url
);
1308 strbuf_release(&path
);
1313 static int push_dav(int nr_spec
, const char **specs
)
1315 struct child_process child
= CHILD_PROCESS_INIT
;
1319 strvec_push(&child
.args
, "http-push");
1320 strvec_push(&child
.args
, "--helper-status");
1321 if (options
.dry_run
)
1322 strvec_push(&child
.args
, "--dry-run");
1323 if (options
.verbosity
> 1)
1324 strvec_push(&child
.args
, "--verbose");
1325 strvec_push(&child
.args
, url
.buf
);
1326 for (i
= 0; i
< nr_spec
; i
++)
1327 strvec_push(&child
.args
, specs
[i
]);
1329 if (run_command(&child
))
1330 die(_("git-http-push failed"));
1334 static int push_git(struct discovery
*heads
, int nr_spec
, const char **specs
)
1336 struct rpc_state rpc
= RPC_STATE_INIT
;
1339 struct string_list_item
*cas_option
;
1340 struct strbuf preamble
= STRBUF_INIT
;
1341 struct strbuf rpc_result
= STRBUF_INIT
;
1344 strvec_pushl(&args
, "send-pack", "--stateless-rpc", "--helper-status",
1348 strvec_push(&args
, "--thin");
1349 if (options
.dry_run
)
1350 strvec_push(&args
, "--dry-run");
1351 if (options
.push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
)
1352 strvec_push(&args
, "--signed=yes");
1353 else if (options
.push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
)
1354 strvec_push(&args
, "--signed=if-asked");
1356 strvec_push(&args
, "--atomic");
1357 if (options
.verbosity
== 0)
1358 strvec_push(&args
, "--quiet");
1359 else if (options
.verbosity
> 1)
1360 strvec_push(&args
, "--verbose");
1361 for (i
= 0; i
< options
.push_options
.nr
; i
++)
1362 strvec_pushf(&args
, "--push-option=%s",
1363 options
.push_options
.items
[i
].string
);
1364 strvec_push(&args
, options
.progress
? "--progress" : "--no-progress");
1365 for_each_string_list_item(cas_option
, &cas_options
)
1366 strvec_push(&args
, cas_option
->string
);
1367 strvec_push(&args
, url
.buf
);
1369 if (options
.force_if_includes
)
1370 strvec_push(&args
, "--force-if-includes");
1372 strvec_push(&args
, "--stdin");
1373 for (i
= 0; i
< nr_spec
; i
++)
1374 packet_buf_write(&preamble
, "%s\n", specs
[i
]);
1375 packet_buf_flush(&preamble
);
1377 memset(&rpc
, 0, sizeof(rpc
));
1378 rpc
.service_name
= "git-receive-pack",
1380 err
= rpc_service(&rpc
, heads
, args
.v
, &preamble
, &rpc_result
);
1382 write_or_die(1, rpc_result
.buf
, rpc_result
.len
);
1383 strbuf_release(&rpc_result
);
1384 strbuf_release(&preamble
);
1385 strvec_clear(&args
);
1389 static int push(int nr_spec
, const char **specs
)
1391 struct discovery
*heads
= discover_refs("git-receive-pack", 1);
1394 if (heads
->proto_git
)
1395 ret
= push_git(heads
, nr_spec
, specs
);
1397 ret
= push_dav(nr_spec
, specs
);
1398 free_discovery(heads
);
1402 static void parse_push(struct strbuf
*buf
)
1404 struct strvec specs
= STRVEC_INIT
;
1409 if (skip_prefix(buf
->buf
, "push ", &arg
))
1410 strvec_push(&specs
, arg
);
1412 die(_("http transport does not support %s"), buf
->buf
);
1415 if (strbuf_getline_lf(buf
, stdin
) == EOF
)
1421 ret
= push(specs
.nr
, specs
.v
);
1426 exit(128); /* error already reported */
1429 strvec_clear(&specs
);
1432 static int stateless_connect(const char *service_name
)
1434 struct discovery
*discover
;
1435 struct rpc_state rpc
= RPC_STATE_INIT
;
1436 struct strbuf buf
= STRBUF_INIT
;
1437 const char *accept_language
;
1440 * Run the info/refs request and see if the server supports protocol
1441 * v2. If and only if the server supports v2 can we successfully
1442 * establish a stateless connection, otherwise we need to tell the
1443 * client to fallback to using other transport helper functions to
1444 * complete their request.
1446 discover
= discover_refs(service_name
, 0);
1447 if (discover
->version
!= protocol_v2
) {
1448 printf("fallback\n");
1452 /* Stateless Connection established */
1456 accept_language
= http_get_accept_language_header();
1457 if (accept_language
)
1458 rpc
.hdr_accept_language
= xstrfmt("%s", accept_language
);
1460 rpc
.service_name
= service_name
;
1461 rpc
.service_url
= xstrfmt("%s%s", url
.buf
, rpc
.service_name
);
1462 rpc
.hdr_content_type
= xstrfmt("Content-Type: application/x-%s-request", rpc
.service_name
);
1463 rpc
.hdr_accept
= xstrfmt("Accept: application/x-%s-result", rpc
.service_name
);
1464 if (get_protocol_http_header(discover
->version
, &buf
)) {
1465 rpc
.protocol_header
= strbuf_detach(&buf
, NULL
);
1467 rpc
.protocol_header
= NULL
;
1468 strbuf_release(&buf
);
1470 rpc
.buf
= xmalloc(http_post_buffer
);
1471 rpc
.alloc
= http_post_buffer
;
1476 rpc
.any_written
= 0;
1477 rpc
.gzip_request
= 1;
1478 rpc
.initial_buffer
= 0;
1479 rpc
.write_line_lengths
= 1;
1480 rpc
.flush_read_but_not_sent
= 0;
1483 * Dump the capability listing that we got from the server earlier
1484 * during the info/refs request.
1486 write_or_die(rpc
.in
, discover
->buf
, discover
->len
);
1488 /* Until we see EOF keep sending POSTs */
1491 enum packet_read_status status
;
1493 if (!rpc_read_from_out(&rpc
, PACKET_READ_GENTLE_ON_EOF
, &avail
,
1495 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1496 if (status
== PACKET_READ_EOF
)
1498 if (post_rpc(&rpc
, 1, status
== PACKET_READ_FLUSH
))
1499 /* We would have an err here */
1501 /* Reset the buffer for next request */
1505 free(rpc
.service_url
);
1506 free(rpc
.hdr_content_type
);
1507 free(rpc
.hdr_accept
);
1508 free(rpc
.hdr_accept_language
);
1509 free(rpc
.protocol_header
);
1511 strbuf_release(&buf
);
1516 int cmd_main(int argc
, const char **argv
)
1518 struct strbuf buf
= STRBUF_INIT
;
1522 setup_git_directory_gently(&nongit
);
1524 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1528 options
.verbosity
= 1;
1529 options
.progress
= !!isatty(2);
1531 string_list_init_dup(&options
.deepen_not
);
1532 string_list_init_dup(&options
.push_options
);
1535 * Just report "remote-curl" here (folding all the various aliases
1536 * ("git-remote-http", "git-remote-https", and etc.) here since they
1537 * are all just copies of the same actual executable.
1539 trace2_cmd_name("remote-curl");
1541 remote
= remote_get(argv
[1]);
1544 end_url_with_slash(&url
, argv
[2]);
1546 end_url_with_slash(&url
, remote
->url
[0]);
1549 http_init(remote
, url
.buf
, 0);
1554 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
1556 error(_("remote-curl: error reading command stream from git"));
1561 if (starts_with(buf
.buf
, "fetch ")) {
1563 die(_("remote-curl: fetch attempted without a local repo"));
1566 } else if (!strcmp(buf
.buf
, "list") || starts_with(buf
.buf
, "list ")) {
1567 int for_push
= !!strstr(buf
.buf
+ 4, "for-push");
1568 output_refs(get_refs(for_push
));
1570 } else if (starts_with(buf
.buf
, "push ")) {
1573 } else if (skip_prefix(buf
.buf
, "option ", &arg
)) {
1574 char *value
= strchr(arg
, ' ');
1582 result
= set_option(arg
, value
);
1585 else if (result
< 0)
1586 printf("error invalid value\n");
1588 printf("unsupported\n");
1591 } else if (skip_prefix(buf
.buf
, "get ", &arg
)) {
1595 } else if (!strcmp(buf
.buf
, "capabilities")) {
1596 printf("stateless-connect\n");
1601 printf("check-connectivity\n");
1602 printf("object-format\n");
1605 } else if (skip_prefix(buf
.buf
, "stateless-connect ", &arg
)) {
1606 if (!stateless_connect(arg
))
1609 error(_("remote-curl: unknown command '%s' from git"), buf
.buf
);
1618 strbuf_release(&buf
);