1
0
forked from blue/pica

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 untrusted user: blue
GPG Key ID: 9B203B252A63EE38
9 changed files with 149 additions and 0 deletions

View File

@ -2,12 +2,14 @@ set(HEADERS
handler.h
info.h
env.h
register.h
)
set(SOURCES
handler.cpp
info.cpp
env.cpp
register.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

23
handler/register.cpp Normal file
View File

@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "register.h"
Handler::Register::Register():
Handler("register", Request::Method::post)
{}
void Handler::Register::handle(Request& request) {
std::map form = request.getForm();
std::cout << "Received form:" << std::endl;
for (const auto& pair : form)
std::cout << '\t' << pair.first << ": " << pair.second << std::endl;
Response res(request);
nlohmann::json body = nlohmann::json::object();
body["result"] = "ok";
res.setBody(body);
res.send();
}

16
handler/register.h Normal file
View File

@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "handler.h"
namespace Handler {
class Register : public Handler::Handler {
public:
Register();
virtual void handle(Request& request);
};
}

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);

View File

@ -5,6 +5,7 @@
#include "handler/info.h"
#include "handler/env.h"
#include "handler/register.h"
constexpr uint8_t currentDbVesion = 1;
@ -28,6 +29,7 @@ Server::Server():
router.addRoute(std::make_unique<Handler::Info>());
router.addRoute(std::make_unique<Handler::Env>());
router.addRoute(std::make_unique<Handler::Register>());
}
Server::~Server() {}

View File

@ -1,9 +1,11 @@
set(HEADER
helpers.h
formdecode.h
)
set(SOURCES
helpers.cpp
formdecode.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

46
utils/formdecode.cpp Normal file
View File

@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "formdecode.h"
#include <sstream>
std::map<std::string, std::string> urlDecodeAndParse(const std::string_view& encoded) {
std::map<std::string, std::string> result;
std::istringstream iss(std::string(encoded.begin(), encoded.end()));
std::string pair;
while (std::getline(iss, pair, '&')) {
size_t equalsPos = pair.find('=');
if (equalsPos != std::string::npos)
result.emplace(
urlDecode(pair.substr(0, equalsPos)),
urlDecode(pair.substr(equalsPos + 1))
);
}
return result;
}
std::string urlDecode(const std::string_view& encoded) {
std::ostringstream decoded;
for (size_t i = 0; i < encoded.length(); ++i) {
if (encoded[i] == '%') {
if (i + 2 < encoded.length()) {
std::string hexStr(encoded.substr(i + 1, 2));
char hexValue = static_cast<char>(std::stoul(hexStr, nullptr, 16));
decoded.put(hexValue);
i += 2;
} else {
decoded.put(encoded[i]);
}
} else if (encoded[i] == '+') {
decoded.put(' ');
} else {
decoded.put(encoded[i]);
}
}
return decoded.str();
}

11
utils/formdecode.h Normal file
View File

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <map>
#include <string>
#include <string_view>
std::map<std::string, std::string> urlDecodeAndParse(const std::string_view& encoded);
std::string urlDecode(const std::string_view& encoded);