http.h 12 KB

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