-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathh2svr.c
520 lines (461 loc) · 15.6 KB
/
h2svr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* h2sim - HTTP2 Simple Application Framework using nghttp2
*
* Copyright (c) 2019 Lee Yongjae, Telcoware Co.,LTD.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/time.h> /* for gettimeofday() */
#include <sys/stat.h> /* for file read */
#include <fcntl.h> /* for file read */
#ifdef TLS_MODE
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#else
/* NOTE: h2_util.h defines dummy SSL_CTX and SSL */
#endif
#include "h2sim/h2.h"
int verbose = 1;
h2_settings h2svr_settings;
#define GET_FILE 1 /* GET request handling for file of request path */
#define RSP_CASE_MAX 100
#define PUSH_PRM_MAX 100
/* application context */
typedef struct http2_rsp_case {
/* request pattern */
char *req_method; /* static string; may be NULL */
char *req_authority; /* static string; may be NULL */
char *req_path_prefix; /* static string; may be NULL */
int req_path_prefix_len;
/* corresponding response */
h2_msg *rsp;
#ifdef GET_FILE
/* GET path returns file contents of rsp_base_dir as root dir */
char *rsp_base_dir; /* static string; may be NULL */
#endif
/* push promise and push reponse */
int push_prm_idx; /* start of push promises for this req */
int push_prm_num; /* number of push promises for this req */
} http2_rsp_case;
typedef struct app_context {
http2_rsp_case rsp_case[RSP_CASE_MAX];
int rsp_case_num;
http2_rsp_case push_prm[PUSH_PRM_MAX];
int push_prm_num;
} app_context;
/*
* Application logics -------------------------------------------------------
*/
int request_cb(h2_sess *sess, h2_strm *strm,
h2_msg *req, void *sess_user_data) {
/* TEST CASE FOR RST_STREAM */
/*
static int cnt = 0;
cnt++;
if (cnt % 100 == 0)
return -1; // the caller should handle this as RST_STREAM case
*/
/* returns: 0(msg handled), >0(status code to be retured), 0<(error) */
app_context *app_ctx = sess_user_data;
if (verbose) {
h2_dump_msg(stdout, req, "", "REQUEST");
}
/* find rsp_case for the request */
http2_rsp_case *rc = app_ctx->rsp_case;
int n = app_ctx->rsp_case_num;
for ( ; n > 0; n--, rc++) {
if ((rc->req_method == NULL ||
!strcmp(h2_method(req), rc->req_method)) &&
(rc->req_authority == NULL ||
!strcmp(h2_authority(req), rc->req_authority)) &&
(rc->req_path_prefix == NULL ||
!strncmp(h2_path(req), rc->req_path_prefix,
rc->req_path_prefix_len))) {
break; /* rc is matched */
}
}
if (n <= 0) {
return 404;
}
/* set response body from rc->rsp */
h2_msg *rsp = h2_msg_init();
h2_cpy_msg(rsp, rc->rsp);
h2_prepare_rsp(rsp, req);
/* HERE: TODO: NEED TO PREPARE RESPONSE FROM REQUERST MESSAGE */
#ifdef GET_FILE
if (h2_body_len(rsp) == 0 && rc->rsp_base_dir) {
const char *rel_path = h2_path(req);
char path[1024], *body;
int body_len;
if (rel_path[0] == '/') {
rel_path++;
}
sprintf(path, "%s%s", rc->rsp_base_dir, rel_path);
/* TODO: need to check resulting path; might be security hole */
if (h2_body_from_file(path, (void **)&body, &body_len) < 0) {
h2_msg_free(rsp);
return 404;
}
h2_set_body(rsp, body, body_len);
}
#endif
/* send optional push promise */
http2_rsp_case *prm = &app_ctx->push_prm[rc->push_prm_idx];
for (n = rc->push_prm_num; n > 0; n--, prm++) {
/* prepare push_promise message */
h2_msg *prm_req = h2_msg_init();
h2_prepare_prm(prm_req, req, prm->req_method,
(prm->req_path_prefix)? prm->req_path_prefix : h2_path(req));
/* prepare push_response message */
h2_msg *prm_rsp = h2_msg_init();
h2_cpy_msg(prm_rsp, prm->rsp);
h2_prepare_rsp(prm_rsp, req);
h2_send_push_promise(sess, strm, prm_req, prm_rsp);
h2_msg_free(prm_req);
h2_msg_free(prm_rsp);
}
/* send response */
if (verbose) {
h2_dump_msg(stdout, rsp, "", "RESPONSE");
}
int rs = h2_send_response(sess, strm, rsp);
h2_msg_free(rsp);
return (rs < 0)? -1 : 0;
}
int accept_cb(h2_svr *svr, void *server_user_data,
const char *peer_ip, unsigned short peer_port,
SSL_CTX **ssl_ctx_ret, h2_settings *settings_ret,
h2_request_cb *request_cb_ret,
h2_sess_free_cb *sess_free_cb_ret, void **sess_user_data_ret) {
(void)svr;
(void)peer_ip;
(void)peer_port;
/* set accepted session paramters */
*ssl_ctx_ret = NULL/* use svr_ssl_ctx */;
*settings_ret = h2svr_settings;
*request_cb_ret = request_cb;
*sess_free_cb_ret = NULL/* static user data */;
*sess_user_data_ret = server_user_data/* app_ctx * */;
return 0;
}
void svr_free_cb(h2_svr *svr, void *svr_user_data) {
(void)svr_user_data;
if (h2_svr_ssl_ctx(svr)) {
SSL_CTX_free(h2_svr_ssl_ctx(svr));
}
}
#ifdef TLS_MODE
int cli_cert_verify_pass_cb(int preverify_ok, X509_STORE_CTX *ctx) {
(void)preverify_ok;
(void)ctx;
return 1; /* always ok regardless of preverify_ok */
}
#endif
/*
* Application main and runtime argument parsers ----------------------------
*/
static void help(char *prog) {
fprintf(stderr, "Usage: %s [server_options] [rsp_case_options]...\n", prog);
fprintf(stderr, "server_options:\n");
#ifdef TLS_MODE
fprintf(stderr, " -k key_file # default:eckey.pem\n");
fprintf(stderr, " -c cert_file # default:eccert.pem\n");
fprintf(stderr, " -V ssl_verify_opt # default:none\n");
fprintf(stderr, " # %s\n", H2_SSL_VERIFY_STR_FORMAT);
fprintf(stderr, " # -S option uses previous -k, -c, -V option values\n");
fprintf(stderr, " -S https://<ip>:<port> # tls server listen ip:port\n");
#endif
fprintf(stderr, " -S http://<ip>:<port> # tcp server listen ip:port\n");
fprintf(stderr, " -H <settings_id>=<value> # set http2 settings value\n");
fprintf(stderr, " # <settings_id> := header_table_size | enable_push |\n");
fprintf(stderr, " # max_concurrent_streams, initial_window_size | max_frame_size\n");
fprintf(stderr, " # max_header_list_size, enable_connect_protocol\n");
fprintf(stderr, " -1 # use HTTP/1.1 instead of HTTP/2\n");
fprintf(stderr, " -Q # h2sim io quiet mode\n");
fprintf(stderr, " -q # all quiet mode\n");
fprintf(stderr, "rsp_case_options:\n");
fprintf(stderr, " # -m starts each case\n");
fprintf(stderr, " # -a and -p are optional matching condition\n");
fprintf(stderr, " -m req_method # GET|POST|PUT|PATCH|DELETE\n");
fprintf(stderr, " -a req_authority\n");
fprintf(stderr, " -p req_path_prefix\n");
fprintf(stderr, " # -o starts push promose req and response on the case\n");
fprintf(stderr, " -o push_prmise_req_path # assume GET method\n");
fprintf(stderr, " -s rsp_status # default:200\n");
fprintf(stderr, " -x rsp_header_name=value\n");
fprintf(stderr, " -t rsp_body_text\n");
fprintf(stderr, " -b rsp_body_hex_binary\n");
fprintf(stderr, " -f rsp_body_file\n");
fprintf(stderr, " -e rsp_body_size # dummy zero value body of given size\n");
#ifdef GET_FILE
fprintf(stderr, " -d rsp_file_base_directory # req path is / mapped to this directory\n");
#endif
#ifdef TLS_MODE
fprintf(stderr, "default: -S https -P 8080 -K eckey.pem -C eccert.pem -m GET -s 200 -r ./\n");
#else
fprintf(stderr, "default: -S http -P 8080 -m GET -s 200 -r ./\n");
#endif
}
h2_ctx *ctx = NULL;
void sighdlr_mark_stop(int signo) {
(void)signo;
h2_ctx_stop(ctx);
}
int main(int argc, char **argv) {
/* set app_ctx default */
app_context app_ctx;
memset(&app_ctx, 0, sizeof(app_ctx));
app_ctx.rsp_case_num = 0;
int verbose_h2 = 1;
#ifdef TLS_MODE
char *key_file = "eckey.pem"; /* default private key file */
char *cert_file = "eccert.pem"; /* default certificate file */
char *ssl_verify_str = "none"; /* default verify none */
#endif
char *authority = NULL;
SSL_CTX *ssl_ctx = NULL;
void *body;
int body_len;
int http_ver = H2_HTTP_V2;
h2_settings_init(&h2svr_settings);
/* intialize current response case */
int rc_is_push_prm = 0;
http2_rsp_case *rc = &app_ctx.rsp_case[0];
http2_rsp_case *rc_for_push_prm = rc;
app_ctx.rsp_case[0].rsp = h2_msg_init();
app_ctx.push_prm[0].rsp = h2_msg_init();
#ifdef TLS_MODE
SSL_load_error_strings();
SSL_library_init();
#endif
ctx = h2_ctx_init(http_ver, verbose_h2);
int c;
int listen_num = 0;
char scale;
while ((c = getopt(argc, argv, "k:c:V:S:H:1Qqm:a:p:o:s:x:t:b:f:e:d:h")) >= 0) {
switch (c) {
#ifdef TLS_MODE
case 'k':
key_file = optarg;
break;
case 'c':
cert_file = optarg;
break;
case 'V':
ssl_verify_str = optarg;
break;
#endif
case 'S':
#ifdef TLS_MODE
if (!strncasecmp(optarg, "https://", 8)) {
authority = optarg + 8;
if (!(ssl_ctx = h2_ssl_ctx_init(1/*server*/, key_file, cert_file))) {
fprintf(stderr, "cannot initialize ssl_ctx: "
"key_file=%s cert_file=%s\n", key_file, cert_file);
return EXIT_FAILURE;
}
if (h2_ssl_ctx_set_verify_from_str(ssl_ctx, 1, ssl_verify_str) < 0) {
fprintf(stderr, "cannot set client certificate verify option: %s\n",
ssl_verify_str);
return EXIT_FAILURE;
}
} else
#endif
if (!strncasecmp(optarg, "http://", 7)) {
authority = optarg + 7;
ssl_ctx = NULL;
} else {
fprintf(stderr, "unknown server binding format: %s\n", optarg);
return EXIT_FAILURE;
}
if (!h2_listen(ctx, authority, ssl_ctx, accept_cb,
svr_free_cb, &app_ctx)) {
fprintf(stderr, "server binding failed; quit: %s\n", authority);
return EXIT_FAILURE;
}
listen_num++;
break;
case 'H':
if (h2_set_settings(&h2svr_settings, optarg) < 0) {
fprintf(stderr, "invalid argument for options <id>=<value>: %s\n",
optarg);
return EXIT_FAILURE;
}
break;
case '1':
http_ver = H2_HTTP_V1_1;
h2_ctx_set_http_ver(ctx, http_ver);
break;
case 'Q':
verbose_h2 = 0;
h2_ctx_set_verbose(ctx, verbose_h2);
break;
case 'q':
verbose = 0;
verbose_h2 = 0;
h2_ctx_set_verbose(ctx, verbose_h2);
break;
/* reponse case request matching parameters */
case 'm': /* http request method to match; ALSO start of reponse case */
if (app_ctx.rsp_case_num >= RSP_CASE_MAX) {
fprintf(stderr, "too many response cases: %d\n", app_ctx.rsp_case_num);
return EXIT_FAILURE;
}
if (rc_is_push_prm == 1) {
rc_is_push_prm = 0;
}
rc = &app_ctx.rsp_case[app_ctx.rsp_case_num];
rc->req_method = optarg;
h2_set_status(rc->rsp, 200);
app_ctx.rsp_case_num++;
rc_is_push_prm = 0;
if (app_ctx.rsp_case_num < RSP_CASE_MAX) {
/* pre-init next rsp in rsp_case */
(rc+1)->rsp = h2_msg_init();
}
break;
case 'a': /* http request authority to match */
rc->req_authority = optarg;
break;
case 'p': /* http request path prefix to match */
rc->req_path_prefix = optarg;
rc->req_path_prefix_len = strlen(optarg);
break;
case 'o':
if (app_ctx.push_prm_num >= PUSH_PRM_MAX) {
fprintf(stderr, "too many push promise cases: %d\n",
app_ctx.push_prm_num);
return EXIT_FAILURE;
}
if (rc_is_push_prm == 0) {
rc_is_push_prm = 1;
rc_for_push_prm = rc;
rc_for_push_prm->push_prm_idx = app_ctx.push_prm_num;
}
rc_for_push_prm->push_prm_num++;
rc = &app_ctx.push_prm[app_ctx.push_prm_num];
rc->req_method = "GET";
rc->req_path_prefix = optarg; /* use req_path_prefix as req_path */
rc->req_path_prefix_len = strlen(optarg);
h2_set_status(rc->rsp, 200);
app_ctx.push_prm_num++;
if (app_ctx.push_prm_num < RSP_CASE_MAX) {
/* pre-init next push promise rsp in push_prm */
(rc+1)->rsp = h2_msg_init();
}
break;
/* reponse case response parameters */
case 's': /* http reponse status */
h2_set_status(rc->rsp, atoi(optarg));
break;
case 'x': /* https response user header value */
if (h2_add_hdr_s(rc->rsp, optarg) < 0) {
return EXIT_FAILURE;
}
break;
case 't': /* http response body as text */
h2_set_body(rc->rsp, strdup(optarg), strlen(optarg));
break;
case 'b': /* http response body as hex binary */
if (h2_body_from_hex_str(optarg, &body, &body_len) < 0) {
return EXIT_FAILURE;
}
h2_set_body(rc->rsp, body, body_len);
break;
case 'f': /* http response body as file */
if (h2_body_from_file(optarg, &body, &body_len) < 0) {
return EXIT_FAILURE;
}
h2_set_body(rc->rsp, body, body_len);
break;
case 'e':
if (sscanf(optarg, "%d%c", &body_len, &scale) == 2 &&
(scale == 'k' || scale == 'K')) {
body_len *= 1024;
} else if (sscanf(optarg, "%d%c", &body_len, &scale) != 1) {
fprintf(stderr, "invalid -e req_body_size option value: %s\n", optarg);
return EXIT_FAILURE;
}
h2_set_body(rc->rsp, calloc(1, body_len + 1), body_len);
break;
#ifdef GET_FILE
case 'd': /* http response file base directory */
rc->rsp_base_dir = optarg;
break;
#endif
case 'h':
help(argv[0]);
return EXIT_FAILURE;
case '?':
c = optopt;
default:
fprintf(stderr, "unknown option: %c", c);
return EXIT_FAILURE;
}
}
if (argc - optind > 0) {
fprintf(stderr, "unknown option: argc=%d optind=%d optval='%s'\n",
argc, optind, argv[optind]);
return EXIT_FAILURE;
}
if (listen_num == 0) {
help(argv[0]);
return EXIT_FAILURE;
}
/* default rsp_case */
if (app_ctx.rsp_case_num == 0) {
rc = &app_ctx.rsp_case[0];
rc->req_method = "GET";
h2_set_status(rc->rsp, 200);
#ifdef GET_FILE
rc->rsp_base_dir = strdup("./");
#endif
app_ctx.rsp_case_num = 1;
}
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, sighdlr_mark_stop);
h2_ctx_run(ctx);
h2_ctx_free(ctx);
int i;
for (i = 0; i <= app_ctx.rsp_case_num && i < RSP_CASE_MAX; i++) {
h2_msg_free(app_ctx.rsp_case[i].rsp);
}
for (i = 0; i <= app_ctx.push_prm_num && i < PUSH_PRM_MAX; i++) {
h2_msg_free(app_ctx.push_prm[i].rsp);
}
#ifdef TLS_MODE
ERR_free_strings();
#endif
return 0;
}