1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
30 /* The last 3 #include files should be in this order */
31 #include "curl_printf.h"
32 #include "curl_memory.h"
36 * Curl_pseudo_headers() creates the array with pseudo headers to be
37 * used in a HTTP/2 or HTTP/3 request.
40 #if defined(USE_NGHTTP2) || defined(ENABLE_QUIC)
42 /* Index where :authority header field will appear in request header
44 #define AUTHORITY_DST_IDX 3
46 /* USHRT_MAX is 65535 == 0xffff */
47 #define HEADER_OVERFLOW(x) \
48 (x.namelen > 0xffff || x.valuelen > 0xffff - x.namelen)
51 * Check header memory for the token "trailers".
52 * Parse the tokens as separated by comma and surrounded by whitespace.
53 * Returns TRUE if found or FALSE if not.
55 static bool contains_trailers(const char *p, size_t len)
57 const char *end = p + len;
59 for(; p != end && (*p == ' ' || *p == '\t'); ++p)
61 if(p == end || (size_t)(end - p) < sizeof("trailers") - 1)
63 if(strncasecompare("trailers", p, sizeof("trailers") - 1)) {
64 p += sizeof("trailers") - 1;
65 for(; p != end && (*p == ' ' || *p == '\t'); ++p)
67 if(p == end || *p == ',')
70 /* skip to next token */
71 for(; p != end && *p != ','; ++p)
80 /* Send header to server */
82 /* Don't send header to server */
84 /* Discard header, and replace it with "te: trailers" */
85 HEADERINST_TE_TRAILERS
88 /* Decides how to treat given header field. */
89 static header_instruction inspect_header(const char *name, size_t namelen,
90 const char *value, size_t valuelen) {
93 if(!strncasecompare("te", name, namelen))
94 return HEADERINST_FORWARD;
96 return contains_trailers(value, valuelen) ?
97 HEADERINST_TE_TRAILERS : HEADERINST_IGNORE;
99 return strncasecompare("upgrade", name, namelen) ?
100 HEADERINST_IGNORE : HEADERINST_FORWARD;
102 return (strncasecompare("connection", name, namelen) ||
103 strncasecompare("keep-alive", name, namelen)) ?
104 HEADERINST_IGNORE : HEADERINST_FORWARD;
106 return strncasecompare("proxy-connection", name, namelen) ?
107 HEADERINST_IGNORE : HEADERINST_FORWARD;
109 return strncasecompare("transfer-encoding", name, namelen) ?
110 HEADERINST_IGNORE : HEADERINST_FORWARD;
112 return HEADERINST_FORWARD;
116 CURLcode Curl_pseudo_headers(struct Curl_easy *data,
117 const char *mem, /* the request */
118 const size_t len /* size of request */,
121 struct connectdata *conn = data->conn;
124 size_t authority_idx;
125 char *hdbuf = (char *)mem;
126 char *end, *line_end;
127 struct h2h3pseudo *nva = NULL;
128 struct h2h3req *hreq = NULL;
131 /* Calculate number of headers contained in [mem, mem + len). Assumes a
132 correctly generated HTTP header field block. */
133 for(i = 1; i < len; ++i) {
134 if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
142 /* We counted additional 2 \r\n in the first and last line. We need 3
143 new headers: :method, :path and :scheme. Therefore we need one
146 hreq = malloc(sizeof(struct h2h3req) +
147 sizeof(struct h2h3pseudo) * (nheader - 1));
152 nva = &hreq->header[0];
154 /* Extract :method, :path from request line
155 We do line endings with CRLF so checking for CR is enough */
156 line_end = memchr(hdbuf, '\r', len);
161 /* Method does not contain spaces */
162 end = memchr(hdbuf, ' ', line_end - hdbuf);
163 if(!end || end == hdbuf)
165 nva[0].name = H2H3_PSEUDO_METHOD;
166 nva[0].namelen = sizeof(H2H3_PSEUDO_METHOD) - 1;
167 nva[0].value = hdbuf;
168 nva[0].valuelen = (size_t)(end - hdbuf);
172 /* Path may contain spaces so scan backwards */
174 for(i = (size_t)(line_end - hdbuf); i; --i) {
175 if(hdbuf[i - 1] == ' ') {
180 if(!end || end == hdbuf)
182 nva[1].name = H2H3_PSEUDO_PATH;
183 nva[1].namelen = sizeof(H2H3_PSEUDO_PATH) - 1;
184 nva[1].value = hdbuf;
185 nva[1].valuelen = (end - hdbuf);
187 nva[2].name = H2H3_PSEUDO_SCHEME;
188 nva[2].namelen = sizeof(H2H3_PSEUDO_SCHEME) - 1;
189 vptr = Curl_checkheaders(data, STRCONST(H2H3_PSEUDO_SCHEME));
191 vptr += sizeof(H2H3_PSEUDO_SCHEME);
192 while(*vptr && ISSPACE(*vptr))
195 infof(data, "set pseudo header %s to %s", H2H3_PSEUDO_SCHEME, vptr);
198 if(conn->handler->flags & PROTOPT_SSL)
199 nva[2].value = "https";
201 nva[2].value = "http";
203 nva[2].valuelen = strlen((char *)nva[2].value);
210 hdbuf = line_end + 2;
212 /* check for next CR, but only within the piece of data left in the given
214 line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
215 if(!line_end || (line_end == hdbuf))
218 /* header continuation lines are not supported */
219 if(*hdbuf == ' ' || *hdbuf == '\t')
222 for(end = hdbuf; end < line_end && *end != ':'; ++end)
224 if(end == hdbuf || end == line_end)
228 if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
230 nva[i].name = H2H3_PSEUDO_AUTHORITY;
231 nva[i].namelen = sizeof(H2H3_PSEUDO_AUTHORITY) - 1;
234 nva[i].namelen = (size_t)(end - hdbuf);
235 /* Lower case the header name for HTTP/3 */
236 Curl_strntolower((char *)hdbuf, hdbuf, nva[i].namelen);
240 while(*hdbuf == ' ' || *hdbuf == '\t')
244 switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
246 case HEADERINST_IGNORE:
247 /* skip header fields prohibited by HTTP/2 specification. */
250 case HEADERINST_TE_TRAILERS:
251 nva[i].value = "trailers";
252 nva[i].valuelen = sizeof("trailers") - 1;
255 nva[i].value = hdbuf;
256 nva[i].valuelen = (end - hdbuf);
259 nva[i].value = hdbuf;
260 nva[i].valuelen = (end - hdbuf);
265 /* :authority must come before non-pseudo header fields */
266 if(authority_idx && authority_idx != AUTHORITY_DST_IDX) {
267 struct h2h3pseudo authority = nva[authority_idx];
268 for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
274 /* Warn stream may be rejected if cumulative length of headers is too
276 #define MAX_ACC 60000 /* <64KB to account for some overhead */
280 for(i = 0; i < nheader; ++i) {
281 acc += nva[i].namelen + nva[i].valuelen;
283 infof(data, "h2h3 [%.*s: %.*s]",
284 (int)nva[i].namelen, nva[i].name,
285 (int)nva[i].valuelen, nva[i].value);
289 infof(data, "http_request: Warning: The cumulative length of all "
290 "headers exceeds %d bytes and that could cause the "
291 "stream to be rejected.", MAX_ACC);
295 hreq->entries = nheader;
302 return CURLE_OUT_OF_MEMORY;
305 void Curl_pseudo_free(struct h2h3req *hp)
310 #endif /* USE_NGHTTP2 or HTTP/3 enabled */