-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathws_start_stop.cpp
More file actions
1950 lines (1715 loc) · 74 KB
/
Copy pathws_start_stop.cpp
File metadata and controls
1950 lines (1715 loc) · 74 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
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#if defined(_WIN32) && !defined(__CYGWIN__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <curl/curl.h>
#include <pthread.h>
#include <unistd.h>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <memory>
#include <string>
#include <utility>
// <gnutls/gnutls.h> no longer needed here. The previous
// version of `tls_info_resource` accessed `req.get_tls_session()` to
// inspect the raw gnutls_session_t; that accessor is gone. Migrated to
// `req.has_tls_session()` only.
#include "./httpserver.hpp"
#include "./littletest.hpp"
#include "./test_utils.hpp"
using std::shared_ptr;
#define STR2(p) #p
#define STR(p) STR2(p)
#ifdef HTTPSERVER_DATA_ROOT
#define ROOT STR(HTTPSERVER_DATA_ROOT)
#else
#define ROOT "."
#endif // HTTPSERVER_DATA_ROOT
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s) {
s->append(reinterpret_cast<char*>(ptr), size*nmemb);
return size*nmemb;
}
// File-local helper owning the curl easy-handle lifecycle for the
// plain GET round-trip repeated throughout this file: init, set the
// URL / HTTPGET / write-callback options (disabling TLS peer/host
// verification when verify_ssl is false), perform, cleanup. Returns
// the CURLcode from curl_easy_perform. Call sites that need extra
// options (client certs, CURLOPT_IPRESOLVE, CURLINFO_RESPONSE_CODE,
// custom methods, ...) keep their explicit easy-handles.
namespace {
CURLcode curl_get(const std::string& url, std::string* out, bool verify_ssl = true) {
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
if (!verify_ssl) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
}
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
} // namespace
class ok_resource : public httpserver::http_resource {
public:
httpserver::http_response render_get(const httpserver::http_request&) {
return httpserver::http_response::string("OK");
}
};
#ifdef HAVE_GNUTLS
// Migrated off the raw gnutls_session_t accessor (which has
// been removed from the public surface). The high-level
// `has_tls_session()` predicate carries the same observable signal for
// this test (the existing `tls_session_getters` test only checks for
// "TLS_SESSION_PRESENT").
class tls_info_resource : public httpserver::http_resource {
public:
httpserver::http_response render_get(const httpserver::http_request& req) {
std::string response = req.has_tls_session() ? "TLS_SESSION_PRESENT"
: "NO_TLS_SESSION";
return httpserver::http_response::string(response);
}
};
#endif // HAVE_GNUTLS
httpserver::http_response not_found_custom(const httpserver::http_request&) {
return httpserver::http_response::string("Not found custom").with_status(404);
}
httpserver::http_response not_allowed_custom(const httpserver::http_request&) {
return httpserver::http_response::string("Not allowed custom").with_status(405);
}
LT_BEGIN_SUITE(ws_start_stop_suite)
void set_up() {
}
void tear_down() {
}
LT_END_SUITE(ws_start_stop_suite)
// Wide skip of TLS / IPv6 / SNI / PSK / custom-socket / bind-address tests:
// MinGW64 curl + gnutls + MHD round-trips are flaky, IPv6 is gated on
// IPV6_TESTS_ENABLED (not set on the Windows lanes), and POSIX socket
// primitives the custom-socket test relies on are not cleanly exposed by
// MSYS. The simplest non-TLS GET round-trip is restored on Windows via the
// `windows_smoke` test (in a `#ifdef _WINDOWS` block
// below this skip).
// reason: see test/PORTABILITY.md §ws_start_stop.cpp wide skip.
#ifndef _WINDOWS
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop)
{ // NOLINT (internal scope opening - not method start)
httpserver::webserver ws{httpserver::create_webserver(0)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
}
{
httpserver::webserver ws{httpserver::create_webserver(0).start_method(httpserver::http::http_utils::INTERNAL_SELECT)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
}
{
httpserver::webserver ws{httpserver::create_webserver(0).start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
}
LT_END_AUTO_TEST(start_stop)
#if defined(IPV6_TESTS_ENABLED)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ipv6)
{ // NOLINT (internal scope opening - not method start)
httpserver::webserver ws{httpserver::create_webserver(0).use_ipv6()};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
std::string url = "http://localhost:" + std::to_string(port) + "/base";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
LT_END_AUTO_TEST(ipv6)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, dual_stack)
{ // NOLINT (internal scope opening - not method start)
httpserver::webserver ws{httpserver::create_webserver(0).use_dual_stack()};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
std::string url = "http://localhost:" + std::to_string(port) + "/base";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
LT_END_AUTO_TEST(dual_stack)
#endif
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, stop_and_wait)
httpserver::webserver ws{httpserver::create_webserver(0)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
}
ws.stop_and_wait();
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 7);
}
LT_END_AUTO_TEST(stop_and_wait)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options)
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl(false)
.use_ipv6(false)
.debug(false)
.pedantic(false)
#ifdef HAVE_BAUTH
.basic_auth(false)
#endif // HAVE_BAUTH
.digest_auth(false)
.deferred(false)
.regex_checking(false)
.ip_access_control(false)
.post_process(false)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(disable_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, enable_options)
httpserver::webserver ws{httpserver::create_webserver(0)
.debug()
.pedantic()
.deferred()
.regex_checking()
.ip_access_control()
.post_process()};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(enable_options)
// On macOS the user-bound listening socket interacts poorly with MHD's
// kqueue-based accept loop unless SO_REUSEPORT is set alongside
// SO_REUSEADDR; the bind-address tests below also depend on the same
// socket-setup path. Confirming the fix requires a macos-latest CI run
// with the modified setsockopt sequence.
// reason: see test/PORTABILITY.md §ws_start_stop.cpp custom_socket.
#ifndef DARWIN
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_socket)
int fd = socket(AF_INET, SOCK_STREAM, 0);
int one = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(0); // bind an ephemeral port; read it back below
bind(fd, (struct sockaddr*) &address, sizeof(address));
listen(fd, 10000);
struct sockaddr_in bound_addr;
socklen_t bound_len = sizeof(bound_addr);
getsockname(fd, (struct sockaddr*) &bound_addr, &bound_len);
const uint16_t port = ntohs(bound_addr.sin_port);
httpserver::webserver ws{httpserver::create_webserver(-1).bind_socket(fd)}; // whatever port here doesn't matter
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "127.0.0.1:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(custom_socket)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, bind_address_string)
httpserver::webserver ws{httpserver::create_webserver(0).bind_address("127.0.0.1")};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "127.0.0.1:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(bind_address_string)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, bind_address_string_invalid)
LT_CHECK_THROW(httpserver::create_webserver(0).bind_address("not_an_ip"));
LT_END_AUTO_TEST(bind_address_string_invalid)
#endif
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource)
httpserver::webserver ws{httpserver::create_webserver(0).single_resource()};
auto ok = std::make_shared<ok_resource>();
ws.register_prefix("/", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/any/url/works";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(single_resource)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource_not_default_resource)
httpserver::webserver ws{httpserver::create_webserver(0).single_resource()};
auto ok = std::make_shared<ok_resource>();
LT_CHECK_THROW(ws.register_prefix("/other", ok));
LT_CHECK_THROW(ws.register_path("/", ok));
ws.start(false);
ws.stop();
LT_END_AUTO_TEST(single_resource_not_default_resource)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, register_path_nullptr_throws)
httpserver::webserver ws{httpserver::create_webserver(0)};
// Both smart-pointer overloads throw on null.
LT_CHECK_THROW(ws.register_path("/test", std::shared_ptr<httpserver::http_resource>{}));
LT_CHECK_THROW(ws.register_path("/test", std::unique_ptr<httpserver::http_resource>{}));
LT_END_AUTO_TEST(register_path_nullptr_throws)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, register_empty_resource_non_family)
httpserver::webserver ws{httpserver::create_webserver(0)};
auto ok = std::make_shared<ok_resource>();
// Register empty resource as exact path (non-prefix)
ws.register_path("", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(register_empty_resource_non_family)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, register_path_with_url_params_non_family)
httpserver::webserver ws{httpserver::create_webserver(0).regex_checking()};
auto ok = std::make_shared<ok_resource>();
// Register resource with URL parameters as exact path
ws.register_path("/user/{id}", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/user/123";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(register_path_with_url_params_non_family)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, register_duplicate_resource_throws)
httpserver::webserver ws{httpserver::create_webserver(0)};
auto ok1 = std::make_shared<ok_resource>();
auto ok2 = std::make_shared<ok_resource>();
// First registration should succeed.
ws.register_path("/duplicate", ok1);
// The second registration of the same path now throws
// std::invalid_argument (replaces v1's silent `return false`).
LT_CHECK_THROW(ws.register_path("/duplicate", ok2));
// Registering as a prefix on the same path is now also
// forbidden — the (method, path) cache key cannot discriminate
// exact vs prefix. Earlier the call below silently succeeded.
LT_CHECK_THROW(ws.register_prefix("/duplicate", ok2));
LT_END_AUTO_TEST(register_duplicate_resource_throws)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads)
{ // NOLINT (internal scope opening - not method start)
httpserver::webserver ws{httpserver::create_webserver(0)
.start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION)
.max_threads(5)};
LT_CHECK_THROW(ws.start(false));
}
LT_END_AUTO_TEST(thread_per_connection_fails_with_max_threads)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads_stack_size)
{ // NOLINT (internal scope opening - not method start)
httpserver::webserver ws{httpserver::create_webserver(0)
.start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION)
.max_thread_stack_size(4*1024*1024)};
LT_CHECK_THROW(ws.start(false));
}
LT_END_AUTO_TEST(thread_per_connection_fails_with_max_threads_stack_size)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tuning_options)
httpserver::webserver ws{httpserver::create_webserver(0)
.max_connections(10)
.max_threads(10)
.memory_limit(10000)
.per_IP_connection_limit(10)
.max_thread_stack_size(4*1024*1024)
.nonce_nc_size(10)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
LT_CHECK_NOTHROW(ws.start(false));
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(tuning_options)
// use_ssl(true) on a HAVE_GNUTLS-off build now throws
// feature_unavailable at webserver construction (PRD-FLG-REQ-001 / §7).
// The TLS round-trip integ tests below only make sense when the library
// was built with TLS support, so gate them on HAVE_GNUTLS.
#ifdef HAVE_GNUTLS
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_base)
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "https://localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s, false);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(ssl_base)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_protocol_priorities)
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")
.https_priorities("NORMAL:-MD5")};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "https://localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s, false);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(ssl_with_protocol_priorities)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_trust)
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")
.https_mem_trust(ROOT "/test_root_ca.pem")};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "https://localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s, false);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(ssl_with_trust)
#endif // HAVE_GNUTLS
void* start_ws_blocking(void* par) {
httpserver::webserver* ws = (httpserver::webserver*) par;
auto ok = std::make_shared<ok_resource>();
try {
ws->register_path("base", ok);
ws->start(true);
} catch (...) {
return PTHREAD_CANCELED;
}
return nullptr;
}
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server)
httpserver::webserver ws{httpserver::create_webserver(0)};
pthread_t tid;
pthread_create(&tid, nullptr, start_ws_blocking, reinterpret_cast<void*>(&ws));
sleep(1);
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
char* b;
pthread_join(tid, reinterpret_cast<void**>(&b));
LT_CHECK_EQ(b, nullptr);
free(b);
LT_END_AUTO_TEST(blocking_server)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources)
httpserver::webserver ws{httpserver::create_webserver(0)
.not_found_handler(not_found_custom)
.method_not_allowed_handler(not_allowed_custom)};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
const uint16_t port = ws.get_bound_port();
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
}
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
std::string url = "localhost:" + std::to_string(port) + "/not_registered";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "Not found custom");
int64_t http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(http_code, 404);
curl_easy_cleanup(curl);
}
{
ok->set_allowing(httpserver::http_method::put, false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
std::string url = "localhost:" + std::to_string(port) + "/base";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "Not allowed custom");
int64_t http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(http_code, 405);
curl_easy_cleanup(curl);
}
ws.stop();
LT_END_AUTO_TEST(custom_error_resources)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ipv6_webserver)
httpserver::webserver ws{httpserver::create_webserver(0).use_ipv6()};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
try {
ws.start(false);
} catch (const std::exception& e) {
// IPv6 may be unavailable in the host network stack;
// distinguish that from "TLS support actually broke" by emitting
// a SKIP rather than a tautological pass.
LT_SKIP(std::string("IPv6 webserver start failed: ") + e.what());
}
if (ws.is_running()) {
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
// Bind an ephemeral port; talk to the actual bound port.
std::string url = "http://[::1]:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
if (res == CURLE_COULDNT_RESOLVE_HOST) {
// The server bound IPv6 fine (is_running() is true), but this
// host has no IPv6 client path — getaddrinfo("::1") fails, so
// curl cannot reach loopback (seen on macOS CI runners). That
// is environmental, not an IPv6-serving regression. A real
// serving break surfaces as a different curl error or a wrong
// body, both still asserted below.
LT_SKIP("IPv6 loopback unreachable from client (no host IPv6 stack)");
}
// Once the server is confirmed running and the client can reach
// IPv6 loopback, a curl failure is a genuine test failure.
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
}
LT_END_AUTO_TEST(ipv6_webserver)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, dual_stack_webserver)
httpserver::webserver ws{httpserver::create_webserver(0).use_dual_stack()};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
try {
ws.start(false);
} catch (const std::exception& e) {
// Dual-stack may be disabled on the host kernel; SKIP
// rather than tautological-pass so a broken-build regression is
// observable.
LT_SKIP(std::string("dual-stack webserver start failed: ") + e.what());
}
if (ws.is_running()) {
const uint16_t port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
// Bind an ephemeral port; talk to the actual bound port.
std::string url = "localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
// Once the server is confirmed running, a curl failure is a
// genuine test failure — not an environmental skip.
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
}
LT_END_AUTO_TEST(dual_stack_webserver)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, bind_address_ipv4)
int port = 0; // set to the kernel-assigned port after start
httpserver::webserver ws{httpserver::create_webserver(0).bind_address("127.0.0.1")};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
ws.start(false);
port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "http://127.0.0.1:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
LT_END_AUTO_TEST(bind_address_ipv4)
// Test bind_address with IPv6 address string (covers IPv6 branch in create_webserver.cpp)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, bind_address_ipv6_string)
int port = 0; // set to the kernel-assigned port after start
// Webserver is non-movable so we use a shared_ptr to
// separate construction / start (environmental SKIP) from the curl
// block (unconditional assertion). Previously a single catch(...)
// swallowed assert_unattended and turned a curl failure into a SKIP.
std::shared_ptr<httpserver::webserver> ws_ptr;
try {
ws_ptr = std::make_shared<httpserver::webserver>(
httpserver::create_webserver(0).bind_address("::1"));
} catch (...) {
LT_SKIP("IPv6 bind: construction failed on this host");
}
// ws_ptr is guaranteed non-null here: LT_SKIP() always throws
// skip_unattended and unwinds the entire test body on failure, so
// execution only reaches this point when the try block above
// completed successfully.
auto ok = std::make_shared<ok_resource>();
ws_ptr->register_path("base", ok);
try {
ws_ptr->start(false);
port = ws_ptr->get_bound_port();
} catch (...) {
LT_SKIP("IPv6 bind: start failed on this host");
}
if (ws_ptr->is_running()) {
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "http://[::1]:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s);
if (res == CURLE_COULDNT_RESOLVE_HOST) {
// Server bound IPv6, but this host has no IPv6 client path
// (getaddrinfo("::1") fails; seen on macOS CI runners).
// Environmental, not a serving regression — a real break shows
// up as a different error or a wrong body, both asserted below.
LT_SKIP("IPv6 loopback unreachable from client (no host IPv6 stack)");
}
// Once the server is confirmed running and the client can reach
// IPv6 loopback, a curl failure is a genuine test failure.
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws_ptr->stop();
}
LT_END_AUTO_TEST(bind_address_ipv6_string)
#ifdef HAVE_GNUTLS
// Test TLS session getters on non-TLS connection (should return false/nullptr)
class tls_check_non_tls_resource : public httpserver::http_resource {
public:
httpserver::http_response render_get(const httpserver::http_request& req) {
// On non-TLS connection, has_tls_session should return false
std::string response = req.has_tls_session() ? "HAS_TLS" : "NO_TLS";
return httpserver::http_response::string(response);
}
};
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tls_session_on_non_tls_connection)
int port = 0; // set to the kernel-assigned port after start
httpserver::webserver ws{httpserver::create_webserver(0)}; // No SSL
auto tls_check = std::make_shared<tls_check_non_tls_resource>();
ws.register_path("tls_check", tls_check);
ws.start(false);
port = ws.get_bound_port();
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "http://localhost:" + std::to_string(port) + "/tls_check";
CURLcode res = curl_get(url, &s);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "NO_TLS");
ws.stop();
LT_END_AUTO_TEST(tls_session_on_non_tls_connection)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, https_webserver)
int port = 0; // set to the kernel-assigned port after start
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")};
auto ok = std::make_shared<ok_resource>();
ws.register_path("base", ok);
try {
ws.start(false);
port = ws.get_bound_port();
} catch (const std::exception& e) {
// TLS setup may fail in environments without the
// certs or library; SKIP rather than tautological-pass so a
// build that silently lost TLS support reports SKIP, not PASS.
LT_SKIP(std::string("TLS start failed: ") + e.what());
}
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "https://localhost:" + std::to_string(port) + "/base";
CURLcode res = curl_get(url, &s, false);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
ws.stop();
}
LT_END_AUTO_TEST(https_webserver)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tls_session_getters)
int port = 0; // set to the kernel-assigned port after start
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")};
auto tls_info = std::make_shared<tls_info_resource>();
ws.register_path("tls_info", tls_info);
try {
ws.start(false);
port = ws.get_bound_port();
} catch (const std::exception& e) {
// TLS setup may fail in environments without the
// certs or library; SKIP rather than tautological-pass so a
// build that silently lost TLS support reports SKIP, not PASS.
LT_SKIP(std::string("TLS start failed: ") + e.what());
}
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "https://localhost:" + std::to_string(port) + "/tls_info";
CURLcode res = curl_get(url, &s, false);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "TLS_SESSION_PRESENT");
ws.stop();
LT_END_AUTO_TEST(tls_session_getters)
// Resource that extracts client certificate info
class client_cert_info_resource : public httpserver::http_resource {
public:
httpserver::http_response render_get(const httpserver::http_request& req) {
std::string response;
if (req.has_client_certificate()) {
response = "HAS_CLIENT_CERT";
// The four cert-string accessors return
// std::string_view; the explicit string(string_view) ctor
// requires direct-init, so we use parens not `=`.
std::string dn(req.get_client_cert_dn());
std::string issuer(req.get_client_cert_issuer_dn());
std::string cn(req.get_client_cert_cn());
std::string fingerprint(req.get_client_cert_fingerprint_sha256());
bool verified = req.is_client_cert_verified();
// The two time accessors return std::int64_t.
std::int64_t not_before = req.get_client_cert_not_before();
std::int64_t not_after = req.get_client_cert_not_after();
response += "|DN:" + dn;
response += "|ISSUER:" + issuer;
response += "|CN:" + cn;
response += "|FP:" + fingerprint;
response += "|VERIFIED:" + std::string(verified ? "yes" : "no");
response += "|NOT_BEFORE:" + std::to_string(not_before);
response += "|NOT_AFTER:" + std::to_string(not_after);
} else {
response = "NO_CLIENT_CERT";
}
return httpserver::http_response::string(response);
}
};
// Test client certificate methods without a client certificate (no mTLS)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, client_cert_no_certificate)
int port = 0; // set to the kernel-assigned port after start
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")};
auto cert_info = std::make_shared<client_cert_info_resource>();
ws.register_path("cert_info", cert_info);
try {
ws.start(false);
port = ws.get_bound_port();
} catch (const std::exception& e) {
// TLS setup may fail in environments without the
// certs or library; SKIP rather than tautological-pass so a
// build that silently lost TLS support reports SKIP, not PASS.
LT_SKIP(std::string("TLS start failed: ") + e.what());
}
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
std::string url = "https://localhost:" + std::to_string(port) + "/cert_info";
CURLcode res = curl_get(url, &s, false);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "NO_CLIENT_CERT");
ws.stop();
LT_END_AUTO_TEST(client_cert_no_certificate)
// Test client certificate methods with mTLS (client sends certificate)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, client_cert_with_certificate)
int port = 0; // set to the kernel-assigned port after start
httpserver::webserver ws{httpserver::create_webserver(0)
.use_ssl()
.https_mem_key(ROOT "/key.pem")
.https_mem_cert(ROOT "/cert.pem")
.https_mem_trust(ROOT "/client_cert.pem")}; // Trust the client cert as CA
auto cert_info = std::make_shared<client_cert_info_resource>();
ws.register_path("cert_info", cert_info);
try {
ws.start(false);
port = ws.get_bound_port();
} catch (const std::exception& e) {
// TLS setup may fail in environments without the
// certs or library; SKIP rather than tautological-pass so a
// build that silently lost TLS support reports SKIP, not PASS.
LT_SKIP(std::string("TLS start failed: ") + e.what());
}
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
std::string url = "https://localhost:" + std::to_string(port) + "/cert_info";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSLCERT, ROOT "/client_cert.pem");
curl_easy_setopt(curl, CURLOPT_SSLKEY, ROOT "/client_key.pem");
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
// Check that we got client cert info
LT_CHECK_NEQ(s.find("HAS_CLIENT_CERT"), std::string::npos);
LT_CHECK_NEQ(s.find("CN:Test Client"), std::string::npos);