Files
moloapp/src/clients/transports/tls.h
T

73 lines
2.7 KiB
C++

#pragma once
// TLS session interface. The only implementation shipped in this part is the
// marked wolfSSL stub (tls_stub.cpp): wolfSSL is NOT installed in this
// environment, so HTTPS URLs fail loud through TlsStubError and never
// silently downgrade to cleartext. Part 01/03 provides the real session.
#include <cstddef>
#include <stdexcept>
#include <string>
namespace molo::clients::transports {
class TlsError : public std::runtime_error {
public:
explicit TlsError(const std::string& msg)
: std::runtime_error("molo::clients::transports::Tls: " + msg) {}
};
// A socket read or write timed out (not a dropped connection). Subclass of
// TlsError so existing handlers keep working.
class TlsTimeoutError : public TlsError {
public:
explicit TlsTimeoutError(const std::string& msg) : TlsError(msg) {}
};
// Why the last recv_some() returned. kUnknown: the backend does not say.
// kClosed covers a TLS close_notify and a TCP drop without one.
enum class TlsReadStatus { kUnknown, kData, kClosed };
// MARKED STUB: thrown whenever a real TLS session is required but only the
// stub backend is linked (wolfSSL missing).
class TlsStubError : public TlsError {
public:
explicit TlsStubError(const std::string& what)
: TlsError("STUB backend, wolfSSL not installed: " + what) {}
};
class H2StubError : public std::runtime_error {
public:
explicit H2StubError(const std::string& what)
: std::runtime_error("molo::clients::transports::H2 STUB: " + what) {}
};
// Minimal handshake-plus-record interface the HTTP transport needs.
class ITlsSession {
public:
virtual ~ITlsSession() = default;
virtual void handshake(int fd, const std::string& hostname,
bool want_h2) = 0;
virtual void send_all(const char* data, std::size_t size) = 0;
virtual std::size_t recv_some(char* out, std::size_t max_size) = 0;
// A socket read timeout throws TlsTimeoutError; a return of 0 is a closed
// connection when last_read_status() says kClosed.
virtual TlsReadStatus last_read_status() const { return TlsReadStatus::kUnknown; }
virtual std::string negotiated_protocol() const = 0; // "h2", "http/1.1", ""
virtual void shutdown() = 0;
virtual const char* backend() const = 0;
};
// MARKED STUB backend. Every operation throws TlsStubError.
class StubTlsSession : public ITlsSession {
public:
void handshake(int fd, const std::string& hostname, bool want_h2) override;
void send_all(const char* data, std::size_t size) override;
std::size_t recv_some(char* out, std::size_t max_size) override;
std::string negotiated_protocol() const override;
void shutdown() override;
const char* backend() const override { return "stub-no-wolfssl"; }
};
} // namespace molo::clients::transports