http.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, <daniel@haxx.se>, 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. #if defined(USE_MSH3) && !defined(_WIN32)
  28. #include <pthread.h>
  29. #endif
  30. #include "bufq.h"
  31. #include "dynhds.h"
  32. #include "ws.h"
  33. typedef enum {
  34. HTTPREQ_GET,
  35. HTTPREQ_POST,
  36. HTTPREQ_POST_FORM, /* we make a difference internally */
  37. HTTPREQ_POST_MIME, /* we make a difference internally */
  38. HTTPREQ_PUT,
  39. HTTPREQ_HEAD
  40. } Curl_HttpReq;
  41. #ifndef CURL_DISABLE_HTTP
  42. #if defined(USE_HTTP3)
  43. #include <stdint.h>
  44. #endif
  45. extern const struct Curl_handler Curl_handler_http;
  46. #ifdef USE_SSL
  47. extern const struct Curl_handler Curl_handler_https;
  48. #endif
  49. struct dynhds;
  50. CURLcode Curl_bump_headersize(struct Curl_easy *data,
  51. size_t delta,
  52. bool connect_only);
  53. /* Header specific functions */
  54. bool Curl_compareheader(const char *headerline, /* line to check */
  55. const char *header, /* header keyword _with_ colon */
  56. const size_t hlen, /* len of the keyword in bytes */
  57. const char *content, /* content string to find */
  58. const size_t clen); /* len of the content in bytes */
  59. char *Curl_copy_header_value(const char *header);
  60. char *Curl_checkProxyheaders(struct Curl_easy *data,
  61. const struct connectdata *conn,
  62. const char *thisheader,
  63. const size_t thislen);
  64. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  65. #ifndef USE_HYPER
  66. struct dynbuf *req
  67. #else
  68. void *headers
  69. #endif
  70. );
  71. CURLcode Curl_add_custom_headers(struct Curl_easy *data,
  72. bool is_connect,
  73. #ifndef USE_HYPER
  74. struct dynbuf *req
  75. #else
  76. void *headers
  77. #endif
  78. );
  79. void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
  80. const char **method, Curl_HttpReq *);
  81. CURLcode Curl_http_useragent(struct Curl_easy *data);
  82. CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn);
  83. CURLcode Curl_http_target(struct Curl_easy *data, struct connectdata *conn,
  84. struct dynbuf *req);
  85. CURLcode Curl_http_statusline(struct Curl_easy *data,
  86. struct connectdata *conn);
  87. CURLcode Curl_http_header(struct Curl_easy *data,
  88. const char *hd, size_t hdlen);
  89. CURLcode Curl_transferencode(struct Curl_easy *data);
  90. CURLcode Curl_http_req_set_reader(struct Curl_easy *data,
  91. Curl_HttpReq httpreq,
  92. const char **tep);
  93. CURLcode Curl_http_req_complete(struct Curl_easy *data,
  94. struct dynbuf *r, Curl_HttpReq httpreq);
  95. bool Curl_use_http_1_1plus(const struct Curl_easy *data,
  96. const struct connectdata *conn);
  97. #ifndef CURL_DISABLE_COOKIES
  98. CURLcode Curl_http_cookies(struct Curl_easy *data,
  99. struct connectdata *conn,
  100. struct dynbuf *r);
  101. #else
  102. #define Curl_http_cookies(a,b,c) CURLE_OK
  103. #endif
  104. CURLcode Curl_http_range(struct Curl_easy *data,
  105. Curl_HttpReq httpreq);
  106. CURLcode Curl_http_firstwrite(struct Curl_easy *data);
  107. /* protocol-specific functions set up to be called by the main engine */
  108. CURLcode Curl_http_setup_conn(struct Curl_easy *data,
  109. struct connectdata *conn);
  110. CURLcode Curl_http(struct Curl_easy *data, bool *done);
  111. CURLcode Curl_http_done(struct Curl_easy *data, CURLcode, bool premature);
  112. CURLcode Curl_http_connect(struct Curl_easy *data, bool *done);
  113. int Curl_http_getsock_do(struct Curl_easy *data, struct connectdata *conn,
  114. curl_socket_t *socks);
  115. CURLcode Curl_http_write_resp(struct Curl_easy *data,
  116. const char *buf, size_t blen,
  117. bool is_eos);
  118. CURLcode Curl_http_write_resp_hd(struct Curl_easy *data,
  119. const char *hd, size_t hdlen,
  120. bool is_eos);
  121. /* These functions are in http.c */
  122. CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
  123. const char *auth);
  124. CURLcode Curl_http_auth_act(struct Curl_easy *data);
  125. /* If only the PICKNONE bit is set, there has been a round-trip and we
  126. selected to use no auth at all. Ie, we actively select no auth, as opposed
  127. to not having one selected. The other CURLAUTH_* defines are present in the
  128. public curl/curl.h header. */
  129. #define CURLAUTH_PICKNONE (1<<30) /* do not use auth */
  130. /* MAX_INITIAL_POST_SIZE indicates the number of bytes that will make the POST
  131. data get included in the initial data chunk sent to the server. If the
  132. data is larger than this, it will automatically get split up in multiple
  133. system calls.
  134. This value used to be fairly big (100K), but we must take into account that
  135. if the server rejects the POST due for authentication reasons, this data
  136. will always be unconditionally sent and thus it may not be larger than can
  137. always be afforded to send twice.
  138. It must not be greater than 64K to work on VMS.
  139. */
  140. #ifndef MAX_INITIAL_POST_SIZE
  141. #define MAX_INITIAL_POST_SIZE (64*1024)
  142. #endif
  143. /* EXPECT_100_THRESHOLD is the request body size limit for when libcurl will
  144. * automatically add an "Expect: 100-continue" header in HTTP requests. When
  145. * the size is unknown, it will always add it.
  146. *
  147. */
  148. #ifndef EXPECT_100_THRESHOLD
  149. #define EXPECT_100_THRESHOLD (1024*1024)
  150. #endif
  151. /* MAX_HTTP_RESP_HEADER_SIZE is the maximum size of all response headers
  152. combined that libcurl allows for a single HTTP response, any HTTP
  153. version. This count includes CONNECT response headers. */
  154. #define MAX_HTTP_RESP_HEADER_SIZE (300*1024)
  155. bool Curl_http_exp100_is_selected(struct Curl_easy *data);
  156. void Curl_http_exp100_got100(struct Curl_easy *data);
  157. #endif /* CURL_DISABLE_HTTP */
  158. /****************************************************************************
  159. * HTTP unique setup
  160. ***************************************************************************/
  161. CURLcode Curl_http_size(struct Curl_easy *data);
  162. CURLcode Curl_http_write_resp_hds(struct Curl_easy *data,
  163. const char *buf, size_t blen,
  164. size_t *pconsumed);
  165. /**
  166. * Curl_http_output_auth() setups the authentication headers for the
  167. * host/proxy and the correct authentication
  168. * method. data->state.authdone is set to TRUE when authentication is
  169. * done.
  170. *
  171. * @param data all information about the current transfer
  172. * @param conn all information about the current connection
  173. * @param request pointer to the request keyword
  174. * @param httpreq is the request type
  175. * @param path pointer to the requested path
  176. * @param proxytunnel boolean if this is the request setting up a "proxy
  177. * tunnel"
  178. *
  179. * @returns CURLcode
  180. */
  181. CURLcode
  182. Curl_http_output_auth(struct Curl_easy *data,
  183. struct connectdata *conn,
  184. const char *request,
  185. Curl_HttpReq httpreq,
  186. const char *path,
  187. bool proxytunnel); /* TRUE if this is the request setting
  188. up the proxy tunnel */
  189. /* Decode HTTP status code string. */
  190. CURLcode Curl_http_decode_status(int *pstatus, const char *s, size_t len);
  191. /**
  192. * All about a core HTTP request, excluding body and trailers
  193. */
  194. struct httpreq {
  195. char method[24];
  196. char *scheme;
  197. char *authority;
  198. char *path;
  199. struct dynhds headers;
  200. struct dynhds trailers;
  201. };
  202. /**
  203. * Create an HTTP request struct.
  204. */
  205. CURLcode Curl_http_req_make(struct httpreq **preq,
  206. const char *method, size_t m_len,
  207. const char *scheme, size_t s_len,
  208. const char *authority, size_t a_len,
  209. const char *path, size_t p_len);
  210. CURLcode Curl_http_req_make2(struct httpreq **preq,
  211. const char *method, size_t m_len,
  212. CURLU *url, const char *scheme_default);
  213. void Curl_http_req_free(struct httpreq *req);
  214. #define HTTP_PSEUDO_METHOD ":method"
  215. #define HTTP_PSEUDO_SCHEME ":scheme"
  216. #define HTTP_PSEUDO_AUTHORITY ":authority"
  217. #define HTTP_PSEUDO_PATH ":path"
  218. #define HTTP_PSEUDO_STATUS ":status"
  219. /**
  220. * Create the list of HTTP/2 headers which represent the request,
  221. * using HTTP/2 pseudo headers preceding the `req->headers`.
  222. *
  223. * Applies the following transformations:
  224. * - if `authority` is set, any "Host" header is removed.
  225. * - if `authority` is unset and a "Host" header is present, use
  226. * that as `authority` and remove "Host"
  227. * - removes and Connection header fields as defined in rfc9113 ch. 8.2.2
  228. * - lower-cases the header field names
  229. *
  230. * @param h2_headers will contain the HTTP/2 headers on success
  231. * @param req the request to transform
  232. * @param data the handle to lookup defaults like ' :scheme' from
  233. */
  234. CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
  235. struct httpreq *req, struct Curl_easy *data);
  236. /**
  237. * All about a core HTTP response, excluding body and trailers
  238. */
  239. struct http_resp {
  240. int status;
  241. char *description;
  242. struct dynhds headers;
  243. struct dynhds trailers;
  244. struct http_resp *prev;
  245. };
  246. /**
  247. * Create an HTTP response struct.
  248. */
  249. CURLcode Curl_http_resp_make(struct http_resp **presp,
  250. int status,
  251. const char *description);
  252. void Curl_http_resp_free(struct http_resp *resp);
  253. #endif /* HEADER_CURL_HTTP_H */