// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt /* This example illustrates the use of the HTTP extension to the server object from the dlib C++ Library. It creates a server that always responds with a simple HTML form. To view the page this program displays you should go to http://localhost:5000 */ #include #include #include #include "dlib/server.h" #include "dlib/ref.h" // for ref() using namespace dlib; using namespace std; class web_server : public server::http_1a_c { const std::string on_request ( const incoming_things& incoming, outgoing_things& outgoing ) { try { ostringstream sout; // We are going to send back a page that contains an HTML form with two text input fields. // One field called name. The HTML form uses the post method but could also use the get // method (just change method='post' to method='get'). sout << " " << "
" << "User Name:
" << "User password: " << "
"; // Write out some of the inputs to this request so that they show up on the // resulting web page. sout << "
path = " << incoming.path << endl; sout << "
request_type = " << incoming.request_type << endl; sout << "
content_type = " << incoming.content_type << endl; sout << "
foreign_ip = " << incoming.foreign_ip << endl; sout << "
foreign_port = " << incoming.foreign_port << endl; sout << "
local_ip = " << incoming.local_ip << endl; sout << "
local_port = " << incoming.local_port << endl; sout << "
body = \"" << incoming.body << "\"" << endl; // If this request is the result of the user submitting the form then echo back // the submission. if (incoming.path == "/form_handler") { sout << "

Stuff from the query string

" << endl; sout << "
user = " << incoming.queries["user"] << endl; sout << "
pass = " << incoming.queries["pass"] << endl; // save these form submissions as cookies. outgoing.cookies["user"] = incoming.queries["user"]; outgoing.cookies["pass"] = incoming.queries["pass"]; } // Echo any cookies back to the client browser sout << "

Cookies we sent to the server

"; for ( key_value_map::const_iterator ci = incoming.cookies.begin(); ci != incoming.cookies.end(); ++ci ) { sout << "
" << ci->first << " = " << ci->second << endl; } sout << "

"; sout << "

HTTP Headers we sent to the server

"; // Echo out all the HTTP headers we received from the client web browser for ( key_value_map::const_iterator ci = incoming.headers.begin(); ci != incoming.headers.end(); ++ci ) { sout << "
" << ci->first << ": " << ci->second << endl; } sout << " "; return sout.str(); } catch (exception& e) { return e.what(); } } }; void thread(web_server& the_server) { try { // Start the server. start() blocks until the server is shutdown // by a call to clear() the_server.start(); } catch (socket_error& e) { cout << "Socket error while starting server: " << e.what() << endl; } catch (exception& e) { cout << "Error while starting server: " << e.what() << endl; } } int main() { try { // create an instance of our web server web_server our_web_server; // make it listen on port 5000 our_web_server.set_listening_port(5000); // create a thread that will start the server. The ref() here allows us to pass // our_web_server into the threaded function by reference. thread_function t(thread, dlib::ref(our_web_server)); cout << "Press enter to end this program" << endl; cin.get(); // this will cause the server to shut down our_web_server.clear(); } catch (exception& e) { cout << e.what() << endl; } }