pica/request/request.h

81 lines
1.9 KiB
C
Raw Normal View History

2023-12-30 22:42:11 +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
#pragma once
#include <memory>
#include <stdexcept>
#include <string_view>
2023-12-14 22:17:28 +00:00
#include <string>
2023-12-13 20:33:11 +00:00
#include <array>
#include <map>
2023-11-21 22:19:08 +00:00
#include <fcgiapp.h>
2023-11-23 19:57:32 +00:00
#include <nlohmann/json.hpp>
2023-11-21 22:19:08 +00:00
#include "stream/ostream.h"
2023-12-14 22:17:28 +00:00
#include "utils/formdecode.h"
2023-12-28 20:26:08 +00:00
#include "utils/helpers.h"
2023-12-22 23:25:20 +00:00
#include "response/response.h"
2023-11-21 22:19:08 +00:00
class Request {
2023-12-22 23:25:20 +00:00
friend class Response;
2023-11-21 22:19:08 +00:00
public:
enum class State {
initial,
accepted,
2023-12-13 20:33:11 +00:00
responding,
2023-12-28 20:26:08 +00:00
responded,
terminated
2023-11-21 22:19:08 +00:00
};
2023-12-13 20:33:11 +00:00
enum class Method {
get,
post,
unknown
};
2023-11-21 22:19:08 +00:00
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);
2023-12-28 20:26:08 +00:00
bool active() const;
2023-11-21 22:19:08 +00:00
2023-12-22 23:25:20 +00:00
Response& createResponse();
Response& createResponse(Response::Status status);
uint16_t responseCode() const;
2023-12-13 20:33:11 +00:00
Method method() const;
2023-12-22 23:25:20 +00:00
std::string_view methodName() const;
2023-12-13 20:33:11 +00:00
State currentState() const;
2023-12-14 22:17:28 +00:00
bool isFormUrlEncoded() const;
unsigned int contentLength() const;
std::map<std::string, std::string> getForm() const;
2023-12-13 20:33:11 +00:00
2023-12-28 20:26:08 +00:00
void readPath(const std::string& serverName);
std::string getPath() const;
2023-11-21 22:19:08 +00:00
std::string getServerName() const;
2023-12-28 20:26:08 +00:00
std::string getAuthorizationToken() const;
2024-01-09 17:02:56 +00:00
void printEnvironment(std::ostream& out) const;
void printEnvironment(nlohmann::json& out) const;
2023-11-21 22:19:08 +00:00
2023-12-13 20:33:11 +00:00
private:
2023-12-22 23:25:20 +00:00
OStream getOutputStream();
OStream getErrorStream();
void responseIsComplete();
2023-12-28 20:26:08 +00:00
void terminate();
2024-01-13 23:57:42 +00:00
std::map<std::string, std::string> getFormPOST() const;
2023-12-13 20:33:11 +00:00
2023-11-21 22:19:08 +00:00
private:
State state;
FCGX_Request raw;
2023-12-22 23:25:20 +00:00
std::unique_ptr<Response> response;
2023-12-28 20:26:08 +00:00
std::string path;
2024-01-13 23:57:42 +00:00
mutable Method cachedMethod;
2023-11-21 22:19:08 +00:00
};