Browse Source

functional autoindex and fixed root route finding

master
nicolas-arnaud 2 years ago
parent
commit
9fdc5b29e2
  1. 4
      includes/Route.hpp
  2. 2
      includes/webserv.hpp
  3. 210
      srcs/load/Route.cpp
  4. 16
      srcs/load/Socket.cpp
  5. 188
      srcs/tools.cpp

4
includes/Route.hpp

@ -16,8 +16,6 @@ class Route {
string getRoot(void);
string getReturn(void);
std::vector<string> getIndexsLst(void);
string getIndex(string path);
string read_file(string path);
string getIndex(string uri, string path);
string correctUri(string uri);
string getMime(string path);
};

2
includes/webserv.hpp

@ -50,6 +50,8 @@ void *ft_memset(void *b, int c, size_t len);
bool isInt(string str);
std::vector<string> split(string str, char delim);
listen_t get_listen_t(string listen);
string getMime(string path);
string read_file(string path);
#include "Nodes.hpp"
#include "Token.hpp"

210
srcs/load/Route.cpp

@ -24,7 +24,7 @@ string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; }
std::vector<string> Route::getIndexsLst(void) { return _indexs; }
string Route::getIndex(string path) {
string Route::getIndex(string uri, string path) {
std::stringstream content;
std::stringstream ret;
DIR *dir;
@ -36,7 +36,7 @@ string Route::getIndex(string path) {
if ((dir = opendir(path.c_str())) == NULL)
return "";
else {
content << path << " files :\n";
content << "<h3 style=\"text-align: center;\">" << path << " files :</h3>\n<ul>\n";
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.')
continue;
@ -44,217 +44,28 @@ string Route::getIndex(string path) {
if (entry->d_name == *it)
return (read_file(path + "/" + *it));
}
content << "- " << entry->d_name << "\n";
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";
}
content << "<ul>";
closedir(dir);
}
if (!_autoindex)
return "";
ret << "Content-type: text/html \n";
ret << "Content-length: "<< content.str().length();
ret << "\n\n" << content;
ret << "\n\n" << content.str();
return ret.str();
}
string Route::getMime(string path)
{
string extension;
size_t position = path.find('.');
if (position != string::npos)
extension = path.substr(position + 1);
else
extension = "html";
if (path.length() == 1)
{
path = "/index.html";
return("text/html\r\n\r\n");
}
else if (!(extension.compare("html")) || !(extension.compare("htm")) || !(extension.compare("shtml")))
return("text/html\r\n\r\n");
else if (!(extension.compare("css")))
return("text/css\r\n\r\n");
else if (!(extension.compare("xml")))
return("text/xml\r\n\r\n");
else if (!(extension.compare("gif")))
return("image/gif\r\n\r\n");
else if (!(extension.compare("jpeg")) || !(extension.compare("jpg")))
return("image/jpeg\r\n\r\n");
else if (!(extension.compare("js")))
return("application/javascript\r\n\r\n");
else if (!(extension.compare("atom")))
return("application/atom+xml\r\n\r\n");
else if (!(extension.compare("rss")))
return("application/rss+xml\r\n\r\n");
else if (!(extension.compare("mml")))
return("text/mathml\r\n\r\n");
else if (!(extension.compare("txt")))
return("text/plain\r\n\r\n");
else if (!(extension.compare("jad")))
return("text/vnd.sun.j2me.app-descriptor\r\n\r\n");
else if (!(extension.compare("wml")))
return("text/vnd.wap.wml\r\n\r\n");
else if (!(extension.compare("htc")))
return("text/x-component\r\n\r\n");
else if (!(extension.compare("avif")))
return("image/avif\r\n\r\n");
else if (!(extension.compare("png")))
return("image/png\r\n\r\n");
else if (!(extension.compare("svg")) || !(extension.compare("svgz")))
return("image/svg+xml\r\n\r\n");
else if (!(extension.compare("tif")) || !(extension.compare("tiff")))
return("image/tiff\r\n\r\n");
else if (!(extension.compare("wbmp")))
return("image/vnd.wap.wbmp\r\n\r\n");
else if (!(extension.compare("webp")))
return("image/webp\r\n\r\n");
else if (!(extension.compare("ico")))
return("image/x-icon\r\n\r\n");
else if (!(extension.compare("jng")))
return("image/x-jng\r\n\r\n");
else if (!(extension.compare("bmp")))
return("image/x-ms-bmp\r\n\r\n");
else if (!(extension.compare("woff")))
return("font/woff\r\n\r\n");
else if (!(extension.compare("woff2")))
return("font/woff2\r\n\r\n");
else if (!(extension.compare("jar")) || !(extension.compare("war")) || !(extension.compare("ear")))
return("application/java-archive\r\n\r\n");
else if (!(extension.compare("json")))
return("application/json\r\n\r\n");
else if (!(extension.compare("hqx")))
return("application/mac-binhex40\r\n\r\n");
else if (!(extension.compare("doc")))
return("application/msword\r\n\r\n");
else if (!(extension.compare("pdf")))
return("application/pdf\r\n\r\n");
else if (!(extension.compare("ps")) || !(extension.compare("eps")) || !(extension.compare("ai")))
return("application/postscript\r\n\r\n");
else if (!(extension.compare("rtf")))
return("application/rtf\r\n\r\n");
else if (!(extension.compare("m3u8")))
return("application/vnd.apple.mpegurl\r\n\r\n");
else if (!(extension.compare("xls")) || !(extension.compare("xlt")) || !(extension.compare("xlm")) || !(extension.compare("xld")) || !(extension.compare("xla")) || !(extension.compare("xlc")) || !(extension.compare("xlw")) || !(extension.compare("xll")))
return("application/vnd.ms-excel\r\n\r\n");
else if (!(extension.compare("ppt")) || !(extension.compare("pps")))
return("application/vnd.ms-powerpoint\r\n\r\n");
else if (!(extension.compare("wmlc")))
return("application/vnd.wap.wmlc\r\n\r\n");
else if (!(extension.compare("kml")))
return("application/vnd.google-earth.kml+xml\r\n\r\n");
else if (!(extension.compare("kmz")))
return("application/vnd.google-earth.kmz\r\n\r\n");
else if (!(extension.compare("7z")))
return("application/x-7z-compressed\r\n\r\n");
else if (!(extension.compare("cco")))
return("application/x-cocoa\r\n\r\n");
else if (!(extension.compare("jardiff")))
return("application/x-java-archive-diff\r\n\r\n");
else if (!(extension.compare("jnlp")))
return("application/x-java-jnlp-file\r\n\r\n");
else if (!(extension.compare("run")))
return("application/x-makeself\r\n\r\n");
else if (!(extension.compare("pl")) || !(extension.compare("pm")))
return("application/x-perl\r\n\r\n");
else if (!(extension.compare("pdb")) || !(extension.compare("pqr")) || !(extension.compare("prc")) || !(extension.compare("pde")))
return("application/x-pilot\r\n\r\n");
else if (!(extension.compare("rar")))
return("application/x-rar-compressed\r\n\r\n");
else if (!(extension.compare("rpm")))
return("application/x-redhat-package-manager\r\n\r\n");
else if (!(extension.compare("sea")))
return("application/x-sea\r\n\r\n");
else if (!(extension.compare("swf")))
return("application/x-shockwave-flash\r\n\r\n");
else if (!(extension.compare("sit")))
return("application/x-stuffit\r\n\r\n");
else if (!(extension.compare("tcl")) || !(extension.compare("tk")))
return("application/x-tcl\r\n\r\n");
else if (!(extension.compare("der")) || !(extension.compare("pem")) || !(extension.compare("crt")))
return("application/x-x509-ca-cert\r\n\r\n");
else if (!(extension.compare("xpi")))
return("application/x-xpinstall\r\n\r\n");
else if (!(extension.compare("xhtml")) || !(extension.compare("xht")))
return("application/xhtml+xml\r\n\r\n");
else if (!(extension.compare("zip")))
return("application/zip\r\n\r\n");
else if (!(extension.compare("bin")) || !(extension.compare("exe")) || !(extension.compare("dll")))
return("application/octet-stream\r\n\r\n");
else if (!(extension.compare("deb")))
return("application/octet-stream\r\n\r\n");
else if (!(extension.compare("dmg")))
return("application/octet-stream\r\n\r\n");
else if (!(extension.compare("eot")))
return("application/octet-stream\r\n\r\n");
else if (!(extension.compare("img")) || !(extension.compare("iso")))
return("application/octet-stream\r\n\r\n");
else if (!(extension.compare("msi")) || !(extension.compare("msp")) || !(extension.compare("msm")))
return("application/octet-stream\r\n\r\n");
else if (!(extension.compare("mid")) || !(extension.compare("midi")) || !(extension.compare("kar")))
return("audio/midi\r\n\r\n");
else if (!(extension.compare("mp3")))
return("audio/mpeg\r\n\r\n");
else if (!(extension.compare("ogg")))
return("audio/ogg\r\n\r\n");
else if (!(extension.compare("m4a")))
return("audio/x-m4a\r\n\r\n");
else if (!(extension.compare("ra")))
return("audio/x-realaudio\r\n\r\n");
else if (!(extension.compare("3gpp")) || !(extension.compare("3gp")))
return("video/3gpp\r\n\r\n");
else if (!(extension.compare("ts")))
return("video/mp2t\r\n\r\n");
else if (!(extension.compare("mp4")))
return("video/mp4\r\n\r\n");
else if (!(extension.compare("mpeg")) || !(extension.compare("mpg")))
return("video/mpeg\r\n\r\n");
else if (!(extension.compare("mov")))
return("video/quicktime\r\n\r\n");
else if (!(extension.compare("webm")))
return("video/webm\r\n\r\n");
else if (!(extension.compare("flv")))
return("video/x-flv\r\n\r\n");
else if (!(extension.compare("m4v")))
return("video/x-m4v\r\n\r\n");
else if (!(extension.compare("mng")))
return("video/x-mng\r\n\r\n");
else if (!(extension.compare("asx")) || !(extension.compare("asf")))
return("video/x-ms-asf\r\n\r\n");
else if (!(extension.compare("wmv")))
return("video/x-ms-wmv\r\n\r\n");
else if (!(extension.compare("avi")))
return("video/x-msvideo\r\n\r\n");
else
return("text/plain\r\n\r\n");
}
string Route::read_file(string path) {
string str;
string content;
std::stringstream ret;
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
return "";
}
std::ifstream file(path.c_str());
while (file) {
std::getline(file, str);
content += str + "\n";
}
ret << "Content-type: " << this->getMime(path) << "\n";
ret << "Content-length: " << content.length();
ret << "\n\n" << content;
return (ret.str());
}
string Route::correctUri(string uri) {
std::stringstream ret;
std::vector<string>::iterator it;
std::vector<string>::iterator it2;
cout << "Correcting request: " << uri << " with root: " << _root << "\n";
//cout << "Correcting request: " << uri << " with root: " << _root << "\n";
ret << _root;
std::vector<string> loc_split = split(_location, '/');
std::vector<string> uri_split = split(uri, '/');
@ -264,12 +75,13 @@ string Route::correctUri(string uri) {
it2++;
while (*it == "")
it++;
it2++;
if (it != loc_split.end())
it2++;
}
while (it2 < uri_split.end()) {
ret << "/" << *(it2++);
}
cout << "resutlt: " << ret.str() << "\n";
//cout << "resutlt: " << ret.str() << "\n";
return ret.str();
}

