|
Boost-Commit : |
From: lists.drrngrvy_at_[hidden]
Date: 2007-11-15 20:20:21
Author: drrngrvy
Date: 2007-11-15 20:20:20 EST (Thu, 15 Nov 2007)
New Revision: 41131
URL: http://svn.boost.org/trac/boost/changeset/41131
Log:
Adding a couple of acgi examples.
Added:
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/cookie_game/
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/Jamfile.v2 (contents, props changed)
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/main.cpp (contents, props changed)
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/CheckCookie.cpp (contents, props changed)
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/Login.cpp (contents, props changed)
sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/Logout.cpp (contents, props changed)
Added: sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/Jamfile.v2 2007-11-15 20:20:20 EST (Thu, 15 Nov 2007)
@@ -0,0 +1,18 @@
+
+#project
+# :
+# ;
+
+exe acgi_hello_world
+ : main.cpp
+ : <include>../../../../../
+ <include>$(BOOST_ROOT)
+ <define>_CRT_SECURE_NO_WARNINGS
+ <define>_SCL_SECURE_NO_WARNINGS
+ ;
+
+install acgi_blah
+ : acgi_hello_world
+ : <location>.
+ <library>../../../util/system//boost_system
+ ;
Added: sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/main.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/hello_world/main.cpp 2007-11-15 20:20:20 EST (Thu, 15 Nov 2007)
@@ -0,0 +1,30 @@
+// -- main.hpp --
+//
+// Copyright (c) Darren Garvey 2007.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+////////////////////////////////////////////////////////////////
+//
+// The simplest CGI program, outputs only "Hello there, universe."
+//
+
+#include <boost/cgi/acgi.hpp>
+
+using namespace std;
+using namespace cgi::acgi;
+
+int main()
+{
+ service srv;
+ request req(srv);
+ response resp;
+
+ resp<< content_type("text/html")
+ << "Hello there, universe.";
+
+ resp.send(req.client());
+
+ return 0;
+}
\ No newline at end of file
Added: sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/CheckCookie.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/CheckCookie.cpp 2007-11-15 20:20:20 EST (Thu, 15 Nov 2007)
@@ -0,0 +1,41 @@
+
+#include <boost/cgi/acgi.hpp>
+
+using namespace boost::acgi;
+
+int main()
+{
+ service s;
+ request req(s);
+
+ req.load();
+
+ response resp;
+
+ if (!req.cookie("uuid").empty())
+ { // The cookie has been set correctly!
+ resp<< location(req.form("fwd"));
+ }else
+ {
+ resp
+ << content_type("text/html")
+ << "<html>"
+ "<head>"
+ "<title>Error</title>"
+ "</head>"
+ "<body>"
+ "<center>"
+ "<p>You have cookies disabled. They are required for logging in.</p>"
+ "<a href='http://www.google.com/search?q=enabling cookies'>Google it</a>"
+ " if you're stuck, or return to "
+ "<a href='" << req.env("referrer") << "'>where you came from</a>"
+ "</center>"
+ "</body>"
+ "</html>";
+ }
+
+ resp.send(req.client());
+
+ return req.end(http::ok);
+}
+
Added: sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/Login.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/Login.cpp 2007-11-15 20:20:20 EST (Thu, 15 Nov 2007)
@@ -0,0 +1,133 @@
+
+#include <boost/cgi/acgi.hpp>
+
+using namespace cgi::acgi;
+
+int show_default_page(request& req, response& resp)
+{
+ resp
+ << content_type("text/html")
+ << "<html>"
+ "<head>"
+ "<title>Login</title>"
+ "</head>"
+ "<body>"
+ "<center>"
+ "<form action='POST'>"
+ "<input type='text' name='name' />"
+ "<input type='button' name='cmd' value='login' />"
+ "</form>"
+ "</center>"
+ "</body>"
+ "</html>";
+
+ resp.send(req.client());
+ return req.end(http::ok);
+}
+
+void show_already_logged_in_page(request& req, response resp)
+{
+ resp
+ << content_type("text/html")
+ << "<html>"
+ "<head><title>Redirecting...</title>"
+ "<meta http-equiv='refresh' content='5;url="<< req.POST("fwd") <<"' />"
+ "</head>"
+ "<body>"
+ "<center>"
+ "You are already logged in. You should be redirected "
+ "<a href='"<< req.POST("fwd") <<"'>here</a>"
+ " in five seconds."
+ "</center>"
+ "</body>"
+ "</html>";
+
+ resp.send(req.client());
+ return req.end(http::ok);
+}
+
+int show_name_error_page(request& req, response& resp)
+{
+ resp
+ << content_type("text/html")
+ << "<html>"
+ "<head>"
+ "<title>Login</title>"
+ "</head>"
+ "<body>"
+ "<center>"
+ "<form action='POST'>"
+ "<span class='red'>Your user name must only use letters, numbers or "
+ "the underscore character."
+ "</span>"
+ "<input type='text' name='name' value='"<< req.POST("name") <<"' />"
+ "<input type='button' name='cmd' value='login' />"
+ "</form>"
+ "</center>"
+ "</body>"
+ "</html>";
+
+ resp.send(req.client());
+ return req.end(http::bad_request);
+}
+
+int verify_name(std::string& name)
+{
+ using namespace boost;
+
+ regex re("\\s*([_a-zA-Z0-9]{5,})\\s*");
+ cmatch what;
+
+ if (!regex_match(name, what, re))
+ {
+ name.erase();
+ return 0;
+ }
+
+ name = what[1];
+ return 0;
+}
+
+string make_uuid()
+{
+ return boost::lexical_cast<string>(time());
+ // better, for when Boost.Uuid is finished:
+ //return boost::uuid::create().to_string();
+}
+
+int main()
+{
+ service s;
+ request req(s);
+
+ req.load();
+
+ response resp;
+
+ // If there's already a session id set, warn them and then redirect
+ // them to where they would be going anyway.
+ if (!req.cookie("uuid").empty()) {
+ return show_already_logged_in_page(req, resp);
+ }
+
+ // If they haven't asked explicitly to log in, show the default page.
+ string cmd(req.POST("cmd"));
+ if (cmd.empty() || cmd != "login") {
+ return show_default_page(req, resp);
+ }
+
+ // If they're name is invalid, inform them.
+ string name(req.POST("name"));
+ if (!verify_name(name)) {
+ return show_name_error_page(req, resp);
+ }
+
+ // Else we can assume all is ok.
+ // Here we give them a 'universally unique id' and forward them to a
+ // cookie checking page.
+ resp<< cookie("uuid", make_uuid())
+ << location("CheckCookie?fwd=" + req.POST("fwd"));
+ resp.send(req.client());
+
+ return req.end(http::ok);
+}
Added: sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/Logout.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/cgi_work/libs/cgi/example/acgi/login/Logout.cpp 2007-11-15 20:20:20 EST (Thu, 15 Nov 2007)
@@ -0,0 +1,21 @@
+
+#include <boost/cgi/acgi.hpp>
+
+using namespace cgi::acgi;
+
+int main()
+{
+ service s;
+ request req(s);
+
+ req.load();
+
+ response resp;
+
+ resp<< cookie("uuid")
+ << location(req.form("fwd"));
+
+ resp.send(req.client());
+ return req.end(http::ok);
+}
+
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