http.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #ifndef HEADER_CURL_HTTP_H
  2. #define HEADER_CURL_HTTP_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  24. #include "curl_setup.h"
  25. typedef enum {
  26. HTTPREQ_GET,
  27. HTTPREQ_POST,
  28. HTTPREQ_POST_FORM, /* we make a difference internally */
  29. HTTPREQ_POST_MIME, /* we make a difference internally */
  30. HTTPREQ_PUT,
  31. HTTPREQ_HEAD
  32. } Curl_HttpReq;
  33. #ifndef CURL_DISABLE_HTTP
  34. #ifdef USE_NGHTTP2
  35. #include <nghttp2/nghttp2.h>
  36. #endif
  37. #if defined(_WIN32) && defined(ENABLE_QUIC)
  38. #include <stdint.h>
  39. #endif
  40. extern const struct Curl_handler Curl_handler_http;
  41. #ifdef USE_SSL
  42. extern const struct Curl_handler Curl_handler_https;
  43. #endif
  44. /* Header specific functions */
  45. bool Curl_compareheader(const char *headerline, /* line to check */
  46. const char *header, /* header keyword _with_ colon */
  47. const size_t hlen, /* len of the keyword in bytes */
  48. const char *content, /* content string to find */
  49. const size_t clen); /* len of the content in bytes */
  50. char *Curl_copy_header_value(const char *header);
  51. char *Curl_checkProxyheaders(struct Curl_easy *data,
  52. const struct connectdata *conn,
  53. const char *thisheader,
  54. const size_t thislen);
  55. CURLcode Curl_buffer_send(struct dynbuf *in,
  56. struct Curl_easy *data,
  57. curl_off_t *bytes_written,
  58. curl_off_t included_body_bytes,
  59. int socketindex);
  60. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  61. #ifndef USE_HYPER
  62. struct dynbuf *req
  63. #else
  64. void *headers
  65. #endif
  66. );
  67. CURLcode Curl_add_custom_headers(struct Curl_easy *data,
  68. bool is_connect,
  69. #ifndef USE_HYPER
  70. struct dynbuf *req
  71. #else
  72. void *headers
  73. #endif
  74. );
  75. CURLcode Curl_http_compile_trailers(struct curl_slist *trailers,
  76. struct dynbuf *buf,
  77. struct Curl_easy *handle);
  78. void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
  79. const char **method, Curl_HttpReq *);
  80. CURLcode Curl_http_useragent(struct Curl_easy *data);
  81. CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn);
  82. CURLcode Curl_http_target(struct Curl_easy *data, struct connectdata *conn,
  83. struct dynbuf *req);
  84. CURLcode Curl_http_statusline(struct Curl_easy *data,
  85. struct connectdata *conn);
  86. CURLcode Curl_http_header(struct Curl_easy *data, struct connectdata *conn,
  87. char *headp);
  88. CURLcode Curl_transferencode(struct Curl_easy *data);
  89. CURLcode Curl_http_body(struct Curl_easy *data, struct connectdata *conn,
  90. Curl_HttpReq httpreq,
  91. const char **teep);
  92. CURLcode Curl_http_bodysend(struct Curl_easy *data, struct connectdata *conn,
  93. struct dynbuf *r, Curl_HttpReq httpreq);
  94. bool Curl_use_http_1_1plus(const struct Curl_easy *data,
  95. const struct connectdata *conn);
  96. #ifndef CURL_DISABLE_COOKIES
  97. CURLcode Curl_http_cookies(struct Curl_easy *data,
  98. struct connectdata *conn,
  99. struct dynbuf *r);
  100. #else
  101. #define Curl_http_cookies(a,b,c) CURLE_OK
  102. #endif
  103. CURLcode Curl_http_resume(struct Curl_easy *data,
  104. struct connectdata *conn,
  105. Curl_HttpReq httpreq);
  106. CURLcode Curl_http_range(struct Curl_easy *data,
  107. Curl_HttpReq httpreq);
  108. CURLcode Curl_http_firstwrite(struct Curl_easy *data,
  109. struct connectdata *conn,
  110. bool *done);
  111. /* protocol-specific functions set up to be called by the main engine */
  112. CURLcode Curl_http(struct Curl_easy *data, bool *done);
  113. CURLcode Curl_http_done(struct Curl_easy *data, CURLcode, bool premature);
  114. CURLcode Curl_http_connect(struct Curl_easy *data, bool *done);
  115. /* These functions are in http.c */
  116. CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
  117. const char *auth);
  118. CURLcode Curl_http_auth_act(struct Curl_easy *data);
  119. /* If only the PICKNONE bit is set, there has been a round-trip and we
  120. selected to use no auth at all. Ie, we actively select no auth, as opposed
  121. to not having one selected. The other CURLAUTH_* defines are present in the
  122. public curl/curl.h header. */
  123. #define CURLAUTH_PICKNONE (1<<30) /* don't use auth */
  124. /* MAX_INITIAL_POST_SIZE indicates the number of bytes that will make the POST
  125. data get included in the initial data chunk sent to the server. If the
  126. data is larger than this, it will automatically get split up in multiple
  127. system calls.
  128. This value used to be fairly big (100K), but we must take into account that
  129. if the server rejects the POST due for authentication reasons, this data
  130. will always be unconditionally sent and thus it may not be larger than can
  131. always be afforded to send twice.
  132. It must not be greater than 64K to work on VMS.
  133. */
  134. #ifndef MAX_INITIAL_POST_SIZE
  135. #define MAX_INITIAL_POST_SIZE (64*1024)
  136. #endif
  137. /* EXPECT_100_THRESHOLD is the request body size limit for when libcurl will
  138. * automatically add an "Expect: 100-continue" header in HTTP requests. When
  139. * the size is unknown, it will always add it.
  140. *
  141. */
  142. #ifndef EXPECT_100_THRESHOLD
  143. #define EXPECT_100_THRESHOLD (1024*1024)
  144. #endif
  145. #endif /* CURL_DISABLE_HTTP */
  146. #ifdef USE_NGHTTP3
  147. struct h3out; /* see ngtcp2 */
  148. #endif
  149. #ifdef USE_MSH3
  150. #ifdef _WIN32
  151. #define msh3_lock CRITICAL_SECTION
  152. #define msh3_lock_initialize(lock) InitializeCriticalSection(lock)
  153. #define msh3_lock_uninitialize(lock) DeleteCriticalSection(lock)
  154. #define msh3_lock_acquire(lock) EnterCriticalSection(lock)
  155. #define msh3_lock_release(lock) LeaveCriticalSection(lock)
  156. #else /* !_WIN32 */
  157. #include <pthread.h>
  158. #define msh3_lock pthread_mutex_t
  159. #define msh3_lock_initialize(lock) { \
  160. pthread_mutexattr_t attr; \
  161. pthread_mutexattr_init(&attr); \
  162. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
  163. pthread_mutex_init(lock, &attr); \
  164. pthread_mutexattr_destroy(&attr); \
  165. }
  166. #define msh3_lock_uninitialize(lock) pthread_mutex_destroy(lock)
  167. #define msh3_lock_acquire(lock) pthread_mutex_lock(lock)
  168. #define msh3_lock_release(lock) pthread_mutex_unlock(lock)
  169. #endif /* _WIN32 */
  170. #endif /* USE_MSH3 */
  171. /****************************************************************************
  172. * HTTP unique setup
  173. ***************************************************************************/
  174. struct HTTP {
  175. curl_mimepart *sendit;
  176. curl_off_t postsize; /* off_t to handle large file sizes */
  177. const char *postdata;
  178. const char *p_pragma; /* Pragma: string */
  179. /* For FORM posting */
  180. curl_mimepart form;
  181. struct back {
  182. curl_read_callback fread_func; /* backup storage for fread pointer */
  183. void *fread_in; /* backup storage for fread_in pointer */
  184. const char *postdata;
  185. curl_off_t postsize;
  186. } backup;
  187. enum {
  188. HTTPSEND_NADA, /* init */
  189. HTTPSEND_REQUEST, /* sending a request */
  190. HTTPSEND_BODY /* sending body */
  191. } sending;
  192. #ifndef CURL_DISABLE_HTTP
  193. struct dynbuf send_buffer; /* used if the request couldn't be sent in one
  194. chunk, points to an allocated send_buffer
  195. struct */
  196. #endif
  197. #ifdef USE_NGHTTP2
  198. /*********** for HTTP/2 we store stream-local data here *************/
  199. int32_t stream_id; /* stream we are interested in */
  200. bool bodystarted;
  201. /* We store non-final and final response headers here, per-stream */
  202. struct dynbuf header_recvbuf;
  203. size_t nread_header_recvbuf; /* number of bytes in header_recvbuf fed into
  204. upper layer */
  205. struct dynbuf trailer_recvbuf;
  206. int status_code; /* HTTP status code */
  207. const uint8_t *pausedata; /* pointer to data received in on_data_chunk */
  208. size_t pauselen; /* the number of bytes left in data */
  209. bool close_handled; /* TRUE if stream closure is handled by libcurl */
  210. char **push_headers; /* allocated array */
  211. size_t push_headers_used; /* number of entries filled in */
  212. size_t push_headers_alloc; /* number of entries allocated */
  213. uint32_t error; /* HTTP/2 stream error code */
  214. #endif
  215. #if defined(USE_NGHTTP2) || defined(USE_NGHTTP3)
  216. bool closed; /* TRUE on HTTP2 stream close */
  217. char *mem; /* points to a buffer in memory to store received data */
  218. size_t len; /* size of the buffer 'mem' points to */
  219. size_t memlen; /* size of data copied to mem */
  220. #endif
  221. #if defined(USE_NGHTTP2) || defined(ENABLE_QUIC)
  222. /* fields used by both HTTP/2 and HTTP/3 */
  223. const uint8_t *upload_mem; /* points to a buffer to read from */
  224. size_t upload_len; /* size of the buffer 'upload_mem' points to */
  225. curl_off_t upload_left; /* number of bytes left to upload */
  226. #endif
  227. #ifdef ENABLE_QUIC
  228. #ifndef USE_MSH3
  229. /*********** for HTTP/3 we store stream-local data here *************/
  230. int64_t stream3_id; /* stream we are interested in */
  231. bool firstheader; /* FALSE until headers arrive */
  232. bool firstbody; /* FALSE until body arrives */
  233. bool h3req; /* FALSE until request is issued */
  234. #endif
  235. bool upload_done;
  236. #endif
  237. #ifdef USE_NGHTTP3
  238. size_t unacked_window;
  239. struct h3out *h3out; /* per-stream buffers for upload */
  240. struct dynbuf overflow; /* excess data received during a single Curl_read */
  241. #endif
  242. #ifdef USE_MSH3
  243. struct MSH3_REQUEST *req;
  244. msh3_lock recv_lock;
  245. /* Receive Buffer (Headers and Data) */
  246. uint8_t* recv_buf;
  247. size_t recv_buf_alloc;
  248. /* Receive Headers */
  249. size_t recv_header_len;
  250. bool recv_header_complete;
  251. /* Receive Data */
  252. size_t recv_data_len;
  253. bool recv_data_complete;
  254. /* General Receive Error */
  255. CURLcode recv_error;
  256. #endif
  257. };
  258. #ifdef USE_NGHTTP2
  259. /* h2 settings for this connection */
  260. struct h2settings {
  261. uint32_t max_concurrent_streams;
  262. bool enable_push;
  263. };
  264. #endif
  265. struct http_conn {
  266. #ifdef USE_NGHTTP2
  267. #define H2_BINSETTINGS_LEN 80
  268. uint8_t binsettings[H2_BINSETTINGS_LEN];
  269. size_t binlen; /* length of the binsettings data */
  270. /* We associate the connnectdata struct with the connection, but we need to
  271. make sure we can identify the current "driving" transfer. This is a
  272. work-around for the lack of nghttp2_session_set_user_data() in older
  273. nghttp2 versions that we want to support. (Added in 1.31.0) */
  274. struct Curl_easy *trnsfr;
  275. nghttp2_session *h2;
  276. Curl_send *send_underlying; /* underlying send Curl_send callback */
  277. Curl_recv *recv_underlying; /* underlying recv Curl_recv callback */
  278. char *inbuf; /* buffer to receive data from underlying socket */
  279. size_t inbuflen; /* number of bytes filled in inbuf */
  280. size_t nread_inbuf; /* number of bytes read from in inbuf */
  281. /* We need separate buffer for transmission and reception because we
  282. may call nghttp2_session_send() after the
  283. nghttp2_session_mem_recv() but mem buffer is still not full. In
  284. this case, we wrongly sends the content of mem buffer if we share
  285. them for both cases. */
  286. int32_t pause_stream_id; /* stream ID which paused
  287. nghttp2_session_mem_recv */
  288. size_t drain_total; /* sum of all stream's UrlState.drain */
  289. /* this is a hash of all individual streams (Curl_easy structs) */
  290. struct h2settings settings;
  291. /* list of settings that will be sent */
  292. nghttp2_settings_entry local_settings[3];
  293. size_t local_settings_num;
  294. #else
  295. int unused; /* prevent a compiler warning */
  296. #endif
  297. };
  298. CURLcode Curl_http_size(struct Curl_easy *data);
  299. CURLcode Curl_http_readwrite_headers(struct Curl_easy *data,
  300. struct connectdata *conn,
  301. ssize_t *nread,
  302. bool *stop_reading);
  303. /**
  304. * Curl_http_output_auth() setups the authentication headers for the
  305. * host/proxy and the correct authentication
  306. * method. data->state.authdone is set to TRUE when authentication is
  307. * done.
  308. *
  309. * @param data all information about the current transfer
  310. * @param conn all information about the current connection
  311. * @param request pointer to the request keyword
  312. * @param httpreq is the request type
  313. * @param path pointer to the requested path
  314. * @param proxytunnel boolean if this is the request setting up a "proxy
  315. * tunnel"
  316. *
  317. * @returns CURLcode
  318. */
  319. CURLcode
  320. Curl_http_output_auth(struct Curl_easy *data,
  321. struct connectdata *conn,
  322. const char *request,
  323. Curl_HttpReq httpreq,
  324. const char *path,
  325. bool proxytunnel); /* TRUE if this is the request setting
  326. up the proxy tunnel */
  327. /*
  328. * Curl_allow_auth_to_host() tells if authentication, cookies or other
  329. * "sensitive data" can (still) be sent to this host.
  330. */
  331. bool Curl_allow_auth_to_host(struct Curl_easy *data);
  332. #endif /* HEADER_CURL_HTTP_H */