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

102 lines
3.8 KiB
Text
Raw Normal View History

2026-07-22 17:55:45 +02:00
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2025-11-02 15:00:53 +01:00
export module Crafter.Network:HTTP;
import std;
namespace Crafter {
2026-05-07 00:06:44 +02:00
// 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.
2025-11-02 15:00:53 +01:00
export struct HTTPRequest {
std::string method;
2026-05-07 00:06:44 +02:00
std::string scheme = "https";
std::string authority;
std::string path = "/";
2025-11-02 15:00:53 +01:00
std::unordered_map<std::string, std::string> headers;
std::string body;
};
2026-05-07 00:06:44 +02:00
// 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.
2025-11-02 15:00:53 +01:00
export struct HTTPResponse {
2026-05-07 00:06:44 +02:00
std::string status = "200";
2025-11-02 15:00:53 +01:00
std::unordered_map<std::string, std::string> headers;
std::string body;
};
2026-05-07 00:06:44 +02:00
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;
2025-11-02 15:00:53 +01:00
}
2026-05-07 00:06:44 +02:00
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;
}
2025-11-02 15:00:53 +01:00
2026-05-07 00:06:44 +02:00
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;
}
2025-11-02 15:00:53 +01:00
2026-05-07 00:06:44 +02:00
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;
}
2025-11-02 15:00:53 +01:00
2026-05-07 00:06:44 +02:00
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;
}
}