1
0
Fork 0
forked from blue/pica

first couple of requests

This commit is contained in:
Blue 2023-11-23 16:57:32 -03:00
parent 68e795f0e6
commit aae7873d67
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
11 changed files with 152 additions and 34 deletions

View file

@ -63,16 +63,23 @@ OStream Request::getErrorStream() {
return OStream(raw.err);
}
std::string_view Request::getPath(const std::string& serverName) const {
std::string Request::getPath(const std::string& serverName) const {
if (state != State::accepted)
throw std::runtime_error("An attempt to request path on a wrong request state");
std::string_view path;
std::string path;
std::string_view scriptFileName(FCGX_GetParam(SCRIPT_FILENAME, raw.envp));
std::string::size_type snLocation = scriptFileName.find(serverName);
if (snLocation != std::string::npos) {
if (snLocation + serverName.size() < scriptFileName.size())
path = scriptFileName.substr(snLocation + serverName.size() + 1);
}
if (!path.empty()) {
while (path.back() == '/')
path.erase(path.end() - 1);
}
return path;
@ -88,6 +95,19 @@ std::string Request::getServerName() const {
void Request::printEnvironment(std::ostream& out) {
char **envp = raw.envp;
for (int i = 0; envp[i] != nullptr; ++i) {
out << envp[i] << "</br>";
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()));
}
}

View file

@ -6,6 +6,8 @@
#include <fcgiapp.h>
#include <nlohmann/json.hpp>
#include "stream/ostream.h"
class Request {
@ -30,9 +32,10 @@ public:
OStream getOutputStream();
OStream getErrorStream();
std::string_view getPath(const std::string& serverName) const;
std::string getPath(const std::string& serverName) const;
std::string getServerName() const;
void printEnvironment(std::ostream& out);
void printEnvironment(nlohmann::json& out);
private:
State state;