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