curl-fuzzer C++ reference
Loading...
Searching...
No Matches
proto_fuzzer::WebSocketMockServer Class Reference

In-process WebSocket peer. More...

#include <websocket_mock_server.h>

Inheritance diagram for proto_fuzzer::WebSocketMockServer:
proto_fuzzer::MockServerBase proto_fuzzer::IncomingDataObserver proto_fuzzer::TlsWebSocketMockServer

Public Member Functions

 WebSocketMockServer ()
 Construct an idle WebSocketMockServer with no queued frames.
 ~WebSocketMockServer () override
 Default destructor; the owned MockConnection (if any) cleans up its socketpair.
void ObserveState () override
 Retain protocol outcomes in coverage and introspector builds.
void Install (CURL *easy) override
 Install the shared socket trampolines via the base, then layer a WebSocket-aware WRITEFUNCTION / HEADERFUNCTION on top.
void SetFrames (std::vector< std::string > frames)
 Queue RFC 6455 wire-byte chunks to emit once the handshake has completed.
void SetManualDelivery (bool manual)
 Toggle streaming (false, default) vs manual (true) chunk delivery.
bool manual_delivery () const
bool TryAdvanceHandshake (bool *made_progress=nullptr)
 Drive the WebSocket opening handshake: read whatever curl has written so far, and once we've seen the end of the request headers, reply with a valid 101 Switching Protocols.
bool handshake_sent () const
bool DeliverNextChunk ()
 Push the next queued frame when curl is ready.
bool has_more_chunks () const
std::size_t remaining_chunks () const
const std::string & PeekChunk (std::size_t index) const
 Access a pending chunk without consuming it.
void ConsumeChunk ()
 Advance the pending-chunk cursor by one. No-op when no chunks remain.
bool PushRawBytes (const unsigned char *data, std::size_t size)
 Push raw bytes onto the server fd.
bool ws_probe_fired () const
 Return the one-shot probe gate.
void MarkWsProbeFired ()
 Flip the one-shot gate closed.
CURL * easy_handle () const
std::size_t manual_received_bytes () const
Public Member Functions inherited from proto_fuzzer::MockServerBase
virtual ~MockServerBase ()
 Out-of-line destructor so MockConnection can stay forward-declared in the base header (its complete type is only needed where unique_ptr is instantiated for destruction).
virtual void ConfigureRequestData (ScenarioRequestData *request_data)
 Bind protocol-specific work to the request-data callbacks after their per-scenario state has been constructed.
CURLcode DriveScenario (CURL *easy, const curl::fuzzer::proto::Scenario &scenario)
 Run 'scenario' to completion on 'easy': allocate a multi, attach 'easy', delegate protocol-specific work to RunLoop, consume its completion message, and clean up.
virtual CURLcode DriveEasyScenario (CURL *easy, const curl::fuzzer::proto::Scenario &scenario, bool use_events=false)
 Run through the public easy entrypoint when a protocol mock can preload all peer work before curl takes control.
virtual ConnectOnlyRunStats DriveConnectOnlyScenario (CURL *easy, const curl::fuzzer::proto::Scenario &scenario)
 Establish a CONNECT_ONLY transport, then perform bounded direct I/O.
void SetMultiObserver (MultiObserver observer)
 Register an observer invoked with the live multi handle after 'easy' is attached and before the drive loop starts.
MockConnection * connection ()

Protected Member Functions

virtual std::unique_ptr< MockConnection > CreateConnection ()
 Construct the transport used for the WebSocket exchange.
virtual std::size_t DrainHandshakeData ()
 Consume the plaintext Upgrade request without applying DrainIncoming's backpressure limit.
virtual void FlushTransport ()
 Flush application writes when the carrier queues them.
void ObserveIncomingData (const unsigned char *data, std::size_t size) override
 Collect application bytes consumed by the active transport.
curl_socket_t HandleOpenSocket (curlsocktype purpose=CURLSOCKTYPE_IPCXN, struct curl_sockaddr *address=nullptr) override
 Called by the OPENSOCKETFUNCTION trampoline in the base class.
void RunLoop (CURLM *multi, CURL *easy, const curl::fuzzer::proto::Scenario &scenario) override
 Seed the mock from the scenario, then run the perform loop.
