-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server_helper.cpp
76 lines (66 loc) · 2.52 KB
/
Server_helper.cpp
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server_helper.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ychen2 <ychen2@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/24 21:32:14 by ychen2 #+# #+# */
/* Updated: 2024/09/07 22:19:35 by ychen2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server_helper.hpp"
void close_fds(std::vector<int> &fds) {
for (std::vector<int>::iterator it = fds.begin(); it != fds.end(); it++) {
close(*it);
}
}
void poll_to_out(int fd, Server & server) {
std::vector<struct pollfd>::iterator next_pfd =
server.find_it_in_nxt(fd);
// if the fd is not in the poll array
if (next_pfd == server.getNextPfdsEnd()) {
server.add_to_poll_out(fd);
return;
}
// else
next_pfd->events = POLLOUT;
}
void poll_to_in(int fd, Server & server) {
std::vector<struct pollfd>::iterator next_pfd =
server.find_it_in_nxt(fd);
// if the fd is not in the poll array
if (next_pfd == server.getNextPfdsEnd()) {
server.add_to_poll_in(fd);
return;
}
// else
next_pfd->events = POLLIN;
}
void wait_to_save_file(State &state, Server & server) {
state.stage = &save_file;
server.remove_from_poll(state.conn_fd);
poll_to_out(state.file_fd, server);
}
void wait_to_read_file(State &state, Server & server) {
state.stage = &read_file;
server.remove_from_poll(state.conn_fd);
poll_to_in(state.file_fd, server);
}
void wait_to_read_cgi(State &state, Server & server) {
state.stage = &read_cgi;
server.remove_from_poll(state.conn_fd);
poll_to_in(state.cgi_pipe_r[0], server);
}
void wait_to_write_cgi(State &state, Server & server) {
state.bytes_sent = 0;
state.stage = &write_cgi;
server.remove_from_poll(state.conn_fd);
poll_to_out(state.cgi_pipe_w[1], server);
}
void wait_to_send_resonpse(State &state, Server & server) {
state.stage = &send_response;
poll_to_out(state.conn_fd, server);
state.bytes_sent = 0;
state.response_buff = state.res.generateResponseString();
}