|
| 1 | +/* Copyright (C) 2026 Sebastiano Merlino; SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | +#include <atomic> |
| 3 | +#include <fstream> |
| 4 | +#include <iostream> |
| 5 | +#include <memory> |
| 6 | +#include <thread> |
| 7 | +#include <httpserver.hpp> |
| 8 | +#include <httpserver/features.hpp> |
| 9 | +#include <httpserver/server/tls_credentials.hpp> |
| 10 | +#include "v3_host_poll.hpp" |
| 11 | + |
| 12 | +namespace srv = httpserver::server; |
| 13 | +namespace http = httpserver::http; |
| 14 | + |
| 15 | +int main(int argc, char** argv) { |
| 16 | + if (!httpserver::query_features().tls_provider) { |
| 17 | + std::cout << "TLS_OFF_H3_REJECTED" << std::endl; return 0; |
| 18 | + } |
| 19 | + if (argc < 3 || argc > 4) { |
| 20 | + std::cerr << "usage: v3_http3 CHAIN_PEM KEY_PEM [managed|external]\n"; return 2; |
| 21 | + } |
| 22 | + const auto read = [](const char* path) { |
| 23 | + std::ifstream input(path); return std::string(std::istreambuf_iterator<char>(input), {}); |
| 24 | + }; |
| 25 | + auto credentials = std::make_shared<srv::tls_credentials>(); |
| 26 | + if (!credentials->replace({{{"a.example", read(argv[1]), read(argv[2]), {"h3", "h2", "http/1.1"}}}, 0}).ok()) return 1; |
| 27 | + srv::server_options options; |
| 28 | + options.concurrency().workers = 2; |
| 29 | + options.loop() = argc == 4 && std::string_view(argv[3]) == "external" ? srv::loop_mode::external : srv::loop_mode::managed; |
| 30 | + options.protocols().enable(http::protocol::http_2); |
| 31 | + options.protocols().enable(http::protocol::http_3); |
| 32 | + options.tls().provider = srv::tls_provider::system_default; |
| 33 | + options.tls().profile = srv::tls_profile::certificates; |
| 34 | + options.add_listener({"127.0.0.1", 0, true, credentials, srv::listener_transport::udp}); |
| 35 | + options.add_listener({"127.0.0.1", 0, true, credentials}); |
| 36 | + srv::native_server server(options); |
| 37 | + if (!server.route(http::method::known(http::method_id::get), "/hello", [](httpserver::exchange& x) -> httpserver::task<void> { |
| 38 | + x.start_response(http::status::from_code(200), {}); |
| 39 | + const std::string body = "hello native h3\n"; |
| 40 | + co_await x.writer().write(std::as_bytes(std::span(body))); |
| 41 | + http::fields trailers; trailers.append("x-end", "native-fin"); |
| 42 | + co_await x.writer().finish(trailers); |
| 43 | + }).ok()) return 1; |
| 44 | + if (!server.route(http::method::known(http::method_id::connect), "/ws/echo", [](httpserver::exchange& x) -> httpserver::task<void> { |
| 45 | + httpserver::ws_upgrade_options upgrade; |
| 46 | + upgrade.subprotocols = {"harness.v1"}; upgrade.require_subprotocol = true; |
| 47 | + upgrade.limits = {8192, 16384, 16384, 4, 4}; |
| 48 | + auto opened = co_await x.upgrade(upgrade); |
| 49 | + if (!opened.status.ok()) { x.respond(opened.rejection_status, opened.rejection_fields); co_return; } |
| 50 | + auto session = std::move(*opened.session); |
| 51 | + httpserver::resume_signal closed; |
| 52 | + session.on_close([&](auto) { closed.signal(); }); |
| 53 | + for (;;) { |
| 54 | + const auto received = co_await session.receive(); |
| 55 | + if (!received.status.ok() || !received.value) { co_await closed.wait(); co_return; } |
| 56 | + for (;;) { |
| 57 | + const auto sent = session.try_send(received.value->kind, received.value->data); |
| 58 | + if (sent.disposition == httpserver::websocket::send_disposition::accepted) break; |
| 59 | + if (!sent.status.ok() || !(co_await session.writable()).ok()) co_return; |
| 60 | + } |
| 61 | + } |
| 62 | + }).ok() || !server.listen().ok()) return 1; |
| 63 | + std::cout << server.get_bound_port(0) << std::endl; |
| 64 | + std::cout << "TCP_PORT " << server.get_bound_port(1) << std::endl; |
| 65 | + std::atomic<bool> stopping{false}; |
| 66 | + auto* driver = server.readiness(); |
| 67 | + std::thread host; |
| 68 | + if (driver) host = std::thread([&] { |
| 69 | + while (!stopping.load()) { |
| 70 | + const auto snapshot = driver->interests(); |
| 71 | + const auto events = v3_host::wait(snapshot, std::chrono::steady_clock::now() + std::chrono::milliseconds(100)); |
| 72 | + if (!driver->dispatch(events, std::chrono::steady_clock::now()).ok()) break; |
| 73 | + } |
| 74 | + }); |
| 75 | + std::string command; |
| 76 | + while (std::getline(std::cin, command) && command != "stop") {} |
| 77 | + server.request_stop(); stopping = true; |
| 78 | + if (host.joinable()) host.join(); |
| 79 | + server.stop(); |
| 80 | +} |
0 commit comments