Protected Member Functions inherited from proto_fuzzer::MockServerBase
 MockServerBase (MultiDrivePolicy drive_policy=MultiDrivePolicy::kPerform)
 Construct an empty base instance with a fixed multi-drive policy.
virtual void HandleDetachedMulti (CurlMultiPtr multi)
 Dispose of or retain the multi after its easy handle has been removed.
virtual SocketSetupDisposition GetSocketSetupDisposition (curl_socket_t curlfd, curlsocktype purpose) const
 Describe the socket returned by HandleOpenSocket without issuing native descriptor queries.
MultiSocketDriver * multi_socket_driver ()
 Expose the callback state installed for the current socket-action drive.
std::size_t additional_handle_cleanup_count () const
void ResumeResponseIfRequested (CURL *easy)
 Resume receive-side callback output at one bounded drive boundary when the dedicated API plan requested it.
void ApplyPendingBackpressure ()
 Apply the BackpressureConfig cached by DriveScenario to the new connection_.

Additional Inherited Members

Public Types inherited from proto_fuzzer::MockServerBase
using MultiObserver = std::function<void(CURLM*)>
 Callback type used to publish the driving multi handle to observers.
Static Protected Member Functions inherited from proto_fuzzer::MockServerBase
static int WaitOnMultiFdset (CURLM *multi, CURLMcode *rc)
 Wait on curl's fdset with a short timeout.
static void ProbeMultiPollset (CURLM *multi)
 Ask curl to construct and inspect its connection-filter pollset without waiting.
static bool MultiPollsetWantsWrite (CURLM *multi, curl_socket_t socket)
 Inspect one socket's current writable interest through curl's public multi fdset API.
static bool UsesTimedDrive (const curl::fuzzer::proto::Scenario &scenario)
 Treat a non-default BackpressureConfig as an explicit request for the slower, timed drive policy.
Protected Attributes inherited from proto_fuzzer::MockServerBase
std::unique_ptr< MockConnection > connection_
 The per-scenario MockConnection, lazily created by HandleOpenSocket().
int pending_recv_buf_bytes_
 Cached SO_RCVBUF/SO_SNDBUF setting in bytes.
std::size_t pending_drain_limit_
 Cached per-call DrainIncoming byte budget.
MultiSocketDriver * multi_socket_driver_
 Non-owning pointer into DriveScenario's stack.
std::size_t additional_handle_cleanup_count_
 Number of application-created handles, excluding the caller's parent, successfully removed from the current multi and cleaned up.
bool resume_response_
 True only for an API-plan multi drive with its one-shot write callback.
MultiObserver multi_observer_
 Observer receiving the live multi handle at the start of a drive.
Static Protected Attributes inherited from proto_fuzzer::MockServerBase
static constexpr int kMaxDriveIterations = 512
 Hard operation budget for one scenario.
static constexpr int kMaxIdleIterations = 8
 Consecutive no-progress budget for ordinary scenarios.
static constexpr int kMaxTimedIdleIterations = 256
 Explicit backpressure scenarios get a larger idle budget and may use the short timed wait above.

Detailed Description

In-process WebSocket peer.

Dynamically generates a 101 response against curl's Upgrade request, then either pushes queued frame bytes via the drive loop (streaming mode) or, for CURLOPT_CONNECT_ONLY=2L, hands them to the caller-driven manual path exercised from DriveScenario.

Definition at line 35 of file websocket_mock_server.h.

Constructor & Destructor Documentation

◆ WebSocketMockServer()

proto_fuzzer::WebSocketMockServer::WebSocketMockServer ( )

Construct an idle WebSocketMockServer with no queued frames.

Install() on the base class and DriveScenario() configure it from a Scenario proto.

Definition at line 194 of file websocket_mock_server.cc.

Member Function Documentation

◆ CreateConnection()

std::unique_ptr< MockConnection > proto_fuzzer::WebSocketMockServer::CreateConnection ( )
protectedvirtual

Construct the transport used for the WebSocket exchange.

Secure WebSocket peers override this factory while retaining the same Upgrade and frame state machine.

Returns
a new plaintext socketpair connection.

Reimplemented in proto_fuzzer::TlsWebSocketMockServer.