16
srcs/load/Socket.cpp

@ -100,7 +100,7 @@ void Socket::refresh(Env *env) {
}
int Socket::answer(Env *env, int fd, string request) {
cout << request << "\n|===|===|===|\n";
cout << "|===|Request|===|\n"<< request << "\n|===|===|===|\n";
std::vector<string> lines = split(request, '\n');
std::vector<string> head = split(lines.at(0), ' ');
string uri;
@ -111,15 +111,19 @@ int Socket::answer(Env *env, int fd, string request) {
string ret;
std::stringstream answer;
answer << "HTTP/1.1 200 OK\n";
answer << "HTTP/1.1";
Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri);
string path = route->correctUri(uri);
if ((ret = route->getIndex(path)) == "")
ret = route->read_file(path);
answer << ret;
cout << answer.str() << "\n|===|===|===|\n";
cout << "Path: " << path << "\n";
ret = route->getIndex(uri, path);
if (ret == "") {
cout << "No index: lf file\n";
ret = read_file(path);
}
answer << (ret == "" ? " 404 Not Found\nContent-length: 0\n\n" : " 200 OK\n") << ret;
cout << "|===|Answer|===|\n" << answer.str() << "\n|===|===|===|\n";
send_answer(fd, answer.str());
return EXIT_SUCCESS;
}

