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
|
/*
* blogc: A blog compiler.
* Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the BSD License.
* See the file LICENSE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "../common/utils.h"
#include "httpd.h"
static void
print_help(const char *default_host, const char *default_port)
{
printf(
"usage:\n"
" blogc-runserver [-h] [-v] [-t HOST] [-p PORT] [-m THREADS] DOCROOT\n"
" - A simple HTTP server to test blogc websites.\n"
"\n"
"positional arguments:\n"
" DOCROOT document root directory\n"
"\n"
"optional arguments:\n"
" -h show this help message and exit\n"
" -v show version and exit\n"
" -t HOST set server listen address (default: %s)\n"
" -p PORT set server listen port (default: %s)\n"
" -m THREADS set maximum number of threads to spawn (default: 20)\n",
default_host, default_port);
}
static void
print_usage(void)
{
printf("usage: blogc-runserver [-h] [-v] [-t HOST] [-p PORT] [-m THREADS] DOCROOT\n");
}
void sigint_handler(int sig) {
printf("\n");
exit(0);
}
int
main(int argc, char **argv)
{
signal(SIGPIPE, SIG_IGN);
//signal(SIGINT, sigint_handler);
int rv = 0;
char *host = NULL;
char *port = NULL;
char *docroot = NULL;
size_t max_threads = 20;
char *ptr;
char *endptr;
char *tmp_host = getenv("BLOGC_RUNSERVER_DEFAULT_HOST");
char *default_host = bc_strdup(tmp_host != NULL ? tmp_host : "127.0.0.1");
char *tmp_port = getenv("BLOGC_RUNSERVER_DEFAULT_PORT");
char *default_port = bc_strdup(tmp_port != NULL ? tmp_port : "8080");
size_t args = 0;
for (size_t i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'h':
print_help(default_host, default_port);
goto cleanup;
case 'v':
printf("%s\n", PACKAGE_STRING);
goto cleanup;
case 't':
if (argv[i][2] != '\0')
host = bc_strdup(argv[i] + 2);
else
host = bc_strdup(argv[++i]);
break;
case 'p':
if (argv[i][2] != '\0')
port = bc_strdup(argv[i] + 2);
else
port = bc_strdup(argv[++i]);
break;
case 'm':
if (argv[i][2] != '\0')
ptr = argv[i] + 2;
else
ptr = argv[++i];
max_threads = strtoul(ptr, &endptr, 10);
if (*ptr != '\0' && *endptr != '\0')
fprintf(stderr, "blogc-runserver: warning: invalid value "
"for -m argument: %s. using %zu instead\n", ptr, max_threads);
break;
default:
print_usage();
fprintf(stderr, "blogc-runserver: error: invalid "
"argument: -%c\n", argv[i][1]);
rv = 3;
goto cleanup;
}
}
else {
if (args > 0) {
print_usage();
fprintf(stderr, "blogc-runserver: error: only one positional "
"argument allowed\n");
rv = 3;
goto cleanup;
}
args++;
docroot = bc_strdup(argv[i]);
}
}
if (docroot == NULL) {
print_usage();
fprintf(stderr, "blogc-runserver: error: document root directory "
"required\n");
rv = 3;
goto cleanup;
}
if (max_threads <= 0 || max_threads > 1000) {
print_usage();
fprintf(stderr, "blogc-runserver: error: invalid value for -m. "
"Must be integer > 0 and <= 1000\n");
rv = 3;
goto cleanup;
}
rv = br_httpd_run(
host != NULL ? host : default_host,
port != NULL ? port : default_port,
docroot, max_threads);
cleanup:
free(default_host);
free(default_port);
free(host);
free(port);
free(docroot);
return rv;
}
|