#include "server_https.hpp" #include "client_https.hpp" #ifdef HAVE_OPENSSL #include "crypto.hpp" #endif //Added for the json-example #define BOOST_SPIRIT_THREADSAFE #include #include //Added for the default_resource example #include #include #include #include using namespace std; //Added for the json-example: using namespace boost::property_tree; typedef SimpleWeb::Server HttpsServer; typedef SimpleWeb::Client HttpsClient; //Added for the default_resource example void default_resource_send(const HttpsServer &server, const shared_ptr &response, const shared_ptr &ifs); #ifdef HAVE_OPENSSL std::string build_hash(const std::string full_file_name); #endif int main() { //HTTPS-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 HttpsServer server(8080, 1, "server.crt", "server.key"); //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 response, shared_ptr 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 response, shared_ptr request) { try { ptree pt; read_json(request->content, pt); string name=pt.get("firstName")+" "+pt.get("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 response, shared_ptr request) { stringstream content_stream; content_stream << "

Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")

"; content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; for(auto& header: request->header) { content_stream << header.first << ": " << header.second << "
"; } //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 response, shared_ptr 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 response, shared_ptr /*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](shared_ptr response, shared_ptr request) { try { auto web_root_path=boost::filesystem::canonical("web"); auto path=boost::filesystem::canonical(web_root_path/request->path); std::string cache_ctrl = ""; //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"); #ifdef HAVE_OPENSSL //Support for HTTP Cache std::string hash = build_hash(path.string()); auto it=request->header.find("If-None-Match"); if(it!=request->header.end()) { if (it->second == "\""+hash+"\"") { *response << "HTTP/1.1 304 Not Modified\r\nCache-Control: max-age=86400\r\nETag: \""+hash+"\"\r\n\r\n"; return; } } cache_ctrl = "Cache-Control: max-age=86400\r\nETag: \""+hash+"\"\r\n"; #endif auto ifs=make_shared(); ifs->open(path.string(), ifstream::in | ios::binary); if(*ifs) { ifs->seekg(0, ios::end); auto length=ifs->tellg(); ifs->seekg(0, ios::beg); *response << "HTTP/1.1 200 OK\r\n" << cache_ctrl << "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 //Second Client() parameter set to false: no certificate verification HttpsClient client("localhost:8080", false); 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 HttpsServer &server, const shared_ptr &response, const shared_ptr &ifs) { //read and send 128 KB at a time static vector 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(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; }); } } } #ifdef HAVE_OPENSSL std::string build_hash(const std::string full_file_name) { std::string ret = ""; std::ifstream file(full_file_name, std::ios::binary | std::ios::ate); std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); std::vector buffer(size); if (!file.read(buffer.data(), size)) return ret; ret = reinterpret_cast (&buffer[0]); return SimpleWeb::Crypto::md5(ret,1); } #endif