1
0

http.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #ifndef HEADER_CURL_HTTP_H
  2. #define HEADER_CURL_HTTP_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include "bufq.h"
  28. #include "dynhds.h"
  29. #include "ws.h"
  30. typedef enum {
  31. HTTPREQ_GET,
  32. HTTPREQ_POST,
  33. HTTPREQ_POST_FORM, /* we make a difference internally */
  34. HTTPREQ_POST_MIME, /* we make a difference internally */
  35. HTTPREQ_PUT,
  36. HTTPREQ_HEAD
  37. } Curl_HttpReq;
  38. /* When redirecting transfers. */
  39. typedef enum {
  40. FOLLOW_NONE, /* not used within the function, just a placeholder to
  41. allow initing to this */
  42. FOLLOW_FAKE, /* only records stuff, not actually following */
  43. FOLLOW_RETRY, /* set if this is a request retry as opposed to a real
  44. redirect following */
  45. FOLLOW_REDIR /* a full true redirect */
  46. } followtype;
  47. #define CURL_HTTP_V1x (1 << 0)
  48. #define CURL_HTTP_V2x (1 << 1)
  49. #define CURL_HTTP_V3x (1 << 2)
  50. /* bitmask of CURL_HTTP_V* values */
  51. typedef unsigned char http_majors;
  52. #ifndef CURL_DISABLE_HTTP
  53. #ifdef USE_HTTP3
  54. #include <stdint.h>
  55. #endif
  56. extern const struct Curl_handler Curl_handler_http;
  57. #ifdef USE_SSL
  58. extern const struct Curl_handler Curl_handler_https;
  59. #endif
  60. struct dynhds;
  61. struct http_negotiation {
  62. unsigned char rcvd_min; /* minimum version seen in responses, 09, 10, 11 */
  63. http_majors wanted; /* wanted major versions when talking to server */
  64. http_majors allowed; /* allowed major versions when talking to server */
  65. BIT(h2_upgrade); /* Do HTTP Upgrade from 1.1 to 2 */
  66. BIT(h2_prior_knowledge); /* Directly do HTTP/2 without ALPN/SSL */
  67. BIT(accept_09); /* Accept an HTTP/0.9 response */
  68. BIT(only_10); /* When using major version 1x, use only 1.0 */
  69. };
  70. void Curl_http_neg_init(struct Curl_easy *data, struct http_negotiation *neg);
  71. CURLcode Curl_bump_headersize(struct Curl_easy *data,
  72. size_t delta,
  73. bool connect_only);
  74. /* Header specific functions */
  75. bool Curl_compareheader(const char *headerline, /* line to check */
  76. const char *header, /* header keyword _with_ colon */
  77. const size_t hlen, /* len of the keyword in bytes */
  78. const char *content, /* content string to find */
  79. const size_t clen); /* len of the content in bytes */
  80. char *Curl_copy_header_value(const char *header);
  81. char *Curl_checkProxyheaders(struct Curl_easy *data,
  82. const struct connectdata *conn,
  83. const char *thisheader,
  84. const size_t thislen);
  85. CURLcode Curl_add_timecondition(struct Curl_easy *data, struct dynbuf *req);
  86. CURLcode Curl_add_custom_headers(struct Curl_easy *data, bool is_connect,
  87. int httpversion, struct dynbuf *req);
  88. CURLcode Curl_dynhds_add_custom(struct Curl_easy *data, bool is_connect,
  89. struct dynhds *hds);
  90. void Curl_http_method(struct Curl_easy *data,
  91. const char **method, Curl_HttpReq *);
  92. /* protocol-specific functions set up to be called by the main engine */
  93. CURLcode Curl_http_setup_conn(struct Curl_easy *data,
  94. struct connectdata *conn);
  95. CURLcode Curl_http(struct Curl_easy *data, bool *done);
  96. CURLcode Curl_http_done(struct Curl_easy *data, CURLcode, bool premature);
  97. CURLcode Curl_http_connect(struct Curl_easy *data, bool *done);
  98. CURLcode Curl_http_do_pollset(struct Curl_easy *data,
  99. struct easy_pollset *ps);
  100. CURLcode Curl_http_write_resp(struct Curl_easy *data,
  101. const char *buf, size_t blen,
  102. bool is_eos);
  103. CURLcode Curl_http_write_resp_hd(struct Curl_easy *data,
  104. const char *hd, size_t hdlen,
  105. bool is_eos);
  106. /* These functions are in http.c */
  107. CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
  108. const char *auth);
  109. CURLcode Curl_http_auth_act(struct Curl_easy *data);
  110. /* follow a redirect or not */
  111. CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
  112. followtype type);
  113. /* If only the PICKNONE bit is set, there has been a round-trip and we
  114. selected to use no auth at all. Ie, we actively select no auth, as opposed
  115. to not having one selected. The other CURLAUTH_* defines are present in the
  116. public curl/curl.h header. */
  117. #define CURLAUTH_PICKNONE (1<<30) /* do not use auth */
  118. /* MAX_INITIAL_POST_SIZE indicates the number of bytes that will make the POST
  119. data get included in the initial data chunk sent to the server. If the
  120. data is larger than this, it will automatically get split up in multiple
  121. system calls.
  122. This value used to be fairly big (100K), but we must take into account that
  123. if the server rejects the POST due for authentication reasons, this data
  124. will always be unconditionally sent and thus it may not be larger than can
  125. always be afforded to send twice.
  126. It must not be greater than 64K to work on VMS.
  127. */
  128. #ifndef MAX_INITIAL_POST_SIZE
  129. #define MAX_INITIAL_POST_SIZE (64*1024)
  130. #endif
  131. /* EXPECT_100_THRESHOLD is the request body size limit for when libcurl will
  132. * automatically add an "Expect: 100-continue" header in HTTP requests. When
  133. * the size is unknown, it will always add it.
  134. *
  135. */
  136. #ifndef EXPECT_100_THRESHOLD
  137. #define EXPECT_100_THRESHOLD (1024*1024)
  138. #endif
  139. /* MAX_HTTP_RESP_HEADER_SIZE is the maximum size of all response headers
  140. combined that libcurl allows for a single HTTP response, any HTTP
  141. version. This count includes CONNECT response headers. */
  142. #define MAX_HTTP_RESP_HEADER_SIZE (300*1024)
  143. /* MAX_HTTP_RESP_HEADER_COUNT is the maximum number of response headers that
  144. libcurl allows for a single HTTP response, including CONNECT and
  145. redirects. */
  146. #define MAX_HTTP_RESP_HEADER_COUNT 5000
  147. #endif /* CURL_DISABLE_HTTP */
  148. /****************************************************************************
  149. * HTTP unique setup
  150. ***************************************************************************/
  151. CURLcode Curl_http_write_resp_hds(struct Curl_easy *data,
  152. const char *buf, size_t blen,
  153. size_t *pconsumed);
  154. /**
  155. * Curl_http_output_auth() setups the authentication headers for the
  156. * host/proxy and the correct authentication
  157. * method. data->state.authdone is set to TRUE when authentication is
  158. * done.
  159. *
  160. * @param data all information about the current transfer
  161. * @param conn all information about the current connection
  162. * @param request pointer to the request keyword
  163. * @param httpreq is the request type
  164. * @param path pointer to the requested path
  165. * @param proxytunnel boolean if this is the request setting up a "proxy
  166. * tunnel"
  167. *
  168. * @returns CURLcode
  169. */
  170. CURLcode
  171. Curl_http_output_auth(struct Curl_easy *data,
  172. struct connectdata *conn,
  173. const char *request,
  174. Curl_HttpReq httpreq,
  175. const char *path,
  176. bool proxytunnel); /* TRUE if this is the request setting
  177. up the proxy tunnel */
  178. /* Decode HTTP status code string. */
  179. CURLcode Curl_http_decode_status(int *pstatus, const char *s, size_t len);
  180. /**
  181. * All about a core HTTP request, excluding body and trailers
  182. */
  183. struct httpreq {
  184. struct dynhds headers;
  185. struct dynhds trailers;
  186. char *scheme;
  187. char *authority;
  188. char *path;
  189. char method[1];
  190. };
  191. /**
  192. * Create an HTTP request struct.
  193. */
  194. CURLcode Curl_http_req_make(struct httpreq **preq,
  195. const char *method, size_t m_len,
  196. const char *scheme, size_t s_len,
  197. const char *authority, size_t a_len,
  198. const char *path, size_t p_len);
  199. CURLcode Curl_http_req_make2(struct httpreq **preq,
  200. const char *method, size_t m_len,
  201. CURLU *url, const char *scheme_default);
  202. void Curl_http_req_free(struct httpreq *req);
  203. #define HTTP_PSEUDO_METHOD ":method"
  204. #define HTTP_PSEUDO_SCHEME ":scheme"
  205. #define HTTP_PSEUDO_AUTHORITY ":authority"
  206. #define HTTP_PSEUDO_PATH ":path"
  207. #define HTTP_PSEUDO_STATUS ":status"
  208. /**
  209. * Create the list of HTTP/2 headers which represent the request,
  210. * using HTTP/2 pseudo headers preceding the `req->headers`.
  211. *
  212. * Applies the following transformations:
  213. * - if `authority` is set, any "Host" header is removed.
  214. * - if `authority` is unset and a "Host" header is present, use
  215. * that as `authority` and remove "Host"
  216. * - removes and Connection header fields as defined in rfc9113 ch. 8.2.2
  217. * - lower-cases the header field names
  218. *
  219. * @param h2_headers will contain the HTTP/2 headers on success
  220. * @param req the request to transform
  221. * @param data the handle to lookup defaults like ' :scheme' from
  222. */
  223. CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
  224. struct httpreq *req, struct Curl_easy *data);
  225. /**
  226. * All about a core HTTP response, excluding body and trailers
  227. */
  228. struct http_resp {
  229. int status;
  230. char *description;
  231. struct dynhds headers;
  232. struct dynhds trailers;
  233. struct http_resp *prev;
  234. };
  235. /**
  236. * Create an HTTP response struct.
  237. */
  238. CURLcode Curl_http_resp_make(struct http_resp **presp,
  239. int status,
  240. const char *description);
  241. void Curl_http_resp_free(struct http_resp *resp);
  242. #endif /* HEADER_CURL_HTTP_H */