server_http_ex.cpp 4.58 KB
Newer Older
1
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*

    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"
16
#include "dlib/ref.h" // for ref()
17
18
19
20
21
22

using namespace dlib;
using namespace std;

class web_server : public server::http_1a_c
{
23
24
25
    const std::string on_request ( 
        const incoming_things& incoming,
        outgoing_things& outgoing
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    )
    {
        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 << " <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>"; 

40
41
42
43
44
45
46
47
48
            // 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>  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;
Davis King's avatar
Davis King committed
49
            sout << "<br>  body = \""         << incoming.body << "\"" << endl;
50
51
52
53


            // If this request is the result of the user submitting the form then echo back
            // the submission.
54
            if (incoming.path == "/form_handler")
55
56
            {
                sout << "<h2> Stuff from the query string </h2>" << endl;
57
58
                sout << "<br>  user = " << incoming.queries["user"] << endl;
                sout << "<br>  pass = " << incoming.queries["pass"] << endl;
59
60

                // save these form submissions as cookies.  
61
62
                outgoing.cookies["user"] = incoming.queries["user"];
                outgoing.cookies["pass"] = incoming.queries["pass"];
63
64
65
66
            }


            // Echo any cookies back to the client browser 
67
68
            sout << "<h2>Cookies we sent to the server</h2>";
            for ( key_value_map::const_iterator ci = incoming.cookies.begin(); ci != incoming.cookies.end(); ++ci )
69
            {
70
                sout << "<br/>" << ci->first << " = " << ci->second << endl;
71
72
73
74
75
76
            }

            sout << "<br/><br/>";

            sout << "<h2>HTTP Headers we sent to the server</h2>";
            // Echo out all the HTTP headers we received from the client web browser
77
            for ( key_value_map::const_iterator ci = incoming.headers.begin(); ci != incoming.headers.end(); ++ci )
78
            {
79
                sout << "<br/>" << ci->first << ": " << ci->second << endl;
80
81
82
83
84
            }


            sout << "</body> </html>";

85
            return sout.str();
86
87
88
        }
        catch (exception& e)
        {
89
            return e.what();
90
91
92
93
94
        }
    }

};

95
void thread(web_server& the_server)
96
{
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    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;
    }
111
112
113
114
115
116
}

int main()
{
    try
    {
117
118
        // create an instance of our web server
        web_server our_web_server;
119
120
121

        // make it listen on port 5000
        our_web_server.set_listening_port(5000);
122
123
124

        // create a thread that will start the server.   The ref() here allows us to pass 
        // our_web_server into the threaded function by reference.
125
        thread_function t(thread, dlib::ref(our_web_server));
126
127
128
129
130

        cout << "Press enter to end this program" << endl;
        cin.get();
        // this will cause the server to shut down 
        our_web_server.clear();
131
132
133
134
135
136
137
    }
    catch (exception& e)
    {
        cout << e.what() << endl;
    }
}

138