2023-12-10 23:23:15 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
#include "request.h"
|
|
|
|
|
2023-12-13 20:33:11 +00:00
|
|
|
#include "response/response.h"
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
constexpr static const char* REQUEST_METHOD("REQUEST_METHOD");
|
|
|
|
constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME");
|
|
|
|
constexpr static const char* SERVER_NAME("SERVER_NAME");
|
2023-12-14 22:17:28 +00:00
|
|
|
constexpr static const char* CONTENT_TYPE("CONTENT_TYPE");
|
|
|
|
constexpr static const char* CONTENT_LENGTH("CONTENT_LENGTH");
|
2023-12-28 20:26:08 +00:00
|
|
|
constexpr static const char* AUTHORIZATION("HTTP_AUTHORIZATION");
|
2023-12-14 22:17:28 +00:00
|
|
|
|
|
|
|
constexpr static const char* urlEncoded("application/x-www-form-urlencoded");
|
2023-11-21 22:19:08 +00:00
|
|
|
|
2023-12-07 20:32:43 +00:00
|
|
|
// constexpr static const char* REQUEST_URI("REQUEST_URI");
|
|
|
|
//
|
|
|
|
// constexpr static const char* DOCUMENT_URI("DOCUMENT_URI");
|
|
|
|
// constexpr static const char* DOCUMENT_ROOT("DOCUMENT_ROOT");
|
|
|
|
//
|
|
|
|
// constexpr static const char* SCRIPT_NAME("SCRIPT_NAME");
|
|
|
|
|
2023-12-13 20:33:11 +00:00
|
|
|
constexpr std::array<
|
|
|
|
std::pair<std::string_view, Request::Method>,
|
|
|
|
static_cast<uint8_t>(Request::Method::unknown)
|
|
|
|
> methods = {{
|
|
|
|
{"GET", Request::Method::get},
|
|
|
|
{"POST", Request::Method::post}
|
|
|
|
}};
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
Request::Request ():
|
|
|
|
state(State::initial),
|
2023-12-13 20:33:11 +00:00
|
|
|
raw(),
|
2023-12-28 20:26:08 +00:00
|
|
|
response(nullptr),
|
|
|
|
path()
|
2023-11-21 22:19:08 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
Request::~Request() {
|
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::terminate() {
|
2023-12-28 20:26:08 +00:00
|
|
|
switch (state) {
|
|
|
|
case State::terminated:
|
|
|
|
case State::initial:
|
|
|
|
break;
|
2023-11-21 22:19:08 +00:00
|
|
|
case State::accepted:
|
2023-12-28 20:26:08 +00:00
|
|
|
std::cout << "A termination of accepted request that was not responded to, it's probably an error" << std::endl;
|
|
|
|
FCGX_Finish_r(&raw);
|
|
|
|
break;
|
|
|
|
case State::responding:
|
|
|
|
std::cout << "A termination of responding request that was not actually sent, it's probably an error" << std::endl;
|
2023-11-21 22:19:08 +00:00
|
|
|
FCGX_Finish_r(&raw);
|
|
|
|
break;
|
2023-12-28 20:26:08 +00:00
|
|
|
case State::responded:
|
|
|
|
FCGX_Finish_r(&raw);
|
2023-11-21 22:19:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-12-28 20:26:08 +00:00
|
|
|
state = State::terminated;
|
|
|
|
}
|
2023-12-22 23:25:20 +00:00
|
|
|
std::string_view Request::methodName() const {
|
2023-12-28 20:26:08 +00:00
|
|
|
if (state == State::initial || state == State::terminated)
|
2023-12-13 20:33:11 +00:00
|
|
|
throw std::runtime_error("An attempt to read request method on not accepted request");
|
2023-11-21 22:19:08 +00:00
|
|
|
|
2023-12-22 23:25:20 +00:00
|
|
|
return FCGX_GetParam(REQUEST_METHOD, raw.envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
Request::Method Request::method() const {
|
2023-12-28 20:26:08 +00:00
|
|
|
std::string_view method = methodName();
|
2023-12-13 20:33:11 +00:00
|
|
|
for (const auto& pair : methods) {
|
|
|
|
if (pair.first == method)
|
|
|
|
return pair.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Request::Method::unknown;
|
2023-11-21 22:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Request::wait(int socketDescriptor) {
|
|
|
|
if (state != State::initial)
|
|
|
|
throw std::runtime_error("An attempt to wait for new incomming request on a wrong request state");
|
|
|
|
|
|
|
|
FCGX_Request* request = &raw;
|
|
|
|
FCGX_InitRequest(request, socketDescriptor, 0);
|
|
|
|
int rc = FCGX_Accept_r(request);
|
|
|
|
|
|
|
|
bool result = rc == 0;
|
|
|
|
if (result)
|
|
|
|
state = State::accepted;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:25:20 +00:00
|
|
|
OStream Request::getOutputStream() {
|
2023-11-21 22:19:08 +00:00
|
|
|
return OStream(raw.out);
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:25:20 +00:00
|
|
|
OStream Request::getErrorStream() {
|
2023-11-21 22:19:08 +00:00
|
|
|
return OStream(raw.err);
|
|
|
|
}
|
|
|
|
|
2023-12-28 20:26:08 +00:00
|
|
|
bool Request::active() const {
|
|
|
|
return state != State::initial && state != State::terminated;
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:25:20 +00:00
|
|
|
Response& Request::createResponse() {
|
|
|
|
if (state != State::accepted)
|
|
|
|
throw std::runtime_error("An attempt create response to the request in the wrong state");
|
|
|
|
|
|
|
|
response = std::unique_ptr<Response>(new Response(*this));
|
|
|
|
state = State::responding;
|
|
|
|
|
|
|
|
return *response.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Response& Request::createResponse(Response::Status status) {
|
|
|
|
if (state != State::accepted)
|
|
|
|
throw std::runtime_error("An attempt create response to the request in the wrong state");
|
|
|
|
|
|
|
|
response = std::unique_ptr<Response>(new Response(*this, status));
|
|
|
|
state = State::responding;
|
|
|
|
|
|
|
|
return *response.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Request::responseCode() const {
|
|
|
|
if (state != State::responded)
|
|
|
|
throw std::runtime_error("An attempt create read response code on the wrong state");
|
|
|
|
|
|
|
|
return response->statusCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::responseIsComplete() {
|
2023-12-13 20:33:11 +00:00
|
|
|
switch (state) {
|
|
|
|
case State::initial:
|
|
|
|
throw std::runtime_error("An attempt to mark the request as complete, but it wasn't even accepted yet");
|
|
|
|
break;
|
|
|
|
case State::accepted:
|
|
|
|
throw std::runtime_error("An attempt to mark the request as complete, but it wasn't responded");
|
|
|
|
break;
|
|
|
|
case State::responding:
|
2023-12-28 20:26:08 +00:00
|
|
|
std::cout << responseCode() << '\t' << methodName() << '\t' << path << std::endl;
|
2023-12-13 20:33:11 +00:00
|
|
|
state = State::responded;
|
|
|
|
break;
|
|
|
|
case State::responded:
|
|
|
|
throw std::runtime_error("An attempt to mark the request as a complete for the second time");
|
|
|
|
break;
|
2023-12-28 20:26:08 +00:00
|
|
|
case State::terminated:
|
|
|
|
throw std::runtime_error("An attempt to mark the request as a complete on a terminated request");
|
|
|
|
break;
|
2023-12-13 20:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Request::State Request::currentState() const {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-12-28 20:26:08 +00:00
|
|
|
void Request::readPath(const std::string& serverName) {
|
2023-11-21 22:19:08 +00:00
|
|
|
if (state != State::accepted)
|
|
|
|
throw std::runtime_error("An attempt to request path on a wrong request state");
|
|
|
|
|
2023-12-28 20:26:08 +00:00
|
|
|
if (!path.empty())
|
|
|
|
std::cout << "Request already has path \"" + path + "\", but it's being read again, probably an error";
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
std::string_view scriptFileName(FCGX_GetParam(SCRIPT_FILENAME, raw.envp));
|
|
|
|
std::string::size_type snLocation = scriptFileName.find(serverName);
|
2023-11-23 19:57:32 +00:00
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
if (snLocation != std::string::npos) {
|
|
|
|
if (snLocation + serverName.size() < scriptFileName.size())
|
|
|
|
path = scriptFileName.substr(snLocation + serverName.size() + 1);
|
2023-11-23 19:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!path.empty()) {
|
|
|
|
while (path.back() == '/')
|
|
|
|
path.erase(path.end() - 1);
|
2023-11-21 22:19:08 +00:00
|
|
|
}
|
2023-12-28 20:26:08 +00:00
|
|
|
}
|
2023-11-21 22:19:08 +00:00
|
|
|
|
2023-12-28 20:26:08 +00:00
|
|
|
std::string Request::getPath() const {
|
2023-11-21 22:19:08 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Request::getServerName() const {
|
|
|
|
if (state != State::accepted)
|
|
|
|
throw std::runtime_error("An attempt to request server name on a wrong request state");
|
|
|
|
|
|
|
|
return FCGX_GetParam(SERVER_NAME, raw.envp);;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::printEnvironment(std::ostream& out) {
|
2023-12-28 20:26:08 +00:00
|
|
|
if (!active())
|
|
|
|
throw std::runtime_error("An attempt to print environment of a request in a wrong state");
|
|
|
|
|
2023-11-21 22:19:08 +00:00
|
|
|
char **envp = raw.envp;
|
|
|
|
for (int i = 0; envp[i] != nullptr; ++i) {
|
2023-11-23 19:57:32 +00:00
|
|
|
out << envp[i] << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::printEnvironment(nlohmann::json& out) {
|
|
|
|
if (!out.is_object())
|
|
|
|
return;
|
|
|
|
|
|
|
|
char **envp = raw.envp;
|
|
|
|
for (int i = 0; envp[i] != nullptr; ++i) {
|
|
|
|
std::string_view value(envp[i]);
|
|
|
|
std::string::size_type pos = value.find('=');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
out[std::string(value.substr(0, pos))] = std::string(value.substr(pos + 1, value.size()));
|
2023-11-21 22:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 22:17:28 +00:00
|
|
|
|
|
|
|
bool Request::isFormUrlEncoded() const {
|
2023-12-28 20:26:08 +00:00
|
|
|
if (!active())
|
|
|
|
throw std::runtime_error("An attempt to read content type of a request in a wrong state");
|
2023-12-14 22:17:28 +00:00
|
|
|
|
|
|
|
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 {
|
2023-12-28 20:26:08 +00:00
|
|
|
if (!active())
|
|
|
|
throw std::runtime_error("An attempt to read content length of a request in a wrong state");
|
2023-12-14 22:17:28 +00:00
|
|
|
|
|
|
|
return atoi(FCGX_GetParam(CONTENT_LENGTH, raw.envp));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::string> Request::getForm() const {
|
2023-12-28 20:26:08 +00:00
|
|
|
if (!active())
|
|
|
|
throw std::runtime_error("An attempt to read form of a request in a wrong state");
|
2023-12-14 22:17:28 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-12-28 20:26:08 +00:00
|
|
|
|
|
|
|
std::string Request::getAuthorizationToken() const {
|
|
|
|
if (!active())
|
|
|
|
throw std::runtime_error("An attempt to read authorization token of a request in a wrong state");
|
|
|
|
|
|
|
|
const char* auth = FCGX_GetParam(AUTHORIZATION, raw.envp);
|
|
|
|
if (auth == nullptr)
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
std::string result(auth);
|
|
|
|
if (result.find("Bearer") != 0)
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
result.erase(0, 6);
|
|
|
|
trim(result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|