-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.hpp
159 lines (130 loc) · 3.82 KB
/
Response.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include <string>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <sys/socket.h>
#include <unordered_map>
#include <fstream>
#include <filesystem>
#include "Request.hpp"
#include "HelperFuncs.hpp"
#include "PollableFileDescriptor.hpp"
#include <fcntl.h>
#include <map>
#include <memory>
class CGI;
enum class StatusCode
{
Null = 0,
OK = 200,
Created = 201,
Accepted = 202,
NoContent = 204,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
RequestTimeout = 408,
LengthRequired = 411,
PayloadTooLarge = 413,
URITooLong = 414,
UnsupportedMediaType = 415,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504
};
class Server;
typedef int fd_t;
class Response : public ReadFileDescriptor, public WriteFileDescriptor
{
public:
Response(std::shared_ptr<Request> client_request);
Response(int status_code);
~Response();
void addHeader();
void addHeader(int status_code);
void createResponse(Server *server);
const std::string &getResponse() const;
void createRedirect();
void sendToClient( void );
void readTimedOut( void );
void writeTimedOut( void );
private:
bool DoesFileExists();
fd_t OpenFile(const std::string &path, int o_flag, int o_perm = 0644) noexcept(false);
std::string ExtensionExtractor(const std::string &path);
void ExecuteCGI();
void GetFile();
void DeleteFile();
void UploadFile();
void respondWithDirectoryListing();
std::string customizeErrorPage(int status_code);
void writingDone( void );
void readingDone( void );
Server *m_server;
std::unique_ptr<CGI> m_cgi_instance;
HTTPMethod m_method;
std::string m_body;
std::string m_path;
StatusCode m_status;
bool m_CGI;
std::shared_ptr<Request> m_client_request;
std::unordered_map<std::string, std::string> m_headers;
std::string m_total_response;
// These are the supported Exit codes.
// The response will look at the m_status to determine its response exit message.
static const inline std::unordered_map<int, std::string> m_DB_status = {
{000, "Null"},
{200, "OK"},
{201, "Created"},
{202, "Accepted"},
{204, "No Content"},
{301, "Moved Permanently"},
{302, "Found"},
{303, "See Other"},
{304, "Not Modified"},
{400, "Bad Request"},
{401, "Unauthorized"},
{403, "Forbidden"},
{404, "Not Found"},
{405, "Method Not Allowed"},
{408, "Request Timeout"},
{411, "Length Required"},
{413, "Payload Too Large"},
{414, "URI Too Long"},
{415, "Unsupported Media Type"},
{500, "Internal Server Error"},
{501, "Not Implemented"},
{502, "Bad Gateway"},
{503, "Service Unavailable"},
{504, "Gateway Timeout"}
};
// These are the supported content-Types if you want to add any more supported content types you can add them here
// Format: [file extension][Content-Type]
static const inline std::unordered_map<std::string, std::string> m_DB_ContentType = {
{"html", "text/html"},
{"txt", "text/plain"},
{"xml", "application/xml"},
{"x-www-form-urlencoded", "application/x-www-form-urlencoded"},
{"jpeg", "image/jpeg"},
{"jpg", "image/jpg"},
{"png", "image/png"},
{"gif", "image/gif"},
{"ico", "image/x-icon"},
{"mpeg", "audio/mpeg"},
{"ogg", "audio/ogg"},
{"mp4", "video/mp4"},
{"webm", "video/webm"},
{"form-data", "multipart/form-data"},
};
};