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