Definition at line 281 of file websocket_mock_server.cc.

◆ DeliverNextChunk()

bool proto_fuzzer::WebSocketMockServer::DeliverNextChunk ( )

Push the next queued frame when curl is ready.

Used in streaming mode; the drive loop calls this after the handshake has been sent. Shuts the write side once the last chunk is delivered.

Returns
true when a chunk was consumed from the script.

Definition at line 359 of file websocket_mock_server.cc.

◆ DrainHandshakeData()

std::size_t proto_fuzzer::WebSocketMockServer::DrainHandshakeData ( )
protectedvirtual

Consume the plaintext Upgrade request without applying DrainIncoming's backpressure limit.

This preserves the historical WS and compatibility behavior; layered transports override the hook to perform their decoding.

Returns
Number of request bytes appended during this call.

Reimplemented in proto_fuzzer::TlsWebSocketMockServer.

Definition at line 287 of file websocket_mock_server.cc.

◆ easy_handle()

CURL * proto_fuzzer::WebSocketMockServer::easy_handle ( ) const
Returns
the curl easy handle cached by Install() for the write callback.

Definition at line 232 of file websocket_mock_server.cc.

◆ FlushTransport()

void proto_fuzzer::WebSocketMockServer::FlushTransport ( )
protectedvirtual

Flush application writes when the carrier queues them.

Plain socket writes are synchronous, so the historical transport has nothing to do here.

Reimplemented in proto_fuzzer::TlsWebSocketMockServer.

Definition at line 298 of file websocket_mock_server.cc.

◆ HandleOpenSocket()

curl_socket_t proto_fuzzer::WebSocketMockServer::HandleOpenSocket ( curlsocktype purpose = CURLSOCKTYPE_IPCXN,
struct curl_sockaddr * address = nullptr )
overrideprotectedvirtual

Called by the OPENSOCKETFUNCTION trampoline in the base class.

Creates the MockConnection but does NOT write anything — the handshake is driven later by TryAdvanceHandshake().

Parameters
purposeSocket role requested by curl; this peer accepts one outbound WebSocket transport only.
addressIntended destination retained by curl; the already-connected socketpair leaves it unchanged.
Returns
the client-side fd to hand to libcurl, or CURL_SOCKET_BAD on failure.

Implements proto_fuzzer::MockServerBase.

Definition at line 320 of file websocket_mock_server.cc.

◆ handshake_sent()

bool proto_fuzzer::WebSocketMockServer::handshake_sent ( ) const
Returns
true once a 101 Switching Protocols response has been written.

Definition at line 255 of file websocket_mock_server.cc.

◆ has_more_chunks()

bool proto_fuzzer::WebSocketMockServer::has_more_chunks ( ) const
Returns
true if at least one frame chunk has not yet been sent.

Definition at line 258 of file websocket_mock_server.cc.

◆ Install()

void proto_fuzzer::WebSocketMockServer::Install ( CURL * easy)
overridevirtual

Install the shared socket trampolines via the base, then layer a WebSocket-aware WRITEFUNCTION / HEADERFUNCTION on top.

These call curl_ws_meta inside the callback to reach its Curl_is_in_callback-guarded branch. WRITEDATA points to this server so the callback can consult the easy handle and the one-shot probe flag.

Parameters
easyThe curl easy handle to configure.

Reimplemented from proto_fuzzer::MockServerBase.

Definition at line 211 of file websocket_mock_server.cc.

◆ manual_delivery()

bool proto_fuzzer::WebSocketMockServer::manual_delivery ( ) const
Returns
true if chunks are caller-driven rather than drive-loop-driven.

Definition at line 252 of file websocket_mock_server.cc.

◆ manual_received_bytes()

std::size_t proto_fuzzer::WebSocketMockServer::manual_received_bytes ( ) const
Returns
Application payload bytes returned by curl_ws_recv during the latest manual-drive tail.

Definition at line 236 of file websocket_mock_server.cc.

◆ MarkWsProbeFired()

void proto_fuzzer::WebSocketMockServer::MarkWsProbeFired ( )

Flip the one-shot gate closed.

Called from the write callback before it invokes curl_ws_send, so subsequent callback entries skip the probe.

