| 20 | class web_server : public server_http |
| 21 | { |
| 22 | const std::string on_request ( |
| 23 | const incoming_things& incoming, |
| 24 | outgoing_things& outgoing |
| 25 | ) |
| 26 | { |
| 27 | ostringstream sout; |
| 28 | // We are going to send back a page that contains an HTML form with two text input fields. |
| 29 | // One field called name. The HTML form uses the post method but could also use the get |
| 30 | // method (just change method='post' to method='get'). |
| 31 | sout << " <html> <body> " |
| 32 | << "<form action='/form_handler' method='post'> " |
| 33 | << "User Name: <input name='user' type='text'><br> " |
| 34 | << "User password: <input name='pass' type='text'> <input type='submit'> " |
| 35 | << " </form>"; |
| 36 | |
| 37 | // Write out some of the inputs to this request so that they show up on the |
| 38 | // resulting web page. |
| 39 | sout << "<br> path = " << incoming.path << endl; |
| 40 | sout << "<br> request_type = " << incoming.request_type << endl; |
| 41 | sout << "<br> content_type = " << incoming.content_type << endl; |
| 42 | sout << "<br> protocol = " << incoming.protocol << endl; |
| 43 | sout << "<br> foreign_ip = " << incoming.foreign_ip << endl; |
| 44 | sout << "<br> foreign_port = " << incoming.foreign_port << endl; |
| 45 | sout << "<br> local_ip = " << incoming.local_ip << endl; |
| 46 | sout << "<br> local_port = " << incoming.local_port << endl; |
| 47 | sout << "<br> body = \"" << incoming.body << "\"" << endl; |
| 48 | |
| 49 | |
| 50 | // If this request is the result of the user submitting the form then echo back |
| 51 | // the submission. |
| 52 | if (incoming.path == "/form_handler") |
| 53 | { |
| 54 | sout << "<h2> Stuff from the query string </h2>" << endl; |
| 55 | sout << "<br> user = " << incoming.queries["user"] << endl; |
| 56 | sout << "<br> pass = " << incoming.queries["pass"] << endl; |
| 57 | |
| 58 | // save these form submissions as cookies. |
| 59 | outgoing.cookies["user"] = incoming.queries["user"]; |
| 60 | outgoing.cookies["pass"] = incoming.queries["pass"]; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // Echo any cookies back to the client browser |
| 65 | sout << "<h2>Cookies the web browser sent to the server</h2>"; |
| 66 | for ( key_value_map::const_iterator ci = incoming.cookies.begin(); ci != incoming.cookies.end(); ++ci ) |
| 67 | { |
| 68 | sout << "<br/>" << ci->first << " = " << ci->second << endl; |
| 69 | } |
| 70 | |
| 71 | sout << "<br/><br/>"; |
| 72 | |
| 73 | sout << "<h2>HTTP Headers the web browser sent to the server</h2>"; |
| 74 | // Echo out all the HTTP headers we received from the client web browser |
| 75 | for ( key_value_map_ci::const_iterator ci = incoming.headers.begin(); ci != incoming.headers.end(); ++ci ) |
| 76 | { |
| 77 | sout << "<br/>" << ci->first << ": " << ci->second << endl; |
| 78 | } |
| 79 | |