From 6a502df135e2c0500f2469bdd7b032afbf81c613 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 6 Apr 2017 20:12:44 +0100 Subject: [PATCH] New free function to get POST values. --- src/CMakeLists.txt | 1 + src/cgi_post.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/cgi_post.hpp | 14 ++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/cgi_post.cpp create mode 100644 src/cgi_post.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b58b646..01f0307 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable(${PROJECT_NAME} envy.cpp cgi_env.cpp num_to_token.cpp + cgi_post.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM diff --git a/src/cgi_post.cpp b/src/cgi_post.cpp new file mode 100644 index 0000000..56f93b4 --- /dev/null +++ b/src/cgi_post.cpp @@ -0,0 +1,45 @@ +#include "cgi_post.hpp" +#include "cgi_env.hpp" +#include "split_get_vars.hpp" +#include +#include +#include +#include +#include +#include + +namespace tawashi { + namespace cgi { + namespace { + } //unnamed namespace + + const PostMapType& read_post (const CGIEnv& parEnv) { + static bool already_read = false; + static PostMapType map; + static std::string original_data; + + if (not already_read) { + assert(original_data.empty()); + assert(map.empty()); + + const auto input_len = parEnv.content_length(); + if (input_len > 0) { + original_data.reserve(input_len); + std::copy_n( + std::istream_iterator(std::cin), + input_len, + std::back_inserter(original_data) + ); + + for (auto& itm : split_env_vars(original_data)) { + map[itm.first] = itm.second; + } + } + + already_read = true; + } + + return map; + } + } //namespace cgi +} //namespace tawashi diff --git a/src/cgi_post.hpp b/src/cgi_post.hpp new file mode 100644 index 0000000..b7ebeec --- /dev/null +++ b/src/cgi_post.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace tawashi { + class CGIEnv; + + namespace cgi { + typedef boost::container::flat_map PostMapType; + + const PostMapType& read_post (const CGIEnv& parEnv); + } //namespace cgi +} //namespace tawashi