-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cc
More file actions
1913 lines (1690 loc) · 41.9 KB
/
Copy pathutil.cc
File metadata and controls
1913 lines (1690 loc) · 41.9 KB
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
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "util.h"
#ifdef HAVE_TIME_H
# include <time.h>
#endif // HAVE_TIME_H
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif // HAVE_SYS_SOCKET_H
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif // HAVE_NETDB_H
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif // HAVE_FCNTL_H
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif // HAVE_NETINET_IN_H
#ifdef _WIN32
# include <ws2tcpip.h>
# include <boost/date_time/posix_time/posix_time.hpp>
#else // !_WIN32
# include <netinet/tcp.h>
#endif // !_WIN32
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif // HAVE_ARPA_INET_H
#include <cmath>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <fstream>
#include <openssl/evp.h>
#include "nghttp2_config.h"
#include <nghttp2/nghttp2.h>
#include "ssl_compat.h"
#include "timegm.h"
namespace nghttp2
{
namespace util
{
#ifndef _WIN32
namespace
{
int nghttp2_inet_pton(int af, const char* src, void* dst)
{
return inet_pton(af, src, dst);
}
} // namespace
#else // _WIN32
namespace
{
// inet_pton-wrapper for Windows
int nghttp2_inet_pton(int af, const char* src, void* dst)
{
# if _WIN32_WINNT >= 0x0600
return InetPtonA(af, src, dst);
# else
// the function takes a 'char*', so we need to make a copy
char addr[INET6_ADDRSTRLEN + 1];
strncpy(addr, src, sizeof(addr));
addr[sizeof(addr) - 1] = 0;
int size = sizeof(struct in6_addr);
if (WSAStringToAddress(addr, af, NULL, (LPSOCKADDR)dst, &size) == 0)
{
return 1;
}
return 0;
# endif
}
} // namespace
#endif // _WIN32
const char UPPER_XDIGITS[] = "0123456789ABCDEF";
bool in_rfc3986_unreserved_chars(const char c)
{
static constexpr char unreserved[] = {'-', '.', '_', '~'};
return is_alpha(c) || is_digit(c) ||
std::find(std::begin(unreserved), std::end(unreserved), c) !=
std::end(unreserved);
}
bool in_rfc3986_sub_delims(const char c)
{
static constexpr char sub_delims[] = {'!', '$', '&', '\'', '(', ')',
'*', '+', ',', ';', '='
};
return std::find(std::begin(sub_delims), std::end(sub_delims), c) !=
std::end(sub_delims);
}
std::string percent_encode(const unsigned char* target, size_t len)
{
std::string dest;
for (size_t i = 0; i < len; ++i)
{
unsigned char c = target[i];
if (in_rfc3986_unreserved_chars(c))
{
dest += c;
}
else
{
dest += '%';
dest += UPPER_XDIGITS[c >> 4];
dest += UPPER_XDIGITS[(c & 0x0f)];
}
}
return dest;
}
std::string percent_encode(const std::string& target)
{
return percent_encode(reinterpret_cast<const unsigned char*>(target.c_str()),
target.size());
}
std::string percent_encode_path(const std::string& s)
{
std::string dest;
for (auto c : s)
{
if (in_rfc3986_unreserved_chars(c) || in_rfc3986_sub_delims(c) ||
c == '/')
{
dest += c;
continue;
}
dest += '%';
dest += UPPER_XDIGITS[(c >> 4) & 0x0f];
dest += UPPER_XDIGITS[(c & 0x0f)];
}
return dest;
}
bool in_token(char c)
{
static constexpr char extra[] = {'!', '#', '$', '%', '&', '\'', '*', '+',
'-', '.', '^', '_', '`', '|', '~'
};
return is_alpha(c) || is_digit(c) ||
std::find(std::begin(extra), std::end(extra), c) != std::end(extra);
}
bool in_attr_char(char c)
{
static constexpr char bad[] = {'*', '\'', '%'};
return util::in_token(c) &&
std::find(std::begin(bad), std::end(bad), c) == std::end(bad);
}
StringRef percent_encode_token(BlockAllocator& balloc,
const StringRef& target)
{
auto iov = make_byte_ref(balloc, target.size() * 3 + 1);
auto p = iov.base;
for (auto first = std::begin(target); first != std::end(target); ++first)
{
uint8_t c = *first;
if (c != '%' && in_token(c))
{
*p++ = c;
continue;
}
*p++ = '%';
*p++ = UPPER_XDIGITS[c >> 4];
*p++ = UPPER_XDIGITS[(c & 0x0f)];
}
*p = '\0';
return StringRef{iov.base, p};
}
uint32_t hex_to_uint(char c)
{
if (c <= '9')
{
return c - '0';
}
if (c <= 'Z')
{
return c - 'A' + 10;
}
if (c <= 'z')
{
return c - 'a' + 10;
}
return 256;
}
StringRef quote_string(BlockAllocator& balloc, const StringRef& target)
{
auto cnt = std::count(std::begin(target), std::end(target), '"');
if (cnt == 0)
{
return make_string_ref(balloc, target);
}
auto iov = make_byte_ref(balloc, target.size() + cnt + 1);
auto p = iov.base;
for (auto c : target)
{
if (c == '"')
{
*p++ = '\\';
*p++ = '"';
}
else
{
*p++ = c;
}
}
*p = '\0';
return StringRef{iov.base, p};
}
namespace
{
template <typename Iterator>
Iterator cpydig(Iterator d, uint32_t n, size_t len)
{
auto p = d + len - 1;
do
{
*p-- = (n % 10) + '0';
n /= 10;
}
while (p >= d);
return d + len;
}
} // namespace
namespace
{
constexpr const char* MONTH[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
constexpr const char* DAY_OF_WEEK[] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"
};
} // namespace
std::string http_date(time_t t)
{
/* Sat, 27 Sep 2014 06:31:15 GMT */
std::string res(29, 0);
http_date(&res[0], t);
return res;
}
char* http_date(char* res, time_t t)
{
struct tm tms;
#if defined(_MSC_VER)
auto err = gmtime_s(&tms, &t);
if (err)
{
return res;
}
#else
if (gmtime_r(&t, &tms) == nullptr)
{
return res;
}
#endif
auto p = res;
auto s = DAY_OF_WEEK[tms.tm_wday];
p = std::copy_n(s, 3, p);
*p++ = ',';
*p++ = ' ';
p = cpydig(p, tms.tm_mday, 2);
*p++ = ' ';
s = MONTH[tms.tm_mon];
p = std::copy_n(s, 3, p);
*p++ = ' ';
p = cpydig(p, tms.tm_year + 1900, 4);
*p++ = ' ';
p = cpydig(p, tms.tm_hour, 2);
*p++ = ':';
p = cpydig(p, tms.tm_min, 2);
*p++ = ':';
p = cpydig(p, tms.tm_sec, 2);
s = " GMT";
p = std::copy_n(s, 4, p);
return p;
}
std::string common_log_date(time_t t)
{
// 03/Jul/2014:00:19:38 +0900
std::string res(26, 0);
common_log_date(&res[0], t);
return res;
}
char* common_log_date(char* res, time_t t)
{
struct tm tms;
#if defined(_MSC_VER)
auto err = localtime_s(&tms, &t);
if (err)
{
return res;
}
#else
if (localtime_r(&t, &tms) == nullptr)
{
return res;
}
#endif
auto p = res;
p = cpydig(p, tms.tm_mday, 2);
*p++ = '/';
auto s = MONTH[tms.tm_mon];
p = std::copy_n(s, 3, p);
*p++ = '/';
p = cpydig(p, tms.tm_year + 1900, 4);
*p++ = ':';
p = cpydig(p, tms.tm_hour, 2);
*p++ = ':';
p = cpydig(p, tms.tm_min, 2);
*p++ = ':';
p = cpydig(p, tms.tm_sec, 2);
*p++ = ' ';
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
auto gmtoff = tms.tm_gmtoff;
#else // !HAVE_STRUCT_TM_TM_GMTOFF
auto gmtoff = nghttp2_timegm(&tms) - t;
#endif // !HAVE_STRUCT_TM_TM_GMTOFF
if (gmtoff >= 0)
{
*p++ = '+';
}
else
{
*p++ = '-';
gmtoff = -gmtoff;
}
p = cpydig(p, gmtoff / 3600, 2);
p = cpydig(p, (gmtoff % 3600) / 60, 2);
return p;
}
std::string iso8601_date(int64_t ms)
{
// 2014-11-15T12:58:24.741Z
// 2014-11-15T12:58:24.741+09:00
std::string res(29, 0);
auto p = iso8601_date(&res[0], ms);
res.resize(p - &res[0]);
return res;
}
char* iso8601_date(char* res, int64_t ms)
{
time_t sec = ms / 1000;
tm tms;
#if defined(_MSC_VER)
auto err = localtime_s(&tms, &sec);
if (err)
{
return res;
}
#else
if (localtime_r(&sec, &tms) == nullptr)
{
return res;
}
#endif
auto p = res;
p = cpydig(p, tms.tm_year + 1900, 4);
*p++ = '-';
p = cpydig(p, tms.tm_mon + 1, 2);
*p++ = '-';
p = cpydig(p, tms.tm_mday, 2);
*p++ = 'T';
p = cpydig(p, tms.tm_hour, 2);
*p++ = ':';
p = cpydig(p, tms.tm_min, 2);
*p++ = ':';
p = cpydig(p, tms.tm_sec, 2);
*p++ = '.';
p = cpydig(p, ms % 1000, 3);
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
auto gmtoff = tms.tm_gmtoff;
#else // !HAVE_STRUCT_TM_TM_GMTOFF
auto gmtoff = nghttp2_timegm(&tms) - sec;
#endif // !HAVE_STRUCT_TM_TM_GMTOFF
if (gmtoff == 0)
{
*p++ = 'Z';
}
else
{
if (gmtoff > 0)
{
*p++ = '+';
}
else
{
*p++ = '-';
gmtoff = -gmtoff;
}
p = cpydig(p, gmtoff / 3600, 2);
*p++ = ':';
p = cpydig(p, (gmtoff % 3600) / 60, 2);
}
return p;
}
#ifdef _WIN32
namespace bt = boost::posix_time;
// one-time definition of the locale that is used to parse UTC strings
// (note that the time_input_facet is ref-counted and deleted automatically)
static const std::locale
ptime_locale(std::locale::classic(),
new bt::time_input_facet("%a, %d %b %Y %H:%M:%S GMT"));
#endif //_WIN32
time_t parse_http_date(const StringRef& s)
{
#ifdef _WIN32
// there is no strptime - use boost
std::stringstream sstr(s.str());
sstr.imbue(ptime_locale);
bt::ptime ltime;
sstr >> ltime;
if (!sstr)
{
return 0;
}
return boost::posix_time::to_time_t(ltime);
#else // !_WIN32
tm tm {};
char* r = strptime(s.c_str(), "%a, %d %b %Y %H:%M:%S GMT", &tm);
if (r == 0)
{
return 0;
}
return nghttp2_timegm_without_yday(&tm);
#endif // !_WIN32
}
char upcase(char c)
{
if ('a' <= c && c <= 'z')
{
return c - 'a' + 'A';
}
else
{
return c;
}
}
std::string format_hex(const unsigned char* s, size_t len)
{
std::string res;
res.resize(len * 2);
for (size_t i = 0; i < len; ++i)
{
unsigned char c = s[i];
res[i * 2] = LOWER_XDIGITS[c >> 4];
res[i * 2 + 1] = LOWER_XDIGITS[c & 0x0f];
}
return res;
}
StringRef format_hex(BlockAllocator& balloc, const StringRef& s)
{
auto iov = make_byte_ref(balloc, s.size() * 2 + 1);
auto p = iov.base;
for (auto cc : s)
{
uint8_t c = cc;
*p++ = LOWER_XDIGITS[c >> 4];
*p++ = LOWER_XDIGITS[c & 0xf];
}
*p = '\0';
return StringRef{iov.base, p};
}
void to_token68(std::string& base64str)
{
std::transform(std::begin(base64str), std::end(base64str),
std::begin(base64str), [](char c)
{
switch (c)
{
case '+':
return '-';
case '/':
return '_';
default:
return c;
}
});
base64str.erase(std::find(std::begin(base64str), std::end(base64str), '='),
std::end(base64str));
}
StringRef to_base64(BlockAllocator& balloc, const StringRef& token68str)
{
// At most 3 padding '='
auto len = token68str.size() + 3;
auto iov = make_byte_ref(balloc, len + 1);
auto p = iov.base;
p = std::transform(std::begin(token68str), std::end(token68str), p,
[](char c)
{
switch (c)
{
case '-':
return '+';
case '_':
return '/';
default:
return c;
}
});
auto rem = token68str.size() & 0x3;
if (rem)
{
p = std::fill_n(p, 4 - rem, '=');
}
*p = '\0';
return StringRef{iov.base, p};
}
namespace
{
// Calculates Damerau–Levenshtein distance between c-string a and b
// with given costs. swapcost, subcost, addcost and delcost are cost
// to swap 2 adjacent characters, substitute characters, add character
// and delete character respectively.
int levenshtein(const char* a, int alen, const char* b, int blen, int swapcost,
int subcost, int addcost, int delcost)
{
auto dp = std::vector<std::vector<int>>(3, std::vector<int>(blen + 1));
for (int i = 0; i <= blen; ++i)
{
dp[1][i] = i;
}
for (int i = 1; i <= alen; ++i)
{
dp[0][0] = i;
for (int j = 1; j <= blen; ++j)
{
dp[0][j] = dp[1][j - 1] + (a[i - 1] == b[j - 1] ? 0 : subcost);
if (i >= 2 && j >= 2 && a[i - 1] != b[j - 1] && a[i - 2] == b[j - 1] &&
a[i - 1] == b[j - 2])
{
dp[0][j] = std::min(dp[0][j], dp[2][j - 2] + swapcost);
}
dp[0][j] = std::min(dp[0][j],
std::min(dp[1][j] + delcost, dp[0][j - 1] + addcost));
}
std::rotate(std::begin(dp), std::begin(dp) + 2, std::end(dp));
}
return dp[1][blen];
}
} // namespace
void show_candidates(const char* unkopt, const option* options)
{
for (; *unkopt == '-'; ++unkopt)
;
if (*unkopt == '\0')
{
return;
}
auto unkoptend = unkopt;
for (; *unkoptend && *unkoptend != '='; ++unkoptend)
;
auto unkoptlen = unkoptend - unkopt;
if (unkoptlen == 0)
{
return;
}
int prefix_match = 0;
auto cands = std::vector<std::pair<int, const char*>>();
for (size_t i = 0; options[i].name != nullptr; ++i)
{
auto optnamelen = strlen(options[i].name);
// Use cost 0 for prefix match
if (istarts_with(options[i].name, options[i].name + optnamelen, unkopt,
unkopt + unkoptlen))
{
if (optnamelen == static_cast<size_t>(unkoptlen))
{
// Exact match, then we don't show any condidates.
return;
}
++prefix_match;
cands.emplace_back(0, options[i].name);
continue;
}
// Use cost 0 for suffix match, but match at least 3 characters
if (unkoptlen >= 3 &&
iends_with(options[i].name, options[i].name + optnamelen, unkopt,
unkopt + unkoptlen))
{
cands.emplace_back(0, options[i].name);
continue;
}
// cost values are borrowed from git, help.c.
int sim =
levenshtein(unkopt, unkoptlen, options[i].name, optnamelen, 0, 2, 1, 3);
cands.emplace_back(sim, options[i].name);
}
if (prefix_match == 1 || cands.empty())
{
return;
}
std::sort(std::begin(cands), std::end(cands));
int threshold = cands[0].first;
// threshold value is a magic value.
if (threshold > 6)
{
return;
}
std::cerr << "\nDid you mean:\n";
for (auto& item : cands)
{
if (item.first > threshold)
{
break;
}
std::cerr << "\t--" << item.second << "\n";
}
}
bool has_uri_field(const http_parser_url& u, http_parser_url_fields field)
{
return u.field_set & (1 << field);
}
bool fieldeq(const char* uri1, const http_parser_url& u1, const char* uri2,
const http_parser_url& u2, http_parser_url_fields field)
{
if (!has_uri_field(u1, field))
{
if (!has_uri_field(u2, field))
{
return true;
}
else
{
return false;
}
}
else if (!has_uri_field(u2, field))
{
return false;
}
if (u1.field_data[field].len != u2.field_data[field].len)
{
return false;
}
return memcmp(uri1 + u1.field_data[field].off,
uri2 + u2.field_data[field].off, u1.field_data[field].len) == 0;
}
bool fieldeq(const char* uri, const http_parser_url& u,
http_parser_url_fields field, const char* t)
{
return fieldeq(uri, u, field, StringRef{t});
}
bool fieldeq(const char* uri, const http_parser_url& u,
http_parser_url_fields field, const StringRef& t)
{
if (!has_uri_field(u, field))
{
return t.empty();
}
auto& f = u.field_data[field];
return StringRef{uri + f.off, f.len} == t;
}
StringRef get_uri_field(const char* uri, const http_parser_url& u,
http_parser_url_fields field)
{
if (!util::has_uri_field(u, field))
{
return StringRef{};
}
return StringRef{uri + u.field_data[field].off, u.field_data[field].len};
}
uint16_t get_default_port(const char* uri, const http_parser_url& u)
{
if (util::fieldeq(uri, u, UF_SCHEMA, "https"))
{
return 443;
}
else if (util::fieldeq(uri, u, UF_SCHEMA, "http"))
{
return 80;
}
else
{
return 443;
}
}
bool porteq(const char* uri1, const http_parser_url& u1, const char* uri2,
const http_parser_url& u2)
{
uint16_t port1, port2;
port1 =
util::has_uri_field(u1, UF_PORT) ? u1.port : get_default_port(uri1, u1);
port2 =
util::has_uri_field(u2, UF_PORT) ? u2.port : get_default_port(uri2, u2);
return port1 == port2;
}
void write_uri_field(std::ostream& o, const char* uri, const http_parser_url& u,
http_parser_url_fields field)
{
if (util::has_uri_field(u, field))
{
o.write(uri + u.field_data[field].off, u.field_data[field].len);
}
}
bool numeric_host(const char* hostname)
{
return numeric_host(hostname, AF_INET) || numeric_host(hostname, AF_INET6);
}
bool numeric_host(const char* hostname, int family)
{
int rv;
std::array<uint8_t, sizeof(struct in6_addr)> dst;
rv = nghttp2_inet_pton(family, hostname, dst.data());
return rv == 1;
}
std::string numeric_name(const struct sockaddr* sa, socklen_t salen)
{
std::array<char, NI_MAXHOST> host;
auto rv = getnameinfo(sa, salen, host.data(), host.size(), nullptr, 0,
NI_NUMERICHOST);
if (rv != 0)
{
return "unknown";
}
return host.data();
}
std::string to_numeric_addr(const Address* addr)
{
auto family = addr->su.storage.ss_family;
#ifndef _WIN32
if (family == AF_UNIX)
{
return addr->su.un.sun_path;
}
#endif // !_WIN32
std::array<char, NI_MAXHOST> host;
std::array<char, NI_MAXSERV> serv;
auto rv =
getnameinfo(&addr->su.sa, addr->len, host.data(), host.size(),
serv.data(), serv.size(), NI_NUMERICHOST | NI_NUMERICSERV);
if (rv != 0)
{
return "unknown";
}
auto hostlen = strlen(host.data());
auto servlen = strlen(serv.data());
std::string s;
char* p;
if (family == AF_INET6)
{
s.resize(hostlen + servlen + 2 + 1);
p = &s[0];
*p++ = '[';
p = std::copy_n(host.data(), hostlen, p);
*p++ = ']';
}
else
{
s.resize(hostlen + servlen + 1);
p = &s[0];
p = std::copy_n(host.data(), hostlen, p);
}
*p++ = ':';
std::copy_n(serv.data(), servlen, p);
return s;
}
void set_port(Address& addr, uint16_t port)
{
switch (addr.su.storage.ss_family)
{
case AF_INET:
addr.su.in.sin_port = htons(port);
break;
case AF_INET6:
addr.su.in6.sin6_port = htons(port);
break;
}
}
std::string ascii_dump(const uint8_t* data, size_t len)
{
std::string res;
for (size_t i = 0; i < len; ++i)
{
auto c = data[i];
if (c >= 0x20 && c < 0x7f)
{
res += c;
}
else
{
res += '.';
}
}
return res;
}
char* get_exec_path(int argc, char** const argv, const char* cwd)
{
if (argc == 0 || cwd == nullptr)
{
return nullptr;
}
auto argv0 = argv[0];
auto len = strlen(argv0);
char* path;
if (argv0[0] == '/')
{
path = static_cast<char*>(malloc(len + 1));
if (path == nullptr)
{
return nullptr;
}
memcpy(path, argv0, len + 1);
}
else
{
auto cwdlen = strlen(cwd);
path = static_cast<char*>(malloc(len + 1 + cwdlen + 1));
if (path == nullptr)
{
return nullptr;
}
memcpy(path, cwd, cwdlen);
path[cwdlen] = '/';
memcpy(path + cwdlen + 1, argv0, len + 1);
}
return path;
}
bool check_path(const std::string& path)
{
// We don't like '\' in path.
return !path.empty() && path[0] == '/' &&
path.find('\\') == std::string::npos &&
path.find("/../") == std::string::npos &&
path.find("/./") == std::string::npos &&
!util::ends_with_l(path, "/..") && !util::ends_with_l(path, "/.");
}
int64_t to_time64(const timeval& tv)
{
return tv.tv_sec * 1000000 + tv.tv_usec;
}
bool check_h2_is_selected(const StringRef& proto)
{
return streq(NGHTTP2_H2, proto) || streq(NGHTTP2_H2_16, proto) ||
streq(NGHTTP2_H2_14, proto);
}
namespace
{
bool select_proto(const unsigned char** out, unsigned char* outlen,
const unsigned char* in, unsigned int inlen,
const StringRef& key)
{
for (auto p = in, end = in + inlen; p + key.size() <= end; p += *p + 1)
{
if (std::equal(std::begin(key), std::end(key), p))
{
*out = p + 1;
*outlen = *p;
return true;
}
}
return false;
}
} // namespace
bool select_h2(const unsigned char** out, unsigned char* outlen,
const unsigned char* in, unsigned int inlen)
{
return select_proto(out, outlen, in, inlen, NGHTTP2_H2_ALPN) ||
select_proto(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN) ||
select_proto(out, outlen, in, inlen, NGHTTP2_H2_14_ALPN);
}
bool select_protocol(const unsigned char** out, unsigned char* outlen,
const unsigned char* in, unsigned int inlen,
std::vector<std::string> proto_list)
{
for (const auto& proto : proto_list)
{
if (select_proto(out, outlen, in, inlen, StringRef{proto}))
{
return true;
}