Merge branch 'sg/subtree-signed-commits' into pu
[git] / http.h
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 #if LIBCURL_VERSION_NUM < 0x071304
14 #error "your libcurl version is too old; Git requires curl >= 7.19.4"
15 #endif
16
17 #define DEFAULT_MAX_REQUESTS 5
18
19 struct slot_results {
20         CURLcode curl_result;
21         long http_code;
22         long auth_avail;
23         long http_connectcode;
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 extern size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *strbuf);
45 extern size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *strbuf);
46 extern size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf);
47 extern curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp);
48
49 /* Slot lifecycle functions */
50 extern struct active_request_slot *get_active_slot(void);
51 extern int start_active_slot(struct active_request_slot *slot);
52 extern void run_active_slot(struct active_request_slot *slot);
53 extern 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 extern void fill_active_slots(void);
65 extern void add_fill_function(void *data, int (*fill)(void *));
66 extern void step_active_slots(void);
67
68 extern void http_init(struct remote *remote, const char *url,
69                       int proactive_auth);
70 extern void http_cleanup(void);
71 extern 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 extern char curl_errorstr[CURL_ERROR_SIZE];
80
81 enum http_follow_config {
82         HTTP_FOLLOW_NONE,
83         HTTP_FOLLOW_ALWAYS,
84         HTTP_FOLLOW_INITIAL
85 };
86 extern enum http_follow_config http_follow_config;
87
88 static inline int missing__target(int code, int result)
89 {
90         return  /* file:// URL -- do we ever use one??? */
91                 (result == CURLE_FILE_COULDNT_READ_FILE) ||
92                 /* http:// and https:// URL */
93                 (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
94                 /* ftp:// URL */
95                 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
96                 ;
97 }
98
99 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
100
101 /* Helpers for modifying and creating URLs */
102 extern void append_remote_object_url(struct strbuf *buf, const char *url,
103                                      const char *hex,
104                                      int only_two_digit_prefix);
105 extern char *get_remote_object_url(const char *url, const char *hex,
106                                    int only_two_digit_prefix);
107
108 /* Options for http_get_*() */
109 struct http_get_options {
110         unsigned no_cache:1,
111                  keep_error:1,
112                  initial_request:1;
113
114         /* If non-NULL, returns the content-type of the response. */
115         struct strbuf *content_type;
116
117         /*
118          * If non-NULL, and content_type above is non-NULL, returns
119          * the charset parameter from the content-type. If none is
120          * present, returns an empty string.
121          */
122         struct strbuf *charset;
123
124         /*
125          * If non-NULL, returns the URL we ended up at, including any
126          * redirects we followed.
127          */
128         struct strbuf *effective_url;
129
130         /*
131          * If both base_url and effective_url are non-NULL, the base URL will
132          * be munged to reflect any redirections going from the requested url
133          * to effective_url. See the definition of update_url_from_redirect
134          * for details.
135          */
136         struct strbuf *base_url;
137
138         struct string_list *extra_headers;
139 };
140
141 /* Return values for http_get_*() */
142 #define HTTP_OK                 0
143 #define HTTP_MISSING_TARGET     1
144 #define HTTP_ERROR              2
145 #define HTTP_START_FAILED       3
146 #define HTTP_REAUTH     4
147 #define HTTP_NOAUTH     5
148
149 /*
150  * Requests a URL and stores the result in a strbuf.
151  *
152  * If the result pointer is NULL, a HTTP HEAD request is made instead of GET.
153  */
154 int http_get_strbuf(const char *url, struct strbuf *result, struct http_get_options *options);
155
156 extern int http_fetch_ref(const char *base, struct ref *ref);
157
158 /* Helpers for fetching packs */
159 extern int http_get_info_packs(const char *base_url,
160         struct packed_git **packs_head);
161
162 struct http_pack_request {
163         char *url;
164         struct packed_git *target;
165         struct packed_git **lst;
166         FILE *packfile;
167         char tmpfile[PATH_MAX];
168         struct active_request_slot *slot;
169 };
170
171 extern struct http_pack_request *new_http_pack_request(
172         struct packed_git *target, const char *base_url);
173 extern int finish_http_pack_request(struct http_pack_request *preq);
174 extern void release_http_pack_request(struct http_pack_request *preq);
175
176 /* Helpers for fetching object */
177 struct http_object_request {
178         char *url;
179         char tmpfile[PATH_MAX];
180         int localfile;
181         CURLcode curl_result;
182         char errorstr[CURL_ERROR_SIZE];
183         long http_code;
184         unsigned char sha1[20];
185         unsigned char real_sha1[20];
186         git_SHA_CTX c;
187         git_zstream stream;
188         int zret;
189         int rename;
190         struct active_request_slot *slot;
191 };
192
193 extern struct http_object_request *new_http_object_request(
194         const char *base_url, unsigned char *sha1);
195 extern void process_http_object_request(struct http_object_request *freq);
196 extern int finish_http_object_request(struct http_object_request *freq);
197 extern void abort_http_object_request(struct http_object_request *freq);
198 extern void release_http_object_request(struct http_object_request *freq);
199
200 /* setup routine for curl_easy_setopt CURLOPT_DEBUGFUNCTION */
201 void setup_curl_trace(CURL *handle);
202 #endif /* HTTP_H */