Added custom response stream and possibility to flush response to clients synchronously and asynchronously. Various speed ups, including reduced use of regex and preprocessing of regex objects. boost::asio::ip::tcp::no_delay is now turned on for both Client and Server. Note: Not backward compatible with earlier versions.

This commit is contained in:
eidheim 2015-02-20 11:14:39 +01:00
commit a6154c7c5b
8 changed files with 331 additions and 168 deletions

View file

@ -10,19 +10,20 @@
#include<fstream>
using namespace std;
using namespace SimpleWeb;
//Added for the json-example:
using namespace boost::property_tree;
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
int main() {
//HTTP-server at port 8080 using 4 threads
Server<HTTP> server(8080, 4);
HttpServer server(8080, 4);
//Add resources using regular expression for path, a method-string, and an anonymous function
//Add resources using path- and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string
// If C++14: use 'auto' instead of 'shared_ptr<Server<HTTPS>::Request>'
server.resource["^/string/?$"]["POST"]=[](ostream& response, shared_ptr<Server<HTTP>::Request> request) {
//Retrieve string from istream (*request.content)
server.resource["/string"]["POST"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
//Retrieve string from istream (request->content)
stringstream ss;
request->content >> ss.rdbuf();
string content=ss.str();
@ -38,7 +39,7 @@ int main() {
// "lastName": "Smith",
// "age": 25
//}
server.resource["^/json/?$"]["POST"]=[](ostream& response, shared_ptr<Server<HTTP>::Request> request) {
server.resource["/json"]["POST"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
try {
ptree pt;
read_json(request->content, pt);
@ -54,7 +55,7 @@ int main() {
//GET-example for the path /info
//Responds with request-information
server.resource["^/info/?$"]["GET"]=[](ostream& response, shared_ptr<Server<HTTP>::Request> request) {
server.resource["/info"]["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
stringstream content_stream;
content_stream << "<h1>Request:</h1>";
content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
@ -68,9 +69,10 @@ int main() {
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)
//GET-example for the path /match/[number] using regex, responds with the matched string in path (number)
//The 'r' at the beginning of the path-string indicates that the following string is a regular expression
//For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)/?$"]["GET"]=[](ostream& response, shared_ptr<Server<HTTP>::Request> request) {
server.resource["r^/match/([0-9]+)/?$"]["GET"]=[](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;
};
@ -79,10 +81,10 @@ int main() {
//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"]=[](ostream& response, shared_ptr<Server<HTTP>::Request> request) {
string filename="web/";
server.default_resource["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
string filename="web";
string path=request->path_match[1];
string path=request->path;
//Replace all ".." with "." (so we can't leave the web-directory)
size_t pos;
@ -106,8 +108,17 @@ int main() {
ifs.seekg(0, ios::beg);
//The file-content is copied to the response-stream. Should not be used for very large files.
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n" << ifs.rdbuf();
response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
//read and send 128 KB at a time
size_t buffer_size=131072;
vector<char> buffer(buffer_size);
stringstream ss;
size_t read_length;
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
ss.write(&buffer[0], read_length);
response << ss.rdbuf() << HttpServer::flush;
}
ifs.close();
}
@ -126,7 +137,7 @@ int main() {
this_thread::sleep_for(chrono::seconds(1));
//Client examples
Client<HTTP> client("localhost:8080");
HttpClient client("localhost:8080");
auto r1=client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl;
@ -138,8 +149,8 @@ int main() {
ss.str(json);
auto r3=client.request("POST", "/json", ss);
cout << r3->content.rdbuf() << endl;
server_thread.join();
return 0;
}
}