1
0
forked from blue/pica
pica/request/request.h

81 lines
1.9 KiB
C++

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include <stdexcept>
#include <string_view>
#include <string>
#include <array>
#include <map>
#include <fcgiapp.h>
#include <nlohmann/json.hpp>
#include "stream/ostream.h"
#include "utils/formdecode.h"
#include "utils/helpers.h"
#include "response/response.h"
class Request {
friend class Response;
public:
enum class State {
initial,
accepted,
responding,
responded,
terminated
};
enum class Method {
get,
post,
unknown
};
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);
bool active() const;
Response& createResponse();
Response& createResponse(Response::Status status);
uint16_t responseCode() const;
Method method() const;
std::string_view methodName() const;
State currentState() const;
bool isFormUrlEncoded() const;
unsigned int contentLength() const;
std::map<std::string, std::string> getForm() const;
void readPath(const std::string& serverName);
std::string getPath() const;
std::string getServerName() const;
std::string getAuthorizationToken() const;
void printEnvironment(std::ostream& out) const;
void printEnvironment(nlohmann::json& out) const;
private:
OStream getOutputStream();
OStream getErrorStream();
void responseIsComplete();
void terminate();
std::map<std::string, std::string> getFormPOST() const;
private:
State state;
FCGX_Request raw;
std::unique_ptr<Response> response;
std::string path;
mutable Method cachedMethod;
};