Crafter.Network/interfaces/Crafter.Network-HTTP.cppm
2026-07-22 17:55:45 +02:00

102 lines
3.8 KiB
C++

//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
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;
}
}