Crafter.Network/interfaces/Crafter.Network-HTTP.cppm

119 lines
4.5 KiB
C++

/*
Crafter®.Network
Copyright (C) 2026 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 {
// HTTP/3 request as carried over a QUIC bidirectional stream. The four
// pseudo-headers (method/scheme/authority/path) are split out as named
// fields rather than living in the headers map, because RFC 9114 forbids
// them from appearing in the regular header section and this shape makes
// route dispatch and request construction cleaner. `headers` keys are
// expected lowercase; HTTP/3 forbids uppercase characters in field names.
export struct HTTPRequest {
std::string method;
std::string scheme = "https";
std::string authority;
std::string path = "/";
std::unordered_map<std::string, std::string> headers;
std::string body;
};
// HTTP/3 response. `status` is the numeric three-digit code as a string
// (e.g. "200") — HTTP/3 has no reason phrase. `headers` keys are expected
// lowercase.
export struct HTTPResponse {
std::string status = "200";
std::unordered_map<std::string, std::string> headers;
std::string body;
};
export inline HTTPRequest CreateRequestHTTP(std::string method, std::string path, std::string authority) {
HTTPRequest r;
r.method = std::move(method);
r.path = std::move(path);
r.authority = std::move(authority);
return r;
}
export inline HTTPRequest CreateRequestHTTP(std::string method, std::string path, std::string authority,
std::unordered_map<std::string, std::string> headers) {
HTTPRequest r;
r.method = std::move(method);
r.path = std::move(path);
r.authority = std::move(authority);
r.headers = std::move(headers);
return r;
}
export inline HTTPRequest CreateRequestHTTP(std::string method, std::string path, std::string authority,
std::string body) {
HTTPRequest r;
r.method = std::move(method);
r.path = std::move(path);
r.authority = std::move(authority);
r.body = std::move(body);
return r;
}
export inline HTTPRequest CreateRequestHTTP(std::string method, std::string path, std::string authority,
std::unordered_map<std::string, std::string> headers,
std::string body) {
HTTPRequest r;
r.method = std::move(method);
r.path = std::move(path);
r.authority = std::move(authority);
r.headers = std::move(headers);
r.body = std::move(body);
return r;
}
export inline HTTPResponse CreateResponseHTTP(std::string status) {
HTTPResponse r;
r.status = std::move(status);
return r;
}
export inline HTTPResponse CreateResponseHTTP(std::string status,
std::unordered_map<std::string, std::string> headers) {
HTTPResponse r;
r.status = std::move(status);
r.headers = std::move(headers);
return r;
}
export inline HTTPResponse CreateResponseHTTP(std::string status, std::string body) {
HTTPResponse r;
r.status = std::move(status);
r.body = std::move(body);
return r;
}
export inline HTTPResponse CreateResponseHTTP(std::string status,
std::unordered_map<std::string, std::string> headers,
std::string body) {
HTTPResponse r;
r.status = std::move(status);
r.headers = std::move(headers);
r.body = std::move(body);
return r;
}
}