Raw
1 #ifndef HTTP_H
2 #define HTTP_H
3
4 struct packed_git;
5 struct packfile_list;
6
7 #include "git-zlib.h"
8
9 #include <curl/curl.h>
10 #include <curl/easy.h>
11
12 #include "gettext.h"
13 #include "strbuf.h"
14 #include "remote.h"
15
16 #define DEFAULT_MAX_REQUESTS 5
17
18 struct slot_results {
19 CURLcode curl_result;
20 long http_code;
21 long auth_avail;
22 long http_connectcode;
23 long retry_after;
24 };
25
26 struct active_request_slot {
27 CURL *curl;
28 int in_use;
29 CURLcode curl_result;
30 long http_code;
31 int *finished;
32 struct slot_results *results;
33 void *callback_data;
34 void (*callback_func)(void *data);
35 struct active_request_slot *next;
36 };
37
38 struct buffer {
39 struct strbuf buf;
40 size_t posn;
41 };
42
43 /* Curl request read/write callbacks */
44 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *strbuf);
45 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *strbuf);
46 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf);
47 int seek_buffer(void *clientp, curl_off_t offset, int origin);
48
49 /* Slot lifecycle functions */
50 struct active_request_slot *get_active_slot(void);
51 int start_active_slot(struct active_request_slot *slot);
52 void run_active_slot(struct active_request_slot *slot);
53 void finish_all_active_slots(void);
54
55 /*
56 * This will run one slot to completion in a blocking manner, similar to how
57 * curl_easy_perform would work (but we don't want to use that, because
58 * we do not want to intermingle calls to curl_multi and curl_easy).
59 *
60 */
61 int run_one_slot(struct active_request_slot *slot,
62 struct slot_results *results);
63
64 void fill_active_slots(void);
65 void add_fill_function(void *data, int (*fill)(void *));
66 void step_active_slots(void);
67
68 void http_init(struct remote *remote, const char *url,
69 int proactive_auth);
70 void http_cleanup(void);
71 struct curl_slist *http_copy_default_headers(void);
72
73 extern long int git_curl_ipresolve;
74 extern int active_requests;
75 extern int http_is_verbose;
76 extern ssize_t http_post_buffer;
77 extern struct credential http_auth;
78
79 /**
80 * Prepare for an HTTP re-authentication retry. This fills credentials
81 * via credential_fill() so the next request can include them.
82 */
83 void http_reauth_prepare(int all_capabilities);
84
85 extern char curl_errorstr[CURL_ERROR_SIZE];
86
87 enum http_follow_config {
88 HTTP_FOLLOW_NONE,
89 HTTP_FOLLOW_ALWAYS,
90 HTTP_FOLLOW_INITIAL
91 };
92 extern enum http_follow_config http_follow_config;
93
94 static inline int missing__target(int code, int result)
95 {
96 return /* file:// URL -- do we ever use one??? */
97 (result == CURLE_FILE_COULDNT_READ_FILE) ||
98 /* http:// and https:// URL */
99 (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
100 /* ftp:// URL */
101 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
102 ;
103 }
104
105 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
106
107 static inline curl_off_t cast_size_t_to_curl_off_t(size_t a)
108 {
109 uintmax_t size = a;
110 if (size > maximum_signed_value_of_type(curl_off_t))
111 die(_("number too large to represent as curl_off_t "
112 "on this platform: %"PRIuMAX), (uintmax_t)a);
113 return (curl_off_t)a;
114 }
115
116 /*
117 * Normalize curl results to handle CURL_FAILONERROR (or lack thereof). Failing
118 * http codes have their "result" converted to CURLE_HTTP_RETURNED_ERROR, and
119 * an appropriate string placed in the errorstr buffer (pass curl_errorstr if
120 * you don't have a custom buffer).
121 */
122 void normalize_curl_result(CURLcode *result, long http_code, char *errorstr,
123 size_t errorlen);
124
125 /* Helpers for modifying and creating URLs */
126 void append_remote_object_url(struct strbuf *buf, const char *url,
127 const char *hex,
128 int only_two_digit_prefix);
129 char *get_remote_object_url(const char *url, const char *hex,
130 int only_two_digit_prefix);
131
132 /* Options for http_get_*() */
133 struct http_get_options {
134 unsigned no_cache:1,
135 initial_request:1;
136
137 /* If non-NULL, returns the content-type of the response. */
138 struct strbuf *content_type;
139
140 /*
141 * If non-NULL, and content_type above is non-NULL, returns
142 * the charset parameter from the content-type. If none is
143 * present, returns an empty string.
144 */
145 struct strbuf *charset;
146
147 /*
148 * If non-NULL, returns the URL we ended up at, including any
149 * redirects we followed.
150 */
151 struct strbuf *effective_url;
152
153 /*
154 * If both base_url and effective_url are non-NULL, the base URL will
155 * be munged to reflect any redirections going from the requested url
156 * to effective_url. See the definition of update_url_from_redirect
157 * for details.
158 */
159 struct strbuf *base_url;
160
161 /*
162 * If not NULL, contains additional HTTP headers to be sent with the
163 * request. The strings in the list must not be freed until after the
164 * request has completed.
165 */
166 struct string_list *extra_headers;
167
168 /*
169 * After a request completes, contains the Retry-After delay in seconds
170 * if the server returned HTTP 429 with a Retry-After header (requires
171 * libcurl 7.66.0 or later), or -1 if no such header was present.
172 */
173 long retry_after;
174 };
175
176 /* Return values for http_get_*() */
177 #define HTTP_OK 0
178 #define HTTP_MISSING_TARGET 1
179 #define HTTP_ERROR 2
180 #define HTTP_START_FAILED 3
181 #define HTTP_REAUTH 4
182 #define HTTP_NOAUTH 5
183 #define HTTP_NOMATCHPUBLICKEY 6
184 #define HTTP_RATE_LIMITED 7
185
186 /*
187 * Requests a URL and stores the result in a strbuf.
188 *
189 * If the result pointer is NULL, a HTTP HEAD request is made instead of GET.
190 */
191 int http_get_strbuf(const char *url, struct strbuf *result, struct http_get_options *options);
192
193 /*
194 * Downloads a URL and stores the result in the given file.
195 *
196 * If a previous interrupted download is detected (i.e. a previous temporary
197 * file is still around) the download is resumed.
198 */
199 int http_get_file(const char *url, const char *filename,
200 struct http_get_options *options);
201
202 int http_fetch_ref(const char *base, struct ref *ref);
203
204 struct curl_slist *http_append_auth_header(const struct credential *c,
205 struct curl_slist *headers);
206
207 /* Helpers for fetching packs */
208 int http_get_info_packs(const char *base_url,
209 struct packfile_list *packs);
210
211 /* Helper for getting Accept-Language header */
212 const char *http_get_accept_language_header(void);
213
214 struct http_pack_request {
215 char *url;
216
217 /*
218 * index-pack command to run. Must be terminated by NULL.
219 *
220 * If NULL, defaults to {"index-pack", "--stdin", NULL}.
221 */
222 const char **index_pack_args;
223 unsigned preserve_index_pack_stdout : 1;
224
225 FILE *packfile;
226 struct strbuf tmpfile;
227 struct active_request_slot *slot;
228 struct curl_slist *headers;
229 };
230
231 struct http_pack_request *new_http_pack_request(
232 const unsigned char *packed_git_hash, const char *base_url);
233 struct http_pack_request *new_direct_http_pack_request(
234 const unsigned char *packed_git_hash, char *url);
235 int finish_http_pack_request(struct http_pack_request *preq);
236 void release_http_pack_request(struct http_pack_request *preq);
237
238 /*
239 * Remove p from the given list, and invoke packfile_store_add_pack() on it.
240 *
241 * This is a convenience function for users that have obtained a list of packs
242 * from http_get_info_packs() and have chosen a specific pack to fetch.
243 */
244 void http_install_packfile(struct packed_git *p,
245 struct packfile_list *list_to_remove_from);
246
247 /* Helpers for fetching object */
248 struct http_object_request {
249 char *url;
250 struct strbuf tmpfile;
251 int localfile;
252 CURLcode curl_result;
253 char errorstr[CURL_ERROR_SIZE];
254 long http_code;
255 struct object_id oid;
256 struct object_id real_oid;
257 struct git_hash_ctx c;
258 git_zstream stream;
259 int zret;
260 int rename;
261 struct active_request_slot *slot;
262 struct curl_slist *headers;
263 };
264
265 struct http_object_request *new_http_object_request(
266 const char *base_url, const struct object_id *oid);
267 void process_http_object_request(struct http_object_request *freq);
268 int finish_http_object_request(struct http_object_request *freq);
269 void abort_http_object_request(struct http_object_request **freq);
270 void release_http_object_request(struct http_object_request **freq);
271
272 /*
273 * Instead of using environment variables to determine if curl tracing happens,
274 * behave as if GIT_TRACE_CURL=1 and GIT_TRACE_CURL_NO_DATA=1 is set. Call this
275 * before calling setup_curl_trace().
276 */
277 void http_trace_curl_no_data(void);
278
279 /* setup routine for curl_easy_setopt CURLOPT_DEBUGFUNCTION */
280 void setup_curl_trace(CURL *handle);
281 #endif /* HTTP_H */