mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:28:46 +02:00
「✨」 feat(requests/mime): added mime type functions and started implementation
This commit is contained in:
@ -25,7 +25,7 @@ cgi.go = "/bin/go"
|
|||||||
[server.location./redir]
|
[server.location./redir]
|
||||||
redirect = "https://kanel.ovh"
|
redirect = "https://kanel.ovh"
|
||||||
|
|
||||||
[server3]
|
[serverr]
|
||||||
server_names = { "ptnnnn.local"}
|
server_names = { "ptnnnn.local"}
|
||||||
host = "127.0.0.2"
|
host = "127.0.0.2"
|
||||||
port = 9090
|
port = 9090
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
|
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/04/22 11:52:00 by mmoussou ### ########.fr */
|
/* Updated: 2025/04/24 14:56:08 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -26,6 +26,7 @@ public:
|
|||||||
static void setEntries(const std::map<int, std::string>);
|
static void setEntries(const std::map<int, std::string>);
|
||||||
|
|
||||||
static std::map<int, std::string> message;
|
static std::map<int, std::string> message;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::map<int, std::string> populateMessages();
|
static std::map<int, std::string> populateMessages();
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/04/22 15:07:02 by adjoly ### ########.fr */
|
/* Updated: 2025/04/24 15:09:52 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include <requests/HttpIMessage.hpp>
|
#include <requests/HttpIMessage.hpp>
|
||||||
#include <requests/HttpResponse.hpp>
|
#include <requests/HttpResponse.hpp>
|
||||||
|
#include <requests/Mime.hpp>
|
||||||
|
|
||||||
#include <config/default.hpp>
|
#include <config/default.hpp>
|
||||||
|
|
||||||
|
32
includes/requests/Mime.hpp
Normal file
32
includes/requests/Mime.hpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Mime.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/24 14:53:25 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2025/04/24 15:10:32 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace webserv {
|
||||||
|
namespace http {
|
||||||
|
|
||||||
|
class Mime {
|
||||||
|
public:
|
||||||
|
static std::string getType(const std::string &filename);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::map<std::string, std::string> initMimeTypes();
|
||||||
|
|
||||||
|
static std::map<std::string, std::string> mimeTypes;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // -namespace http
|
||||||
|
} // -namespace webserv
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/04/22 15:03:46 by adjoly ### ########.fr */
|
/* Updated: 2025/04/24 15:31:56 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,6 +18,8 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
|
#include <log.hpp>
|
||||||
|
|
||||||
using namespace webserv;
|
using namespace webserv;
|
||||||
|
|
||||||
http::IRequest::~IRequest(void) {
|
http::IRequest::~IRequest(void) {
|
||||||
@ -174,7 +176,30 @@ http::Response http::Get::execute(void)
|
|||||||
|
|
||||||
std::sort(files.begin(), files.end());
|
std::sort(files.begin(), files.end());
|
||||||
|
|
||||||
std::string body = "<html><body><ul>\n";
|
std::string body = "<html>";
|
||||||
|
|
||||||
|
body += "<head><style>\n\
|
||||||
|
:root {\n\
|
||||||
|
background-color: -moz-dialog;\n\
|
||||||
|
color: -moz-dialogtext;\n\
|
||||||
|
font: message-box;\n\
|
||||||
|
padding-inline: 2em;\n\
|
||||||
|
color-scheme: light dark;\n\
|
||||||
|
}\n\
|
||||||
|
\n\
|
||||||
|
body {\n\
|
||||||
|
border: 1px solid ThreeDShadow;\n\
|
||||||
|
border-radius: 10px;\n\
|
||||||
|
padding: 3em;\n\
|
||||||
|
min-width: 30em;\n\
|
||||||
|
max-width: 65em;\n\
|
||||||
|
margin: 4em auto;\n\
|
||||||
|
background-color: Field;\n\
|
||||||
|
color: FieldText;\n\
|
||||||
|
}\n\
|
||||||
|
</style></head>";
|
||||||
|
|
||||||
|
body += "<body><ul>\n";
|
||||||
for (size_t i = 0; i < files.size(); i++)
|
for (size_t i = 0; i < files.size(); i++)
|
||||||
body += "<li><a href=\"" + files[i] + "\">" + files[i] + "</a></li>\n";
|
body += "<li><a href=\"" + files[i] + "\">" + files[i] + "</a></li>\n";
|
||||||
body += "</ul></body></html>";
|
body += "</ul></body></html>";
|
||||||
@ -199,13 +224,9 @@ http::Response http::Get::execute(void)
|
|||||||
|
|
||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(200);
|
response.setStatusCode(200);
|
||||||
response.addHeader("Content-Type", "text/html"); // TODO: change it to check the file extension and set it to the corresponding MIME or text/plain if unkown. we will only implement the important MIME types in the Mozilla documentation because https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types
|
response.addHeader("Content-Type", http::Mime::getType(this->_target));
|
||||||
|
|
||||||
//std::ifstream file_end(this->_target.c_str(), std::ios::binary | std::ios::ate);
|
_log->debug(response.str().c_str());
|
||||||
//std::stringstream length;
|
|
||||||
//length << (file_end.tellg() - file.tellg());
|
|
||||||
//std::cout << length.str() << std::endl;
|
|
||||||
//response.addHeader("Content-Length", length.str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
|
113
src/requests_handling/Mime.cpp
Normal file
113
src/requests_handling/Mime.cpp
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Mime.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/24 14:50:37 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2025/04/24 15:24:08 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <requests/Mime.hpp>
|
||||||
|
|
||||||
|
using namespace webserv;
|
||||||
|
|
||||||
|
std::map<std::string, std::string> http::Mime::initMimeTypes() {
|
||||||
|
std::map<std::string, std::string> types;
|
||||||
|
|
||||||
|
types["aac"] = "audio/aac";
|
||||||
|
types["abw"] = "application/x-abiword";
|
||||||
|
types["apng"] = "image/apng";
|
||||||
|
types["arc"] = "application/x-freearc";
|
||||||
|
types["avif"] = "image/avif";
|
||||||
|
types["avi"] = "video/x-msvideo";
|
||||||
|
types["azw"] = "application/vnd.amazon.ebook";
|
||||||
|
types["bin"] = "application/octet-stream";
|
||||||
|
types["bmp"] = "image/bmp";
|
||||||
|
types["bz"] = "application/x-bzip";
|
||||||
|
types["bz2"] = "application/x-bzip2";
|
||||||
|
types["cda"] = "application/x-cdf";
|
||||||
|
types["csh"] = "application/x-csh";
|
||||||
|
types["css"] = "text/css";
|
||||||
|
types["csv"] = "text/csv";
|
||||||
|
types["doc"] = "application/msword";
|
||||||
|
types["docx"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||||||
|
types["eot"] = "application/vnd.ms-fontobject";
|
||||||
|
types["epub"] = "application/epub+zip";
|
||||||
|
types["gz"] = "application/gzip";
|
||||||
|
types["gif"] = "image/gif";
|
||||||
|
types["htm"] = "text/html";
|
||||||
|
types["html"] = "text/html";
|
||||||
|
types["ico"] = "image/vnd.microsoft.icon";
|
||||||
|
types["ics"] = "text/calendar";
|
||||||
|
types["jar"] = "application/java-archive";
|
||||||
|
types["jpeg,"] = "image/jpeg";
|
||||||
|
types["js"] = "text/javascript";
|
||||||
|
types["json"] = "application/json";
|
||||||
|
types["jsonld"] = "application/ld+json";
|
||||||
|
types["md"] = "text/plain";
|
||||||
|
types["mid"] = "audio/midi";
|
||||||
|
types["midi"] = "audio/midi";
|
||||||
|
types["mjs"] = "text/javascript";
|
||||||
|
types["mp3"] = "audio/mpeg";
|
||||||
|
types["mp4"] = "video/mp4";
|
||||||
|
types["mpeg"] = "video/mpeg";
|
||||||
|
types["mpkg"] = "application/vnd.apple.installer+xml";
|
||||||
|
types["odp"] = "application/vnd.oasis.opendocument.presentation";
|
||||||
|
types["ods"] = "application/vnd.oasis.opendocument.spreadsheet";
|
||||||
|
types["odt"] = "application/vnd.oasis.opendocument.text";
|
||||||
|
types["oga"] = "audio/ogg";
|
||||||
|
types["ogv"] = "video/ogg";
|
||||||
|
types["ogx"] = "application/ogg";
|
||||||
|
types["opus"] = "audio/ogg";
|
||||||
|
types["otf"] = "font/otf";
|
||||||
|
types["png"] = "image/png";
|
||||||
|
types["pdf"] = "application/pdf";
|
||||||
|
types["php"] = "application/x-httpd-php";
|
||||||
|
types["ppt"] = "application/vnd.ms-powerpoint";
|
||||||
|
types["pptx"] = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
||||||
|
types["rar"] = "application/vnd.rar";
|
||||||
|
types["rtf"] = "application/rtf";
|
||||||
|
types["sh"] = "application/x-sh";
|
||||||
|
types["svg"] = "image/svg+xml";
|
||||||
|
types["tar"] = "application/x-tar";
|
||||||
|
types["tif,"] = "image/tiff";
|
||||||
|
types["ts"] = "video/mp2t";
|
||||||
|
types["ttf"] = "font/ttf";
|
||||||
|
types["txt"] = "text/plain";
|
||||||
|
types["vsd"] = "application/vnd.visio";
|
||||||
|
types["wav"] = "audio/wav";
|
||||||
|
types["weba"] = "audio/webm";
|
||||||
|
types["webm"] = "video/webm";
|
||||||
|
types["webp"] = "image/webp";
|
||||||
|
types["woff"] = "font/woff";
|
||||||
|
types["woff2"] = "font/woff2";
|
||||||
|
types["xhtml"] = "application/xhtml+xml";
|
||||||
|
types["xls"] = "application/vnd.ms-excel";
|
||||||
|
types["xlsx"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||||
|
types["xml"] = "application/xml";
|
||||||
|
types["xul"] = "application/vnd.mozilla.xul+xml";
|
||||||
|
types["zip"] = "application/zip";
|
||||||
|
types["3gp"] = "video/3gpp";
|
||||||
|
types["3g2"] = "video/3gpp2";
|
||||||
|
types["7z"] = "application/x-7z-compressed";
|
||||||
|
types["nix"] = "text/plain";
|
||||||
|
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> http::Mime::mimeTypes = Mime::initMimeTypes();
|
||||||
|
|
||||||
|
std::string http::Mime::getType(const std::string &filename) {
|
||||||
|
size_t dot_pos = filename.find_last_of('.');
|
||||||
|
if (dot_pos == std::string::npos)
|
||||||
|
return "text/plain"; //default
|
||||||
|
|
||||||
|
std::string ext = filename.substr(dot_pos + 1);
|
||||||
|
std::map<std::string, std::string>::const_iterator it = mimeTypes.find(ext);
|
||||||
|
if (it != mimeTypes.end())
|
||||||
|
return it->second;
|
||||||
|
return "application/octet-stream"; //unknown extension so default
|
||||||
|
}
|
Reference in New Issue
Block a user