188
srcs/tools.cpp

@ -38,3 +38,191 @@ listen_t get_listen_t(string listen) {
ret.port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str());
return ret;
}
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");
else if ((extension == "css"))
return("text/css");
else if ((extension == "xml"))
return("text/xml");
else if ((extension == "gif"))
return("image/gif");
else if ((extension == "jpeg") || (extension == "jpg"))
return("image/jpeg");
else if ((extension == "js"))
return("application/javascript");
else if ((extension == "atom"))
return("application/atom+xml");
else if ((extension == "rss"))
return("application/rss+xml");
else if ((extension == "mml"))
return("text/mathml");
else if ((extension == "txt"))
return("text/plain");
else if ((extension == "jad"))
return("text/vnd.sun.j2me.app-descriptor");
else if ((extension == "wml"))
return("text/vnd.wap.wml");
else if ((extension == "htc"))
return("text/x-component");
else if ((extension == "avif"))
return("image/avif");
else if ((extension == "png"))
return("image/png");
else if ((extension == "svg") || (extension == "svgz"))
return("image/svg+xml");
else if ((extension == "tif") || (extension == "tiff"))
return("image/tiff");
else if ((extension == "wbmp"))
return("image/vnd.wap.wbmp");
else if ((extension == "webp"))
return("image/webp");
else if ((extension == "ico"))
return("image/x-icon");
else if ((extension == "jng"))
return("image/x-jng");
else if ((extension == "bmp"))
return("image/x-ms-bmp");
else if ((extension == "woff"))
return("font/woff");
else if ((extension == "woff2"))
return("font/woff2");
else if ((extension == "jar") || (extension == "war") || (extension == "ear"))
return("application/java-archive");
else if ((extension == "json"))
return("application/json");
else if ((extension == "hqx"))
return("application/mac-binhex40");
else if ((extension == "doc"))
return("application/msword");
else if ((extension == "pdf"))
return("application/pdf");
else if ((extension == "ps") || (extension == "eps") || (extension == "ai"))
return("application/postscript");
else if ((extension == "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");
else if ((extension == "ppt") || (extension == "pps"))
return("application/vnd.ms-powerpoint");
else if ((extension == "wmlc"))
return("application/vnd.wap.wmlc");
else if ((extension == "kml"))
return("application/vnd.google-earth.kml+xml");
else if ((extension == "kmz"))
return("application/vnd.google-earth.kmz");
else if ((extension == "7z"))
return("application/x-7z-compressed");
else if ((extension == "cco"))
return("application/x-cocoa");
else if ((extension == "jardiff"))
return("application/x-java-archive-diff");
else if ((extension == "jnlp"))
return("application/x-java-jnlp-file");
else if ((extension == "run"))
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");
else if ((extension == "rar"))
return("application/x-rar-compressed");
else if ((extension == "rpm"))
return("application/x-redhat-package-manager");
else if ((extension == "sea"))
return("application/x-sea");
else if ((extension == "swf"))
return("application/x-shockwave-flash");
else if ((extension == "sit"))
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");
else if ((extension == "xpi"))
return("application/x-xpinstall");
else if ((extension == "xhtml") || (extension == "xht"))
return("application/xhtml+xml");
else if ((extension == "zip"))
return("application/zip");
else if ((extension == "bin") || (extension == "exe") || (extension == "dll"))
return("application/octet-stream");
else if ((extension == "deb"))
return("application/octet-stream");
else if ((extension == "dmg"))
return("application/octet-stream");
else if ((extension == "eot"))
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");
else if ((extension == "mp3"))
return("audio/mpeg");
else if ((extension == "ogg"))
return("audio/ogg");
else if ((extension == "m4a"))
return("audio/x-m4a");
else if ((extension == "ra"))
return("audio/x-realaudio");
else if ((extension == "3gpp") || (extension == "3gp"))
return("video/3gpp");
else if ((extension == "ts"))
return("video/mp2t");
else if ((extension == "mp4"))
return("video/mp4");
else if ((extension == "mpeg") || (extension == "mpg"))
return("video/mpeg");
else if ((extension == "mov"))
return("video/quicktime");
else if ((extension == "webm"))
return("video/webm");
else if ((extension == "flv"))
return("video/x-flv");
else if ((extension == "m4v"))
return("video/x-m4v");
else if ((extension == "mng"))
return("video/x-mng");
else if ((extension == "asx") || (extension == "asf"))
return("video/x-ms-asf");
else if ((extension == "wmv"))
return("video/x-ms-wmv");
else if ((extension == "avi"))
return("video/x-msvideo");
else
return("text/plain");
}
string read_file(string path) {
string str;
string content;
std::stringstream ret;
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
return "";
}
std::ifstream file(path.c_str());
if (file.fail())
return "";
while (file) {
std::getline(file, str);
content += str + "\n";
}
ret << "Content-type: " << getMime(path) << "\n";
ret << "Content-length: " << content.length();
ret << "\n\n" << content;
return (ret.str());
}

Loading…
Cancel
Save