-
Notifications
You must be signed in to change notification settings - Fork 44
/
airscan-http.c
3391 lines (2843 loc) · 85.6 KB
/
airscan-http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* HTTP Client
*/
#define _GNU_SOURCE
#include <string.h>
#define NO_HTTP_STATUS
#include "airscan.h"
#include "http_parser.h"
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <avahi-common/domain.h>
#include <gnutls/gnutls.h>
/******************** Constants ********************/
/* I/O buffer size
*/
#define HTTP_IOBUF_SIZE 65536
/* Limit of chained HTTP redirects
*/
#define HTTP_REDIRECT_LIMIT 8
/* Default query timeout, milliseconds
* Disabled, if negative
*/
#define HTTP_QUERY_TIMEOUT -1
/******************** Static variables ********************/
static gnutls_certificate_credentials_t gnutls_cred;
/******************** Forward declarations ********************/
typedef struct http_multipart http_multipart;
static http_data*
http_data_new(http_data *parent, const char *bytes, size_t size);
static void
http_data_set_content_type (http_data *data, const char *content_type);
static void
http_query_timeout_cancel (http_query *q);
static http_query*
http_query_by_ll_node (ll_node *node);
static void
http_query_complete (http_query *q, error err);
static void
http_query_connect (http_query *q, error err);
static void
http_query_disconnect (http_query *q);
static ssize_t
http_query_sock_send (http_query *q, const void *data, size_t size);
static ssize_t
http_query_sock_recv (http_query *q, void *data, size_t size);
static error
http_query_sock_err (http_query *q, int rc);
static void
http_query_cancel (http_query *q);
/******************** HTTP URI ********************/
/* Type http_uri represents HTTP URI
*/
struct http_uri {
struct http_parser_url parsed; /* Parsed URI */
const char *str; /* URI string */
const char *path; /* URI path */
const char *host; /* URI host */
HTTP_SCHEME scheme; /* URI scheme */
union { /* Host address*/
struct sockaddr sockaddr;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr;
};
typedef struct {
const char *str;
size_t len;
} http_uri_field;
/* Make http_uri_field
*/
static http_uri_field
http_uri_field_make (const char *str)
{
http_uri_field field = {str, strlen(str)};
return field;
}
/* Check if URI has a particular field
*/
static inline bool
http_uri_field_present (const http_uri *uri, int num)
{
return (uri->parsed.field_set & (1 << num)) != 0;
}
/* Check if URI field is present and non-empty
*/
static inline bool
http_uri_field_nonempty (const http_uri *uri, int num)
{
return uri->parsed.field_data[num].len != 0;
}
/* Get first character of the field. Returns -1, if
* field is empty, or non-negative character code otherwise
*/
static inline int
http_uri_field_begin (const http_uri *uri, int num)
{
if (http_uri_field_nonempty(uri, num)) {
return (unsigned char) uri->str[uri->parsed.field_data[num].off];
} else {
return -1;
}
}
/* Get field from URI
*/
static http_uri_field
http_uri_field_get (const http_uri *uri, int num)
{
http_uri_field field = {
uri->str + uri->parsed.field_data[num].off,
uri->parsed.field_data[num].len
};
return field;
}
/* Append field to buffer, and return pointer to
* updated buffer tail
*/
static inline char *
http_uri_field_append (http_uri_field field, char *buf)
{
memcpy(buf, field.str, field.len);
return buf + field.len;
}
/* Get field from URI and append to buffer
*/
static inline char*
http_uri_field_copy (const http_uri *uri, int num, char *buf)
{
return http_uri_field_append(http_uri_field_get(uri, num), buf);
}
/* Get and strdup the field
*/
static char*
http_uri_field_strdup (const http_uri *uri, int num)
{
http_uri_field field = http_uri_field_get(uri, num);
char *s = mem_new(char, field.len + 1);
memcpy(s, field.str, field.len);
s[field.len] = '\0';
return s;
}
/* Check are fields of two URIs are equal
*/
static bool
http_uri_field_equal (const http_uri *uri1, const http_uri *uri2,
int num, bool nocase)
{
http_uri_field f1 = http_uri_field_get(uri1, num);
http_uri_field f2 = http_uri_field_get(uri2, num);
if (f1.len != f2.len) {
return false;
}
if (nocase) {
return !strncasecmp(f1.str, f2.str, f1.len);
} else {
return !memcmp(f1.str, f2.str, f1.len);
}
}
/* Replace particular URI field with val[len] string
*/
static void
http_uri_field_replace_len (http_uri *uri, int num, const char *val, size_t len)
{
static const struct { char *pfx; int num; char *sfx; } fields[] = {
{"", UF_SCHEMA, "://"},
{"", UF_USERINFO, "@"},
{"", UF_HOST, ""},
{":", UF_PORT, ""},
{"", UF_PATH, ""},
{"?", UF_QUERY, ""},
{"#", UF_FRAGMENT, ""},
{NULL, -1, NULL}
};
int i;
char *buf = alloca(strlen(uri->str) + len + 4);
char *end = buf;
http_uri *uri2;
/* Rebuild URI string */
for (i = 0; fields[i].num != -1; i ++) {
http_uri_field field;
if (num == fields[i].num) {
field.str = val;
field.len = len;
} else {
field = http_uri_field_get(uri, fields[i].num);
}
if (field.len != 0) {
bool ip6_host = false;
if (fields[i].num == UF_HOST) {
ip6_host = memchr(field.str, ':', field.len) != NULL;
}
if (fields[i].pfx != NULL) {
http_uri_field pfx = http_uri_field_make(fields[i].pfx);
end = http_uri_field_append(pfx, end);
}
if (ip6_host) {
*end ++ = '[';
}
end = http_uri_field_append(field, end);
if (ip6_host) {
*end ++ = ']';
}
if (fields[i].sfx != NULL) {
http_uri_field sfx = http_uri_field_make(fields[i].sfx);
end = http_uri_field_append(sfx, end);
}
}
}
*end = '\0';
/* Reconstruct the URI */
uri2 = http_uri_new(buf, false);
log_assert(NULL, uri2 != NULL);
mem_free((char*) uri->str);
mem_free((char*) uri->path);
mem_free((char*) uri->host);
*uri = *uri2;
mem_free(uri2);
}
/* Replace particular URI field
*/
static void
http_uri_field_replace (http_uri *uri, int num, const char *val)
{
http_uri_field_replace_len (uri, num, val, strlen(val));
}
/* Append UF_PATH part of URI to buffer up to the final '/'
* character
*/
static char*
http_uri_field_copy_basepath (const http_uri *uri, char *buf)
{
http_uri_field path = http_uri_field_get(uri, UF_PATH);
const char *end = memrchr(path.str, '/', path.len);
path.len = end ? (size_t)(end - path.str) : 0;
buf = http_uri_field_append(path, buf);
*(buf ++) = '/';
return buf;
}
/* Check if sting has an scheme prefix, where scheme
* must be [a-zA-Z][a-zA-Z0-9+-.]*):
*
* If scheme is found, returns its length. 0 is returned otherwise
*/
static size_t
http_uri_parse_scheme (const char *str)
{
char c = *str;
size_t i;
if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))) {
return 0;
}
i = 1;
for (;;) {
c = str[i ++];
if (('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
c == '+' ||
c == '-' ||
c == '.') {
continue;
}
break;
}
return c == ':' ? i : 0;
}
/* Return error, if UF_HOST field of URI is present and invalid
*/
static error
http_uri_parse_check_host (http_uri *uri)
{
http_uri_field field = http_uri_field_get(uri, UF_HOST);
char *host, *zone;
int rc;
struct in6_addr in6;
/* If UF_HOST is present and in square brackets, it must
* be valid IPv6 literal. Note, http_parser_parse_url()
* doesn't check it
*/
if (field.len == 0 || field.str == uri->str || field.str[-1] != '[') {
return NULL;
}
if (field.str[field.len] != ']') {
return ERROR("URI: missed ']' in IP6 address literal");
}
host = alloca(field.len + 1);
memcpy(host, field.str, field.len);
host[field.len] = '\0';
zone = strchr(host, '%');
if (zone != NULL) {
*zone = '\0';
}
rc = inet_pton(AF_INET6, host, &in6);
if (rc != 1) {
return ERROR("URI: invalid IP6 address literal");
}
return NULL;
}
/* Parse URI in place. The parsed URI doesn't create
* a copy of URI string, and uses supplied string directly
*/
static error
http_uri_parse (http_uri *uri, const char *str)
{
size_t scheme_len = http_uri_parse_scheme(str);
const char *normalized = str;
const char *prefix = NULL;
size_t prefix_len = 0;
size_t path_skip = 0;
error err;
/* Note, github.com/nodejs/http-parser fails to properly
* parse relative URLs (URLs without scheme), so prepend
* fake scheme, then remove it
*/
if (scheme_len == 0) {
char *s;
if (str[0] == '/' && str[1] == '/') {
prefix = "s:";
} else if (str[0] == '/') {
prefix = "s://h";
} else {
prefix = "s://h/";
path_skip = 1;
}
prefix_len = strlen(prefix);
s = alloca(prefix_len + strlen(str) + 1);
memcpy(s, prefix, prefix_len);
strcpy(s + prefix_len, str);
normalized = s;
}
/* Parse URI */
memset(uri, 0, sizeof(*uri));
if (http_parser_parse_url(normalized, strlen(normalized),
0, &uri->parsed) != 0) {
return ERROR("URI: parse error");
}
uri->str = str;
/* Adjust offsets */
if (path_skip != 0) {
uri->parsed.field_data[UF_PATH].off ++;
uri->parsed.field_data[UF_PATH].len --;
}
if (prefix_len != 0) {
unsigned int i;
for (i = 0; i < UF_MAX; i ++) {
if ((uri->parsed.field_set & (1 << i)) != 0) {
if (uri->parsed.field_data[i].off >= (uint16_t) prefix_len) {
uri->parsed.field_data[i].off -= (uint16_t) prefix_len;
} else {
uri->parsed.field_data[i].off = 0;
uri->parsed.field_data[i].len = 0;
uri->parsed.field_set &= ~ (1 << i);
}
}
}
}
/* Decode scheme */
uri->scheme = HTTP_SCHEME_UNSET; /* For sanity */
if (scheme_len != 0) {
if (!strncasecmp(str, "http://", 7)) {
uri->scheme = HTTP_SCHEME_HTTP;
} else if (!strncasecmp(str, "https://", 8)) {
uri->scheme = HTTP_SCHEME_HTTPS;
} else if (!strncasecmp(str, "unix://", 7)) {
uri->scheme = HTTP_SCHEME_UNIX;
} else {
return ERROR("URI: invalid scheme");
}
}
/* Check UF_HOST */
err = http_uri_parse_check_host(uri);
if (err != NULL) {
return err;
}
return NULL;
}
/* Un-escape host name in place
*/
static void
http_uri_unescape_host (char *host)
{
char *zone = strstr(host, "%25");
if (zone != NULL) {
memmove(zone + 1, zone + 3, strlen(zone + 3) + 1);
}
}
/* Parse URI address
*/
static void
http_uri_parse_addr (http_uri *uri)
{
http_uri_field field;
char *host = NULL, *port = NULL;
uint16_t portnum;
int rc;
/* Reset address */
memset(&uri->addr, 0, sizeof(uri->addr));
/* Get host and port */
field = http_uri_field_get(uri, UF_HOST);
if (field.len != 0) {
host = alloca(field.len + 1);
memcpy(host, field.str, field.len);
host[field.len] = '\0';
http_uri_unescape_host(host);
}
field = http_uri_field_get(uri, UF_PORT);
if (field.len != 0) {
port = alloca(field.len + 1);
memcpy(port, field.str, field.len);
port[field.len] = '\0';
}
if (host == NULL) {
return;
}
/* Parse port number */
if (port != NULL) {
char *end;
unsigned long val = strtoul(port, &end, 10);
if (end == port || *end != '\0' || val > 0xffff) {
return;
}
portnum = htons((uint16_t) val);
} else {
switch (uri->scheme) {
case HTTP_SCHEME_HTTP:
portnum = htons(80);
break;
case HTTP_SCHEME_HTTPS:
portnum = htons(443);
break;
default:
return;
}
}
if (strchr(host, ':') != NULL) {
struct in6_addr in6;
/* Strip zone suffix */
char *s = strchr(host, '%');
if (s != NULL) {
*s = '\0';
}
rc = inet_pton(AF_INET6, host, &in6);
if (rc != 1) {
return;
}
uri->addr.in6.sin6_family = AF_INET6;
uri->addr.in6.sin6_addr = in6;
uri->addr.in6.sin6_port = portnum;
} else {
struct in_addr in;
rc = inet_pton(AF_INET, host, &in);
if (rc != 1) {
return;
}
uri->addr.in.sin_family = AF_INET;
uri->addr.in.sin_addr = in;
uri->addr.in.sin_port = portnum;
}
}
/* Create new URI, by parsing URI string
*/
http_uri*
http_uri_new (const char *str, bool strip_fragment)
{
http_uri *uri = mem_new(http_uri, 1);
char *buf;
http_uri_field field;
/* Parse URI */
if (http_uri_parse(uri, str) != NULL) {
goto FAIL;
}
/* Don't allow relative URLs here */
if (uri->scheme == HTTP_SCHEME_UNSET) {
goto FAIL;
}
uri->str = buf = str_dup(str);
/* Honor strip_fragment flag */
if (strip_fragment && http_uri_field_present(uri, UF_FRAGMENT)) {
buf[uri->parsed.field_data[UF_FRAGMENT].off - 1] = '\0';
uri->parsed.field_set &= ~(1 << UF_FRAGMENT);
uri->parsed.field_data[UF_FRAGMENT].off = 0;
uri->parsed.field_data[UF_FRAGMENT].len = 0;
}
/* Prepare addr, path, etc */
http_uri_parse_addr(uri);
uri->path = http_uri_field_strdup(uri, UF_PATH);
field = http_uri_field_get(uri, UF_HOST);
if (memchr(field.str, ':', field.len) != NULL) {
char *host = mem_resize((char*) NULL, field.len + 2, 1);
host[0] = '[';
memcpy(host + 1, field.str, field.len);
host[field.len + 1] = ']';
host[field.len + 2] = '\0';
uri->host = host;
} else {
uri->host = http_uri_field_strdup(uri, UF_HOST);
}
return uri;
/* Error: cleanup and exit */
FAIL:
mem_free(uri);
return NULL;
}
/* Clone the URI
*/
http_uri*
http_uri_clone (const http_uri *old)
{
http_uri *uri = mem_new(http_uri, 1);
*uri = *old;
uri->str = str_dup(uri->str);
uri->path = str_dup(uri->path);
uri->host = str_dup(uri->host);
return uri;
}
/* Check that string, defined by begin and end pointers,
* has a specified prefix
*/
static bool
http_uri_str_prefix(const char *beg, const char *end, const char *pfx)
{
size_t len = end - beg;
size_t pfx_len = strlen(pfx);
return len >= pfx_len && !memcmp(beg, pfx, pfx_len);
}
/* Check that string, defined by begin and end pointers,
* equal to the specified pattern
*/
static bool
http_uri_str_equal(const char *beg, const char *end, const char *pattern)
{
size_t len = end - beg;
size_t plen = strlen(pattern);
return len == plen && !memcmp(beg, pattern, len);
}
/* Find 1st occurrence of the character in the string,
* defined by begin and end pointers
*/
static char *
http_uri_str_chr(const char *beg, const char *end, char c)
{
return memchr(beg, c, end - beg);
}
/* Find last occurrence of the character in the string,
* defined by begin and end pointers
*/
static char *
http_uri_str_rchr(const char *beg, const char *end, char c)
{
return memrchr(beg, c, end - beg);
}
/* Remove last path segment. Returns pointer to the
* path end
*/
static char*
http_uri_remove_last_segment (char *path, char *end)
{
char *s = http_uri_str_rchr(path, end, '/');
return s == NULL ? end : s;
}
/* Remove path dot segments, per rfc3986, 5.2.4.
*/
static char*
http_uri_remove_dot_segments (char *path, char *end)
{
char *input = path;
char *path_end = path;
while (input != end) {
/* A. If the input buffer begins with a prefix of "../" or "./",
* then remove that prefix from the input buffer; otherwise,
*/
if (http_uri_str_prefix(input, end, "../")) {
input += 3;
} else if (http_uri_str_prefix(input, end, "./")) {
input += 2;
/* B. if the input buffer begins with a prefix of "/./" or "/.",
* where "." is a complete path segment, then replace that
* prefix with "/" in the input buffer; otherwise,
*/
} else if (http_uri_str_prefix(input, end, "/./")) {
input += 2;
} else if (http_uri_str_equal(input, end, "/.")) {
input ++;
input[0] = '/';
/* C. if the input buffer begins with a prefix of "/../" or "/..",
* where ".." is a complete path segment, then replace that
* prefix with "/" in the input buffer and remove the last
* segment and its preceding "/" (if any) from the output
* buffer; otherwise,
*/
} else if (http_uri_str_prefix(input, end, "/../")) {
path_end = http_uri_remove_last_segment(path, path_end);
input += 3;
} else if (http_uri_str_equal(input, end, "/..")) {
path_end = http_uri_remove_last_segment(path, path_end);
input += 2;
input[0] = '/';
/* D. if the input buffer consists only of "." or "..", then remove
* that from the input buffer; otherwise,
*/
} else if (http_uri_str_equal(input, end, ".") ||
http_uri_str_equal(input, end, "..")) {
input = end;
/* E. move the first path segment in the input buffer to the end of
* the output buffer, including the initial "/" character (if
* any) and any subsequent characters up to, but not including,
* the next "/" character or the end of the input buffer.
*/
} else {
char *s = http_uri_str_chr(input + 1, end, '/');
size_t sz = s ? s - input : end - input;
memmove(path_end, input, sz);
path_end += sz;
input += sz;
}
}
return path_end;
}
/* Create URI, relative to base URI. If `path_only' is
* true, scheme, host and port are taken from the
* base URI
*/
http_uri*
http_uri_new_relative (const http_uri *base, const char *path,
bool strip_fragment, bool path_only)
{
char *buf = alloca(strlen(base->str) + strlen(path) + 1);
char *end = buf;
http_uri ref;
const http_uri *uri;
http_uri_field field;
char *path_beg;
if (http_uri_parse(&ref, path) != NULL) {
return NULL;
}
/* If the base URI uses the unix:// scheme, don't allow a relative URI to
* change the scheme or host.
*/
if (base->scheme == HTTP_SCHEME_UNIX) {
path_only = true;
}
/* Set schema, userinfo, host and port */
if (path_only || !http_uri_field_present(&ref, UF_SCHEMA)) {
end = http_uri_field_copy(base, UF_SCHEMA, end);
} else {
end = http_uri_field_copy(&ref, UF_SCHEMA, end);
}
end = http_uri_field_append(http_uri_field_make("://"), end);
if (path_only || !http_uri_field_present(&ref, UF_HOST)) {
uri = base;
} else {
uri = &ref;
}
if (http_uri_field_present(uri, UF_USERINFO)) {
end = http_uri_field_copy(uri, UF_USERINFO, end);
end = http_uri_field_append(http_uri_field_make("@"), end);
}
field = http_uri_field_get(uri, UF_HOST);
if (memchr(field.str, ':', field.len) != NULL) {
*end ++ = '[';
end = http_uri_field_append(field, end);
*end ++ = ']';
} else {
end = http_uri_field_append(field, end);
}
if (http_uri_field_present(uri, UF_PORT)) {
end = http_uri_field_append(http_uri_field_make(":"), end);
end = http_uri_field_copy(uri, UF_PORT, end);
}
/* Set path */
path_beg = end;
if (!http_uri_field_nonempty(&ref, UF_PATH)) {
end = http_uri_field_copy(base, UF_PATH, end);
} else {
if (http_uri_field_begin(&ref, UF_PATH) != '/') {
end = http_uri_field_copy_basepath(base, end);
}
end = http_uri_field_copy(&ref, UF_PATH, end);
}
end = http_uri_remove_dot_segments(path_beg, end);
/* Query and fragment */
if (http_uri_field_present(&ref, UF_QUERY)) {
end = http_uri_field_append(http_uri_field_make("?"), end);
end = http_uri_field_copy(&ref, UF_QUERY, end);
}
if (!strip_fragment && http_uri_field_present(&ref, UF_FRAGMENT)) {
end = http_uri_field_append(http_uri_field_make("#"), end);
end = http_uri_field_copy(&ref, UF_FRAGMENT, end);
}
*end = '\0';
return http_uri_new(buf, false);
}
/* Free the URI
*/
#ifndef __clang_analyzer__
void
http_uri_free (http_uri *uri)
{
if (uri != NULL) {
mem_free((char*) uri->str);
mem_free((char*) uri->path);
mem_free((char*) uri->host);
mem_free(uri);
}
}
#endif
/* Get URI string
*/
const char*
http_uri_str (http_uri *uri)
{
return uri->str;
}
/* Get URI's host address. If Host address is not literal, returns NULL
*/
const struct sockaddr*
http_uri_addr (const http_uri *uri)
{
if (uri->addr.sockaddr.sa_family == AF_UNSPEC) {
return NULL;
}
return &uri->addr.sockaddr;
}
/* Get URI path
*
* Note, if URL has empty path (i.e., "http://1.2.3.4"), the
* empty string will be returned
*/
const char*
http_uri_get_path (const http_uri *uri)
{
return uri->path;
}
/* Set URI path
*/
void
http_uri_set_path (http_uri *uri, const char *path)
{
http_uri_field_replace(uri, UF_PATH, path);
}
/* Get URI host. It returns only host name, port number is
* not included.
*
* IPv6 literal addresses are returned in square brackets
* (i.e., [fe80::217:c8ff:fe7b:6a91%4])
*
* Note, the subsequent modifications of URI, such as http_uri_fix_host(),
* http_uri_fix_ipv6_zone() etc, may make the returned string invalid,
* so if you need to keep it for a long time, better make a copy
*/
const char*
http_uri_get_host (const http_uri *uri)
{
return uri->host;
}
/* http_uri_host_is checks if URI's host name is equal to the
* specified string.
*
* It does its best to compare domain names correctly, taking
* in account only significant difference (for example, the difference
* in upper/lower case * in domain names is not significant).
*/
bool
http_uri_host_is (const http_uri *uri, const char *host)
{
const char *uri_host = http_uri_get_host(uri);
if (http_uri_host_is_literal(uri)) {
return !strcasecmp(uri_host, host);
}
if (!avahi_is_valid_domain_name(uri_host)) {
return false;
}
if (!avahi_is_valid_domain_name(host)) {
return false;
}
return avahi_domain_equal(uri_host, host);
}
/* http_uri_host_is_literal returns true if URI uses literal
* IP address
*/
bool
http_uri_host_is_literal (const http_uri *uri)
{
return http_uri_addr(uri) != NULL;
}
/* Set URI host into the literal IP address.
*/
void
http_uri_set_host_addr (http_uri *uri, ip_addr addr)
{
ip_straddr straddr = ip_addr_to_straddr(addr, true);
char *host = straddr.text;
char *s;
/* Remove square brackets around IPv6 address */
if (host[0] == '[' && host[strlen(host) - 1] == ']') {
host[strlen(host) - 1] = '\0';
host ++;
}
/* Escape % character (zone suffix delimiter) */
s = strchr(host, '%');
if (s != NULL) {
/* Note, we replace '%' with '%25' plus don't forget
* the terminating '\0'. Hence strlen(host) + 3
*/
char *host2 = alloca(strlen(host) + 3);
size_t sz = s - host;
memcpy(host2, host, sz);
memcpy(host2 + sz, "%25", 3);
strcpy(host2 + sz + 3, s + 1);
host = host2;
}
/* Set the host */
http_uri_field_replace(uri, UF_HOST, host);
}
/* Fix URI host: if `match` is NULL or uri's host matches `match`,
* replace uri's host and port with values taken from the base_uri
*/
void
http_uri_fix_host (http_uri *uri, const http_uri *base_uri, const char *match)
{
http_uri_field schema, host, port;
if (match != NULL) {
host = http_uri_field_get(uri, UF_HOST);
if (strncasecmp(host.str, match, host.len)) {
return;
}
}
schema = http_uri_field_get(base_uri, UF_SCHEMA);
host = http_uri_field_get(base_uri, UF_HOST);
port = http_uri_field_get(base_uri, UF_PORT);
http_uri_field_replace_len(uri, UF_SCHEMA, schema.str, schema.len);