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