forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws_start_stop.cpp
More file actions
552 lines (472 loc) · 16.4 KB
/
ws_start_stop.cpp
File metadata and controls
552 lines (472 loc) · 16.4 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
/*
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(__MINGW32__) || defined(__CYGWIN32__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif
#include "littletest.hpp"
#include <curl/curl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "httpserver.hpp"
#include <pthread.h>
using namespace std;
using namespace httpserver;
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append((char*) ptr, size*nmemb);
return size*nmemb;
}
class ok_resource : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
}
};
const shared_ptr<http_response> not_found_custom(const http_request& req)
{
return shared_ptr<string_response>(new string_response("Not found custom", 404, "text/plain"));
}
const shared_ptr<http_response> not_allowed_custom(const http_request& req)
{
return shared_ptr<string_response>(new string_response("Not allowed custom", 405, "text/plain"));
}
LT_BEGIN_SUITE(ws_start_stop_suite)
void set_up()
{
}
void tear_down()
{
}
LT_END_SUITE(ws_start_stop_suite)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop)
{
webserver ws = create_webserver(8080);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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();
}
{
webserver ws = create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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();
}
{
webserver ws = create_webserver(8080).start_method(http::http_utils::THREAD_PER_CONNECTION);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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(start_stop)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, sweet_kill)
webserver ws = create_webserver(8080);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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.sweet_kill();
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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, 7);
curl_easy_cleanup(curl);
}
LT_END_AUTO_TEST(sweet_kill)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options)
webserver ws = create_webserver(8080)
.no_ssl()
.no_ipv6()
.no_debug()
.no_pedantic()
.no_basic_auth()
.no_digest_auth()
.no_deferred()
.no_regex_checking()
.no_ban_system()
.no_post_process();
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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(disable_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, enable_options)
webserver ws = create_webserver(8080)
.debug()
.pedantic()
.deferred()
.regex_checking()
.ban_system()
.post_process();
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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(enable_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_socket)
#ifndef DARWIN
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(8181);
bind(fd, (struct sockaddr*) &address, sizeof(address));
listen(fd, 10000);
webserver ws = create_webserver(-1).bind_socket(fd); //whatever port here doesn't matter
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8181/base");
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();
#endif
LT_END_AUTO_TEST(custom_socket)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource)
webserver ws = create_webserver(8080).single_resource();
ok_resource ok;
ws.register_resource("/", &ok, true);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/any/url/works");
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(single_resource)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource_not_default_resource)
webserver ws = create_webserver(8080).single_resource();
ok_resource ok;
LT_CHECK_THROW(ws.register_resource("/other", &ok, true));
LT_CHECK_THROW(ws.register_resource("/", &ok, false));
ws.start(false);
ws.stop();
LT_END_AUTO_TEST(single_resource_not_default_resource)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads)
{
webserver ws = create_webserver(8080)
.start_method(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)
{
webserver ws = create_webserver(8080)
.start_method(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)
webserver ws = create_webserver(8080)
.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);
;
ok_resource ok;
ws.register_resource("base", &ok);
LT_CHECK_NOTHROW(ws.start(false));
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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(tuning_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_base)
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem");
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8080/base");
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);
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(ssl_base)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_protocol_priorities)
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem")
.https_priorities("NONE:+VERS-TLS1.0:+AES-128-CBC:+SHA1:+RSA:+COMP-NULL");
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8080/base");
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(ssl_with_protocol_priorities)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_trust)
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem")
.https_mem_trust("test_root_ca.pem");
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8080/base");
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(ssl_with_trust)
void* start_ws_blocking(void* par)
{
webserver* ws = (webserver*) par;
ok_resource ok;
ws->register_resource("base", &ok);
ws->start(true);
return 0x0;
}
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server)
webserver ws = create_webserver(8080);
pthread_t tid;
pthread_create(&tid, NULL, start_ws_blocking, (void *) &ws);
sleep(1);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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();
char* b;
pthread_join(tid,(void**) &b);
free(b);
LT_END_AUTO_TEST(blocking_server)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources)
webserver ws = create_webserver(8080)
.not_found_resource(not_found_custom)
.method_not_allowed_resource(not_allowed_custom);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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);
}
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/not_registered");
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");
long 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("PUT", false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
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");
long 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_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()