Browse Source

save 22-11-3

master
nicolas-arnaud 2 years ago
parent
commit
0331ff68af
  1. 7
      srcs/load/Route.cpp
  2. 29
      srcs/load/Socket.cpp
  3. 30
      srcs/tools.cpp

7
srcs/load/Route.cpp

@ -18,7 +18,8 @@ Route::Route(Server *server, string location, JSONNode *datas)
} }
if ((tmp = object["add_header"])) { if ((tmp = object["add_header"])) {
JSONList headers = tmp->lst(); JSONList headers = tmp->lst();
for (JSONList::iterator it = headers.begin(); it < headers.end(); it++) { for (JSONList::iterator it = headers.begin(); it < headers.end();
it++) {
_headers.push_back((*it)->str()); _headers.push_back((*it)->str());
} }
} }
@ -56,8 +57,8 @@ string Route::getIndex(string uri, string path) {
content << "<li><a href=\"" << uri + "/" + entry->d_name << "\">" content << "<li><a href=\"" << uri + "/" + entry->d_name << "\">"
<< entry->d_name << "</a></li>\n"; << entry->d_name << "</a></li>\n";
if (stat(path.c_str(), &info) != 0) if (stat(path.c_str(), &info) != 0)
std::cerr << "stat() error on " << path << ": " << strerror(errno) std::cerr << "stat() error on " << path << ": "
<< "\n"; << strerror(errno) << "\n";
} }
content << "<ul>"; content << "<ul>";
closedir(dir); closedir(dir);

29
srcs/load/Socket.cpp

