2009-02-17 09:45:57 +08:00
|
|
|
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
2008-06-23 06:56:19 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
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 <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include "dlib/server.h"
|
|
|
|
|
|
|
|
using namespace dlib;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-11-08 10:41:50 +08:00
|
|
|
class web_server : public server_http
|
2008-06-23 06:56:19 +08:00
|
|
|
{
|
2009-03-15 21:45:37 +08:00
|
|
|
const std::string on_request (
|
|
|
|
const incoming_things& incoming,
|
|
|
|
outgoing_things& outgoing
|
2008-06-23 06:56:19 +08:00
|
|
|
)
|
|
|
|
{
|
2012-11-08 07:11:28 +08:00
|
|
|
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 << " <html> <body> "
|
|
|
|
<< "<form action='/form_handler' method='post'> "
|
|
|
|
<< "User Name: <input name='user' type='text'><br> "
|
|
|
|
<< "User password: <input name='pass' type='text'> <input type='submit'> "
|
|
|
|
<< " </form>";
|
|
|
|
|
|
|
|
// Write out some of the inputs to this request so that they show up on the
|
|
|
|
// resulting web page.
|
|
|
|
sout << "<br> path = " << incoming.path << endl;
|
|
|
|
sout << "<br> request_type = " << incoming.request_type << endl;
|
|
|
|
sout << "<br> content_type = " << incoming.content_type << endl;
|
|
|
|
sout << "<br> protocol = " << incoming.protocol << endl;
|
|
|
|
sout << "<br> foreign_ip = " << incoming.foreign_ip << endl;
|
|
|
|
sout << "<br> foreign_port = " << incoming.foreign_port << endl;
|
|
|
|
sout << "<br> local_ip = " << incoming.local_ip << endl;
|
|
|
|
sout << "<br> local_port = " << incoming.local_port << endl;
|
|
|
|
sout << "<br> 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")
|
2008-06-23 06:56:19 +08:00
|
|
|
{
|
2012-11-08 07:11:28 +08:00
|
|
|
sout << "<h2> Stuff from the query string </h2>" << endl;
|
|
|
|
sout << "<br> user = " << incoming.queries["user"] << endl;
|
|
|
|
sout << "<br> pass = " << incoming.queries["pass"] << endl;
|
|
|
|
|
|
|
|
// save these form submissions as cookies.
|
|
|
|
outgoing.cookies["user"] = incoming.queries["user"];
|
|
|
|
outgoing.cookies["pass"] = incoming.queries["pass"];
|
2008-06-23 06:56:19 +08:00
|
|
|
}
|
2012-11-08 07:11:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Echo any cookies back to the client browser
|
|
|
|
sout << "<h2>Cookies the web browser sent to the server</h2>";
|
|
|
|
for ( key_value_map::const_iterator ci = incoming.cookies.begin(); ci != incoming.cookies.end(); ++ci )
|
2008-06-23 06:56:19 +08:00
|
|
|
{
|
2012-11-08 07:11:28 +08:00
|
|
|
sout << "<br/>" << ci->first << " = " << ci->second << endl;
|
2008-06-23 06:56:19 +08:00
|
|
|
}
|
2012-11-08 07:11:28 +08:00
|
|
|
|
|
|
|
sout << "<br/><br/>";
|
|
|
|
|
|
|
|
sout << "<h2>HTTP Headers the web browser sent to the server</h2>";
|
|
|
|
// 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 << "<br/>" << ci->first << ": " << ci->second << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
sout << "</body> </html>";
|
|
|
|
|
|
|
|
return sout.str();
|
2008-06-23 06:56:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2010-07-02 08:26:59 +08:00
|
|
|
// create an instance of our web server
|
|
|
|
web_server our_web_server;
|
2008-06-23 06:56:19 +08:00
|
|
|
|
|
|
|
// make it listen on port 5000
|
|
|
|
our_web_server.set_listening_port(5000);
|
2012-09-01 09:48:34 +08:00
|
|
|
// Tell the server to begin accepting connections.
|
|
|
|
our_web_server.start_async();
|
2010-07-02 08:26:59 +08:00
|
|
|
|
|
|
|
cout << "Press enter to end this program" << endl;
|
|
|
|
cin.get();
|
2008-06-23 06:56:19 +08:00
|
|
|
}
|
|
|
|
catch (exception& e)
|
|
|
|
{
|
|
|
|
cout << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 21:45:37 +08:00
|
|
|
|