You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.7 KiB

#include "webserv.hpp"
2 years ago
void *ft_memset(void *b, int c, size_t len) {
2 years ago
size_t i;
2 years ago
unsigned char *b_cpy;
2 years ago
2 years ago
b_cpy = (unsigned char *)b;
i = 0;
while (i < len)
*(unsigned char *)(b_cpy + i++) = (unsigned char)c;
return ((void *)b);
2 years ago
}
2 years ago
bool isInt(string str) {
for (string::iterator it = str.begin(); it < str.end(); it++)
2 years ago
if (*it < '0' || *it > '9')
return false;
2 years ago
return true;
}
std::vector< string > split(string str, string delim) {
string temp(str);
2 years ago
string token;
size_t pos;
std::vector< std::string > tokens;
2 years ago
while ((pos = temp.find(delim)) != string::npos) {
token = temp.substr(0, pos);
tokens.push_back(token);
temp.erase(0, pos + delim.length());
}
tokens.push_back(temp);
return tokens;
}
listen_t get_listen_t(string listen) {
listen_t ret;
2 years ago
size_t sep_pos = listen.rfind(':');
string tmp = listen.substr(0, sep_pos);
ret.ip = isInt(tmp) ? "0.0.0.0" : (tmp == "localhost" ? "127.0.0.1" : tmp);
tmp = listen.substr(sep_pos + 1, listen.length() - sep_pos - 1).c_str();
ret.port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str());
return ret;
}
listen_t get_listen_t(string ip, int port) {
listen_t ret;
ret.ip = ip;
ret.port = port;
return ret;
}
2 years ago
string getMime(string path) {
size_t pos = path.rfind('.');
2 years ago
string extension = (pos == string::npos) ? "txt" : path.substr(pos + 1);
if ((extension == "html") || (extension == "htm") || (extension == "shtml"))
2 years ago
return ("text/html");
else if ((extension == "css"))
2 years ago
return ("text/css");
else if ((extension == "xml"))
2 years ago
return ("text/xml");
else if ((extension == "gif"))
2 years ago
return ("image/gif");
else if ((extension == "jpeg") || (extension == "jpg"))
2 years ago
return ("image/jpeg");
else if ((extension == "js"))
2 years ago
return ("application/javascript");
else if ((extension == "atom"))
2 years ago
return ("application/atom+xml");
else if ((extension == "rss"))
2 years ago
return ("application/rss+xml");
else if ((extension == "mml"))
2 years ago
return ("text/mathml");
else if ((extension == "txt"))
2 years ago
return ("text/plain");
else if ((extension == "jad"))
2 years ago
return ("text/vnd.sun.j2me.app-descriptor");
else if ((extension == "wml"))
2 years ago
return ("text/vnd.wap.wml");
else if ((extension == "htc"))
2 years ago
return ("text/x-component");
else if ((extension == "avif"))
2 years ago
return ("image/avif");
else if ((extension == "png"))
2 years ago
return ("image/png");
else if ((extension == "svg") || (extension == "svgz"))
2 years ago
return ("image/svg+xml");
else if ((extension == "tif") || (extension == "tiff"))
2 years ago
return ("image/tiff");
else if ((extension == "wbmp"))
2 years ago
return ("image/vnd.wap.wbmp");
else if ((extension == "webp"))
2 years ago
return ("image/webp");
else if ((extension == "ico"))
2 years ago
return ("image/x-icon");
else if ((extension == "jng"))
2 years ago
return ("image/x-jng");
else if ((extension == "bmp"))
2 years ago
return ("image/x-ms-bmp");
else if ((extension == "woff"))
2 years ago
return ("font/woff");
else if ((extension == "woff2"))
2 years ago
return ("font/woff2");
else if ((extension == "jar") || (extension == "war") ||
(extension == "ear"))
return ("application/java-archive");
else if ((extension == "json"))
2 years ago
return ("application/json");
else if ((extension == "hqx"))
2 years ago
return ("application/mac-binhex40");
else if ((extension == "doc"))
2 years ago
return ("application/msword");
else if ((extension == "pdf"))
2 years ago
return ("application/pdf");
else if ((extension == "ps") || (extension == "eps") || (extension == "ai"))
2 years ago
return ("application/postscript");
else if ((extension == "rtf"))
2 years ago
return ("application/rtf");
else if ((extension == "m3u8"))
2 years ago
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"))
2 years ago
return ("application/vnd.ms-powerpoint");
else if ((extension == "wmlc"))
2 years ago
return ("application/vnd.wap.wmlc");
else if ((extension == "kml"))
2 years ago
return ("application/vnd.google-earth.kml+xml");
else if ((extension == "kmz"))
2 years ago
return ("application/vnd.google-earth.kmz");
else if ((extension == "7z"))
2 years ago
return ("application/x-7z-compressed");
else if ((extension == "cco"))
2 years ago
return ("application/x-cocoa");
else if ((extension == "jardiff"))
2 years ago
return ("application/x-java-archive-diff");
else if ((extension == "jnlp"))
2 years ago
return ("application/x-java-jnlp-file");
else if ((extension == "run"))
2 years ago
return ("application/x-makeself");
else if ((extension == "pl") || (extension == "pm"))
2 years ago
return ("application/x-perl");
else if ((extension == "pdb") || (extension == "pqr") ||
(extension == "prc") || (extension == "pde"))
return ("application/x-pilot");
else if ((extension == "rar"))
2 years ago
return ("application/x-rar-compressed");
else if ((extension == "rpm"))
2 years ago
return ("application/x-redhat-package-manager");
else if ((extension == "sea"))
2 years ago
return ("application/x-sea");
else if ((extension == "swf"))
2 years ago
return ("application/x-shockwave-flash");
else if ((extension == "sit"))
2 years ago
return ("application/x-stuffit");
else if ((extension == "tcl") || (extension == "tk"))
2 years ago
return ("application/x-tcl");
else if ((extension == "der") || (extension == "pem") ||
(extension == "crt"))
return ("application/x-x509-ca-cert");
else if ((extension == "xpi"))
2 years ago
return ("application/x-xpinstall");
else if ((extension == "xhtml") || (extension == "xht"))
2 years ago
return ("application/xhtml+xml");
else if ((extension == "zip"))
2 years ago
return ("application/zip");
else if ((extension == "bin") || (extension == "exe") ||
(extension == "dll"))
return ("application/octet-stream");
else if ((extension == "deb"))
2 years ago
return ("application/octet-stream");
else if ((extension == "dmg"))
2 years ago
return ("application/octet-stream");
else if ((extension == "eot"))
2 years ago
return ("application/octet-stream");
else if ((extension == "img") || (extension == "iso"))
2 years ago
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"))
2 years ago
return ("audio/mpeg");
else if ((extension == "ogg"))
2 years ago
return ("audio/ogg");
else if ((extension == "m4a"))
2 years ago
return ("audio/x-m4a");
else if ((extension == "ra"))
2 years ago
return ("audio/x-realaudio");
else if ((extension == "3gpp") || (extension == "3gp"))
2 years ago
return ("video/3gpp");
else if ((extension == "ts"))
2 years ago
return ("video/mp2t");
else if ((extension == "mp4"))
2 years ago
return ("video/mp4");
else if ((extension == "mpeg") || (extension == "mpg"))
2 years ago
return ("video/mpeg");
else if ((extension == "mov"))
2 years ago
return ("video/quicktime");
else if ((extension == "webm"))
2 years ago
return ("video/webm");
else if ((extension == "flv"))
2 years ago
return ("video/x-flv");
else if ((extension == "m4v"))
2 years ago
return ("video/x-m4v");
else if ((extension == "mng"))
2 years ago
return ("video/x-mng");
else if ((extension == "asx") || (extension == "asf"))
2 years ago
return ("video/x-ms-asf");
else if ((extension == "wmv"))
2 years ago
return ("video/x-ms-wmv");
else if ((extension == "avi"))
2 years ago
return ("video/x-msvideo");
else
2 years ago
return ("text/plain");
}
string read_file(string path) {
2 years ago
string str;
string content;
std::stringstream ret;
2 years ago
// struct stat info;
/*if (stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) {
2 years ago
std::cerr << "stat() error on " << path << ": " << strerror(errno)
<< "\n";
return "";
2 years ago
}*/
std::ifstream file(path.c_str());
2 years ago
if (!file.good())
return "";
while (file) {
std::getline(file, str);
content += str + "\n";
}
2 years ago
ret << "Content-type: " << getMime(path) << "\r\n";
ret << "Content-length: " << content.length() << "\r\n";
ret << "\r\n" << content;
return (ret.str());
}