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