-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTTPConnection.cpp
More file actions
224 lines (216 loc) · 6.9 KB
/
HTTPConnection.cpp
File metadata and controls
224 lines (216 loc) · 6.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "HTTPConnection.hpp"
#include <sstream>
extern NginxConfig::GlobalConfig _config;
HTTPConnection::HTTPConnection(int fd, int block, int server_port, std::string client_ip) {
seq = REQUEST;
socket_fd = fd;
keep_alive = false;
http_data = new HTTPData(block, server_port, client_ip);
request_message = new RequestMessage(http_data);
response_message = new ResponseMessage(http_data);
}
HTTPConnection::~HTTPConnection() {
if (this->http_data->isCGI == true)
delete cgi_process;
delete request_message;
delete response_message;
delete http_data;
close(socket_fd);
}
void HTTPConnection::killConnection(void* hc) {
delete reinterpret_cast<HTTPConnection*>(hc);
}
int HTTPConnection::getServerBlock(void) { return (this->http_data->getSBlock()); }
int HTTPConnection::getFileFd(void) { return (this->file_fd); }
int HTTPConnection::getSocketFd(void) { return (this->socket_fd); }
int HTTPConnection::getCgiOutputFd(void) { return (this->cgi_output_fd); }
int HTTPConnection::getCgiInputFd(void) { return (this->cgi_input_fd); }
int HTTPConnection::run() {
if (seq == REQUEST) {
readLength = read(socket_fd, buffer, BUF_SIZ-1);
buffer[readLength] = '\0';
if (readLength > 0)
request_message->setMessage(buffer);
int request_result = request_message->parsingRequestMessage();
if (request_result == RequestMessage::FINISH_PARSE) {
std::map<std::string, std::string>::iterator res;
if ((res = this->http_data->header_field.find("Connection")) != this->http_data->header_field.end())
if (res->second == "keep-alive")
keep_alive = true;
if (this->http_data->isCGI == true) {
cgi_process = new CGIProcess(http_data);
cgi_process->run();
cgi_input_fd = cgi_process->getInputPair();
cgi_output_fd = cgi_process->getOutputPair();
if (fcntl(cgi_input_fd, F_SETFL, O_NONBLOCK) == -1 ||
fcntl(cgi_output_fd, F_SETFL, O_NONBLOCK) == -1)
throw ErrorHandler(__FILE__, __func__, __LINE__,
"something wrong with fd. check the file exists : ?", ErrorHandler::NON_CRIT);
}
if (http_data->header_field.find("Content-Length") != http_data->header_field.end() &&
(http_data->header_field["Content-Length"] != "0" && http_data->header_field["Content-Length"] != ""))
seq = READY_TO_MESSAGE_BODY;
else
seq = REQUEST_TO_RESPONSE;
}
else if (request_result == RequestMessage::MESSAGE_BODY) {
if (this->http_data->isCGI == true) {
cgi_process = new CGIProcess(http_data);
cgi_process->run();
cgi_input_fd = cgi_process->getInputPair();
cgi_output_fd = cgi_process->getOutputPair();
if (fcntl(cgi_input_fd, F_SETFL, O_NONBLOCK) == -1 ||
fcntl(cgi_output_fd, F_SETFL, O_NONBLOCK) == -1)
throw ErrorHandler(__FILE__, __func__, __LINE__,
"something wrong with fd. check the file exists : ?", ErrorHandler::NON_CRIT);
seq = READY_TO_MESSAGE_BODY;
}
else
seq = REQUEST_TO_RESPONSE;
}
}
else if (seq == READY_TO_MESSAGE_BODY) {
write(cgi_input_fd, request_message->getMessage().data(), request_message->getMessage().size());
std::stringstream ss;
ss << request_message->getMessage().size();
if (ss.str() == http_data->header_field["Content-Length"])
seq = BODY_TO_RESPONSE;
else
seq = MESSAGE_BODY_READ;
}
else if (seq == BODY_TO_RESPONSE) {
seq = REQUEST_TO_RESPONSE;
}
else if (seq == MESSAGE_BODY_READ) {
readLength = read(socket_fd, buffer, BUF_SIZ-1);
if (readLength > 0) {
buffer[readLength] = '\0';
seq = MESSAGE_BODY_WRITE;
}
else {
seq = REQUEST_TO_RESPONSE;
}
}
else if (seq == MESSAGE_BODY_WRITE) {
write(cgi_input_fd, buffer, readLength);
seq = MESSAGE_BODY_READ;
}
else if (seq == REQUEST_TO_RESPONSE) {
response_message->setResponseMessage(http_data->_tmp_directory);
seq = RESPONSE;
}
else if (seq == RESPONSE) {
int
write_size = ((int)response_message->getMessage().size() < BUF_SIZ ? (int)response_message->getMessage().size() : BUF_SIZ);
writeLength = write(socket_fd, response_message->getMessage().data(), write_size);
if (writeLength != BUF_SIZ) {
if (this->http_data->isCGI == true) {
seq = READY_TO_CGI;
}
else {
std::string _pth =
_config._http._server[this->http_data->server_block]._dir_map["root"] +
this->http_data->uri_dir +
this->http_data->uri_file;
_pth = FileController::toAbsPath(_pth);
file_fd = open(_pth.c_str(), O_RDONLY);
if (fcntl(file_fd, F_SETFL, O_NONBLOCK) == -1)
throw ErrorHandler(__FILE__, __func__, __LINE__,
"something wrong with fd. check the file exists : \n" + _pth, ErrorHandler::NON_CRIT);
seq = READY_TO_FILE;
}
}
else
response_message->resetMessage(writeLength);
}
else if (seq == READY_TO_CGI) {
close(cgi_input_fd);
seq = CGI_READ;
}
else if (seq == CGI_READ) {
readLength = read(cgi_output_fd, buffer, BUF_SIZ);
seq = CGI_WRITE;
}
else if (seq == CGI_WRITE) {
if (readLength == 0)
seq = CLOSE;
else if (readLength == -1)
throw ErrorHandler(__FILE__, __func__, __LINE__,
"exit -1 replaced", ErrorHandler::CRIT);
else {
writeLength = write(socket_fd, buffer, readLength);
if (readLength != writeLength) {
throw ErrorHandler(__FILE__, __func__, __LINE__,
"exit -1 replaced", ErrorHandler::CRIT);
}
if (writeLength != BUF_SIZ) {
close(cgi_output_fd);
seq = CLOSE;
}
else
seq = CGI_READ;
}
if (seq == CLOSE && keep_alive == true) {
seq = RE_KEEPALIVE;
}
}
else if (seq == READY_TO_FILE) {
seq = FILE_READ;
}
else if (seq == FILE_READ) {
readLength = read(file_fd, buffer, BUF_SIZ);
seq = FILE_WRITE;
}
else if (seq == FILE_WRITE) {
if (readLength == 0)
seq = CLOSE;
else if (readLength == -1)
throw ErrorHandler(__FILE__, __func__, __LINE__,
"exit -1 replaced", ErrorHandler::CRIT);
else {
writeLength = write(socket_fd, buffer, readLength);
if (readLength != writeLength) {
throw ErrorHandler(__FILE__, __func__, __LINE__,
"exit -1 replaced", ErrorHandler::CRIT);
}
if (writeLength != BUF_SIZ) {
close(file_fd);
seq = CLOSE;
}
else
seq = FILE_READ;
}
if (seq == CLOSE && keep_alive == true) {
seq = RE_KEEPALIVE;
}
}
else if (seq == RE_KEEPALIVE) {
int backup_block;
int backup_port;
std::string backup_ip;
backup_block = this->http_data->server_block;
backup_port = this->http_data->server_port;
backup_ip = this->http_data->client_ip;
if (this->http_data->isCGI == true)
delete cgi_process;
delete request_message;
delete response_message;
delete http_data;
buffer[0] = '\0';
readLength = -2;
writeLength = -2;
if (file_fd > 0)
file_fd = -2;
if (cgi_output_fd > 0)
cgi_output_fd = -2;
if (cgi_input_fd > 0)
cgi_input_fd = -2;
//keep_alive = false;
//다음 연결에서 keep-alive가 안되는 경우도 있다?
http_data = new HTTPData(backup_block, backup_port, backup_ip);
request_message = new RequestMessage(http_data);
response_message = new ResponseMessage(http_data);
seq = REQUEST;
}
return seq;
};