/proc/self/cwd/external/curl/lib/altsvc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
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. |
13 | | * |
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. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | /* |
25 | | * The Alt-Svc: header is defined in RFC 7838: |
26 | | * https://datatracker.ietf.org/doc/html/rfc7838 |
27 | | */ |
28 | | #include "curl_setup.h" |
29 | | |
30 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_ALTSVC) |
31 | | #include <curl/curl.h> |
32 | | #include "urldata.h" |
33 | | #include "altsvc.h" |
34 | | #include "curl_get_line.h" |
35 | | #include "strcase.h" |
36 | | #include "parsedate.h" |
37 | | #include "sendf.h" |
38 | | #include "warnless.h" |
39 | | #include "fopen.h" |
40 | | #include "rename.h" |
41 | | #include "strdup.h" |
42 | | #include "inet_pton.h" |
43 | | |
44 | | /* The last 3 #include files should be in this order */ |
45 | | #include "curl_printf.h" |
46 | | #include "curl_memory.h" |
47 | | #include "memdebug.h" |
48 | | |
49 | 0 | #define MAX_ALTSVC_LINE 4095 |
50 | | #define MAX_ALTSVC_DATELENSTR "64" |
51 | | #define MAX_ALTSVC_DATELEN 64 |
52 | | #define MAX_ALTSVC_HOSTLENSTR "512" |
53 | 0 | #define MAX_ALTSVC_HOSTLEN 512 |
54 | | #define MAX_ALTSVC_ALPNLENSTR "10" |
55 | | #define MAX_ALTSVC_ALPNLEN 10 |
56 | | |
57 | 0 | #define H3VERSION "h3" |
58 | | |
59 | | static enum alpnid alpn2alpnid(char *name) |
60 | 0 | { |
61 | 0 | if(strcasecompare(name, "h1")) |
62 | 0 | return ALPN_h1; |
63 | 0 | if(strcasecompare(name, "h2")) |
64 | 0 | return ALPN_h2; |
65 | 0 | if(strcasecompare(name, H3VERSION)) |
66 | 0 | return ALPN_h3; |
67 | 0 | return ALPN_none; /* unknown, probably rubbish input */ |
68 | 0 | } |
69 | | |
70 | | /* Given the ALPN ID, return the name */ |
71 | | const char *Curl_alpnid2str(enum alpnid id) |
72 | 0 | { |
73 | 0 | switch(id) { |
74 | 0 | case ALPN_h1: |
75 | 0 | return "h1"; |
76 | 0 | case ALPN_h2: |
77 | 0 | return "h2"; |
78 | 0 | case ALPN_h3: |
79 | 0 | return H3VERSION; |
80 | 0 | default: |
81 | 0 | return ""; /* bad */ |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | |
86 | | static void altsvc_free(struct altsvc *as) |
87 | 0 | { |
88 | 0 | free(as->src.host); |
89 | 0 | free(as->dst.host); |
90 | 0 | free(as); |
91 | 0 | } |
92 | | |
93 | | static struct altsvc *altsvc_createid(const char *srchost, |
94 | | const char *dsthost, |
95 | | enum alpnid srcalpnid, |
96 | | enum alpnid dstalpnid, |
97 | | unsigned int srcport, |
98 | | unsigned int dstport) |
99 | 0 | { |
100 | 0 | struct altsvc *as = calloc(sizeof(struct altsvc), 1); |
101 | 0 | size_t hlen; |
102 | 0 | size_t dlen; |
103 | 0 | if(!as) |
104 | 0 | return NULL; |
105 | 0 | hlen = strlen(srchost); |
106 | 0 | dlen = strlen(dsthost); |
107 | 0 | DEBUGASSERT(hlen); |
108 | 0 | DEBUGASSERT(dlen); |
109 | 0 | if(!hlen || !dlen) |
110 | | /* bad input */ |
111 | 0 | return NULL; |
112 | 0 | if((hlen > 2) && srchost[0] == '[') { |
113 | | /* IPv6 address, strip off brackets */ |
114 | 0 | srchost++; |
115 | 0 | hlen -= 2; |
116 | 0 | } |
117 | 0 | else if(srchost[hlen - 1] == '.') |
118 | | /* strip off trailing dot */ |
119 | 0 | hlen--; |
120 | 0 | if((dlen > 2) && dsthost[0] == '[') { |
121 | | /* IPv6 address, strip off brackets */ |
122 | 0 | dsthost++; |
123 | 0 | dlen -= 2; |
124 | 0 | } |
125 | |
|
126 | 0 | as->src.host = Curl_memdup(srchost, hlen + 1); |
127 | 0 | if(!as->src.host) |
128 | 0 | goto error; |
129 | 0 | as->src.host[hlen] = 0; |
130 | |
|
131 | 0 | as->dst.host = Curl_memdup(dsthost, dlen + 1); |
132 | 0 | if(!as->dst.host) |
133 | 0 | goto error; |
134 | 0 | as->dst.host[dlen] = 0; |
135 | |
|
136 | 0 | as->src.alpnid = srcalpnid; |
137 | 0 | as->dst.alpnid = dstalpnid; |
138 | 0 | as->src.port = curlx_ultous(srcport); |
139 | 0 | as->dst.port = curlx_ultous(dstport); |
140 | |
|
141 | 0 | return as; |
142 | 0 | error: |
143 | 0 | altsvc_free(as); |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | | static struct altsvc *altsvc_create(char *srchost, |
148 | | char *dsthost, |
149 | | char *srcalpn, |
150 | | char *dstalpn, |
151 | | unsigned int srcport, |
152 | | unsigned int dstport) |
153 | 0 | { |
154 | 0 | enum alpnid dstalpnid = alpn2alpnid(dstalpn); |
155 | 0 | enum alpnid srcalpnid = alpn2alpnid(srcalpn); |
156 | 0 | if(!srcalpnid || !dstalpnid) |
157 | 0 | return NULL; |
158 | 0 | return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid, |
159 | 0 | srcport, dstport); |
160 | 0 | } |
161 | | |
162 | | /* only returns SERIOUS errors */ |
163 | | static CURLcode altsvc_add(struct altsvcinfo *asi, char *line) |
164 | 0 | { |
165 | | /* Example line: |
166 | | h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1 |
167 | | */ |
168 | 0 | char srchost[MAX_ALTSVC_HOSTLEN + 1]; |
169 | 0 | char dsthost[MAX_ALTSVC_HOSTLEN + 1]; |
170 | 0 | char srcalpn[MAX_ALTSVC_ALPNLEN + 1]; |
171 | 0 | char dstalpn[MAX_ALTSVC_ALPNLEN + 1]; |
172 | 0 | char date[MAX_ALTSVC_DATELEN + 1]; |
173 | 0 | unsigned int srcport; |
174 | 0 | unsigned int dstport; |
175 | 0 | unsigned int prio; |
176 | 0 | unsigned int persist; |
177 | 0 | int rc; |
178 | |
|
179 | 0 | rc = sscanf(line, |
180 | 0 | "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u " |
181 | 0 | "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u " |
182 | 0 | "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u", |
183 | 0 | srcalpn, srchost, &srcport, |
184 | 0 | dstalpn, dsthost, &dstport, |
185 | 0 | date, &persist, &prio); |
186 | 0 | if(9 == rc) { |
187 | 0 | struct altsvc *as; |
188 | 0 | time_t expires = Curl_getdate_capped(date); |
189 | 0 | as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport); |
190 | 0 | if(as) { |
191 | 0 | as->expires = expires; |
192 | 0 | as->prio = prio; |
193 | 0 | as->persist = persist ? 1 : 0; |
194 | 0 | Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node); |
195 | 0 | } |
196 | 0 | } |
197 | |
|
198 | 0 | return CURLE_OK; |
199 | 0 | } |
200 | | |
201 | | /* |
202 | | * Load alt-svc entries from the given file. The text based line-oriented file |
203 | | * format is documented here: https://curl.se/docs/alt-svc.html |
204 | | * |
205 | | * This function only returns error on major problems that prevent alt-svc |
206 | | * handling to work completely. It will ignore individual syntactical errors |
207 | | * etc. |
208 | | */ |
209 | | static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file) |
210 | 0 | { |
211 | 0 | CURLcode result = CURLE_OK; |
212 | 0 | char *line = NULL; |
213 | 0 | FILE *fp; |
214 | | |
215 | | /* we need a private copy of the file name so that the altsvc cache file |
216 | | name survives an easy handle reset */ |
217 | 0 | free(asi->filename); |
218 | 0 | asi->filename = strdup(file); |
219 | 0 | if(!asi->filename) |
220 | 0 | return CURLE_OUT_OF_MEMORY; |
221 | | |
222 | 0 | fp = fopen(file, FOPEN_READTEXT); |
223 | 0 | if(fp) { |
224 | 0 | line = malloc(MAX_ALTSVC_LINE); |
225 | 0 | if(!line) |
226 | 0 | goto fail; |
227 | 0 | while(Curl_get_line(line, MAX_ALTSVC_LINE, fp)) { |
228 | 0 | char *lineptr = line; |
229 | 0 | while(*lineptr && ISBLANK(*lineptr)) |
230 | 0 | lineptr++; |
231 | 0 | if(*lineptr == '#') |
232 | | /* skip commented lines */ |
233 | 0 | continue; |
234 | | |
235 | 0 | altsvc_add(asi, lineptr); |
236 | 0 | } |
237 | 0 | free(line); /* free the line buffer */ |
238 | 0 | fclose(fp); |
239 | 0 | } |
240 | 0 | return result; |
241 | | |
242 | 0 | fail: |
243 | 0 | Curl_safefree(asi->filename); |
244 | 0 | free(line); |
245 | 0 | fclose(fp); |
246 | 0 | return CURLE_OUT_OF_MEMORY; |
247 | 0 | } |
248 | | |
249 | | /* |
250 | | * Write this single altsvc entry to a single output line |
251 | | */ |
252 | | |
253 | | static CURLcode altsvc_out(struct altsvc *as, FILE *fp) |
254 | 0 | { |
255 | 0 | struct tm stamp; |
256 | 0 | const char *dst6_pre = ""; |
257 | 0 | const char *dst6_post = ""; |
258 | 0 | const char *src6_pre = ""; |
259 | 0 | const char *src6_post = ""; |
260 | 0 | CURLcode result = Curl_gmtime(as->expires, &stamp); |
261 | 0 | if(result) |
262 | 0 | return result; |
263 | 0 | #ifdef ENABLE_IPV6 |
264 | 0 | else { |
265 | 0 | char ipv6_unused[16]; |
266 | 0 | if(1 == Curl_inet_pton(AF_INET6, as->dst.host, ipv6_unused)) { |
267 | 0 | dst6_pre = "["; |
268 | 0 | dst6_post = "]"; |
269 | 0 | } |
270 | 0 | if(1 == Curl_inet_pton(AF_INET6, as->src.host, ipv6_unused)) { |
271 | 0 | src6_pre = "["; |
272 | 0 | src6_post = "]"; |
273 | 0 | } |
274 | 0 | } |
275 | 0 | #endif |
276 | 0 | fprintf(fp, |
277 | 0 | "%s %s%s%s %u " |
278 | 0 | "%s %s%s%s %u " |
279 | 0 | "\"%d%02d%02d " |
280 | 0 | "%02d:%02d:%02d\" " |
281 | 0 | "%u %d\n", |
282 | 0 | Curl_alpnid2str(as->src.alpnid), |
283 | 0 | src6_pre, as->src.host, src6_post, |
284 | 0 | as->src.port, |
285 | |
|
286 | 0 | Curl_alpnid2str(as->dst.alpnid), |
287 | 0 | dst6_pre, as->dst.host, dst6_post, |
288 | 0 | as->dst.port, |
289 | |
|
290 | 0 | stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday, |
291 | 0 | stamp.tm_hour, stamp.tm_min, stamp.tm_sec, |
292 | 0 | as->persist, as->prio); |
293 | 0 | return CURLE_OK; |
294 | 0 | } |
295 | | |
296 | | /* ---- library-wide functions below ---- */ |
297 | | |
298 | | /* |
299 | | * Curl_altsvc_init() creates a new altsvc cache. |
300 | | * It returns the new instance or NULL if something goes wrong. |
301 | | */ |
302 | | struct altsvcinfo *Curl_altsvc_init(void) |
303 | 0 | { |
304 | 0 | struct altsvcinfo *asi = calloc(sizeof(struct altsvcinfo), 1); |
305 | 0 | if(!asi) |
306 | 0 | return NULL; |
307 | 0 | Curl_llist_init(&asi->list, NULL); |
308 | | |
309 | | /* set default behavior */ |
310 | 0 | asi->flags = CURLALTSVC_H1 |
311 | | #ifdef USE_HTTP2 |
312 | | | CURLALTSVC_H2 |
313 | | #endif |
314 | | #ifdef ENABLE_QUIC |
315 | | | CURLALTSVC_H3 |
316 | | #endif |
317 | 0 | ; |
318 | 0 | return asi; |
319 | 0 | } |
320 | | |
321 | | /* |
322 | | * Curl_altsvc_load() loads alt-svc from file. |
323 | | */ |
324 | | CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file) |
325 | 0 | { |
326 | 0 | CURLcode result; |
327 | 0 | DEBUGASSERT(asi); |
328 | 0 | result = altsvc_load(asi, file); |
329 | 0 | return result; |
330 | 0 | } |
331 | | |
332 | | /* |
333 | | * Curl_altsvc_ctrl() passes on the external bitmask. |
334 | | */ |
335 | | CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl) |
336 | 0 | { |
337 | 0 | DEBUGASSERT(asi); |
338 | 0 | if(!ctrl) |
339 | | /* unexpected */ |
340 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
341 | 0 | asi->flags = ctrl; |
342 | 0 | return CURLE_OK; |
343 | 0 | } |
344 | | |
345 | | /* |
346 | | * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated |
347 | | * resources. |
348 | | */ |
349 | | void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp) |
350 | 0 | { |
351 | 0 | struct Curl_llist_element *e; |
352 | 0 | struct Curl_llist_element *n; |
353 | 0 | if(*altsvcp) { |
354 | 0 | struct altsvcinfo *altsvc = *altsvcp; |
355 | 0 | for(e = altsvc->list.head; e; e = n) { |
356 | 0 | struct altsvc *as = e->ptr; |
357 | 0 | n = e->next; |
358 | 0 | altsvc_free(as); |
359 | 0 | } |
360 | 0 | free(altsvc->filename); |
361 | 0 | free(altsvc); |
362 | 0 | *altsvcp = NULL; /* clear the pointer */ |
363 | 0 | } |
364 | 0 | } |
365 | | |
366 | | /* |
367 | | * Curl_altsvc_save() writes the altsvc cache to a file. |
368 | | */ |
369 | | CURLcode Curl_altsvc_save(struct Curl_easy *data, |
370 | | struct altsvcinfo *altsvc, const char *file) |
371 | 0 | { |
372 | 0 | struct Curl_llist_element *e; |
373 | 0 | struct Curl_llist_element *n; |
374 | 0 | CURLcode result = CURLE_OK; |
375 | 0 | FILE *out; |
376 | 0 | char *tempstore = NULL; |
377 | |
|
378 | 0 | if(!altsvc) |
379 | | /* no cache activated */ |
380 | 0 | return CURLE_OK; |
381 | | |
382 | | /* if not new name is given, use the one we stored from the load */ |
383 | 0 | if(!file && altsvc->filename) |
384 | 0 | file = altsvc->filename; |
385 | |
|
386 | 0 | if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file || !file[0]) |
387 | | /* marked as read-only, no file or zero length file name */ |
388 | 0 | return CURLE_OK; |
389 | | |
390 | 0 | result = Curl_fopen(data, file, &out, &tempstore); |
391 | 0 | if(!result) { |
392 | 0 | fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n" |
393 | 0 | "# This file was generated by libcurl! Edit at your own risk.\n", |
394 | 0 | out); |
395 | 0 | for(e = altsvc->list.head; e; e = n) { |
396 | 0 | struct altsvc *as = e->ptr; |
397 | 0 | n = e->next; |
398 | 0 | result = altsvc_out(as, out); |
399 | 0 | if(result) |
400 | 0 | break; |
401 | 0 | } |
402 | 0 | fclose(out); |
403 | 0 | if(!result && tempstore && Curl_rename(tempstore, file)) |
404 | 0 | result = CURLE_WRITE_ERROR; |
405 | |
|
406 | 0 | if(result && tempstore) |
407 | 0 | unlink(tempstore); |
408 | 0 | } |
409 | 0 | free(tempstore); |
410 | 0 | return result; |
411 | 0 | } |
412 | | |
413 | | static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen) |
414 | 0 | { |
415 | 0 | size_t len; |
416 | 0 | const char *protop; |
417 | 0 | const char *p = *ptr; |
418 | 0 | while(*p && ISBLANK(*p)) |
419 | 0 | p++; |
420 | 0 | protop = p; |
421 | 0 | while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '=')) |
422 | 0 | p++; |
423 | 0 | len = p - protop; |
424 | 0 | *ptr = p; |
425 | |
|
426 | 0 | if(!len || (len >= buflen)) |
427 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
428 | 0 | memcpy(alpnbuf, protop, len); |
429 | 0 | alpnbuf[len] = 0; |
430 | 0 | return CURLE_OK; |
431 | 0 | } |
432 | | |
433 | | /* hostcompare() returns true if 'host' matches 'check'. The first host |
434 | | * argument may have a trailing dot present that will be ignored. |
435 | | */ |
436 | | static bool hostcompare(const char *host, const char *check) |
437 | 0 | { |
438 | 0 | size_t hlen = strlen(host); |
439 | 0 | size_t clen = strlen(check); |
440 | |
|
441 | 0 | if(hlen && (host[hlen - 1] == '.')) |
442 | 0 | hlen--; |
443 | 0 | if(hlen != clen) |
444 | | /* they can't match if they have different lengths */ |
445 | 0 | return FALSE; |
446 | 0 | return strncasecompare(host, check, hlen); |
447 | 0 | } |
448 | | |
449 | | /* altsvc_flush() removes all alternatives for this source origin from the |
450 | | list */ |
451 | | static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid, |
452 | | const char *srchost, unsigned short srcport) |
453 | 0 | { |
454 | 0 | struct Curl_llist_element *e; |
455 | 0 | struct Curl_llist_element *n; |
456 | 0 | for(e = asi->list.head; e; e = n) { |
457 | 0 | struct altsvc *as = e->ptr; |
458 | 0 | n = e->next; |
459 | 0 | if((srcalpnid == as->src.alpnid) && |
460 | 0 | (srcport == as->src.port) && |
461 | 0 | hostcompare(srchost, as->src.host)) { |
462 | 0 | Curl_llist_remove(&asi->list, e, NULL); |
463 | 0 | altsvc_free(as); |
464 | 0 | } |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | #ifdef DEBUGBUILD |
469 | | /* to play well with debug builds, we can *set* a fixed time this will |
470 | | return */ |
471 | | static time_t altsvc_debugtime(void *unused) |
472 | | { |
473 | | char *timestr = getenv("CURL_TIME"); |
474 | | (void)unused; |
475 | | if(timestr) { |
476 | | unsigned long val = strtol(timestr, NULL, 10); |
477 | | return (time_t)val; |
478 | | } |
479 | | return time(NULL); |
480 | | } |
481 | | #undef time |
482 | | #define time(x) altsvc_debugtime(x) |
483 | | #endif |
484 | | |
485 | 0 | #define ISNEWLINE(x) (((x) == '\n') || (x) == '\r') |
486 | | |
487 | | /* |
488 | | * Curl_altsvc_parse() takes an incoming alt-svc response header and stores |
489 | | * the data correctly in the cache. |
490 | | * |
491 | | * 'value' points to the header *value*. That's contents to the right of the |
492 | | * header name. |
493 | | * |
494 | | * Currently this function rejects invalid data without returning an error. |
495 | | * Invalid host name, port number will result in the specific alternative |
496 | | * being rejected. Unknown protocols are skipped. |
497 | | */ |
498 | | CURLcode Curl_altsvc_parse(struct Curl_easy *data, |
499 | | struct altsvcinfo *asi, const char *value, |
500 | | enum alpnid srcalpnid, const char *srchost, |
501 | | unsigned short srcport) |
502 | 0 | { |
503 | 0 | const char *p = value; |
504 | 0 | size_t len; |
505 | 0 | char namebuf[MAX_ALTSVC_HOSTLEN] = ""; |
506 | 0 | char alpnbuf[MAX_ALTSVC_ALPNLEN] = ""; |
507 | 0 | struct altsvc *as; |
508 | 0 | unsigned short dstport = srcport; /* the same by default */ |
509 | 0 | CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf)); |
510 | 0 | size_t entries = 0; |
511 | | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
512 | | (void)data; |
513 | | #endif |
514 | 0 | if(result) { |
515 | 0 | infof(data, "Excessive alt-svc header, ignoring."); |
516 | 0 | return CURLE_OK; |
517 | 0 | } |
518 | | |
519 | 0 | DEBUGASSERT(asi); |
520 | | |
521 | | /* "clear" is a magic keyword */ |
522 | 0 | if(strcasecompare(alpnbuf, "clear")) { |
523 | | /* Flush cached alternatives for this source origin */ |
524 | 0 | altsvc_flush(asi, srcalpnid, srchost, srcport); |
525 | 0 | return CURLE_OK; |
526 | 0 | } |
527 | | |
528 | 0 | do { |
529 | 0 | if(*p == '=') { |
530 | | /* [protocol]="[host][:port]" */ |
531 | 0 | enum alpnid dstalpnid = alpn2alpnid(alpnbuf); /* the same by default */ |
532 | 0 | p++; |
533 | 0 | if(*p == '\"') { |
534 | 0 | const char *dsthost = ""; |
535 | 0 | const char *value_ptr; |
536 | 0 | char option[32]; |
537 | 0 | unsigned long num; |
538 | 0 | char *end_ptr; |
539 | 0 | bool quoted = FALSE; |
540 | 0 | time_t maxage = 24 * 3600; /* default is 24 hours */ |
541 | 0 | bool persist = FALSE; |
542 | 0 | bool valid = TRUE; |
543 | 0 | p++; |
544 | 0 | if(*p != ':') { |
545 | | /* host name starts here */ |
546 | 0 | const char *hostp = p; |
547 | 0 | if(*p == '[') { |
548 | | /* pass all valid IPv6 letters - does not handle zone id */ |
549 | 0 | len = strspn(++p, "0123456789abcdefABCDEF:."); |
550 | 0 | if(p[len] != ']') |
551 | | /* invalid host syntax, bail out */ |
552 | 0 | break; |
553 | | /* we store the IPv6 numerical address *with* brackets */ |
554 | 0 | len += 2; |
555 | 0 | p = &p[len-1]; |
556 | 0 | } |
557 | 0 | else { |
558 | 0 | while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-'))) |
559 | 0 | p++; |
560 | 0 | len = p - hostp; |
561 | 0 | } |
562 | 0 | if(!len || (len >= MAX_ALTSVC_HOSTLEN)) { |
563 | 0 | infof(data, "Excessive alt-svc host name, ignoring."); |
564 | 0 | valid = FALSE; |
565 | 0 | } |
566 | 0 | else { |
567 | 0 | memcpy(namebuf, hostp, len); |
568 | 0 | namebuf[len] = 0; |
569 | 0 | dsthost = namebuf; |
570 | 0 | } |
571 | 0 | } |
572 | 0 | else { |
573 | | /* no destination name, use source host */ |
574 | 0 | dsthost = srchost; |
575 | 0 | } |
576 | 0 | if(*p == ':') { |
577 | 0 | unsigned long port = 0; |
578 | 0 | p++; |
579 | 0 | if(ISDIGIT(*p)) |
580 | | /* a port number */ |
581 | 0 | port = strtoul(p, &end_ptr, 10); |
582 | 0 | else |
583 | 0 | end_ptr = (char *)p; /* not left uninitialized */ |
584 | 0 | if(!port || port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') { |
585 | 0 | infof(data, "Unknown alt-svc port number, ignoring."); |
586 | 0 | valid = FALSE; |
587 | 0 | } |
588 | 0 | else { |
589 | 0 | dstport = curlx_ultous(port); |
590 | 0 | p = end_ptr; |
591 | 0 | } |
592 | 0 | } |
593 | 0 | if(*p++ != '\"') |
594 | 0 | break; |
595 | | /* Handle the optional 'ma' and 'persist' flags. Unknown flags |
596 | | are skipped. */ |
597 | 0 | for(;;) { |
598 | 0 | while(ISBLANK(*p)) |
599 | 0 | p++; |
600 | 0 | if(*p != ';') |
601 | 0 | break; |
602 | 0 | p++; /* pass the semicolon */ |
603 | 0 | if(!*p || ISNEWLINE(*p)) |
604 | 0 | break; |
605 | 0 | result = getalnum(&p, option, sizeof(option)); |
606 | 0 | if(result) { |
607 | | /* skip option if name is too long */ |
608 | 0 | option[0] = '\0'; |
609 | 0 | } |
610 | 0 | while(*p && ISBLANK(*p)) |
611 | 0 | p++; |
612 | 0 | if(*p != '=') |
613 | 0 | return CURLE_OK; |
614 | 0 | p++; |
615 | 0 | while(*p && ISBLANK(*p)) |
616 | 0 | p++; |
617 | 0 | if(!*p) |
618 | 0 | return CURLE_OK; |
619 | 0 | if(*p == '\"') { |
620 | | /* quoted value */ |
621 | 0 | p++; |
622 | 0 | quoted = TRUE; |
623 | 0 | } |
624 | 0 | value_ptr = p; |
625 | 0 | if(quoted) { |
626 | 0 | while(*p && *p != '\"') |
627 | 0 | p++; |
628 | 0 | if(!*p++) |
629 | 0 | return CURLE_OK; |
630 | 0 | } |
631 | 0 | else { |
632 | 0 | while(*p && !ISBLANK(*p) && *p!= ';' && *p != ',') |
633 | 0 | p++; |
634 | 0 | } |
635 | 0 | num = strtoul(value_ptr, &end_ptr, 10); |
636 | 0 | if((end_ptr != value_ptr) && (num < ULONG_MAX)) { |
637 | 0 | if(strcasecompare("ma", option)) |
638 | 0 | maxage = num; |
639 | 0 | else if(strcasecompare("persist", option) && (num == 1)) |
640 | 0 | persist = TRUE; |
641 | 0 | } |
642 | 0 | } |
643 | 0 | if(dstalpnid && valid) { |
644 | 0 | if(!entries++) |
645 | | /* Flush cached alternatives for this source origin, if any - when |
646 | | this is the first entry of the line. */ |
647 | 0 | altsvc_flush(asi, srcalpnid, srchost, srcport); |
648 | |
|
649 | 0 | as = altsvc_createid(srchost, dsthost, |
650 | 0 | srcalpnid, dstalpnid, |
651 | 0 | srcport, dstport); |
652 | 0 | if(as) { |
653 | | /* The expires time also needs to take the Age: value (if any) into |
654 | | account. [See RFC 7838 section 3.1] */ |
655 | 0 | as->expires = maxage + time(NULL); |
656 | 0 | as->persist = persist; |
657 | 0 | Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node); |
658 | 0 | infof(data, "Added alt-svc: %s:%d over %s", dsthost, dstport, |
659 | 0 | Curl_alpnid2str(dstalpnid)); |
660 | 0 | } |
661 | 0 | } |
662 | 0 | } |
663 | 0 | else |
664 | 0 | break; |
665 | | /* after the double quote there can be a comma if there's another |
666 | | string or a semicolon if no more */ |
667 | 0 | if(*p == ',') { |
668 | | /* comma means another alternative is presented */ |
669 | 0 | p++; |
670 | 0 | result = getalnum(&p, alpnbuf, sizeof(alpnbuf)); |
671 | 0 | if(result) |
672 | 0 | break; |
673 | 0 | } |
674 | 0 | } |
675 | 0 | else |
676 | 0 | break; |
677 | 0 | } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r')); |
678 | | |
679 | 0 | return CURLE_OK; |
680 | 0 | } |
681 | | |
682 | | /* |
683 | | * Return TRUE on a match |
684 | | */ |
685 | | bool Curl_altsvc_lookup(struct altsvcinfo *asi, |
686 | | enum alpnid srcalpnid, const char *srchost, |
687 | | int srcport, |
688 | | struct altsvc **dstentry, |
689 | | const int versions) /* one or more bits */ |
690 | 0 | { |
691 | 0 | struct Curl_llist_element *e; |
692 | 0 | struct Curl_llist_element *n; |
693 | 0 | time_t now = time(NULL); |
694 | 0 | DEBUGASSERT(asi); |
695 | 0 | DEBUGASSERT(srchost); |
696 | 0 | DEBUGASSERT(dstentry); |
697 | |
|
698 | 0 | for(e = asi->list.head; e; e = n) { |
699 | 0 | struct altsvc *as = e->ptr; |
700 | 0 | n = e->next; |
701 | 0 | if(as->expires < now) { |
702 | | /* an expired entry, remove */ |
703 | 0 | Curl_llist_remove(&asi->list, e, NULL); |
704 | 0 | altsvc_free(as); |
705 | 0 | continue; |
706 | 0 | } |
707 | 0 | if((as->src.alpnid == srcalpnid) && |
708 | 0 | hostcompare(srchost, as->src.host) && |
709 | 0 | (as->src.port == srcport) && |
710 | 0 | (versions & as->dst.alpnid)) { |
711 | | /* match */ |
712 | 0 | *dstentry = as; |
713 | 0 | return TRUE; |
714 | 0 | } |
715 | 0 | } |
716 | 0 | return FALSE; |
717 | 0 | } |
718 | | |
719 | | #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */ |