forked from tgrasset/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.hpp
134 lines (122 loc) · 3.95 KB
/
Client.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tgrasset <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/09 19:09:21 by mbocquel #+# #+# */
/* Updated: 2023/09/12 10:08:44 by tgrasset ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CLIENT_HPP
# define CLIENT_HPP
# include "HttpRes.hpp"
# include "HttpReq.hpp"
# include "Server.hpp"
# include "webserv.hpp"
# include "Launcher.hpp"
class HttpRes;
class HttpReq;
class Server;
class Launcher;
typedef enum e_status_c {
WANT_TO_RECIVE_REQ,
RECIVING_REQ_HEADER,
RECIVING_REQ_BODY,
REQ_RECIVED,
WAITING_FOR_RES,
RES_READY_TO_BE_SENT,
SENDING_RES_HEADER,
SENDING_RES_BODY,
UPLOADING_FILE,
RES_SENT,
ERROR_WHILE_SENDING
} t_status_c;
typedef enum e_type_fd {
COM_SOCKET = 0,
CGI_PIPE = 1,
RES_FILE_FD = 2,
REQ_FILE_FD = 3,
UPLOAD_TMP_IN = 4,
UPLOAD_OUT = 5,
NOT_MINE = 6
} t_fd;
class Client
{
private:
int _com_socket;
t_status_c _status;
HttpReq *_req;
HttpRes *_res;
std::string _incoming_msg;
std::vector< Server *> _server_ptr;
struct sockaddr_in _client_addr;
std::vector<char> _req_recived;
std::string _req_header;
unsigned int _id;
unsigned int _byte_sent_header;
unsigned int _byte_sent_body;
unsigned int _byteRecivedReqBody;
struct timeval _last_activity;
unsigned int _file_to_send_size;
Launcher *_launcher;
int _percentSentFile;
static unsigned int _count;
public:
Client(void);
Client(Launcher *launcher, struct sockaddr_in _client_addr, int com_socket);
Client(Client const & copy);
~Client(void);
Client &operator=(Client const & client);
void AddServerPtr(Server * new_server_ptr);
int getComSocket(void) const;
void setStatus(t_status_c status);
t_status_c getStatus(void) const;
struct sockaddr_in getClient_addr(void) const;
unsigned int getId(void) const;
void printClientServer(void) const;
int getByteSentHeader(void) const;
void setByteSentHeader(int byte);
void sendResponse(void);
void sendResponseHeader(void);
void sendResponseBody(void);
void sendResponseBodyError(void);
void sendResponseBodyNormalFile(void);
void sendResponseBodyCgi(void);
int receiveRequest(void);
int receiveRequestHeader(void);
int receiveRequestBody(void);
void resetLastActivity(void);
unsigned long timeSinceLastActivityUs(void) const;
void resetClient(void);
bool getKeepAlive(void) const;
t_fd getSocketType(int fd) const;
void removeFdFromPoll(int fd);
void addFdToPollIn(int fd);
void addFdToPollOut(int fd);
int addBodyFileToBuff(void);
int addCgiToBuff(void);
int writeReqBodyFile(void);
void cgiPipeFinishedWriting(void);
Launcher *getLauncher(void) const;
void transferUploadFileInSide(void);
HttpReq *getHttpReq(void) const;
HttpRes *getHttpRes(void) const;
void transferUploadFileOutSide(void);
void printClientFd(void) const;
int getTimeOut(void) const;
class ClientException : public std::exception {
public :
ClientException(std::string errMessage) throw() {
_errMessage = "Client Error: " + errMessage;
}
virtual const char* what() const throw() {
return (_errMessage.c_str());
}
~ClientException() throw() {}
private:
std::string _errMessage;
};
};
#endif