Initial setup
This commit is contained in:
parent
3a0fa57a06
commit
68e795f0e6
17 changed files with 466 additions and 8 deletions
9
request/CMakeLists.txt
Normal file
9
request/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
set(HEADERS
|
||||
request.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
request.cpp
|
||||
)
|
||||
|
||||
target_sources(pica PRIVATE ${SOURCES})
|
93
request/request.cpp
Normal file
93
request/request.cpp
Normal file
|
@ -0,0 +1,93 @@
|
|||
#include "request.h"
|
||||
|
||||
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");
|
||||
|
||||
Request::Request ():
|
||||
state(State::initial),
|
||||
raw()
|
||||
{}
|
||||
|
||||
Request::~Request() {
|
||||
terminate();
|
||||
}
|
||||
|
||||
void Request::terminate() {
|
||||
switch (state) {
|
||||
case State::accepted:
|
||||
case State::responded:
|
||||
FCGX_Finish_r(&raw);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool Request::isGet() const {
|
||||
if (state != State::accepted)
|
||||
throw std::runtime_error("An attempt to read request type on a wrong request state");
|
||||
|
||||
std::string_view method(FCGX_GetParam(REQUEST_METHOD, raw.envp));
|
||||
return method == GET;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
OStream Request::getOutputStream() {
|
||||
if (state != State::accepted)
|
||||
throw std::runtime_error("An attempt to request output stream on a wrong request state");
|
||||
|
||||
return OStream(raw.out);
|
||||
}
|
||||
|
||||
OStream Request::getErrorStream() {
|
||||
if (state != State::accepted)
|
||||
throw std::runtime_error("An attempt to request error stream on a wrong request state");
|
||||
|
||||
return OStream(raw.err);
|
||||
}
|
||||
|
||||
std::string_view 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_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);
|
||||
}
|
||||
|
||||
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) {
|
||||
char **envp = raw.envp;
|
||||
for (int i = 0; envp[i] != nullptr; ++i) {
|
||||
out << envp[i] << "</br>";
|
||||
}
|
||||
}
|
40
request/request.h
Normal file
40
request/request.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
|
||||
#include <fcgiapp.h>
|
||||
|
||||
#include "stream/ostream.h"
|
||||
|
||||
class Request {
|
||||
public:
|
||||
enum class State {
|
||||
initial,
|
||||
accepted,
|
||||
responded
|
||||
};
|
||||
|
||||
Request();
|
||||
~Request();
|
||||
Request(const Request& other) = delete;
|
||||
Request(Request&& other) = delete;
|
||||
Request& operator = (const Request& other) = delete;
|
||||
Request& operator = (Request&& other) = delete;
|
||||
|
||||
bool wait(int socketDescriptor);
|
||||
void terminate();
|
||||
bool isGet() const;
|
||||
|
||||
OStream getOutputStream();
|
||||
OStream getErrorStream();
|
||||
|
||||
std::string_view getPath(const std::string& serverName) const;
|
||||
std::string getServerName() const;
|
||||
void printEnvironment(std::ostream& out);
|
||||
|
||||
private:
|
||||
State state;
|
||||
FCGX_Request raw;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue