#include "client_http.hpp" #include "server_http.hpp" // Added for the json-example #define BOOST_SPIRIT_THREADSAFE #include #include // Added for the default_resource example #include #include #include #include #ifdef HAVE_OPENSSL #include "crypto.hpp" #endif using namespace std; // Added for the json-example: using namespace boost::property_tree; using HttpServer = SimpleWeb::Server; using HttpClient = SimpleWeb::Client; 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.thread_pool_size server.config.address = "192.168.188.128"; server.config.port = 8090; // 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(); // auto content=ss.str(); *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; // Alternatively, use one of the convenience functions, for instance: // response->write(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); auto name = pt.get("firstName") + " " + pt.get("lastName"); // write_json() // write_json(*response,pt); // *response << "HTTP/1.1 200 OK\r\n" // << "Content-Length: " << name.length() << "\r\n\r\n" // << name; string res = "{\"aaaa\": \"cccc\",\"dasfd\": \"asdfasdf\",\"age\": 123212}"; *response << "HTTP/1.1 200 OK\r\n" << "Content-Length: " << res.size() << "\r\n\r\n"<content, pt); // auto name=pt.get("firstName")+" "+pt.get("lastName"); // response->write(name); // } // catch(const exception &e) { // response->write(SimpleWeb::StatusCode::client_error_bad_request, e.what()); // } }; // GET-example for the path /info // Responds with request-information server.resource["^/info$"]["GET"] = [](shared_ptr response, shared_ptr request) { stringstream stream; stream << "

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

"; stream << request->method << " " << request->path << " HTTP/" << request->http_version; stream << "

Query Fields

"; auto query_fields = request->parse_query_string(); for (auto &field : query_fields) stream << field.first << ": " << field.second << "
"; stream << "

Header Fields

"; for (auto &field : request->header) stream << field.first << ": " << field.second << "
"; response->write(stream); }; // 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"] = [](shared_ptr response, shared_ptr request) { response->write(request->path_match[1]); }; // GET-example simulating heavy work in a separate thread server.resource["^/work$"]["GET"] = [](shared_ptr response, shared_ptr /*request*/) { thread work_thread([response] { this_thread::sleep_for(chrono::seconds(5)); response->write("Work done"); }); 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"] = [](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); // 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"; SimpleWeb::CaseInsensitiveMultimap header; // Uncomment the following line to enable Cache-Control // header.emplace("Cache-Control", "max-age=86400"); #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)); // header.emplace("ETag", "\"" + hash + "\""); // 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->write(SimpleWeb::StatusCode::redirection_not_modified, header); // return; // } // } // } // else // throw invalid_argument("could not read file"); // } #endif auto ifs = make_shared(); ifs->open(path.string(), ifstream::in | ios::binary | ios::ate); if (*ifs) { auto length = ifs->tellg(); ifs->seekg(0, ios::beg); header.emplace("Content-Length", to_string(length)); response->write(header); // Trick to define a recursive function within this scope (for example purposes) class FileServer { public: static void read_and_send(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], static_cast(buffer.size())).gcount()) > 0) { response->write(&buffer[0], read_length); if (read_length == static_cast(buffer.size())) { response->send([response, ifs](const SimpleWeb::error_code &ec) { if (!ec) read_and_send(response, ifs); else cerr << "Connection interrupted" << endl; }); } } } }; FileServer::read_and_send(response, ifs); } else throw invalid_argument("could not read file"); } catch (const exception &e) { response->write(SimpleWeb::StatusCode::client_error_bad_request, "Could not open path " + request->path + ": " + e.what()); } }; server.on_error = [](shared_ptr /*request*/, const SimpleWeb::error_code & /*ec*/) { // Handle errors here // Note that connection timeouts will also call this handle with ec set to SimpleWeb::errc::operation_canceled }; 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 HttpClient client("192.168.188.128:8090"); string json_string = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}"; // Synchronous request examples try { auto r1 = client.request("GET", "/match/123"); cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string() auto r2 = client.request("POST", "/string", json_string); cout << r2->content.rdbuf() << endl; } catch (const SimpleWeb::system_error &e) { cerr << "Client request error: " << e.what() << endl; } // Asynchronous request example client.request("POST", "/json", json_string, [](shared_ptr response, const SimpleWeb::error_code &ec) { if (!ec) cout << response->content.rdbuf() << endl; }); client.io_service->run(); server_thread.join(); }