|
Boost-Commit : |
From: lists.drrngrvy_at_[hidden]
Date: 2008-04-29 22:31:32
Author: drrngrvy
Date: 2008-04-29 22:31:32 EDT (Tue, 29 Apr 2008)
New Revision: 44909
URL: http://svn.boost.org/trac/boost/changeset/44909
Log:
Fix examples because of an interface change.
Text files modified:
sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/echo/main.cpp | 2 +
sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp | 71 +++++++++++++++++++++++++++++----------
2 files changed, 54 insertions(+), 19 deletions(-)
Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/echo/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/echo/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/echo/main.cpp 2008-04-29 22:31:32 EDT (Tue, 29 Apr 2008)
@@ -41,6 +41,8 @@
request req; // A basic CGI request auto-parses everything (including POST data).
response resp;
+ resp<< "Request id = " << req.id() << "<p/>";
+
show_map_contents(resp, req[env_data], "Environment Variables");
show_map_contents(resp, req[get_data], "GET Variables");
show_map_contents(resp, req[post_data], "POST Variables");
Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp 2008-04-29 22:31:32 EDT (Tue, 29 Apr 2008)
@@ -27,10 +27,12 @@
// This is a file to put internal logging info into
#define LOG_FILE "/var/www/log/fcgi_echo.txt"
+//
// This function writes the title and map contents to the ostream in an
// HTML-encoded format (to make them easier on the eye).
+//
template<typename Map, typename OStream>
-void format_map(Map& m, OStream& os, const std::string& title)
+void format_map(OStream& os, Map& m, const std::string& title)
{
os<< "<h2>" << title << "</h2>";
if (m.empty()) os<< "NONE<br />";
@@ -42,45 +44,63 @@
}
/// This function accepts and handles a single request.
-template<typename Service, typename Acceptor, typename LogStream>
-int handle_request(Service& s, Acceptor& a, LogStream& of)
+template<typename Request, typename LogStream>
+int handle_request(Request& req, LogStream& of)
{
- // First we construct a `request` object.
- request req(s); // This is in a protocol-specific state at the moment.
-
- of<< "Constructed request" << endl;
boost::system::error_code ec;
- // The program blocks here until a request arrives.
- a.accept(req, ec);
-
of<< "Called accept" << endl;
// Result should be "Success".
of<< "Accept had result: " << ec.message() << endl;
+ //
// Load in the request data so we can access it easily.
+ //
req.load(ec, true); // The 'true' means read and parse STDIN (ie. POST) data.
+ //
// Construct a `response` object (makes writing/sending responses easier).
+ //
response resp;
+ //
// Responses in CGI programs require at least a 'Content-type' header. The
// library provides helpers for several common headers:
+ //
resp<< content_type("text/html")
// You can also stream text to a response object.
- << "Hello there, universe!<p />";
+ << "Hello there, universe!<p />"
+ << "Request id = " << req.id() << "<p />"
+ << "<form method=POST enctype='multipart/form-data'>"
+ "<input type=text name=name value='" << req.POST("name") << "' />"
+ "<br />"
+ "<input type=text name=hello value='" << req.POST("hello") << "' />"
+ "<br />"
+ "<input type=file name=user_file />"
+ "<input type=hidden name=cmd value=multipart_test />"
+ "<br />"
+ "<input type=submit value=submit />"
+ "</form><p />";
+ //
// Use the function defined above to show some of the request data.
- format_map(req.env(), resp, "Environment Variables");
- format_map(req.GET(), resp, "GET Variables");
- format_map(req.cookie(), resp, "Cookie Variables");
+ //
+ format_map(resp, req[env_data], "Environment Variables");
+ format_map(resp, req[get_data], "GET Variables");
+ format_map(resp, req[post_data], "POST Variables");
+ format_map(resp, req[cookie_data], "Cookie Variables");
+ //
// Response headers can be added at any time before send/flushing it:
+ //
resp<< "<content-length == " << content_length(resp.content_length())
- << content_length(resp.content_length());
+ << content_length(resp.content_length()) << ">";
+ //
+ //
// This funky macro finishes up:
return_(resp, req, 0);
+ //
// It is equivalent to the below, where the third argument is represented by
// `program_status`:
//
@@ -89,6 +109,7 @@
// return program_status;
//
// Note: in this case `program_status == 0`.
+ //
}
int main()
@@ -110,15 +131,27 @@
// Make an `acceptor` for accepting requests through.
acceptor a(s);
+ //
// After the initial setup, we can enter a loop to handle one request at a
// time until there's an error of some sort.
+ //
int ret(0);
for (;;)
{
- ret = handle_request(s, a, of);
- of<< "handle_request() returned: " << ret << endl;
- if (ret)
- break;
+ request req(s);
+ //
+ // Now we enter another loop that reuses the request's connection (and
+ // memory - makes things more efficient). You should always do this for
+ // now; this requirement will be removed in future.
+ //
+ for (;;)
+ {
+ a.accept(req);
+ ret = handle_request(req, of);
+ of<< "handle_request() returned: " << ret << endl;
+ if (ret)
+ break;
+ }
}
return ret;
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk