-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhttpd.cpp
334 lines (297 loc) · 10 KB
/
webhttpd.cpp
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
#ifndef _WEBHTTPD_CPP_
#define _WEBHTTPD_CPP_
#include "webhttpd.h"
#include <cstdio>
namespace twebhttpd {
std::set<int> WebHttpd::g_sock_sets;
int HttpRequest::parse_request(HttpInfoTrans* http_info) {
std::string tmp, req_line;
int len = WebHttpd::read_line(http_info->sock_, req_line);
if(len < 0) return -1; // read socket error return -1 to close connection
std::vector<std::string> req_header = this->split_header(req_line.c_str(), (char*)" ");
if(req_header.size() < 3) {
return 1; // error header, not close connection
}
this->header.method = req_header[0];
this->header.path = req_header[1];
this->header.http_version = req_header[2];
while(1) {
len = WebHttpd::read_line(http_info->sock_, tmp);
if(len < 0) return -1;
else if(0 == len) break;
req_header = this->split_header(tmp.c_str(), (char*)" ");
if(req_header.size() < 2) continue;
this->header.add(req_header[0], req_header[1]);
}
if((int)this->header.method.find("POST") >= 0) {
int length = atoi(this->header.get("Content-Length").c_str());
WebHttpd::read_bytes(http_info->sock_, this->body_, length);
puts(this->body_.c_str());
}
return 0;
}
void HttpRequest::parse_get_param(std::string param_str) {
std::string path = "";
char *tmp = (char*)param_str.c_str();
int p = 0;
for(;p < (int)param_str.length();p++) {
if('?' == tmp[p]) break;
path += tmp[p];
}
param_str = tmp;
}
void HttpRequest::parse_post_param(std::string param_str) {
}
void HttpRequest::parse_param(std::string param_str) {
}
std::vector<std::string> HttpRequest::split_header(const char* s, char* step) {
std::vector<std::string> arr;
arr.clear();
if(NULL == s) return arr;
std::string tmp;
char* ptr = NULL;
while(1) {
ptr = (char*)strstr(s, step);
if(NULL == ptr) {
tmp = s;
arr.push_back(tmp);
break;
}
*ptr = '\0';
tmp = s;
arr.push_back(tmp);
s = ptr + strlen(step);
}
return arr;
}
HttpResponse::HttpResponse() {
this->header.http_version = "HTTP/1.1";
this->header.status = "OK";
this->header.status_code = "200";
this->header.add("Content-Type", "text/plain; charset=utf-8");
this->body_ = "";
}
HttpResponse::~HttpResponse() {
}
void HttpResponse::init(HttpRequest* req, HttpInfoTrans* http_info) {
if(NULL == req || NULL == http_info) return;
this->sock_ = http_info->sock_;
std::string conn = req->header.get("Connection");
std::transform(conn.begin(), conn.end(), conn.begin(), toupper);
if((int)conn.find("KEEP-ALIVE") >= 0 ||
(int)req->header.http_version.find("1.1") >= 0) {
this->header.add("Connection", "Keep-Alive");
}
}
void HttpResponse::writer(const std::string& str) {
this->body_ += str;
}
void HttpResponse::do_resp(const std::string& str) {
this->body_ += str;
this->do_resp();
}
void HttpResponse::do_resp() {
char buff[BUFFSIZE];
memset(buff, 0, sizeof(buff));
sprintf(buff, "%s %s %s", this->header.http_version.c_str(), this->header.status_code.c_str(), this->header.status.c_str());
this->send_line(buff);
sprintf(buff, "Content-Length: %lu", this->body_.length());
this->send_line(buff);
for(std::map<std::string, std::string>::iterator iter = this->header.header_.begin(); iter != this->header.header_.end();iter++) {
sprintf(buff, "%s: %s", iter->first.c_str(), iter->second.c_str());
this->send_line(buff);
}
this->send_line((char*)"");
memcpy(buff, this->body_.c_str(), this->body_.length() + 1);
this->send_line(buff);
}
void HttpResponse::send_line(char buff[]) {
if(strlen(buff) > 0) {
send(this->sock_, buff, strlen(buff), 0);
memset(buff, 0, BUFFSIZE);
}
send(this->sock_, "\r\n", 2, 0);
}
void HttpResponse::resp_error404() {
this->header.status = "Not Found";
this->header.status_code = "404";
this->body_ = "404 page not found";
this->do_resp();
}
WebHttpd::WebHttpd() {
this->port_ = 80;
this->server_addr_ = htonl(INADDR_ANY);
this->handler_route_ = NULL;
//this->file_handler_route_ = NULL;
shutdown(this->sock_, SHUT_RDWR);
close(this->sock_);
}
WebHttpd::~WebHttpd() {
if(this->handler_route_ != NULL)
delete this->handler_route_;
this->stop(0);
//if(this->file_handler_route_ != NULL)
// delete this->file_handler_route_;
}
int WebHttpd::read_line(int sock, std::string& line) {
char c = 0;
int len = 0;
line.clear();
while(1) {
len = recv(sock, &c, 1, 0);
if(len < 0) {
shutdown(sock, SHUT_RDWR);
close(sock);
return -1;
}
if(0 == len) break;
if('\r' == c) continue;
if('\n' == c) break;
line += c;
}
return line.length();
}
int WebHttpd::read_bytes(int sock, std::string& line, int len) {
if(len <= 0) return 0;
char buffer[len + 1];
memset(buffer, 0, sizeof(buffer));
line.clear();
int ll = recv(sock, buffer, len, 0);
buffer[len] = '\0';
line = buffer;
return ll;
}
void file_handle(std::map<std::string, FileHandler>* router, HttpRequest* req, HttpResponse resp) {}
void* WebHttpd::response_handler(void* p) {
try {
if(NULL == p) {
return NULL;
}
HttpInfoTrans* http_info = (HttpInfoTrans*)p;
bool keep_alive = true;
int res = 0;
puts("hahahaha");
while(keep_alive) {
HttpRequest* req = new HttpRequest;
HttpResponse resp;
if(req != NULL) {
res = req->parse_request(http_info);
} else {
resp.resp_error500();
return NULL;
}
if(res < 0) {
if(req != NULL) delete req;
resp.resp_error500();
return NULL;
}
if(res > 0) {
if(req != NULL) delete req;
resp.resp_error500();
return NULL;
}
resp.init(req, http_info);
router::iterator iter;
if(NULL != http_info->handler_route_)
iter = http_info->handler_route_->find(req->header.path);
if((http_info->handler_route_ != NULL && http_info->handler_route_->end() != iter) || http_info->static_file_handler != NULL) {
HandlerFunction* serve = NULL;
if(http_info->handler_route_ != NULL && http_info->handler_route_->end() != iter) {
serve = (HandlerFunction*)iter->second;
} else {
serve = http_info->static_file_handler;
}
// if not keep-alive
std::string conn_pro = req->header.get("Connection");
if(!((req->header.http_version.find("1.1") && (int)conn_pro.find("CLOSE") < 0) ||
(int)conn_pro.find("KEEP-ALIVE") >= 0)) {
keep_alive = false;
resp.header.add("Connection", "close");
}
serve(req, resp);
if(req != NULL) delete req;
} else {
resp.resp_error404();
}
//if(http_info->handler_route_ == NULL || http_info->handler_route_->end() == iter) {
// file_handle(WebHttpd::file_handler_route_, req, resp);
//}
}
shutdown(http_info->sock_, SHUT_RDWR);
close(http_info->sock_);
WebHttpd::g_sock_sets.erase(http_info->sock_);
if(http_info != NULL) delete http_info;
} catch(...) {
printf("catch exception!\n");
}
return NULL;
}
void WebHttpd::handle_func(std::string path, HandlerFunction* func) {
if(NULL == this->handler_route_) {
this->handler_route_ = new router;
}
(*this->handler_route_)[path] = func;
}
//void* WebHttpd::add_file_handler(std::string prefix, std::string stripfix, std::string path) {
/*twebhttpd::FileHandler handler;
handler.strip_prefix = stripfix;
handler.path = path;
if(NULL == this->file_handler_route_)
this->file_handler_route_ = new(std::map<std::string, FileHandler>);
(*this->file_handler_route_)[prefix] = handler;
return NULL; */
//}
int WebHttpd::start_service() {
this->server_sock_addr_.sin_family = AF_INET;
this->server_sock_addr_.sin_addr.s_addr = this->server_addr_;
this->server_sock_addr_.sin_port = htons(this->port_);
if((this->sock_ = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("create socket error!\n");
return -1;
}
if(bind(this->sock_, (struct sockaddr*)&this->server_sock_addr_, sizeof(struct sockaddr))) {
printf("bind sock addr error!\n");
return -1;
}
if(listen(this->sock_, SOMAXCONN) < 0) {
printf("listen error!\n");
return -1;
}
WebHttpd::g_sock_sets.insert(this->sock_);
signal(SIGINT, WebHttpd::stop);
while(true) {
try {
socklen_t sin_size = sizeof(struct sockaddr_in);
sockaddr_in* client_sock_addr = new sockaddr_in;
int client_sock = 0;
pthread_t pth_handler;
if((client_sock = accept(this->sock_, (struct sockaddr*)client_sock_addr, &sin_size )) < 0) {
printf("accept error!\n");
if(client_sock_addr != NULL) delete client_sock_addr;
break;
}
WebHttpd::g_sock_sets.insert(client_sock);
HttpInfoTrans* http_info = new HttpInfoTrans;
http_info->sock_ = client_sock;
http_info->handler_route_ = this->handler_route_;
http_info->static_file_handler = this->static_file_handler_;
http_info->client_addr_ = client_sock_addr;
pthread_create(&pth_handler, NULL, WebHttpd::response_handler, (void*)http_info);
pthread_detach(pth_handler);
} catch(...) {
puts("catch exception!");
}
}
return 0;
}
void WebHttpd::stop(int) {
printf("\nexecute stop!\n");
for(std::set<int>::iterator iter = WebHttpd::g_sock_sets.begin(); iter != WebHttpd::g_sock_sets.end();iter++) {
shutdown(*iter, SHUT_RDWR);
close(*iter);
}
WebHttpd::g_sock_sets.clear();
exit(0);
}
}
#endif