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