Definition at line 229 of file websocket_mock_server.cc.

◆ ObserveIncomingData()

void proto_fuzzer::WebSocketMockServer::ObserveIncomingData ( const unsigned char * data,
std::size_t size )
overrideprotectedvirtual

Collect application bytes consumed by the active transport.

Plaintext connections report socket bytes directly; TLS connections report them only after record decryption, keeping the WebSocket handshake transport-agnostic.

Parameters
dataFirst byte of one application-data fragment.
sizeNumber of bytes available at data.

Implements proto_fuzzer::IncomingDataObserver.

Definition at line 305 of file websocket_mock_server.cc.

◆ PeekChunk()

const std::string & proto_fuzzer::WebSocketMockServer::PeekChunk ( std::size_t index) const

Access a pending chunk without consuming it.

Parameters
indexOffset from the next-pending cursor.
Returns
reference to the chunk byte string.

Definition at line 268 of file websocket_mock_server.cc.

◆ PushRawBytes()

bool proto_fuzzer::WebSocketMockServer::PushRawBytes ( const unsigned char * data,
std::size_t size )

Push raw bytes onto the server fd.

Used by the manual-drive path to feed frame bytes directly into curl without any mock-side framing.

Parameters
dataBuffer to send.
sizeNumber of bytes in 'data'.
Returns
false on short or failed write.

Definition at line 343 of file websocket_mock_server.cc.

◆ remaining_chunks()

std::size_t proto_fuzzer::WebSocketMockServer::remaining_chunks ( ) const
Returns
the number of queued chunks not yet consumed.

Definition at line 261 of file websocket_mock_server.cc.

◆ RunLoop()

void proto_fuzzer::WebSocketMockServer::RunLoop ( CURLM * multi,
CURL * easy,
const curl::fuzzer::proto::Scenario & scenario )
overrideprotectedvirtual

Seed the mock from the scenario, then run the perform loop.

Drives the 101 handshake on every iteration; in streaming mode also pushes queued frame chunks as curl becomes readable. In manual mode (CURLOPT_CONNECT_ONLY=2) chunks are left alone during the loop — once the loop returns, this method pushes them as raw bytes and exercises curl_ws_recv / curl_ws_send against a small flag matrix.

Parameters
multicaller-owned multi; 'easy' is already added.
easythe curl easy handle attached to this mock.
scenariosource of the frame chunks and CONNECT_ONLY setting.

Implements proto_fuzzer::MockServerBase.

Definition at line 432 of file websocket_mock_server.cc.

◆ SetFrames()

void proto_fuzzer::WebSocketMockServer::SetFrames ( std::vector< std::string > frames)

Queue RFC 6455 wire-byte chunks to emit once the handshake has completed.

Resets the next-chunk cursor.

Parameters
framesOrdered list of chunk byte strings.

Definition at line 241 of file websocket_mock_server.cc.

◆ SetManualDelivery()

void proto_fuzzer::WebSocketMockServer::SetManualDelivery ( bool manual)

Toggle streaming (false, default) vs manual (true) chunk delivery.

Parameters
manualWhether to suppress automatic chunk pushing by the drive loop.

Definition at line 249 of file websocket_mock_server.cc.

◆ TryAdvanceHandshake()

bool proto_fuzzer::WebSocketMockServer::TryAdvanceHandshake ( bool * made_progress = nullptr)

Drive the WebSocket opening handshake: read whatever curl has written so far, and once we've seen the end of the request headers, reply with a valid 101 Switching Protocols.

Parameters
made_progressOptional result set when transport or handshake state advanced during this call.
Returns
true once the 101 response has been written (idempotent afterwards).

Definition at line 382 of file websocket_mock_server.cc.

◆ ws_probe_fired()

bool proto_fuzzer::WebSocketMockServer::ws_probe_fired ( ) const

Return the one-shot probe gate.

The write callback calls curl_ws_send once per scenario to reach ws_send_raw_blocking; firing it per callback would wedge each frame under backpressure. Public so the free-function callback can read and flip it; reset by Install().

Returns
true once the one-shot WS probe has fired for this scenario.

Definition at line 225 of file websocket_mock_server.cc.


The documentation for this class was generated from the following files: