forked from mattn/tinytinyhttpd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cxx
259 lines (240 loc) · 6.46 KB
/
main.cxx
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "httpd.h"
#include <stdio.h>
#include <string.h>
int opterr = 1;
int optind = 1;
int optopt;
char *optarg;
static int getopt(int argc, char** argv, const char* opts) {
static int sp = 1;
register int c;
register char *cp;
if(sp == 1) {
if(optind >= argc ||
argv[optind][0] != '-' || argv[optind][1] == '\0')
return(EOF);
else if(strcmp(argv[optind], "--") == 0) {
optind++;
return(EOF);
}
}
optopt = c = argv[optind][sp];
if(c == ':' || (cp=strchr((char*)opts, c)) == NULL) {
if(argv[optind][++sp] == '\0') {
optind++;
sp = 1;
}
return('?');
}
if(*++cp == ':') {
if(argv[optind][sp+1] != '\0')
optarg = &argv[optind++][sp+1];
else if(++optind >= argc) {
sp = 1;
return('?');
} else
optarg = argv[optind++];
sp = 1;
} else {
if(argv[optind][++sp] == '\0') {
sp = 1;
optind++;
}
optarg = NULL;
}
return(c);
}
typedef std::map<std::string, std::string> Config;
typedef std::map<std::string, Config> ConfigList;
ConfigList loadConfigs(const char* filename) {
ConfigList configs;
Config config;
char buffer[BUFSIZ];
FILE* fp = fopen(filename, "r");
std::string profile = "global";
while(fp && fgets(buffer, sizeof(buffer), fp)) {
char* line = buffer;
char* ptr = strpbrk(line, "\r\n");
if (*line == '#') continue;
if (ptr) *ptr = 0;
ptr = strchr(line, ']');
if (*line == '[' && ptr) {
*ptr = 0;
if (config.size())
configs[profile] = config;
config.clear();
profile = line+1;
continue;
}
ptr = strchr(line, '=');
if (ptr && *line != ';') {
*ptr++ = 0;
config[line] = ptr;
}
}
configs[profile] = config;
if (fp) fclose(fp);
return configs;
}
bool loadAuthfile(const char* filename, std::vector<tthttpd::server::AuthInfo>& auths) {
char buffer[BUFSIZ];
auths.clear();
FILE* fp = fopen(filename, "r");
if (!fp) return false;
while(fp && fgets(buffer, sizeof(buffer), fp)) {
char* line = buffer;
char* ptr = strpbrk(line, "\r\n");
if (ptr) *ptr = 0;
ptr = strchr(line, ':');
if (ptr) *ptr++ = 0;
tthttpd::server::AuthInfo info;
info.user = line;
info.pass = ptr;
auths.push_back(info);
}
fclose(fp);
return true;
}
int main(int argc, char* argv[]) {
int c;
const char* root = ".";
const char* port = "www";
const char* cfg = NULL;
bool spawn_exec = false;
int verbose = 0;
int family = AF_UNSPEC;
#ifdef _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
opterr = 0;
while ((c = getopt(argc, (char**)argv, "46p:c:d:xvh") != -1)) {
switch (optopt) {
case '4': family = AF_INET; break;
case '6': family = AF_INET6; break;
case 'p': port = optarg; break;
case 'c': cfg = optarg; break;
case 'd': root = optarg; break;
case 'v': verbose++; break;
#ifdef PACKAGE_VERSION
case 'V':
printf("%s\n", PACKAGE_VERSION);
break;
#endif
case 'x': spawn_exec = true; break;
case 'h': argc = 0; break;
case '?': break;
default: argc = 0; break;
}
optarg = NULL;
}
if (argc == 0) {
const char* lines[] = {
#ifdef PACKAGE_VERSION
"tthttpd (tinytinyhttpd) " PACKAGE_VERSION,
#else
"tthttpd (tinytinyhttpd)",
#endif
" usage: tthttpd [-4|-6] [-p server-port] [-c config-file] [-d root-dir] [-v] [-x] [-h]",
" -4 : ipv4 only",
" -6 : ipv6 only",
" -p : server port (name or numeric)",
" -c : config file",
" -d : root directory",
" -v : verbose mode (-vvv mean level 3)",
" -x : spawn file as cgi if possible",
" -h : show this usage",
#ifdef PACKAGE_VERSION
" -V : show version",
#endif
NULL
};
for (const char** ptr = lines; *ptr; ptr++)
std::cerr << *ptr << std::endl;
exit(1);
}
tthttpd::server httpd(port);
httpd.bindRoot(root);
httpd.spawn_executable = spawn_exec;
httpd.verbose_mode = verbose;
httpd.family = family;
if (cfg) {
ConfigList configs = loadConfigs(cfg);
Config config;
Config::iterator it;
std::string val;
val = configs["global"]["path"];
#ifdef _WIN32
if (val.size()) {
std::string tmp = "PATH=";
tmp += val;
putenv(tmp.c_str());
}
#else
if (val.size()) setenv("PATH", val.c_str(), true);
#endif
val = configs["global"]["root"];
if (val.size()) httpd.bindRoot(val);
val = configs["global"]["ipv4_only"];
if (val == "on") httpd.family = AF_INET;
val = configs["global"]["ipv6_only"];
if (val == "on") httpd.family = AF_INET6;
val = configs["global"]["hostname"];
if (val.size()) httpd.hostname = val;
val = configs["global"]["port"];
if (val.size()) httpd.port = val;
val = configs["global"]["indexpages"];
if (val.size()) httpd.default_pages = tthttpd::split_string(val, ",");
val = configs["global"]["charset"];
if (val.size()) httpd.fs_charset = val;
val = configs["global"]["chroot"];
if (val.size()) httpd.chroot = val;
val = configs["global"]["user"];
if (val.size()) httpd.user = val;
val = configs["global"]["debug"];
if (val == "on") httpd.verbose_mode = 1;
else if (val.size()) httpd.verbose_mode = atol(val.c_str());
val = configs["global"]["spawnexec"];
if (val == "on") httpd.spawn_executable = true;
config = configs["request/aliases"];
for (it = config.begin(); it != config.end(); it++)
httpd.request_aliases[it->first] = it->second;
config = configs["mime/types"];
for (it = config.begin(); it != config.end(); it++)
httpd.mime_types[it->first] = it->second;
config = configs["request/environments"];
for (it = config.begin(); it != config.end(); it++)
httpd.request_environments[it->first] = it->second;
config = configs["authentication"];
for (it = config.begin(); it != config.end(); it++) {
tthttpd::server::BasicAuthInfo basic_auth_info;
basic_auth_info.target = it->first;
std::vector<std::string> infos = tthttpd::split_string(it->second, ",");
basic_auth_info.method = infos[0];
basic_auth_info.realm = infos[1];
std::vector<tthttpd::server::AuthInfo> auth_infos;
if (loadAuthfile(infos[2].c_str(), auth_infos))
basic_auth_info.auths = auth_infos;
httpd.basic_auths.push_back(basic_auth_info);
}
} else {
#ifdef _WIN32
httpd.mime_types["cgi"] = "@c:/strawberry/perl/bin/perl.exe";
httpd.mime_types["php"] = "@c:/progra~1/php/php-cgi.exe";
httpd.mime_types["rb"] = "@c:/ruby/bin/ruby.exe";
httpd.mime_types["py"] = "@c:/python25/python.exe";
#else
httpd.mime_types["cgi"] = "@/usr/bin/perl";
httpd.mime_types["php"] = "@/usr/bin/php-cgi";
httpd.mime_types["rb"] = "@/usr/bin/ruby";
httpd.mime_types["py"] = "@/usr/bin/python";
#endif
}
httpd.start();
httpd.wait();
// Ctrl-C to break
return 0;
}