THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.1KB

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