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. 188
      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"])) {
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());
}
}
@ -56,8 +57,8 @@ string Route::getIndex(string uri, string path) {
content << "<li><a href=\"" << uri + "/" + entry->d_name << "\">"
<< entry->d_name << "</a></li>\n";
if (stat(path.c_str(), &info) != 0)
std::cerr << "stat() error on " << path << ": " << strerror(errno)
<< "\n";
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
}
content << "<ul>";
closedir(dir);

29
srcs/load/Socket.cpp

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

188
srcs/tools.cpp

@ -39,167 +39,175 @@ listen_t get_listen_t(string listen) {
return ret;
}
string getMime(string path)
{
string getMime(string path) {
string extension;
size_t pos = path.rfind('.');
extension = (pos == string::npos) ? "txt" : path.substr(pos + 1);
if ((extension == "html") || (extension == "htm") || (extension == "shtml"))
return("text/html");
return ("text/html");
else if ((extension == "css"))
return("text/css");
return ("text/css");
else if ((extension == "xml"))
return("text/xml");
return ("text/xml");
else if ((extension == "gif"))
return("image/gif");
return ("image/gif");
else if ((extension == "jpeg") || (extension == "jpg"))
return("image/jpeg");
return ("image/jpeg");
else if ((extension == "js"))
return("application/javascript");
return ("application/javascript");
else if ((extension == "atom"))
return("application/atom+xml");
return ("application/atom+xml");
else if ((extension == "rss"))
return("application/rss+xml");
return ("application/rss+xml");
else if ((extension == "mml"))
return("text/mathml");
return ("text/mathml");
else if ((extension == "txt"))
return("text/plain");
return ("text/plain");
else if ((extension == "jad"))
return("text/vnd.sun.j2me.app-descriptor");
return ("text/vnd.sun.j2me.app-descriptor");
else if ((extension == "wml"))
return("text/vnd.wap.wml");
return ("text/vnd.wap.wml");
else if ((extension == "htc"))
return("text/x-component");
return ("text/x-component");
else if ((extension == "avif"))
return("image/avif");
return ("image/avif");
else if ((extension == "png"))
return("image/png");
return ("image/png");
else if ((extension == "svg") || (extension == "svgz"))
return("image/svg+xml");
return ("image/svg+xml");
else if ((extension == "tif") || (extension == "tiff"))
return("image/tiff");
return ("image/tiff");
else if ((extension == "wbmp"))
return("image/vnd.wap.wbmp");
return ("image/vnd.wap.wbmp");
else if ((extension == "webp"))
return("image/webp");
return ("image/webp");
else if ((extension == "ico"))
return("image/x-icon");
return ("image/x-icon");
else if ((extension == "jng"))
return("image/x-jng");
return ("image/x-jng");
else if ((extension == "bmp"))
return("image/x-ms-bmp");
return ("image/x-ms-bmp");
else if ((extension == "woff"))
return("font/woff");
return ("font/woff");
else if ((extension == "woff2"))
return("font/woff2");
else if ((extension == "jar") || (extension == "war") || (extension == "ear"))
return("application/java-archive");
return ("font/woff2");
else if ((extension == "jar") || (extension == "war") ||
(extension == "ear"))
return ("application/java-archive");
else if ((extension == "json"))
return("application/json");
return ("application/json");
else if ((extension == "hqx"))
return("application/mac-binhex40");
return ("application/mac-binhex40");
else if ((extension == "doc"))
return("application/msword");
return ("application/msword");
else if ((extension == "pdf"))
return("application/pdf");
return ("application/pdf");
else if ((extension == "ps") || (extension == "eps") || (extension == "ai"))
return("application/postscript");
return ("application/postscript");
else if ((extension == "rtf"))
return("application/rtf");
return ("application/rtf");
else if ((extension == "m3u8"))
return("application/vnd.apple.mpegurl");
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.apple.mpegurl");
else if ((extension == "xls") || (extension == "xlt") ||
(extension == "xlm") || (extension == "xld") ||
(extension == "xla") || (extension == "xlc") ||
(extension == "xlw") || (extension == "xll"))
return ("application/vnd.ms-excel");
else if ((extension == "ppt") || (extension == "pps"))
return("application/vnd.ms-powerpoint");
return ("application/vnd.ms-powerpoint");
else if ((extension == "wmlc"))
return("application/vnd.wap.wmlc");
return ("application/vnd.wap.wmlc");
else if ((extension == "kml"))
return("application/vnd.google-earth.kml+xml");
return ("application/vnd.google-earth.kml+xml");
else if ((extension == "kmz"))
return("application/vnd.google-earth.kmz");
return ("application/vnd.google-earth.kmz");
else if ((extension == "7z"))
return("application/x-7z-compressed");
return ("application/x-7z-compressed");
else if ((extension == "cco"))
return("application/x-cocoa");
return ("application/x-cocoa");
else if ((extension == "jardiff"))
return("application/x-java-archive-diff");
return ("application/x-java-archive-diff");
else if ((extension == "jnlp"))
return("application/x-java-jnlp-file");
return ("application/x-java-jnlp-file");
else if ((extension == "run"))
return("application/x-makeself");
return ("application/x-makeself");
else if ((extension == "pl") || (extension == "pm"))
return("application/x-perl");
else if ((extension == "pdb") || (extension == "pqr") || (extension == "prc") || (extension == "pde"))
return("application/x-pilot");
return ("application/x-perl");
else if ((extension == "pdb") || (extension == "pqr") ||
(extension == "prc") || (extension == "pde"))
return ("application/x-pilot");
else if ((extension == "rar"))
return("application/x-rar-compressed");
return ("application/x-rar-compressed");
else if ((extension == "rpm"))
return("application/x-redhat-package-manager");
return ("application/x-redhat-package-manager");
else if ((extension == "sea"))
return("application/x-sea");
return ("application/x-sea");
else if ((extension == "swf"))
return("application/x-shockwave-flash");
return ("application/x-shockwave-flash");
else if ((extension == "sit"))
return("application/x-stuffit");
return ("application/x-stuffit");
else if ((extension == "tcl") || (extension == "tk"))
return("application/x-tcl");
else if ((extension == "der") || (extension == "pem") || (extension == "crt"))
return("application/x-x509-ca-cert");
return ("application/x-tcl");
else if ((extension == "der") || (extension == "pem") ||
(extension == "crt"))
return ("application/x-x509-ca-cert");
else if ((extension == "xpi"))
return("application/x-xpinstall");
return ("application/x-xpinstall");
else if ((extension == "xhtml") || (extension == "xht"))
return("application/xhtml+xml");
return ("application/xhtml+xml");
else if ((extension == "zip"))
return("application/zip");
else if ((extension == "bin") || (extension == "exe") || (extension == "dll"))
return("application/octet-stream");
return ("application/zip");
else if ((extension == "bin") || (extension == "exe") ||
(extension == "dll"))
return ("application/octet-stream");
else if ((extension == "deb"))
return("application/octet-stream");
return ("application/octet-stream");
else if ((extension == "dmg"))
return("application/octet-stream");
return ("application/octet-stream");
else if ((extension == "eot"))
return("application/octet-stream");
return ("application/octet-stream");
else if ((extension == "img") || (extension == "iso"))
return("application/octet-stream");
else if ((extension == "msi") || (extension == "msp") || (extension == "msm"))
return("application/octet-stream");
else if ((extension == "mid") || (extension == "midi") || (extension == "kar"))
return("audio/midi");
return ("application/octet-stream");
else if ((extension == "msi") || (extension == "msp") ||
(extension == "msm"))
return ("application/octet-stream");
else if ((extension == "mid") || (extension == "midi") ||
(extension == "kar"))
return ("audio/midi");
else if ((extension == "mp3"))
return("audio/mpeg");
return ("audio/mpeg");
else if ((extension == "ogg"))
return("audio/ogg");
return ("audio/ogg");
else if ((extension == "m4a"))
return("audio/x-m4a");
return ("audio/x-m4a");
else if ((extension == "ra"))
return("audio/x-realaudio");
return ("audio/x-realaudio");
else if ((extension == "3gpp") || (extension == "3gp"))
return("video/3gpp");
return ("video/3gpp");
else if ((extension == "ts"))
return("video/mp2t");
return ("video/mp2t");
else if ((extension == "mp4"))
return("video/mp4");
return ("video/mp4");
else if ((extension == "mpeg") || (extension == "mpg"))
return("video/mpeg");
return ("video/mpeg");
else if ((extension == "mov"))
return("video/quicktime");
return ("video/quicktime");
else if ((extension == "webm"))
return("video/webm");
return ("video/webm");
else if ((extension == "flv"))
return("video/x-flv");
return ("video/x-flv");
else if ((extension == "m4v"))
return("video/x-m4v");
return ("video/x-m4v");
else if ((extension == "mng"))
return("video/x-mng");
return ("video/x-mng");
else if ((extension == "asx") || (extension == "asf"))
return("video/x-ms-asf");
return ("video/x-ms-asf");
else if ((extension == "wmv"))
return("video/x-ms-wmv");
return ("video/x-ms-wmv");
else if ((extension == "avi"))
return("video/x-msvideo");
return ("video/x-msvideo");
else
return("text/plain");
return ("text/plain");
}
string read_file(string path) {
@ -208,12 +216,12 @@ string read_file(string path) {
std::stringstream ret;
struct stat info;
if (stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) {
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
std::cerr << "stat() error on " << path << ": " << strerror(errno)
<< "\n";
return "";
}
std::ifstream file(path.c_str());
//if (!file.good())
// if (!file.good())
// return "";
while (file) {
std::getline(file, str);

Loading…
Cancel
Save