Initial commit
This commit is contained in:
76
src/http/mime.cpp
Normal file
76
src/http/mime.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "../logger.hpp"
|
||||
#include "mime.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Http
|
||||
{
|
||||
/* Existing MIME types
|
||||
text
|
||||
image
|
||||
audio
|
||||
video
|
||||
application
|
||||
multipart
|
||||
*/
|
||||
|
||||
std::string GetMimeType(std::filesystem::path const & path)
|
||||
{
|
||||
auto const extension = path.extension();
|
||||
return GetMimeType(extension.string());
|
||||
}
|
||||
|
||||
std::string GetMimeType(std::string const & extension)
|
||||
{
|
||||
static std::unordered_map<std::string, FileType> fileExtensionMap
|
||||
{
|
||||
{".html", FileType::HTML},
|
||||
{".htm", FileType::HTML},
|
||||
{".css", FileType::CSS},
|
||||
{".js", FileType::JS},
|
||||
{".jpg", FileType::JPG},
|
||||
{".jpeg", FileType::JPG},
|
||||
{".png", FileType::PNG},
|
||||
{".mp3", FileType::MP3},
|
||||
{".mp4", FileType::MP4},
|
||||
};
|
||||
|
||||
auto const findResult = fileExtensionMap.find(extension);
|
||||
if (findResult == fileExtensionMap.end())
|
||||
{
|
||||
return GetMimeType(FileType::UNKNOWN);
|
||||
}
|
||||
|
||||
return GetMimeType(findResult->second);
|
||||
}
|
||||
|
||||
std::string GetMimeType(FileType const fileType)
|
||||
{
|
||||
switch(fileType)
|
||||
{
|
||||
default:
|
||||
case FileType::UNKNOWN:
|
||||
return "text/plain";
|
||||
|
||||
case FileType::HTML:
|
||||
return "text/html";
|
||||
|
||||
case FileType::CSS:
|
||||
return "text/css";
|
||||
|
||||
case FileType::JS:
|
||||
return "application/javascript";
|
||||
|
||||
case FileType::JPG:
|
||||
return "image/jpeg";
|
||||
|
||||
case FileType::PNG:
|
||||
return "image/png";
|
||||
|
||||
case FileType::MP3:
|
||||
return "audio/mpeg3";
|
||||
|
||||
case FileType::MP4:
|
||||
return "video/mp4";
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/http/mime.hpp
Normal file
23
src/http/mime.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Http
|
||||
{
|
||||
enum class FileType
|
||||
{
|
||||
UNKNOWN = -1,
|
||||
HTML,
|
||||
CSS,
|
||||
JS,
|
||||
PNG,
|
||||
JPG,
|
||||
MP3,
|
||||
MP4
|
||||
};
|
||||
|
||||
std::string GetMimeType(std::filesystem::path const & path);
|
||||
std::string GetMimeType(std::string const & extension);
|
||||
std::string GetMimeType(FileType const fileType);
|
||||
}
|
||||
38
src/http/request.cpp
Normal file
38
src/http/request.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "request.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace Http
|
||||
{
|
||||
template<class T>
|
||||
T ToEnum(std::string const & str, std::vector<std::string> const & enumStrings)
|
||||
{
|
||||
for(unsigned i = 0; i < enumStrings.size(); ++i)
|
||||
{
|
||||
if (str.compare(enumStrings[i]) == 0)
|
||||
{
|
||||
return static_cast<T>(i);
|
||||
}
|
||||
}
|
||||
|
||||
return static_cast<T>(-1);
|
||||
}
|
||||
|
||||
Http::Request Request::Deserialize(std::vector<char> const & bytes)
|
||||
{
|
||||
// TODO serialize more than just the start
|
||||
Http::Request request;
|
||||
|
||||
std::stringstream ss(std::string(bytes.begin(), bytes.end()));
|
||||
|
||||
std::string requestTypeString;
|
||||
ss >> requestTypeString;
|
||||
request.requestType = ToEnum<HttpRequest::Type>(requestTypeString, HttpRequest::typeStrings);
|
||||
|
||||
ss >> request.path;
|
||||
|
||||
std::string httpProtocolString;
|
||||
ss >> httpProtocolString;
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
13
src/http/request.hpp
Normal file
13
src/http/request.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "../constants/httprequest.hpp"
|
||||
|
||||
namespace Http
|
||||
{
|
||||
struct Request
|
||||
{
|
||||
HttpRequest::Type requestType;
|
||||
std::string path;
|
||||
|
||||
static Request Deserialize(std::vector<char> const & bytes);
|
||||
};
|
||||
}
|
||||
37
src/http/response.cpp
Normal file
37
src/http/response.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "response.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace Http
|
||||
{
|
||||
std::vector<char> Response::Serialize()
|
||||
{
|
||||
// TODO implement headers properly
|
||||
std::stringstream ss;
|
||||
ss << "HTTP/1.1";
|
||||
ss << ' ' << HttpResponse::codeValues[static_cast<int>(code)];
|
||||
ss << ' ' << HttpResponse::codeStrings[static_cast<int>(code)];
|
||||
ss << "\r\n";
|
||||
ss << "Server: http-server/0.1\r\n";
|
||||
|
||||
if (contentType.size() > 0)
|
||||
{
|
||||
ss << "Content-Type: ";
|
||||
ss << contentType << "\r\n";
|
||||
}
|
||||
|
||||
ss << "\r\n";
|
||||
|
||||
auto header = ss.str();
|
||||
std::vector<char> buffer (header.begin(), header.end());
|
||||
buffer.insert(buffer.end(), content.begin(), content.end());
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Response::Response()
|
||||
: code(HttpResponse::Code::UNKNOWN),
|
||||
contentType(),
|
||||
content()
|
||||
{
|
||||
}
|
||||
}
|
||||
16
src/http/response.hpp
Normal file
16
src/http/response.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "../constants/httpresponse.hpp"
|
||||
|
||||
namespace Http
|
||||
{
|
||||
struct Response
|
||||
{
|
||||
HttpResponse::Code code;
|
||||
std::string contentType;
|
||||
std::vector<char> content;
|
||||
|
||||
std::vector<char> Serialize();
|
||||
|
||||
Response();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user