webserver.cpp
4.69 KB
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
/*
WebServer.cpp
Copyright (C) 2003-2004 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
Thanks to Tom Lynn who pointed out an error in this source code.
*/
#include <ctime>
#include <process.h>
#include <iostream>
#include <string>
#include <map>
#include <sstream>
//<process.h>
#include "webserver.h"
#include "socket/src/Socket.h"
#include "UrlHelper.h"
#include "base64/base64.h"
webserver::request_func webserver::request_func_=0;
unsigned webserver::Request(Socket* ptr_s) {
Socket s = *ptr_s;
std::string line = s.ReceiveLine();
if (line.empty()) {
return 1;
}
http_request req;
if (line.find("GET") == 0) {
req.method_="GET";
}
else if (line.find("POST") == 0) {
req.method_="POST";
}
std::string path;
std::map<std::string, std::string> params;
const size_t posStartPath = line.find_first_not_of(" ",3);
SplitGetReq(line.substr(posStartPath), path, params);
req.status_ = "202 OK";
req.s_ = &s;
req.path_ = path;
req.params_ = params;
static const std::string authorization = "Authorization: Basic ";
static const std::string accept = "Accept: " ;
static const std::string accept_language = "Accept-Language: " ;
static const std::string accept_encoding = "Accept-Encoding: " ;
static const std::string user_agent = "User-Agent: " ;
while(true) {
line=s.ReceiveLine();
if (line.empty()) break;
const unsigned int pos_cr_lf = line.find_first_of("\x0a\x0d");
if (pos_cr_lf == 0) break;
line = line.substr(0,pos_cr_lf);
if (line.substr(0, authorization.size()) == authorization) {
req.authentication_given_ = true;
std::string encoded = line.substr(authorization.size());
std::string decoded = base64_decode(encoded);
const unsigned int pos_colon = decoded.find(":");
req.username_ = decoded.substr(0, pos_colon);
req.password_ = decoded.substr(pos_colon+1 );
}
else if (line.substr(0, accept.size()) == accept) {
req.accept_ = line.substr(accept.size());
}
else if (line.substr(0, accept_language.size()) == accept_language) {
req.accept_language_ = line.substr(accept_language.size());
}
else if (line.substr(0, accept_encoding.size()) == accept_encoding) {
req.accept_encoding_ = line.substr(accept_encoding.size());
}
else if (line.substr(0, user_agent.size()) == user_agent) {
req.user_agent_ = line.substr(user_agent.size());
}
}
request_func_(&req);
std::stringstream str_str;
str_str << req.answer_.size();
time_t ltime;
time(<ime);
#ifdef _CRT_SECURE_NO_WARNINGS
tm* gmt= gmtime(<ime);
#else
tm gmt{};
gmtime_s(&gmt, <ime);
#endif
static std::string const serverName = "RenesWebserver (Windows)";
#ifdef _CRT_SECURE_NO_WARNINGS
char* asctime_remove_nl = asctime(gmt);
#else
char asctime_remove_nl[26];
asctime_s(asctime_remove_nl,&gmt);
#endif
asctime_remove_nl[24] = 0;
s.SendBytes("HTTP/1.1 ");
if (! req.auth_realm_.empty() ) {
s.SendLine("401 Unauthorized");
s.SendBytes("WWW-Authenticate: Basic Realm=\"");
s.SendBytes(req.auth_realm_);
s.SendLine("\"");
}
else {
s.SendLine(req.status_);
}
s.SendLine(std::string("Date: ") + asctime_remove_nl + " GMT");
s.SendLine(std::string("Server: ") +serverName);
s.SendLine("Connection: close");
s.SendLine("Content-Type: text/html; charset=UTF-8");
s.SendLine("Content-Length: " + str_str.str());
s.SendLine("");
s.SendLine(req.answer_);
s.Close();
return 0;
}
webserver::webserver(unsigned int port_to_listen, request_func r) {
SocketServer in(port_to_listen,5);
request_func_ = r;
std::cout<<"Listening on port "<<port_to_listen<<std::endl;
while (true) {
Socket* ptr_s=in.Accept();
unsigned ret;
_beginthreadex(nullptr,0,Request, ptr_s,0,&ret);
}
}