forked from nghttp2/nghttp2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrpx_config.cc
1345 lines (1067 loc) · 35 KB
/
shrpx_config.cc
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 "shrpx_config.h"
#include <pwd.h>
#include <netdb.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <limits>
#include <fstream>
#include <nghttp2/nghttp2.h>
#include "http-parser/http_parser.h"
#include "shrpx_log.h"
#include "shrpx_ssl.h"
#include "shrpx_http.h"
#include "http2.h"
#include "util.h"
#include "template.h"
using namespace nghttp2;
namespace shrpx {
const char SHRPX_OPT_PRIVATE_KEY_FILE[] = "private-key-file";
const char SHRPX_OPT_PRIVATE_KEY_PASSWD_FILE[] = "private-key-passwd-file";
const char SHRPX_OPT_CERTIFICATE_FILE[] = "certificate-file";
const char SHRPX_OPT_DH_PARAM_FILE[] = "dh-param-file";
const char SHRPX_OPT_SUBCERT[] = "subcert";
const char SHRPX_OPT_BACKEND[] = "backend";
const char SHRPX_OPT_FRONTEND[] = "frontend";
const char SHRPX_OPT_WORKERS[] = "workers";
const char SHRPX_OPT_HTTP2_MAX_CONCURRENT_STREAMS[] =
"http2-max-concurrent-streams";
const char SHRPX_OPT_LOG_LEVEL[] = "log-level";
const char SHRPX_OPT_DAEMON[] = "daemon";
const char SHRPX_OPT_HTTP2_PROXY[] = "http2-proxy";
const char SHRPX_OPT_HTTP2_BRIDGE[] = "http2-bridge";
const char SHRPX_OPT_CLIENT_PROXY[] = "client-proxy";
const char SHRPX_OPT_ADD_X_FORWARDED_FOR[] = "add-x-forwarded-for";
const char SHRPX_OPT_STRIP_INCOMING_X_FORWARDED_FOR[] =
"strip-incoming-x-forwarded-for";
const char SHRPX_OPT_NO_VIA[] = "no-via";
const char SHRPX_OPT_FRONTEND_HTTP2_READ_TIMEOUT[] =
"frontend-http2-read-timeout";
const char SHRPX_OPT_FRONTEND_READ_TIMEOUT[] = "frontend-read-timeout";
const char SHRPX_OPT_FRONTEND_WRITE_TIMEOUT[] = "frontend-write-timeout";
const char SHRPX_OPT_BACKEND_READ_TIMEOUT[] = "backend-read-timeout";
const char SHRPX_OPT_BACKEND_WRITE_TIMEOUT[] = "backend-write-timeout";
const char SHRPX_OPT_STREAM_READ_TIMEOUT[] = "stream-read-timeout";
const char SHRPX_OPT_STREAM_WRITE_TIMEOUT[] = "stream-write-timeout";
const char SHRPX_OPT_ACCESSLOG_FILE[] = "accesslog-file";
const char SHRPX_OPT_ACCESSLOG_SYSLOG[] = "accesslog-syslog";
const char SHRPX_OPT_ACCESSLOG_FORMAT[] = "accesslog-format";
const char SHRPX_OPT_ERRORLOG_FILE[] = "errorlog-file";
const char SHRPX_OPT_ERRORLOG_SYSLOG[] = "errorlog-syslog";
const char SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT[] =
"backend-keep-alive-timeout";
const char SHRPX_OPT_FRONTEND_HTTP2_WINDOW_BITS[] =
"frontend-http2-window-bits";
const char SHRPX_OPT_BACKEND_HTTP2_WINDOW_BITS[] = "backend-http2-window-bits";
const char SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_BITS[] =
"frontend-http2-connection-window-bits";
const char SHRPX_OPT_BACKEND_HTTP2_CONNECTION_WINDOW_BITS[] =
"backend-http2-connection-window-bits";
const char SHRPX_OPT_FRONTEND_NO_TLS[] = "frontend-no-tls";
const char SHRPX_OPT_BACKEND_NO_TLS[] = "backend-no-tls";
const char SHRPX_OPT_BACKEND_TLS_SNI_FIELD[] = "backend-tls-sni-field";
const char SHRPX_OPT_PID_FILE[] = "pid-file";
const char SHRPX_OPT_USER[] = "user";
const char SHRPX_OPT_SYSLOG_FACILITY[] = "syslog-facility";
const char SHRPX_OPT_BACKLOG[] = "backlog";
const char SHRPX_OPT_CIPHERS[] = "ciphers";
const char SHRPX_OPT_CLIENT[] = "client";
const char SHRPX_OPT_INSECURE[] = "insecure";
const char SHRPX_OPT_CACERT[] = "cacert";
const char SHRPX_OPT_BACKEND_IPV4[] = "backend-ipv4";
const char SHRPX_OPT_BACKEND_IPV6[] = "backend-ipv6";
const char SHRPX_OPT_BACKEND_HTTP_PROXY_URI[] = "backend-http-proxy-uri";
const char SHRPX_OPT_READ_RATE[] = "read-rate";
const char SHRPX_OPT_READ_BURST[] = "read-burst";
const char SHRPX_OPT_WRITE_RATE[] = "write-rate";
const char SHRPX_OPT_WRITE_BURST[] = "write-burst";
const char SHRPX_OPT_WORKER_READ_RATE[] = "worker-read-rate";
const char SHRPX_OPT_WORKER_READ_BURST[] = "worker-read-burst";
const char SHRPX_OPT_WORKER_WRITE_RATE[] = "worker-write-rate";
const char SHRPX_OPT_WORKER_WRITE_BURST[] = "worker-write-burst";
const char SHRPX_OPT_NPN_LIST[] = "npn-list";
const char SHRPX_OPT_TLS_PROTO_LIST[] = "tls-proto-list";
const char SHRPX_OPT_VERIFY_CLIENT[] = "verify-client";
const char SHRPX_OPT_VERIFY_CLIENT_CACERT[] = "verify-client-cacert";
const char SHRPX_OPT_CLIENT_PRIVATE_KEY_FILE[] = "client-private-key-file";
const char SHRPX_OPT_CLIENT_CERT_FILE[] = "client-cert-file";
const char SHRPX_OPT_FRONTEND_HTTP2_DUMP_REQUEST_HEADER[] =
"frontend-http2-dump-request-header";
const char SHRPX_OPT_FRONTEND_HTTP2_DUMP_RESPONSE_HEADER[] =
"frontend-http2-dump-response-header";
const char SHRPX_OPT_HTTP2_NO_COOKIE_CRUMBLING[] = "http2-no-cookie-crumbling";
const char SHRPX_OPT_FRONTEND_FRAME_DEBUG[] = "frontend-frame-debug";
const char SHRPX_OPT_PADDING[] = "padding";
const char SHRPX_OPT_ALTSVC[] = "altsvc";
const char SHRPX_OPT_ADD_RESPONSE_HEADER[] = "add-response-header";
const char SHRPX_OPT_WORKER_FRONTEND_CONNECTIONS[] =
"worker-frontend-connections";
const char SHRPX_OPT_NO_LOCATION_REWRITE[] = "no-location-rewrite";
const char SHRPX_OPT_NO_HOST_REWRITE[] = "no-host-rewrite";
const char SHRPX_OPT_BACKEND_HTTP1_CONNECTIONS_PER_HOST[] =
"backend-http1-connections-per-host";
const char SHRPX_OPT_BACKEND_HTTP1_CONNECTIONS_PER_FRONTEND[] =
"backend-http1-connections-per-frontend";
const char SHRPX_OPT_LISTENER_DISABLE_TIMEOUT[] = "listener-disable-timeout";
const char SHRPX_OPT_TLS_TICKET_KEY_FILE[] = "tls-ticket-key-file";
const char SHRPX_OPT_RLIMIT_NOFILE[] = "rlimit-nofile";
const char SHRPX_OPT_TLS_CTX_PER_WORKER[] = "tls-ctx-per-worker";
const char SHRPX_OPT_BACKEND_REQUEST_BUFFER[] = "backend-request-buffer";
const char SHRPX_OPT_BACKEND_RESPONSE_BUFFER[] = "backend-response-buffer";
const char SHRPX_OPT_NO_SERVER_PUSH[] = "no-server-push";
namespace {
Config *config = nullptr;
} // namespace
const Config *get_config() { return config; }
Config *mod_config() { return config; }
void create_config() { config = new Config(); }
TicketKeys::~TicketKeys() {
/* Erase keys from memory */
for (auto &key : keys) {
memset(&key, 0, sizeof(key));
}
}
namespace {
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
const char *hostport) {
// host and port in |hostport| is separated by single ','.
const char *p = strchr(hostport, ',');
if (!p) {
LOG(ERROR) << "Invalid host, port: " << hostport;
return -1;
}
size_t len = p - hostport;
if (hostlen < len + 1) {
LOG(ERROR) << "Hostname too long: " << hostport;
return -1;
}
memcpy(host, hostport, len);
host[len] = '\0';
errno = 0;
unsigned long d = strtoul(p + 1, nullptr, 10);
if (errno == 0 && 1 <= d && d <= std::numeric_limits<uint16_t>::max()) {
*port_ptr = d;
return 0;
} else {
LOG(ERROR) << "Port is invalid: " << p + 1;
return -1;
}
}
} // namespace
namespace {
bool is_secure(const char *filename) {
struct stat buf;
int rv = stat(filename, &buf);
if (rv == 0) {
if ((buf.st_mode & S_IRWXU) && !(buf.st_mode & S_IRWXG) &&
!(buf.st_mode & S_IRWXO)) {
return true;
}
}
return false;
}
} // namespace
std::unique_ptr<TicketKeys>
read_tls_ticket_key_file(const std::vector<std::string> &files) {
auto ticket_keys = make_unique<TicketKeys>();
auto &keys = ticket_keys->keys;
keys.resize(files.size());
size_t i = 0;
for (auto &file : files) {
std::ifstream f(file.c_str());
if (!f) {
LOG(ERROR) << "tls-ticket-key-file: could not open file " << file;
return nullptr;
}
char buf[48];
f.read(buf, sizeof(buf));
if (f.gcount() != sizeof(buf)) {
LOG(ERROR) << "tls-ticket-key-file: want to read 48 bytes but read "
<< f.gcount() << " bytes from " << file;
return nullptr;
}
auto &key = keys[i++];
auto p = buf;
memcpy(key.name, p, sizeof(key.name));
p += sizeof(key.name);
memcpy(key.aes_key, p, sizeof(key.aes_key));
p += sizeof(key.aes_key);
memcpy(key.hmac_key, p, sizeof(key.hmac_key));
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "session ticket key: " << util::format_hex(key.name,
sizeof(key.name));
}
}
return ticket_keys;
}
FILE *open_file_for_write(const char *filename) {
#if defined O_CLOEXEC
auto fd = open(filename, O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR);
#else
auto fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
// We get race condition if execve is called at the same time.
if (fd != -1) {
make_socket_closeonexec(fd);
}
#endif
if (fd == -1) {
LOG(ERROR) << "Failed to open " << filename
<< " for writing. Cause: " << strerror(errno);
return nullptr;
}
auto f = fdopen(fd, "wb");
if (f == nullptr) {
LOG(ERROR) << "Failed to open " << filename
<< " for writing. Cause: " << strerror(errno);
return nullptr;
}
return f;
}
std::string read_passwd_from_file(const char *filename) {
std::string line;
if (!is_secure(filename)) {
LOG(ERROR) << "Private key passwd file " << filename
<< " has insecure mode.";
return line;
}
std::ifstream in(filename, std::ios::binary);
if (!in) {
LOG(ERROR) << "Could not open key passwd file " << filename;
return line;
}
std::getline(in, line);
return line;
}
std::unique_ptr<char[]> strcopy(const char *val) {
return strcopy(val, strlen(val));
}
std::unique_ptr<char[]> strcopy(const char *val, size_t len) {
auto res = make_unique<char[]>(len + 1);
memcpy(res.get(), val, len);
res[len] = '\0';
return res;
}
std::unique_ptr<char[]> strcopy(const std::string &val) {
return strcopy(val.c_str(), val.size());
}
std::vector<char *> parse_config_str_list(const char *s) {
size_t len = 1;
for (const char *first = s, *p = nullptr; (p = strchr(first, ','));
++len, first = p + 1)
;
auto list = std::vector<char *>(len);
auto first = strdup(s);
len = 0;
for (;;) {
auto p = strchr(first, ',');
if (p == nullptr) {
break;
}
list[len++] = first;
*p = '\0';
first = p + 1;
}
list[len++] = first;
return list;
}
void clear_config_str_list(std::vector<char *> &list) {
if (list.empty()) {
return;
}
free(list[0]);
list.clear();
}
std::pair<std::string, std::string> parse_header(const char *optarg) {
// We skip possible ":" at the start of optarg.
const auto *colon = strchr(optarg + 1, ':');
// name = ":" is not allowed
if (colon == nullptr || (optarg[0] == ':' && colon == optarg + 1)) {
return {"", ""};
}
auto value = colon + 1;
for (; *value == '\t' || *value == ' '; ++value)
;
return {std::string(optarg, colon), std::string(value, strlen(value))};
}
template <typename T>
int parse_uint(T *dest, const char *opt, const char *optarg) {
char *end = nullptr;
errno = 0;
auto val = strtol(optarg, &end, 10);
if (!optarg[0] || errno != 0 || *end || val < 0) {
LOG(ERROR) << opt << ": bad value. Specify an integer >= 0.";
return -1;
}
*dest = val;
return 0;
}
namespace {
template <typename T>
int parse_uint_with_unit(T *dest, const char *opt, const char *optarg) {
auto n = util::parse_uint_with_unit(optarg);
if (n == -1) {
LOG(ERROR) << opt << ": bad value: '" << optarg << "'";
return -1;
}
*dest = n;
return 0;
}
} // namespace
template <typename T>
int parse_int(T *dest, const char *opt, const char *optarg) {
char *end = nullptr;
errno = 0;
auto val = strtol(optarg, &end, 10);
if (!optarg[0] || errno != 0 || *end) {
LOG(ERROR) << opt << ": bad value. Specify an integer.";
return -1;
}
*dest = val;
return 0;
}
namespace {
LogFragment make_log_fragment(LogFragmentType type,
std::unique_ptr<char[]> value = nullptr) {
return LogFragment{type, std::move(value)};
}
} // namespace
namespace {
bool var_token(char c) {
return util::isAlpha(c) || util::isDigit(c) || c == '_';
}
} // namespace
std::vector<LogFragment> parse_log_format(const char *optarg) {
auto literal_start = optarg;
auto p = optarg;
auto eop = p + strlen(optarg);
auto res = std::vector<LogFragment>();
for (; p != eop;) {
if (*p != '$') {
++p;
continue;
}
auto var_start = p;
++p;
for (; p != eop && var_token(*p); ++p)
;
auto varlen = p - var_start;
auto type = SHRPX_LOGF_NONE;
const char *value = nullptr;
size_t valuelen = 0;
if (util::strieq("$remote_addr", var_start, varlen)) {
type = SHRPX_LOGF_REMOTE_ADDR;
} else if (util::strieq("$time_local", var_start, varlen)) {
type = SHRPX_LOGF_TIME_LOCAL;
} else if (util::strieq("$time_iso8601", var_start, varlen)) {
type = SHRPX_LOGF_TIME_ISO8601;
} else if (util::strieq("$request", var_start, varlen)) {
type = SHRPX_LOGF_REQUEST;
} else if (util::strieq("$status", var_start, varlen)) {
type = SHRPX_LOGF_STATUS;
} else if (util::strieq("$body_bytes_sent", var_start, varlen)) {
type = SHRPX_LOGF_BODY_BYTES_SENT;
} else if (util::istartsWith(var_start, varlen, "$http_")) {
type = SHRPX_LOGF_HTTP;
value = var_start + sizeof("$http_") - 1;
valuelen = varlen - (sizeof("$http_") - 1);
} else if (util::strieq("$remote_port", var_start, varlen)) {
type = SHRPX_LOGF_REMOTE_PORT;
} else if (util::strieq("$server_port", var_start, varlen)) {
type = SHRPX_LOGF_SERVER_PORT;
} else if (util::strieq("$request_time", var_start, varlen)) {
type = SHRPX_LOGF_REQUEST_TIME;
} else if (util::strieq("$pid", var_start, varlen)) {
type = SHRPX_LOGF_PID;
} else if (util::strieq("$alpn", var_start, varlen)) {
type = SHRPX_LOGF_ALPN;
} else {
LOG(WARN) << "Unrecognized log format variable: "
<< std::string(var_start, varlen);
continue;
}
if (literal_start < var_start) {
res.push_back(
make_log_fragment(SHRPX_LOGF_LITERAL,
strcopy(literal_start, var_start - literal_start)));
}
if (value == nullptr) {
res.push_back(make_log_fragment(type));
} else {
res.push_back(make_log_fragment(type, strcopy(value, valuelen)));
auto &v = res.back().value;
for (size_t i = 0; v[i]; ++i) {
if (v[i] == '_') {
v[i] = '-';
}
}
}
literal_start = var_start + varlen;
}
if (literal_start != eop) {
res.push_back(make_log_fragment(
SHRPX_LOGF_LITERAL, strcopy(literal_start, eop - literal_start)));
}
return res;
}
namespace {
int parse_duration(ev_tstamp *dest, const char *opt, const char *optarg) {
auto t = util::parse_duration_with_unit(optarg);
if (t == std::numeric_limits<double>::infinity()) {
LOG(ERROR) << opt << ": bad value: '" << optarg << "'";
return -1;
}
*dest = t;
return 0;
}
} // namespace
int parse_config(const char *opt, const char *optarg) {
char host[NI_MAXHOST];
uint16_t port;
if (util::strieq(opt, SHRPX_OPT_BACKEND)) {
if (split_host_port(host, sizeof(host), &port, optarg) == -1) {
return -1;
}
DownstreamAddr addr;
addr.host = strcopy(host);
addr.port = port;
mod_config()->downstream_addrs.push_back(std::move(addr));
return 0;
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND)) {
if (split_host_port(host, sizeof(host), &port, optarg) == -1) {
return -1;
}
mod_config()->host = strcopy(host);
mod_config()->port = port;
return 0;
}
if (util::strieq(opt, SHRPX_OPT_WORKERS)) {
return parse_uint(&mod_config()->num_worker, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_HTTP2_MAX_CONCURRENT_STREAMS)) {
return parse_uint(&mod_config()->http2_max_concurrent_streams, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_LOG_LEVEL)) {
if (Log::set_severity_level_by_name(optarg) == -1) {
LOG(ERROR) << opt << ": Invalid severity level: " << optarg;
return -1;
}
return 0;
}
if (util::strieq(opt, SHRPX_OPT_DAEMON)) {
mod_config()->daemon = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_HTTP2_PROXY)) {
mod_config()->http2_proxy = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_HTTP2_BRIDGE)) {
mod_config()->http2_bridge = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CLIENT_PROXY)) {
mod_config()->client_proxy = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_ADD_X_FORWARDED_FOR)) {
mod_config()->add_x_forwarded_for = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_STRIP_INCOMING_X_FORWARDED_FOR)) {
mod_config()->strip_incoming_x_forwarded_for = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_NO_VIA)) {
mod_config()->no_via = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_READ_TIMEOUT)) {
return parse_duration(&mod_config()->http2_upstream_read_timeout, opt,
optarg);
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_READ_TIMEOUT)) {
return parse_duration(&mod_config()->upstream_read_timeout, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_WRITE_TIMEOUT)) {
return parse_duration(&mod_config()->upstream_write_timeout, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_READ_TIMEOUT)) {
return parse_duration(&mod_config()->downstream_read_timeout, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_WRITE_TIMEOUT)) {
return parse_duration(&mod_config()->downstream_write_timeout, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_STREAM_READ_TIMEOUT)) {
return parse_duration(&mod_config()->stream_read_timeout, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_STREAM_WRITE_TIMEOUT)) {
return parse_duration(&mod_config()->stream_write_timeout, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_ACCESSLOG_FILE)) {
mod_config()->accesslog_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_ACCESSLOG_SYSLOG)) {
mod_config()->accesslog_syslog = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_ACCESSLOG_FORMAT)) {
mod_config()->accesslog_format = parse_log_format(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_ERRORLOG_FILE)) {
mod_config()->errorlog_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_ERRORLOG_SYSLOG)) {
mod_config()->errorlog_syslog = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT)) {
return parse_duration(&mod_config()->downstream_idle_read_timeout, opt,
optarg);
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_WINDOW_BITS) ||
util::strieq(opt, SHRPX_OPT_BACKEND_HTTP2_WINDOW_BITS)) {
size_t *resp;
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_WINDOW_BITS)) {
resp = &mod_config()->http2_upstream_window_bits;
} else {
resp = &mod_config()->http2_downstream_window_bits;
}
errno = 0;
int n;
if (parse_uint(&n, opt, optarg) != 0) {
return -1;
}
if (n >= 31) {
LOG(ERROR) << opt
<< ": specify the integer in the range [0, 30], inclusive";
return -1;
}
*resp = n;
return 0;
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_BITS) ||
util::strieq(opt, SHRPX_OPT_BACKEND_HTTP2_CONNECTION_WINDOW_BITS)) {
size_t *resp;
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_CONNECTION_WINDOW_BITS)) {
resp = &mod_config()->http2_upstream_connection_window_bits;
} else {
resp = &mod_config()->http2_downstream_connection_window_bits;
}
errno = 0;
int n;
if (parse_uint(&n, opt, optarg) != 0) {
return -1;
}
if (n < 16 || n >= 31) {
LOG(ERROR) << opt
<< ": specify the integer in the range [16, 30], inclusive";
return -1;
}
*resp = n;
return 0;
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_NO_TLS)) {
mod_config()->upstream_no_tls = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_NO_TLS)) {
mod_config()->downstream_no_tls = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_TLS_SNI_FIELD)) {
mod_config()->backend_tls_sni_name = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_PID_FILE)) {
mod_config()->pid_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_USER)) {
auto pwd = getpwnam(optarg);
if (!pwd) {
LOG(ERROR) << opt << ": failed to get uid from " << optarg << ": "
<< strerror(errno);
return -1;
}
mod_config()->user = strcopy(pwd->pw_name);
mod_config()->uid = pwd->pw_uid;
mod_config()->gid = pwd->pw_gid;
return 0;
}
if (util::strieq(opt, SHRPX_OPT_PRIVATE_KEY_FILE)) {
mod_config()->private_key_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_PRIVATE_KEY_PASSWD_FILE)) {
auto passwd = read_passwd_from_file(optarg);
if (passwd.empty()) {
LOG(ERROR) << opt << ": Couldn't read key file's passwd from " << optarg;
return -1;
}
mod_config()->private_key_passwd = strcopy(passwd);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CERTIFICATE_FILE)) {
mod_config()->cert_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_DH_PARAM_FILE)) {
mod_config()->dh_param_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_SUBCERT)) {
// Private Key file and certificate file separated by ':'.
const char *sp = strchr(optarg, ':');
if (sp) {
std::string keyfile(optarg, sp);
// TODO Do we need private key for subcert?
mod_config()->subcerts.emplace_back(keyfile, sp + 1);
}
return 0;
}
if (util::strieq(opt, SHRPX_OPT_SYSLOG_FACILITY)) {
int facility = int_syslog_facility(optarg);
if (facility == -1) {
LOG(ERROR) << opt << ": Unknown syslog facility: " << optarg;
return -1;
}
mod_config()->syslog_facility = facility;
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKLOG)) {
int n;
if (parse_int(&n, opt, optarg) != 0) {
return -1;
}
if (n < -1) {
LOG(ERROR) << opt << ": " << optarg << " is not allowed";
return -1;
}
mod_config()->backlog = n;
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CIPHERS)) {
mod_config()->ciphers = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CLIENT)) {
mod_config()->client = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_INSECURE)) {
mod_config()->insecure = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CACERT)) {
mod_config()->cacert = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_IPV4)) {
mod_config()->backend_ipv4 = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_IPV6)) {
mod_config()->backend_ipv6 = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_BACKEND_HTTP_PROXY_URI)) {
// parse URI and get hostname, port and optionally userinfo.
http_parser_url u;
memset(&u, 0, sizeof(u));
int rv = http_parser_parse_url(optarg, strlen(optarg), 0, &u);
if (rv == 0) {
std::string val;
if (u.field_set & UF_USERINFO) {
http2::copy_url_component(val, &u, UF_USERINFO, optarg);
// Surprisingly, u.field_set & UF_USERINFO is nonzero even if
// userinfo component is empty string.
if (!val.empty()) {
val = util::percentDecode(val.begin(), val.end());
mod_config()->downstream_http_proxy_userinfo = strcopy(val);
}
}
if (u.field_set & UF_HOST) {
http2::copy_url_component(val, &u, UF_HOST, optarg);
mod_config()->downstream_http_proxy_host = strcopy(val);
} else {
LOG(ERROR) << opt << ": no hostname specified";
return -1;
}
if (u.field_set & UF_PORT) {
mod_config()->downstream_http_proxy_port = u.port;
} else {
LOG(ERROR) << opt << ": no port specified";
return -1;
}
} else {
LOG(ERROR) << opt << ": parse error";
return -1;
}
return 0;
}
if (util::strieq(opt, SHRPX_OPT_READ_RATE)) {
return parse_uint_with_unit(&mod_config()->read_rate, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_READ_BURST)) {
return parse_uint_with_unit(&mod_config()->read_burst, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_WRITE_RATE)) {
return parse_uint_with_unit(&mod_config()->write_rate, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_WRITE_BURST)) {
return parse_uint_with_unit(&mod_config()->write_burst, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_WORKER_READ_RATE)) {
LOG(WARN) << opt << ": not implemented yet";
return parse_uint_with_unit(&mod_config()->worker_read_rate, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_WORKER_READ_BURST)) {
LOG(WARN) << opt << ": not implemented yet";
return parse_uint_with_unit(&mod_config()->worker_read_burst, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_WORKER_WRITE_RATE)) {
LOG(WARN) << opt << ": not implemented yet";
return parse_uint_with_unit(&mod_config()->worker_write_rate, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_WORKER_WRITE_BURST)) {
LOG(WARN) << opt << ": not implemented yet";
return parse_uint_with_unit(&mod_config()->worker_write_burst, opt, optarg);
}
if (util::strieq(opt, SHRPX_OPT_NPN_LIST)) {
clear_config_str_list(mod_config()->npn_list);
mod_config()->npn_list = parse_config_str_list(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_TLS_PROTO_LIST)) {
clear_config_str_list(mod_config()->tls_proto_list);
mod_config()->tls_proto_list = parse_config_str_list(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_VERIFY_CLIENT)) {
mod_config()->verify_client = util::strieq(optarg, "yes");
return 0;
}
if (util::strieq(opt, SHRPX_OPT_VERIFY_CLIENT_CACERT)) {
mod_config()->verify_client_cacert = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CLIENT_PRIVATE_KEY_FILE)) {
mod_config()->client_private_key_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_CLIENT_CERT_FILE)) {
mod_config()->client_cert_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_DUMP_REQUEST_HEADER)) {
mod_config()->http2_upstream_dump_request_header_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_FRONTEND_HTTP2_DUMP_RESPONSE_HEADER)) {
mod_config()->http2_upstream_dump_response_header_file = strcopy(optarg);
return 0;
}
if (util::strieq(opt, SHRPX_OPT_HTTP2_NO_COOKIE_CRUMBLING)) {
mod_config()->http2_no_cookie_crumbling = util::strieq(optarg, "yes");