forked from bbotfatimezzahra/Webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodPost.cpp
More file actions
198 lines (186 loc) · 4.73 KB
/
Copy pathMethodPost.cpp
File metadata and controls
198 lines (186 loc) · 4.73 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
#include"includes/Connection.hpp"
#include"includes/Request.hpp"
std::string ExtGet(std::string type){
std::string ext;
if(type == "image/png" || type == "image/gif" || type == "image/webp"
|| type == "image/jpeg" || type == "video/mp4")
ext = "." + type.substr(6);
else if( type == "text/plain" || type == "application/x-www-form-urlencoded")
ext = ".txt";
else if (type == "application/octet-stream")
ext = ".bin";
else if (type == "text/html" || type == "text/css" || type == "text/pdf" || type == "text/json")
ext = "." + type.substr(5);
else if (type == "text/markdown")
ext = ".md";
else if (type == "application/pdf")
ext = ".pdf";
else if (type == "text/x-csrc" )
ext = ".c";
return ext;
}
std::string generate_name(){
std::string name;
std::string str;
int pos;
srand(time(NULL));
int s ;
str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
s = (rand() % 3) + 4;
for(int j = 0; j < s; j++){
pos = rand() % str.size();
name.push_back(str[pos]);
}
return name;
}
void sign_handler(int i){
throw MyError ("exit");
(void)i;
}
void Connection::run_cgi_post(std::string url_path, std::string path, Location &lc,char **env){
std::string pth = lc.getCgi()["." + get_extension(url_path)];
time_t start = time(NULL);
int status , fd, body;
pid_t pid;
pid_t w = 0;
char *scri[3];
cgi = generate_name();
signal(SIGINT,SIG_IGN);
body = open(path.c_str(), O_RDONLY);
if(body == -1)
return get_err(500);
fd = open(cgi.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
if(fd ==-1 ){
close(body);
std::remove(cgi.c_str());
return get_err(500);
}
pid = fork();
if(pid == -1){
close(fd);
close(body);
std::remove(cgi.c_str());
return get_err(500);
}
if(pid == 0){
signal(SIGINT,sign_handler);
scri[0] = strdup(pth.c_str());
scri[1] = strdup(url_path.c_str());
scri[2] = NULL;
dup2(body, STDIN_FILENO);
close(body);
dup2(fd,STDOUT_FILENO);
close(fd);
execve(pth.c_str(), scri, env);
free(scri[0]);
free(scri[1]);
free_env(env);
throw MyError ("exit");
}
while(w == 0 && time(NULL) - start < 7){
w = waitpid(pid, &status, WNOHANG);
if(w == pid && WIFEXITED(status) && !WEXITSTATUS(status) ){
close(body);
close(fd);
if (!get_respo(cgi, true, path)){
std::remove(cgi.c_str());
return get_err(500);
}
return;
}
if(WIFEXITED(status) && WEXITSTATUS(status) == 1){
close(fd);
std::remove(cgi.c_str());
return get_err(502);
}
}
if(w == 0){
kill(pid,SIGKILL) ;
waitpid(pid,&status,0);
close(body);
close(fd);
std::remove(cgi.c_str());
return get_err(504);
}
close(fd);
close(body);
std::remove(cgi.c_str());
return get_err(500);
}
std::string check_dir_cgi(std::string url_path,Location &lc){
std::string tmp;
size_t i = 0;
if(url_path[url_path.size() - 1] != '/')
url_path += '/';
while(i < lc.getIndex().size()){
tmp.clear();
tmp = url_path + lc.getIndex()[i];
if(!access(tmp.c_str(), F_OK) && check_cgi(tmp, lc)){
return tmp;
}
i++;
}
tmp.clear();
return tmp;
}
void Connection::MethodPost(Location &lc){
struct stat buf ;
std::string tmp;
std::string path = lc.getRoot() + lc.getUpload();
std::vector<std::string> vect = lc.getMethods();
std::string url_path = lc.getRoot() + this->route;
std::string name = generate_name() + ExtGet(_request.getHeaders()["Content-Type"]);
std::vector<std::string>::iterator it = std::find(vect.begin(), vect.end(), "POST");
if(it == vect.end()){
std::remove(_request.getBody().c_str());
get_err(405);
return ;
}
if(ExtGet(_request.getHeaders()["Content-Type"]).empty()){
std::remove(_request.getBody().c_str());
get_err(415);
return;
}
int err = stat(path.c_str(),&buf);
if ( !err && S_ISDIR(buf.st_mode)){
path += "/" + name;
if(std::rename(_request.getBody().c_str(), path.c_str())){
get_err(500);
std::remove(_request.getBody().c_str());
return;
}
err = stat(url_path.c_str(),&buf);
if(err){
get_err(404);
std::remove(path.c_str());
return;
}
else if(S_ISREG(buf.st_mode) && check_cgi(url_path, lc)){
char **env = initialize_env(url_path,lc);
run_cgi_post(url_path,path,lc,env);
std::remove(path.c_str());
free_env(env);
}
else if (S_ISDIR(buf.st_mode) ){
tmp = check_dir_cgi(url_path,lc);
if (!tmp.empty()){
char **env = initialize_env(tmp,lc);
run_cgi_post(tmp,path,lc,env);
std::remove(path.c_str());
free_env(env);
}
else{
this->upload_name = lc.getUpload()+ "/" + name;
get_err(201);
}
}
else{
this->upload_name = lc.getUpload() +"/" + name;
get_err(201);
}
}
else {
get_err(500);
std::remove(_request.getBody().c_str());
}
}