websocket: improve handling of 0-len frames

Write out 9-length frames to client's WRITEFUNCTION
Read 0-length frames from READFUNCTION *if* the function
started a new frame via `curl_ws_start_frame()`.

Fixes #18286
Closes #18332
Reported-by: Andriy Druk
This commit is contained in:
Stefan Eissing 2025-08-18 17:12:35 +02:00 committed by Daniel Stenberg
parent fd2a204c23
commit fa3baabbd8
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
11 changed files with 275 additions and 141 deletions

View file

@ -151,7 +151,7 @@ CURLcode Curl_client_start(struct Curl_easy *data)
CURL_TRC_READ(data, "client start, rewind readers");
while(r) {
result = r->crt->rewind(data, r);
result = r->crt->cntrl(data, r, CURL_CRCNTRL_REWIND);
if(result) {
failf(data, "rewind of client reader '%s' failed: %d",
r->crt->name, result);
@ -543,6 +543,15 @@ CURLcode Curl_creader_read(struct Curl_easy *data,
return reader->crt->do_read(data, reader, buf, blen, nread, eos);
}
void Curl_creader_clear_eos(struct Curl_easy *data,
struct Curl_creader *reader)
{
while(reader) {
(void)reader->crt->cntrl(data, reader, CURL_CRCNTRL_CLEAR_EOS);
reader = reader->next;
}
}
CURLcode Curl_creader_def_init(struct Curl_easy *data,
struct Curl_creader *reader)
{
@ -598,19 +607,13 @@ CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
return CURLE_READ_ERROR;
}
CURLcode Curl_creader_def_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
{
(void)data;
(void)reader;
return CURLE_OK;
}
CURLcode Curl_creader_def_unpause(struct Curl_easy *data,
struct Curl_creader *reader)
CURLcode Curl_creader_def_cntrl(struct Curl_easy *data,
struct Curl_creader *reader,
Curl_creader_cntrl opcode)
{
(void)data;
(void)reader;
(void)opcode;
return CURLE_OK;
}
@ -891,12 +894,24 @@ static CURLcode cr_in_rewind(struct Curl_easy *data,
return CURLE_OK;
}
static CURLcode cr_in_unpause(struct Curl_easy *data,
struct Curl_creader *reader)
static CURLcode cr_in_cntrl(struct Curl_easy *data,
struct Curl_creader *reader,
Curl_creader_cntrl opcode)
{
struct cr_in_ctx *ctx = reader->ctx;
(void)data;
ctx->is_paused = FALSE;
switch(opcode) {
case CURL_CRCNTRL_REWIND:
return cr_in_rewind(data, reader);
case CURL_CRCNTRL_UNPAUSE:
ctx->is_paused = FALSE;
break;
case CURL_CRCNTRL_CLEAR_EOS:
ctx->seen_eos = FALSE;
break;
default:
break;
}
return CURLE_OK;
}
@ -916,8 +931,7 @@ static const struct Curl_crtype cr_in = {
cr_in_needs_rewind,
cr_in_total_length,
cr_in_resume_from,
cr_in_rewind,
cr_in_unpause,
cr_in_cntrl,
cr_in_is_paused,
Curl_creader_def_done,
sizeof(struct cr_in_ctx)
@ -1077,8 +1091,7 @@ static const struct Curl_crtype cr_lc = {
Curl_creader_def_needs_rewind,
cr_lc_total_length,
Curl_creader_def_resume_from,
Curl_creader_def_rewind,
Curl_creader_def_unpause,
Curl_creader_def_cntrl,
Curl_creader_def_is_paused,
Curl_creader_def_done,
sizeof(struct cr_lc_ctx)
@ -1251,8 +1264,7 @@ static const struct Curl_crtype cr_null = {
Curl_creader_def_needs_rewind,
cr_null_total_length,
Curl_creader_def_resume_from,
Curl_creader_def_rewind,
Curl_creader_def_unpause,
Curl_creader_def_cntrl,
Curl_creader_def_is_paused,
Curl_creader_def_done,
sizeof(struct Curl_creader)
@ -1312,12 +1324,19 @@ static bool cr_buf_needs_rewind(struct Curl_easy *data,
return ctx->index > 0;
}
static CURLcode cr_buf_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
static CURLcode cr_buf_cntrl(struct Curl_easy *data,
struct Curl_creader *reader,
Curl_creader_cntrl opcode)
{
struct cr_buf_ctx *ctx = reader->ctx;
(void)data;
ctx->index = 0;
switch(opcode) {
case CURL_CRCNTRL_REWIND:
ctx->index = 0;
break;
default:
break;
}
return CURLE_OK;
}
@ -1360,8 +1379,7 @@ static const struct Curl_crtype cr_buf = {
cr_buf_needs_rewind,
cr_buf_total_length,
cr_buf_resume_from,
cr_buf_rewind,
Curl_creader_def_unpause,
cr_buf_cntrl,
Curl_creader_def_is_paused,
Curl_creader_def_done,
sizeof(struct cr_buf_ctx)
@ -1417,7 +1435,7 @@ CURLcode Curl_creader_unpause(struct Curl_easy *data)
CURLcode result = CURLE_OK;
while(reader) {
result = reader->crt->unpause(data, reader);
result = reader->crt->cntrl(data, reader, CURL_CRCNTRL_UNPAUSE);
if(result)
break;
reader = reader->next;