forked from tgrasset/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRes.hpp
181 lines (162 loc) · 6.03 KB
/
HttpRes.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HttpRes.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbocquel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/09 19:19:07 by mbocquel #+# #+# */
/* Updated: 2023/09/11 17:57:18 by mbocquel ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HTTP_RES_HPP
# define HTTP_RES_HPP
# include "webserv.hpp"
# include "HttpReq.hpp"
# include "Server.hpp"
# include "CGI.hpp"
# include "Client.hpp"
class Client;
class HttpReq;
class Server;
class CGI;
typedef enum e_type_c {
NORMALFILE,
PYTHON,
PHP,
REDIRECTION,
FORBIDDEN,
NOT_FOUND,
NOT_ACCEPTABLE,
AUTOINDEX
} r_type;
class HttpRes
{
public:
HttpRes(Client *client, HttpReq *request);
HttpRes(HttpRes const & copy);
~HttpRes(void);
HttpRes &operator=(HttpRes const & http_res);
std::string getHttpVersion(void) const;
int getStatusCode(void) const;
std::string getMethod(void) const;
std::string getStatusMessage(void) const;
std::map<std::string, std::string> getHeader(void) const;
std::string getBody(void) const;
Server *getServer(void) const;
std::string getFormattedHeader(void) const;
bool getKeepAlive(void) const;
std::string getUriPath(void) const;
std::string getUriPathInfo(void) const;
std::string getScriptName(void) const;
std::string getUriQuery(void) const;
r_type getResourceType(void) const;
Location *getLocation(void) const;
size_t getContentLength(void) const;
std::string getCgiFilePath(void) const;
std::ifstream getFileToSend(void) const;
int getFileToSendFd(void) const;
size_t getFileToSendSize(void) const;
std::vector<char> getFileToSendBuff(void) const;
std::vector<char> getCgiBuff(void) const;
std::vector<char> getCgiBuff_header(void) const;
s_file getStatusFileToSend(void) const;
s_pipe getStatusCgiPipe(void) const;
void openBodyFile(void);
void closeBodyFile(void);
void clearFileToSendBuff(void);
void clearCgiBuff(void);
void clearCgiBuff_header(void);
int addBodyFileToBuff(void);
int getUploadTmpInFd(void) const;
int getUploadOutFd(void) const;
int getCgiPipeFd(void) const;
pid_t getCgiPid(void) const;
void setStatusCode(int statusCode);
void setCgiPipeFd(int cgiPipeFd);
void setCgiPid(int cgiPid);
void handleRequest(void);
int checkHttpVersion(std::string version);
int checkUri(std::string uri);
r_type checkResourceType(void);
int checkRequestHeader(std::map<std::string, std::string> header);
void bodyBuild(std::string requestUri);
void headerBuild(void);
void formatHeader(void);
void fillMimeTypes(void);
void checkIfAcceptable(std::vector<std::string> acceptable);
bool methodIsAllowed(std::string method);
void uploadFileToServer(void);
void removeFdFromPoll(int fd);
void addFdToPollIn(int fd);
void addFdToPollOut(int fd);
int addCgiToBuff(void);
void closeCgiPipe(void);
void cgiPipeFinishedWriting(void);
void transferUploadFileInSide(void);
void transferUploadFileOutSide(void);
void finishBuildingResAfterUpload(void);
bool isAllowedCGI(void);
std::string getErrorPageContent(std::string path, int code, std::string message);
class HttpResException : public std::exception {
public :
HttpResException(std::string errMessage) throw() {
_errMessage = "HttpRes Error: " + errMessage;
}
virtual const char* what() const throw() {
return (_errMessage.c_str());
}
~HttpResException() throw() {}
private:
std::string _errMessage;
};
private:
HttpRes(void);
Client *_client;
HttpReq *_request;
std::string _httpVersion;
int _statusCode;
std::string _method;
std::string _statusMessage;
std::map<std::string, std::string> _header;
std::string _body;
Server *_server;
Location *_location;
std::string _formattedHeader;
bool _keepAlive;
std::string _uriPath;
std::string _uriPathInfo;
int _fileToSendFd;
size_t _fileToSendSize;
std::vector<char> _fileToSendBuff;
std::vector<char> _cgiBuff;
std::vector<char> _cgiBuff_header;
s_file _statusFileToSend;
std::string _scriptName;
r_type _resourceType;
std::string _uriQuery;
size_t _contentLength;
static std::map<std::string, std::string> _mimeTypes;
std::string _cgiFilePath;
s_pipe _statusCgiPipe;
int _cgiPipeFd;
pid_t _cgiPid;
int _uploadTmpInFd;
int _uploadOutFd;
std::ifstream _uploadTmpInStream;
std::ofstream _uploadOutStream;
std::string _uploadBuff;
bool _uploadBuffClean;
std::string _nameTmpUploadFile;
bool _uploadFileHeader;
bool _uploadFileBody;
bool _uploadFileBodyFirstLine;
bool _backslashRFound;
std::string _nameFinalUploadFile;
int _percentSentFile;
size_t _byteWrittenUploadFile;
size_t _percentWrittenUploadFile;
bool _first_line_of_header;
bool _is_cgi;
};
#endif