forked from mattn/tinytinyhttpd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpd.h
221 lines (206 loc) · 5.66 KB
/
httpd.h
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
/* Copyright 2009 by Yasuhiro Matsumoto
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _HTTPD_H_
#define _HTTPD_H_
#define HTTPD_VERSION 0x0006
#ifdef _MSC_VER
#pragma warning(disable:4018 4503 4530 4786 4996)
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "mswsock.lib")
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <sys/types.h>
#include <process.h>
#include <direct.h>
#include <io.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/uio.h>
#include <limits.h>
#include <unistd.h>
#endif
#include <sstream>
#ifndef _WIN32
#include <pthread.h>
#endif
#include "utils.h"
namespace tthttpd {
class server {
public:
typedef struct {
std::string name;
unsigned long size;
bool isdir;
struct tm date;
} ListInfo;
typedef struct {
int msgsock;
server *httpd;
std::string address;
std::string port;
int servno;
} HttpdInfo;
typedef struct {
std::string user;
std::string pass;
} AuthInfo;
typedef struct {
std::string target;
std::string method;
std::string realm;
std::vector<AuthInfo> auths;
} BasicAuthInfo;
typedef std::vector<BasicAuthInfo> BasicAuths;
typedef struct {
std::vector<std::string> accept_list;
} AcceptAuth;
typedef std::map<std::string, AcceptAuth> AcceptAuths;
typedef std::vector<std::string> AcceptIPs;
typedef void (*LoggerFunc)(const HttpdInfo* httpd_info, const std::string& request);
typedef std::map<std::string, std::string> MimeTypes;
typedef std::vector<std::string> DefaultPages;
typedef std::map<std::string, std::string> RequestAliases;
typedef std::map<std::string, std::string> RequestEnvironments;
private:
#ifdef _WIN32
HANDLE thread;
#else
pthread_t thread;
#endif
public:
std::vector<unsigned int> socks;
int family;
std::string hostname;
std::vector<std::string> hostaddr;
std::string root;
std::string fs_charset;
std::string chroot;
std::string user;
std::string port;
BasicAuths basic_auths;
AcceptAuths accept_auths;
AcceptIPs accept_ips;
MimeTypes mime_types;
DefaultPages default_pages;
RequestAliases request_aliases;
RequestEnvironments request_environments;
LoggerFunc loggerfunc;
bool spawn_executable;
int verbose_mode;
void initialize() {
port = "www";
fs_charset = "utf-8";
thread = 0;
loggerfunc = NULL;
mime_types["gif"] = "image/gif";
mime_types["jpg"] = "image/jpeg";
mime_types["png"] = "image/png";
mime_types["htm"] = "text/html";
mime_types["html"] = "text/html";
mime_types["txt"] = "text/plain";
mime_types["xml"] = "text/xml";
mime_types["js"] = "application/x-javascript";
mime_types["css"] = "text/css";
default_pages.push_back("index.html");
default_pages.push_back("index.php");
default_pages.push_back("index.rb");
default_pages.push_back("index.cgi");
spawn_executable = false;
verbose_mode = 0;
};
server() {
initialize();
}
server(std::string _port) {
initialize();
port = _port;
}
server(std::string _port, std::string _target) {
initialize();
port = _port;
}
~server() {
stop();
}
bool start();
bool stop();
bool wait();
void set_fs_charset(std::string _fs_charset) {
fs_charset = _fs_charset;
}
std::string get_fs_charset() {
return fs_charset;
}
void setAuthentication(BasicAuths _basic_auths) {
basic_auths = _basic_auths;
}
void bindRoot(std::string _root) {
root = get_realpath(_root + "/");
}
static std::string get_realpath(std::string abspath) {
std::string path = abspath;
#ifdef _WIN32
char fullpath[_MAX_PATH] = {0};
char* filepart = NULL;
if (GetFullPathNameA(path.c_str(), _MAX_PATH, fullpath, &filepart))
path = fullpath;
#else
char fullpath[PATH_MAX] = {0};
if (realpath((char*)path.c_str(), fullpath)) {
path = fullpath;
}
#endif
std::replace(path.begin(), path.end(), '\\', '/');
size_t end_pos = path.find_last_of('?');
if (end_pos != std::string::npos) path.resize(end_pos);
std::vector<std::string> path_sep = split_string(path, "/");
std::vector<std::string>::iterator it;
while(true) {
it = std::find(path_sep.begin(), path_sep.end(), "..");
if (it == path_sep.end()) break;
if (it == path_sep.begin()) {
continue;
}
path_sep.erase(it-1);
path_sep.erase(it-1);
}
std::string path_real;
for(it = path_sep.begin(); it != path_sep.end(); it++) {
path_real += *it;
if (it+1 != path_sep.end())
path_real += "/";
}
if (abspath[abspath.size()-1] == '/')
path_real += "/";
return path_real;
}
};
}
#endif /* _HTTPD_H_ */