summaryrefslogtreecommitdiff
path: root/project2/cgi/cgiRequestID.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'project2/cgi/cgiRequestID.cpp')
-rw-r--r--project2/cgi/cgiRequestID.cpp55
1 files changed, 55 insertions, 0 deletions
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);
+
+