Skip to content

websocket: support CURLOPT_READFUNCTION#17683

Closed
icing wants to merge 3 commits intocurl:masterfrom
icing:ws-perform-mode
Closed

websocket: support CURLOPT_READFUNCTION#17683
icing wants to merge 3 commits intocurl:masterfrom
icing:ws-perform-mode

Conversation

@icing
Copy link
Copy Markdown
Contributor

@icing icing commented Jun 20, 2025

Add support for CURLOPT_READFUNCTION with WebSocket urls when not in connect-only mode, e.g. when using curl_multi_perform.

Install the callback function and set CURLOPT_UPLOAD. Return CURL_READFUNC_PAUSE when having nothing more to send and unpause the transfer when more data is ready.

This will send the read bytes in a WebSocket BINARY frame. For other frame types, add a new API function:

/*
 * NAME curl_ws_start_frame()
 *
 * DESCRIPTION
 *
 * Buffers a websocket frame header with the given flags and length.
 * Errors when a previous frame is not complete, e.g. not all its
 * payload has been added.
 */
CURL_EXTERN CURLcode curl_ws_start_frame(CURL *curl,
                                         unsigned int flags,
                                         curl_off_t frame_len);

Add an example using CURLOPT_READFUNCTION and curl_ws_start_frame() in docs/examples/websocket-updown.c.

Add support for this mode in the pytest "ws_data" client and have all tests run in 'curl_ws_send/recv' and 'perform' mode as well.

This allows on the command line to "talk" to an echo server:

> curl --no-progress-meter  -T . wss://echo.websocket.org

so stdin and stdout are the websocket connection with auto-en- and decoding. For libcurl users, they can multi handle many such websocket transfers and have READFUNCTION and WRITEFUNCTION to do with the data whatever they like.

Update: when CURLWS_RAW_MODE is set for the transfer, the websocket encoding client reader is not installed and the CURLOPT_READFUNCTION can add raw websocket frames.

@github-actions github-actions bot added the tests label Jun 20, 2025
@icing icing requested a review from bagder June 20, 2025 14:11
@icing icing mentioned this pull request Jul 2, 2025
@icing icing force-pushed the ws-perform-mode branch from 55e4b38 to e06439c Compare July 7, 2025 11:45
@icing icing added the feature-window A merge of this requires an open feature window label Jul 7, 2025
@icing icing force-pushed the ws-perform-mode branch from c6afa37 to 8ade59a Compare July 8, 2025 07:17
@bagder
Copy link
Copy Markdown
Member

bagder commented Jul 27, 2025

This branch has conflicts that must be resolved

@icing icing force-pushed the ws-perform-mode branch from 8ade59a to c524fd2 Compare July 28, 2025 07:10
@bagder
Copy link
Copy Markdown
Member

bagder commented Jul 30, 2025

this branch has conflicts ...

icing added 2 commits July 31, 2025 10:51
    Add support for CURLOPT_READFUNCTION with WebSocket urls when
    *not* in connect-only mode, e.g. when using curl_multi_perform.

    Install the callback function and set CURLOPT_UPLOAD. Return
    CURL_READFUNC_PAUSE when having nothing more to send and unpause
    the transfer when more data is ready.

    This will send the read bytes in a WebSocket BINARY frame.

    Add support for this mode in the pytest "ws_data" client and
    have all tests run in 'curl_ws_send/recv' and 'peform' mode
    as well.

    Add `curl_ws_start_frame()`. Document, cover in libcurl-ws.md
    and explain the READFUNCTION mode for websockets.

    Add example `websocket-updown` for this.
@icing icing force-pushed the ws-perform-mode branch from 21291be to e2bbfc4 Compare July 31, 2025 08:51
@icing icing requested a review from bagder July 31, 2025 09:50
@icing
Copy link
Copy Markdown
Contributor Author

icing commented Aug 2, 2025

I wonder if we should add a --ws-raw option to the command line, for when people want to use a wss:// pipe and manage the websocket protocol frames themselves. It would only work for a single url, but might be handy.

Update: on second thought, let's do this if someone really needs it.

@bagder bagder closed this in 37cecfc Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature-window A merge of this requires an open feature window libcurl API tests WebSocket

Development

Successfully merging this pull request may close these issues.

2 participants