some thinking around passing the form

This commit is contained in:
Blue 2023-12-14 19:17:28 -03:00
parent 3fe6d25448
commit 0c50cfa639
Signed by: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 149 additions and 0 deletions

View file

@ -10,6 +10,10 @@ constexpr static const char* GET("GET");
constexpr static const char* REQUEST_METHOD("REQUEST_METHOD");
constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME");
constexpr static const char* SERVER_NAME("SERVER_NAME");
constexpr static const char* CONTENT_TYPE("CONTENT_TYPE");
constexpr static const char* CONTENT_LENGTH("CONTENT_LENGTH");
constexpr static const char* urlEncoded("application/x-www-form-urlencoded");
// constexpr static const char* REQUEST_URI("REQUEST_URI");
//
@ -178,3 +182,41 @@ void Request::printEnvironment(nlohmann::json& out) {
out[std::string(value.substr(0, pos))] = std::string(value.substr(pos + 1, value.size()));
}
}
bool Request::isFormUrlEncoded() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read request content type on not accepted request");
std::string_view contentType(FCGX_GetParam(CONTENT_TYPE, raw.envp));
if (!contentType.empty() && contentType.find(urlEncoded) != std::string_view::npos) {
return true;
}
return false;
}
unsigned int Request::contentLength() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read request content length on not accepted request");
return atoi(FCGX_GetParam(CONTENT_LENGTH, raw.envp));
}
std::map<std::string, std::string> Request::getForm() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read form on not accepted request");
std::map<std::string, std::string> result;
std::string_view contentType(FCGX_GetParam(CONTENT_TYPE, raw.envp));
if (contentType.empty())
return result;
unsigned int length = contentLength();
if (contentType.find(urlEncoded) != std::string_view::npos) {
std::string postData(length, '\0');
FCGX_GetStr(&postData[0], length, raw.in);
result = urlDecodeAndParse(postData);
}
return result;
}

View file

@ -6,6 +6,7 @@
#include <memory>
#include <stdexcept>
#include <string_view>
#include <string>
#include <array>
#include <map>
@ -14,6 +15,7 @@
#include <nlohmann/json.hpp>
#include "stream/ostream.h"
#include "utils/formdecode.h"
class Response;
class Request {
@ -43,6 +45,9 @@ public:
Method method() const;
State currentState() const;
bool isFormUrlEncoded() const;
unsigned int contentLength() const;
std::map<std::string, std::string> getForm() const;
OStream getOutputStream(const Response* response);
OStream getErrorStream(const Response* response);