This repository was archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 767
Expand file tree
/
Copy pathhttp_examples.cpp
More file actions
311 lines (265 loc) · 12.2 KB
/
http_examples.cpp
File metadata and controls
311 lines (265 loc) · 12.2 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
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
#include "server_http.hpp"
#include "client_http.hpp"
//Added for the json-example
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
//Added for the default_resource example
#include <fstream>
#include <boost/filesystem.hpp>
#include <vector>
#include <algorithm>
#ifdef HAVE_OPENSSL
#include "crypto.hpp"
#endif
using namespace std;
//Added for the json-example:
using namespace boost::property_tree;
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
//Added for the default_resource example
void default_resource_send(const HttpServer &server, const shared_ptr<HttpServer::Response> &response,
const shared_ptr<ifstream> &ifs);
class MimeTypes {
public:
void load(const std::string &f);
void parse(const std::string &mimestring);
void parse(std::istream &mimestream);
const std::string &lookup(const std::string &extension);
std::unordered_map<std::string, std::string> _mime_db;
};
int main() {
//HTTP-server at port 8080 using 1 thread
//Unless you do more heavy non-threaded processing in the resources,
//1 thread is usually faster than several threads
HttpServer server;
server.config.port=8080;
// Mime Types
MimeTypes mime_types;
//Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
//Retrieve string:
auto content=request->content.string();
//request->content.string() is a convenience function for:
//stringstream ss;
//ss << request->content.rdbuf();
//string content=ss.str();
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
};
//POST-example for the path /json, responds firstName+" "+lastName from the posted json
//Responds with an appropriate error message if the posted json is not valid, or if firstName or lastName is missing
//Example posted json:
//{
// "firstName": "John",
// "lastName": "Smith",
// "age": 25
//}
server.resource["^/json$"]["POST"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
try {
ptree pt;
read_json(request->content, pt);
string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName");
*response << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: application/json\r\n"
<< "Content-Length: " << name.length() << "\r\n\r\n"
<< name;
}
catch(exception& e) {
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
}
};
//GET-example for the path /info
//Responds with request-information
server.resource["^/info$"]["GET"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
stringstream content_stream;
content_stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
for(auto& header: request->header) {
content_stream << header.first << ": " << header.second << "<br>";
}
//find length of content_stream (length received using content_stream.tellp())
content_stream.seekp(0, ios::end);
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf();
};
//GET-example for the path /match/[number], responds with the matched string in path (number)
//For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
string number=request->path_match[1];
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
};
//Get example simulating heavy work in a separate thread
server.resource["^/work$"]["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
thread work_thread([response] {
this_thread::sleep_for(chrono::seconds(5));
string message="Work done";
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << message.length() << "\r\n\r\n" << message;
});
work_thread.detach();
};
//Default GET-example. If no other matches, this anonymous function will be called.
//Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server.default_resource["GET"]=[&server, &mime_types](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
try {
auto web_root_path=boost::filesystem::canonical("web");
auto path=boost::filesystem::canonical(web_root_path/request->path);
//Check if path is within web_root_path
if(distance(web_root_path.begin(), web_root_path.end())>distance(path.begin(), path.end()) ||
!equal(web_root_path.begin(), web_root_path.end(), path.begin()))
throw invalid_argument("path must be within root path");
if(boost::filesystem::is_directory(path))
path/="index.html";
if(!(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)))
throw invalid_argument("file does not exist");
std::string cache_control, etag;
// Uncomment the following line to enable Cache-Control
// cache_control="Cache-Control: max-age=86400\r\n";
#ifdef HAVE_OPENSSL
// Uncomment the following lines to enable ETag
// {
// ifstream ifs(path.string(), ifstream::in | ios::binary);
// if(ifs) {
// auto hash=SimpleWeb::Crypto::to_hex_string(SimpleWeb::Crypto::md5(ifs));
// etag = "ETag: \""+hash+"\"\r\n";
// auto it=request->header.find("If-None-Match");
// if(it!=request->header.end()) {
// if(!it->second.empty() && it->second.compare(1, hash.size(), hash)==0) {
// *response << "HTTP/1.1 304 Not Modified\r\n" << cache_control << etag << "\r\n\r\n";
// return;
// }
// }
// }
// else
// throw invalid_argument("could not read file");
// }
#endif
auto ifs=make_shared<ifstream>();
ifs->open(path.string(), ifstream::in | ios::binary | ios::ate);
if(*ifs) {
auto length=ifs->tellg();
ifs->seekg(0, ios::beg);
// Get the file extension and look up the corresponding mime type
auto ext = path.filename().extension().string();
if (!ext.empty() && ext[0] == '.') {
ext.erase(0, 1);
}
auto mime = mime_types.lookup(ext);
std::string content_type;
if (!mime.empty()) {
content_type = "Content-Type: " + mime + "\r\n";
} // else if empty: Do not set Content-Type
*response << "HTTP/1.1 200 OK\r\n" << cache_control << etag << content_type << "Content-Length: " << length << "\r\n\r\n";
default_resource_send(server, response, ifs);
}
else
throw invalid_argument("could not read file");
}
catch(const exception &e) {
string content="Could not open path "+request->path+": "+e.what();
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}
};
thread server_thread([&server](){
//Start server
server.start();
});
//Wait for server to start so that the client can connect
this_thread::sleep_for(chrono::seconds(1));
//Client examples
// Load mime.types from Apache web site
// If this site is not reachable or the response is not valid nothing will happen
HttpClient apache_client("svn.apache.org");
auto mr = apache_client.request("GET", "/repos/asf/httpd/httpd/trunk/docs/conf/mime.types");
// Make sure we got a valid response
if(mr->status_code == "200 OK") {
mime_types.parse(mr->content);
}
HttpClient client("localhost:8080");
auto r1=client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl;
string json_string="{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
auto r2=client.request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
auto r3=client.request("POST", "/json", json_string);
cout << r3->content.rdbuf() << endl;
server_thread.join();
return 0;
}
void default_resource_send(const HttpServer &server, const shared_ptr<HttpServer::Response> &response,
const shared_ptr<ifstream> &ifs) {
//read and send 128 KB at a time
static vector<char> buffer(131072); // Safe when server is running on one thread
streamsize read_length;
if((read_length=ifs->read(&buffer[0], buffer.size()).gcount())>0) {
response->write(&buffer[0], read_length);
if(read_length==static_cast<streamsize>(buffer.size())) {
server.send(response, [&server, response, ifs](const boost::system::error_code &ec) {
if(!ec)
default_resource_send(server, response, ifs);
else
cerr << "Connection interrupted" << endl;
});
}
}
}
/**
* Parses Apaches mime.types files
* @param path and file name to mime.types like file
*/
void MimeTypes::load(const std::string &f) {
try {
std::ifstream mimefile(f);
if (mimefile.is_open()) {
parse(mimefile);
}
mimefile.close();
} catch (const std::exception &e) {
// Something bad happened. How to handle this kind of exception?
// Don't care for now
}
}
void MimeTypes::parse(const std::string &mimestring) {
std::stringstream mimestream(mimestring);
parse(mimestream);
}
void MimeTypes::parse(std::istream &mimestream) {
try {
std::string line;
while (std::getline(mimestream, line)) {
// discard comments
auto start_of_comment = line.find("#");
if (start_of_comment != std::string::npos) {
line.erase(start_of_comment, std::string::npos);
}
// mime type followed by zero or more file extensions
std::istringstream iss(line);
std::string mime;
if (iss >> mime) {
std::string ext; // mime successful read
while (iss >> ext) { // read and insert extensions
if (!ext.empty()) {
_mime_db[ext] = mime;
}
}
}
}
} catch (const std::exception &e) {
// Something bad happened. How to handle this kind of exception?
// Don't care for now
}
}
/**
* Looks up the Mime Type belonging to the File Extension
* @param file extension to look up
* @return Mime Type corresponding to extension. Or the empty string if nothing found. In case of "" do not set the Content Header!
*/
const std::string& MimeTypes::lookup(const std::string &extension) {
auto finding = _mime_db.find(extension);
if (finding != _mime_db.end()) {
return (*finding).second;
}
static std::string unknown_mime_type = "";
return unknown_mime_type;
}