2
0

http.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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(ENABLE_QUIC)
  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. #ifdef USE_WEBSOCKETS
  50. extern const struct Curl_handler Curl_handler_ws;
  51. #ifdef USE_SSL
  52. extern const struct Curl_handler Curl_handler_wss;
  53. #endif
  54. #endif /* websockets */
  55. struct dynhds;
  56. /* Header specific functions */
  57. bool Curl_compareheader(const char *headerline, /* line to check */
  58. const char *header, /* header keyword _with_ colon */
  59. const size_t hlen, /* len of the keyword in bytes */
  60. const char *content, /* content string to find */
  61. const size_t clen); /* len of the content in bytes */
  62. char *Curl_copy_header_value(const char *header);
  63. char *Curl_checkProxyheaders(struct Curl_easy *data,
  64. const struct connectdata *conn,
  65. const char *thisheader,
  66. const size_t thislen);
  67. struct HTTP; /* see below */
  68. CURLcode Curl_buffer_send(struct dynbuf *in,
  69. struct Curl_easy *data,
  70. struct HTTP *http,
  71. curl_off_t *bytes_written,
  72. curl_off_t included_body_bytes,
  73. int socketindex);
  74. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  75. #ifndef USE_HYPER
  76. struct dynbuf *req
  77. #else
  78. void *headers
  79. #endif
  80. );
  81. CURLcode Curl_add_custom_headers(struct Curl_easy *data,
  82. bool is_connect,
  83. #ifndef USE_HYPER
  84. struct dynbuf *req
  85. #else
  86. void *headers
  87. #endif
  88. );
  89. CURLcode Curl_dynhds_add_custom(struct Curl_easy *data,
  90. bool is_connect,
  91. struct dynhds *hds);
  92. CURLcode Curl_http_compile_trailers(struct curl_slist *trailers,
  93. struct dynbuf *buf,
  94. struct Curl_easy *handle);
  95. void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
  96. const char **method, Curl_HttpReq *);
  97. CURLcode Curl_http_useragent(struct Curl_easy *data);
  98. CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn);
  99. CURLcode Curl_http_target(struct Curl_easy *data, struct connectdata *conn,
  100. struct dynbuf *req);
  101. CURLcode Curl_http_statusline(struct Curl_easy *data,
  102. struct connectdata *conn);
  103. CURLcode Curl_http_header(struct Curl_easy *data, struct connectdata *conn,
  104. char *headp);
  105. CURLcode Curl_transferencode(struct Curl_easy *data);
  106. CURLcode Curl_http_body(struct Curl_easy *data, struct connectdata *conn,
  107. Curl_HttpReq httpreq,
  108. const char **teep);
  109. CURLcode Curl_http_bodysend(struct Curl_easy *data, struct connectdata *conn,
  110. struct dynbuf *r, Curl_HttpReq httpreq);
  111. bool Curl_use_http_1_1plus(const struct Curl_easy *data,
  112. const struct connectdata *conn);
  113. #ifndef CURL_DISABLE_COOKIES
  114. CURLcode Curl_http_cookies(struct Curl_easy *data,
  115. struct connectdata *conn,
  116. struct dynbuf *r);
  117. #else
  118. #define Curl_http_cookies(a,b,c) CURLE_OK
  119. #endif
  120. CURLcode Curl_http_resume(struct Curl_easy *data,
  121. struct connectdata *conn,
  122. Curl_HttpReq httpreq);
  123. CURLcode Curl_http_range(struct Curl_easy *data,
  124. Curl_HttpReq httpreq);
  125. CURLcode Curl_http_firstwrite(struct Curl_easy *data,
  126. struct connectdata *conn,
  127. bool *done);
  128. /* protocol-specific functions set up to be called by the main engine */
  129. CURLcode Curl_http(struct Curl_easy *data, bool *done);
  130. CURLcode Curl_http_done(struct Curl_easy *data, CURLcode, bool premature);
  131. CURLcode Curl_http_connect(struct Curl_easy *data, bool *done);
  132. /* These functions are in http.c */
  133. CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
  134. const char *auth);
  135. CURLcode Curl_http_auth_act(struct Curl_easy *data);
  136. /* If only the PICKNONE bit is set, there has been a round-trip and we
  137. selected to use no auth at all. Ie, we actively select no auth, as opposed
  138. to not having one selected. The other CURLAUTH_* defines are present in the
  139. public curl/curl.h header. */
  140. #define CURLAUTH_PICKNONE (1<<30) /* don't use auth */
  141. /* MAX_INITIAL_POST_SIZE indicates the number of bytes that will make the POST
  142. data get included in the initial data chunk sent to the server. If the
  143. data is larger than this, it will automatically get split up in multiple
  144. system calls.
  145. This value used to be fairly big (100K), but we must take into account that
  146. if the server rejects the POST due for authentication reasons, this data
  147. will always be unconditionally sent and thus it may not be larger than can
  148. always be afforded to send twice.
  149. It must not be greater than 64K to work on VMS.
  150. */
  151. #ifndef MAX_INITIAL_POST_SIZE
  152. #define MAX_INITIAL_POST_SIZE (64*1024)
  153. #endif
  154. /* EXPECT_100_THRESHOLD is the request body size limit for when libcurl will
  155. * automatically add an "Expect: 100-continue" header in HTTP requests. When
  156. * the size is unknown, it will always add it.
  157. *
  158. */
  159. #ifndef EXPECT_100_THRESHOLD
  160. #define EXPECT_100_THRESHOLD (1024*1024)
  161. #endif
  162. #endif /* CURL_DISABLE_HTTP */
  163. /****************************************************************************
  164. * HTTP unique setup
  165. ***************************************************************************/
  166. struct HTTP {
  167. curl_mimepart *sendit;
  168. curl_off_t postsize; /* off_t to handle large file sizes */
  169. const char *postdata;
  170. const char *p_pragma; /* Pragma: string */
  171. /* For FORM posting */
  172. curl_mimepart form;
  173. struct back {
  174. curl_read_callback fread_func; /* backup storage for fread pointer */
  175. void *fread_in; /* backup storage for fread_in pointer */
  176. const char *postdata;
  177. curl_off_t postsize;
  178. struct Curl_easy *data;
  179. } backup;
  180. enum {
  181. HTTPSEND_NADA, /* init */
  182. HTTPSEND_REQUEST, /* sending a request */
  183. HTTPSEND_BODY /* sending body */
  184. } sending;
  185. #ifndef CURL_DISABLE_HTTP
  186. void *h2_ctx; /* HTTP/2 implementation context */
  187. void *h3_ctx; /* HTTP/3 implementation context */
  188. struct dynbuf send_buffer; /* used if the request couldn't be sent in one
  189. chunk, points to an allocated send_buffer
  190. struct */
  191. #endif
  192. };
  193. CURLcode Curl_http_size(struct Curl_easy *data);
  194. CURLcode Curl_http_readwrite_headers(struct Curl_easy *data,
  195. struct connectdata *conn,
  196. ssize_t *nread,
  197. bool *stop_reading);
  198. /**
  199. * Curl_http_output_auth() setups the authentication headers for the
  200. * host/proxy and the correct authentication
  201. * method. data->state.authdone is set to TRUE when authentication is
  202. * done.
  203. *
  204. * @param data all information about the current transfer
  205. * @param conn all information about the current connection
  206. * @param request pointer to the request keyword
  207. * @param httpreq is the request type
  208. * @param path pointer to the requested path
  209. * @param proxytunnel boolean if this is the request setting up a "proxy
  210. * tunnel"
  211. *
  212. * @returns CURLcode
  213. */
  214. CURLcode
  215. Curl_http_output_auth(struct Curl_easy *data,
  216. struct connectdata *conn,
  217. const char *request,
  218. Curl_HttpReq httpreq,
  219. const char *path,
  220. bool proxytunnel); /* TRUE if this is the request setting
  221. up the proxy tunnel */
  222. /* Decode HTTP status code string. */
  223. CURLcode Curl_http_decode_status(int *pstatus, const char *s, size_t len);
  224. /**
  225. * All about a core HTTP request, excluding body and trailers
  226. */
  227. struct httpreq {
  228. char method[12];
  229. char *scheme;
  230. char *authority;
  231. char *path;
  232. struct dynhds headers;
  233. struct dynhds trailers;
  234. };
  235. /**
  236. * Create a HTTP request struct.
  237. */
  238. CURLcode Curl_http_req_make(struct httpreq **preq,
  239. const char *method, size_t m_len,
  240. const char *scheme, size_t s_len,
  241. const char *authority, size_t a_len,
  242. const char *path, size_t p_len);
  243. CURLcode Curl_http_req_make2(struct httpreq **preq,
  244. const char *method, size_t m_len,
  245. CURLU *url, const char *scheme_default);
  246. void Curl_http_req_free(struct httpreq *req);
  247. #define HTTP_PSEUDO_METHOD ":method"
  248. #define HTTP_PSEUDO_SCHEME ":scheme"
  249. #define HTTP_PSEUDO_AUTHORITY ":authority"
  250. #define HTTP_PSEUDO_PATH ":path"
  251. #define HTTP_PSEUDO_STATUS ":status"
  252. /**
  253. * Create the list of HTTP/2 headers which represent the request,
  254. * using HTTP/2 pseudo headers preceeding the `req->headers`.
  255. *
  256. * Applies the following transformations:
  257. * - if `authority` is set, any "Host" header is removed.
  258. * - if `authority` is unset and a "Host" header is present, use
  259. * that as `authority` and remove "Host"
  260. * - removes and Connection header fields as defined in rfc9113 ch. 8.2.2
  261. * - lower-cases the header field names
  262. *
  263. * @param h2_headers will contain the HTTP/2 headers on success
  264. * @param req the request to transform
  265. * @param data the handle to lookup defaults like ' :scheme' from
  266. */
  267. CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
  268. struct httpreq *req, struct Curl_easy *data);
  269. /**
  270. * All about a core HTTP response, excluding body and trailers
  271. */
  272. struct http_resp {
  273. int status;
  274. char *description;
  275. struct dynhds headers;
  276. struct dynhds trailers;
  277. struct http_resp *prev;
  278. };
  279. /**
  280. * Create a HTTP response struct.
  281. */
  282. CURLcode Curl_http_resp_make(struct http_resp **presp,
  283. int status,
  284. const char *description);
  285. void Curl_http_resp_free(struct http_resp *resp);
  286. #endif /* HEADER_CURL_HTTP_H */