summaryrefslogtreecommitdiff
path: root/project2/cgi
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-03-28 20:55:37 +0000
committerrandomdan <randomdan@localhost>2013-03-28 20:55:37 +0000
commit1d6533583e724bc02a4e5fd89596b0bea0941dde (patch)
treea91df241d062ab3b093e769699614cac56e89fdb /project2/cgi
parentMove pwd out of common into files (diff)
downloadproject2-1d6533583e724bc02a4e5fd89596b0bea0941dde.tar.bz2
project2-1d6533583e724bc02a4e5fd89596b0bea0941dde.tar.xz
project2-1d6533583e724bc02a4e5fd89596b0bea0941dde.zip
Strip all uri parameter stuff from common, make it cgi only
Add component to get a unique cgi request ID Change file presenter cache to use a configurable component to determine its ID
Diffstat (limited to 'project2/cgi')
-rw-r--r--project2/cgi/Jamfile.jam2
-rw-r--r--project2/cgi/cgiAppEngine.cpp2
-rw-r--r--project2/cgi/cgiEnvironment.cpp8
-rw-r--r--project2/cgi/cgiEnvironment.h1
-rw-r--r--project2/cgi/cgiRequestID.cpp55
-rw-r--r--project2/cgi/cgiSimpleRouter.cpp2
-rw-r--r--project2/cgi/cgiUriParam.cpp31
7 files changed, 100 insertions, 1 deletions
diff --git a/project2/cgi/Jamfile.jam b/project2/cgi/Jamfile.jam
index 017dd11..63844b0 100644
--- a/project2/cgi/Jamfile.jam
+++ b/project2/cgi/Jamfile.jam
@@ -6,6 +6,7 @@ lib boost_filesystem : : <name>boost_filesystem ;
lib cgicc : : <name>cgicc ;
lib fcgi : : <name>fcgi ;
lib fcgi++ : : <name>fcgi++ ;
+lib gcrypt : : <name>gcrypt ;
cpp-pch pch : pch.hpp :
<include>../../libmisc
@@ -25,6 +26,7 @@ lib p2web :
<library>../common//p2common
<library>boost_filesystem
<library>../xml//p2xml
+ <library>gcrypt
: :
<library>..//p2parts
<library>cgicc
diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp
index f2c7f86..985789b 100644
--- a/project2/cgi/cgiAppEngine.cpp
+++ b/project2/cgi/cgiAppEngine.cpp
@@ -182,7 +182,7 @@ CgiApplicationEngine::addEnvData(const MultiRowSetPresenter * p, OutputOptionsPt
p->finishRowSet();
}
- if (!outputCachingActive && (!o || o->Parameters())) {
+ if (!o || o->Parameters()) {
// Parameters
p->addNewRowSet("params", env()->scriptNamespacePrefix);
BOOST_FOREACH(cgicc::FormEntry fe, _env->cgi->getElements()) {
diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp
index c106f67..6462f5c 100644
--- a/project2/cgi/cgiEnvironment.cpp
+++ b/project2/cgi/cgiEnvironment.cpp
@@ -140,6 +140,14 @@ CgiEnvironment::getParamUriCount() const
return router->parameterCount();
}
+void
+CgiEnvironment::applyAllParameters(const boost::function<void (const std::string &, const std::string &)> & func) const
+{
+ BOOST_FOREACH(const auto & f, cgi->getElements()) {
+ func(f.getName(), f.getValue());
+ }
+}
+
Glib::ustring
CgiEnvironment::getParamQuery(const std::string & p) const
{
diff --git a/project2/cgi/cgiEnvironment.h b/project2/cgi/cgiEnvironment.h
index 330cd97..176ca8d 100644
--- a/project2/cgi/cgiEnvironment.h
+++ b/project2/cgi/cgiEnvironment.h
@@ -38,6 +38,7 @@ class CgiEnvironment : public Environment {
Glib::ustring getParamUri(VariableType idx) const;
unsigned int getParamUriCount() const;
Glib::ustring getParamQuery(const std::string & idx) const;
+ void applyAllParameters(const boost::function<void (const std::string &, const std::string &)> &) const;
std::string getServerName() const;
std::string getScriptName() const;
std::string getRedirectURL() const;
diff --git a/project2/cgi/cgiRequestID.cpp b/project2/cgi/cgiRequestID.cpp
new file mode 100644
index 0000000..6f4d01e
--- /dev/null
+++ b/project2/cgi/cgiRequestID.cpp
@@ -0,0 +1,55 @@
+#include <pch.hpp>
+#include <variables.h>
+#include <scriptLoader.h>
+#include <scriptStorage.h>
+#include <appEngine.h>
+#include <gcrypt.h>
+#include <scopeObject.h>
+#include <iomanip>
+
+/// Variable implementation that returns a unique ID for a page request
+class CgiRequestID : public VariableImplDyn {
+ public:
+ CgiRequestID(ScriptNodePtr e) :
+ VariableImplDyn(e)
+ {
+ }
+
+ VariableType value() const
+ {
+ gcry_md_hd_t state;
+ gcry_md_open(&state, GCRY_MD_SHA1, 0);
+ ScopeObject gcryClose([&state] { gcry_md_close(state); });
+
+ auto _env = static_cast<const CgiEnvironment *>(ApplicationEngine::getCurrent()->env());
+ gcryApplyString(state, _env->getRedirectURL());
+
+ _env->applyAllParameters([&state,this](const std::string & name, const std::string & value) {
+ gcryApplyString(state, name);
+ gcryApplyString(state, value);
+ });
+
+ return hash(state);
+ }
+
+ private:
+ static void gcryApplyString(gcry_md_hd_t & state, const std::string & str)
+ {
+ gcry_md_write(state, str.data(), str.length());
+ }
+
+ static std::string hash(gcry_md_hd_t & state)
+ {
+ int hash_len = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
+ unsigned char * hash = gcry_md_read(state, GCRY_MD_SHA1);
+ std::stringstream hashstr;
+ hashstr << std::hex << std::setfill('0');
+ for (int i = 0; i < hash_len; i++) {
+ hashstr << std::setw(2) << static_cast<unsigned>(hash[i]);
+ }
+ return hashstr.str();
+ }
+};
+DECLARE_COMPONENT_LOADER("requestid", CgiRequestID, VariableLoader);
+
+
diff --git a/project2/cgi/cgiSimpleRouter.cpp b/project2/cgi/cgiSimpleRouter.cpp
index a28cf1d..2359c4e 100644
--- a/project2/cgi/cgiSimpleRouter.cpp
+++ b/project2/cgi/cgiSimpleRouter.cpp
@@ -2,6 +2,8 @@
#include "scriptLoader.h"
#include "presenter.h"
+SimpleNumericException(UriElementOutOfRange);
+
std::vector<std::string>
makeVector(const boost::filesystem::path & y)
{
diff --git a/project2/cgi/cgiUriParam.cpp b/project2/cgi/cgiUriParam.cpp
new file mode 100644
index 0000000..04a2c36
--- /dev/null
+++ b/project2/cgi/cgiUriParam.cpp
@@ -0,0 +1,31 @@
+#include <pch.hpp>
+#include <variables.h>
+#include <scriptLoader.h>
+#include <scriptStorage.h>
+#include <appEngine.h>
+
+/// Variable implementation to access URI path fragments
+class VariableUri : public VariableImplDyn {
+ public:
+ VariableUri(ScriptNodePtr e) :
+ VariableImplDyn(e),
+ index(e, "index")
+ {
+ }
+ VariableType value() const
+ {
+ try {
+ return static_cast<const CgiEnvironment *>(ApplicationEngine::getCurrent()->env())->getParamUri(index());
+ }
+ catch (...) {
+ if (!defaultValue) {
+ throw;
+ }
+ return (*defaultValue)();
+ }
+ }
+ private:
+ Variable index;
+};
+DECLARE_COMPONENT_LOADER("uri", VariableUri, VariableLoader);
+