@ -49,8 +49,8 @@ int Socket::launch(void) {
void Socket::set_fds(void) { void Socket::set_fds(void) {
int child_fd; int child_fd;
FD_SET(_fd, &_readfds); FD_SET(_fd, &_readfds);
for (std::vector<Socket *>::iterator it = _childs.begin(); it < _childs.end(); for (std::vector<Socket *>::iterator it = _childs.begin();
it++) { it < _childs.end(); it++) {
child_fd = (*it)->_fd; child_fd = (*it)->_fd;
FD_SET(child_fd, &_readfds); FD_SET(child_fd, &_readfds);
if (child_fd > _max_fd) if (child_fd > _max_fd)
@ -79,8 +79,8 @@ void Socket::refresh(Env *env) {
_childs.push_back(new Socket(new_socket, this)); _childs.push_back(new Socket(new_socket, this));
} }
int child_fd; int child_fd;
for (std::vector<Socket *>::iterator it = _childs.begin(); it < _childs.end(); for (std::vector<Socket *>::iterator it = _childs.begin();
it++) { it < _childs.end(); it++) {
child_fd = (*it)->_fd; child_fd = (*it)->_fd;
if (FD_ISSET(child_fd, &_readfds)) { if (FD_ISSET(child_fd, &_readfds)) {
valread = read(child_fd, buffer, 10000); valread = read(child_fd, buffer, 10000);
@ -134,15 +134,21 @@ int Socket::answer(Env *env, string request) {
Route *route = server->get_route(uri); Route *route = server->get_route(uri);
std::vector<string> headers; std::vector<string> headers;
if ((head.at(0) != "GET" && head.at(0) != "POST" && head.at(0) != "DELETE") || if ((head.at(0) != "GET" && head.at(0) != "POST" &&
head.at(0) != "DELETE") ||
head.size() < 2) head.size() < 2)
send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
else if ((headers = route->getHeadersLst()).size() > 0) { else if ((headers = route->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end()) if (std::find(headers.begin(), headers.end(), head.at(0)) ==
send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); headers.end())
send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
} else if ((headers = server->getHeadersLst()).size() > 0) { } else if ((headers = server->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end()) if (std::find(headers.begin(), headers.end(), head.at(0)) ==
send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); headers.end())
send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
} }
string path = route->correctUri(uri); string path = route->correctUri(uri);
@ -152,7 +158,8 @@ int Socket::answer(Env *env, string request) {
cout << "No index: lf file\n"; cout << "No index: lf file\n";
ret = read_file(path); ret = read_file(path);
} }
answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n" : " 200 OK\r\n") answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n"
: " 200 OK\r\n")
<< ret; << ret;
cout << "|===|Answer|===>" << answer.str() << "|===||\n"; cout << "|===|Answer|===>" << answer.str() << "|===||\n";
send_answer(answer.str()); send_answer(answer.str());

30
srcs/tools.cpp

@ -39,8 +39,7 @@ listen_t get_listen_t(string listen) {
return ret; return ret;
} }
string getMime(string path) string getMime(string path) {
{
string extension; string extension;
size_t pos = path.rfind('.'); size_t pos = path.rfind('.');
extension = (pos == string::npos) ? "txt" : path.substr(pos + 1); extension = (pos == string::npos) ? "txt" : path.substr(pos + 1);
@ -92,7 +91,8 @@ string getMime(string path)
return ("font/woff"); return ("font/woff");
else if ((extension == "woff2")) else if ((extension == "woff2"))
return ("font/woff2"); return ("font/woff2");
else if ((extension == "jar") || (extension == "war") || (extension == "ear")) else if ((extension == "jar") || (extension == "war") ||
(extension == "ear"))
return ("application/java-archive"); return ("application/java-archive");
else if ((extension == "json")) else if ((extension == "json"))
return ("application/json"); return ("application/json");
@ -108,7 +108,10 @@ string getMime(string path)
return ("application/rtf"); return ("application/rtf");
else if ((extension == "m3u8")) else if ((extension == "m3u8"))
return ("application/vnd.apple.mpegurl"); return ("application/vnd.apple.mpegurl");
else if ((extension == "xls") || (extension == "xlt") || (extension == "xlm") || (extension == "xld") || (extension == "xla") || (extension == "xlc") || (extension == "xlw") || (extension == "xll")) else if ((extension == "xls") || (extension == "xlt") ||
(extension == "xlm") || (extension == "xld") ||
(extension == "xla") || (extension == "xlc") ||
(extension == "xlw") || (extension == "xll"))
return ("application/vnd.ms-excel"); return ("application/vnd.ms-excel");
else if ((extension == "ppt") || (extension == "pps")) else if ((extension == "ppt") || (extension == "pps"))
return ("application/vnd.ms-powerpoint"); return ("application/vnd.ms-powerpoint");
@ -130,7 +133,8 @@ string getMime(string path)
return ("application/x-makeself"); return ("application/x-makeself");
else if ((extension == "pl") || (extension == "pm")) else if ((extension == "pl") || (extension == "pm"))
return ("application/x-perl"); return ("application/x-perl");
else if ((extension == "pdb") || (extension == "pqr") || (extension == "prc") || (extension == "pde")) else if ((extension == "pdb") || (extension == "pqr") ||
(extension == "prc") || (extension == "pde"))
return ("application/x-pilot"); return ("application/x-pilot");
else if ((extension == "rar")) else if ((extension == "rar"))
return ("application/x-rar-compressed"); return ("application/x-rar-compressed");
@ -144,7 +148,8 @@ string getMime(string path)
return ("application/x-stuffit"); return ("application/x-stuffit");
else if ((extension == "tcl") || (extension == "tk")) else if ((extension == "tcl") || (extension == "tk"))
return ("application/x-tcl"); return ("application/x-tcl");
else if ((extension == "der") || (extension == "pem") || (extension == "crt")) else if ((extension == "der") || (extension == "pem") ||
(extension == "crt"))
return ("application/x-x509-ca-cert"); return ("application/x-x509-ca-cert");
else if ((extension == "xpi")) else if ((extension == "xpi"))
return ("application/x-xpinstall"); return ("application/x-xpinstall");
@ -152,7 +157,8 @@ string getMime(string path)
return ("application/xhtml+xml"); return ("application/xhtml+xml");
else if ((extension == "zip")) else if ((extension == "zip"))
return ("application/zip"); return ("application/zip");
else if ((extension == "bin") || (extension == "exe") || (extension == "dll")) else if ((extension == "bin") || (extension == "exe") ||
(extension == "dll"))
return ("application/octet-stream"); return ("application/octet-stream");
else if ((extension == "deb")) else if ((extension == "deb"))
return ("application/octet-stream"); return ("application/octet-stream");
@ -162,9 +168,11 @@ string getMime(string path)
return ("application/octet-stream"); return ("application/octet-stream");
else if ((extension == "img") || (extension == "iso")) else if ((extension == "img") || (extension == "iso"))
return ("application/octet-stream"); return ("application/octet-stream");
else if ((extension == "msi") || (extension == "msp") || (extension == "msm")) else if ((extension == "msi") || (extension == "msp") ||
(extension == "msm"))
return ("application/octet-stream"); return ("application/octet-stream");
else if ((extension == "mid") || (extension == "midi") || (extension == "kar")) else if ((extension == "mid") || (extension == "midi") ||
(extension == "kar"))
return ("audio/midi"); return ("audio/midi");
else if ((extension == "mp3")) else if ((extension == "mp3"))
return ("audio/mpeg"); return ("audio/mpeg");
@ -208,8 +216,8 @@ string read_file(string path) {
std::stringstream ret; std::stringstream ret;
struct stat info; struct stat info;
if (stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) { if (stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) {
std::cerr << "stat() error on " << path << ": " std::cerr << "stat() error on " << path << ": " << strerror(errno)
<< strerror(errno) << "\n"; << "\n";
return ""; return "";
} }
std::ifstream file(path.c_str()); std::ifstream file(path.c_str());

Loading…
Cancel
Save