Crafter.Network/interfaces/Crafter.Network-HTTP.cppm
2025-11-02 15:00:53 +01:00

84 lines
No EOL
3.9 KiB
C++

/*
Crafter®.Network
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network:HTTP;
import std;
namespace Crafter {
export struct HTTPRequest {
std::string method;
std::unordered_map<std::string, std::string> headers;
std::string body;
};
export struct HTTPResponse {
std::string status;
std::unordered_map<std::string, std::string> headers;
std::string body;
};
export constexpr std::string CreateResponseHTTP(std::string status) {
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n\r\n", status);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map<std::string, std::string> headers) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n{}\r\n", status, headersString);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::string body) {
return std::format("HTTP/1.1 {}\r\nContent-Length: {}\r\nConnection: keep-alive\r\n\r\n{}", status, body.size(), body);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map<std::string, std::string> headers, std::string body) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\nContent-Length: {}\r\n{}\r\n{}", status, body.size(), headersString, body);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host) {
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nHost: {}\r\n\r\n", method, route, host);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::unordered_map<std::string, std::string> headers, std::string host) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\nAccept-Encoding: identity\r\nHost: {}\r\n{}\r\n", method, route, host, headersString);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::string body) {
return std::format("{} {} HTTP/1.1\r\nContent-Length: {}\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nHost: {}\r\n\r\n{}", method, route, body.size(), host, body);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::unordered_map<std::string, std::string> headers, std::string body) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: {}\r\nHost: {}\r\nAccept-Encoding: identity\r\n{}\r\n{}", method, route, body.size(), host, headersString, body);
}
}