mirror of
https://github.com/curl/curl.git
synced 2026-08-25 21:53:32 +03:00
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()`. TODO: prevent base client reader to get stuck in EOS refs #18286
This commit is contained in:
parent
9a37dd3494
commit
476076e6c2
5 changed files with 100 additions and 47 deletions
74
lib/cw-out.c
74
lib/cw-out.c
|
|
@ -74,6 +74,7 @@
|
|||
typedef enum {
|
||||
CW_OUT_NONE,
|
||||
CW_OUT_BODY,
|
||||
CW_OUT_BODY_0LEN,
|
||||
CW_OUT_HDS
|
||||
} cw_out_type;
|
||||
|
||||
|
|
@ -170,6 +171,7 @@ static void cw_get_writefunc(struct Curl_easy *data, cw_out_type otype,
|
|||
{
|
||||
switch(otype) {
|
||||
case CW_OUT_BODY:
|
||||
case CW_OUT_BODY_0LEN:
|
||||
*pwcb = data->set.fwrite_func;
|
||||
*pwcb_data = data->set.out;
|
||||
*pmax_write = CURL_MAX_WRITE_SIZE;
|
||||
|
|
@ -217,40 +219,50 @@ static CURLcode cw_out_ptr_flush(struct cw_out_ctx *ctx,
|
|||
}
|
||||
|
||||
*pconsumed = 0;
|
||||
while(blen && !ctx->paused) {
|
||||
if(!flush_all && blen < min_write)
|
||||
break;
|
||||
wlen = max_write ? CURLMIN(blen, max_write) : blen;
|
||||
if(otype == CW_OUT_BODY_0LEN) {
|
||||
DEBUGASSERT(!blen);
|
||||
Curl_set_in_callback(data, TRUE);
|
||||
nwritten = wcb((char *)CURL_UNCONST(buf), 1, wlen, wcb_data);
|
||||
nwritten = wcb((char *)CURL_UNCONST(buf), 1, blen, wcb_data);
|
||||
Curl_set_in_callback(data, FALSE);
|
||||
CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu",
|
||||
wlen, (otype == CW_OUT_BODY) ? "body" : "header",
|
||||
nwritten);
|
||||
if(CURL_WRITEFUNC_PAUSE == nwritten) {
|
||||
if(data->conn && data->conn->handler->flags & PROTOPT_NONETWORK) {
|
||||
/* Protocols that work without network cannot be paused. This is
|
||||
actually only FILE:// just now, and it cannot pause since the
|
||||
transfer is not done using the "normal" procedure. */
|
||||
failf(data, "Write callback asked for PAUSE when not supported");
|
||||
CURL_TRC_WRITE(data, "[OUT] wrote %zu BODY bytes -> %zu",
|
||||
blen, nwritten);
|
||||
}
|
||||
else {
|
||||
while(blen && !ctx->paused) {
|
||||
if(!flush_all && blen < min_write)
|
||||
break;
|
||||
wlen = max_write ? CURLMIN(blen, max_write) : blen;
|
||||
Curl_set_in_callback(data, TRUE);
|
||||
nwritten = wcb((char *)CURL_UNCONST(buf), 1, wlen, wcb_data);
|
||||
Curl_set_in_callback(data, FALSE);
|
||||
CURL_TRC_WRITE(data, "[OUT] wrote %zu %s bytes -> %zu",
|
||||
wlen, (otype == CW_OUT_BODY) ? "body" : "header",
|
||||
nwritten);
|
||||
if(CURL_WRITEFUNC_PAUSE == nwritten) {
|
||||
if(data->conn && data->conn->handler->flags & PROTOPT_NONETWORK) {
|
||||
/* Protocols that work without network cannot be paused. This is
|
||||
actually only FILE:// just now, and it cannot pause since the
|
||||
transfer is not done using the "normal" procedure. */
|
||||
failf(data, "Write callback asked for PAUSE when not supported");
|
||||
return CURLE_WRITE_ERROR;
|
||||
}
|
||||
ctx->paused = TRUE;
|
||||
CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client");
|
||||
return Curl_xfer_pause_recv(data, TRUE);
|
||||
}
|
||||
else if(CURL_WRITEFUNC_ERROR == nwritten) {
|
||||
failf(data, "client returned ERROR on write of %zu bytes", wlen);
|
||||
return CURLE_WRITE_ERROR;
|
||||
}
|
||||
ctx->paused = TRUE;
|
||||
CURL_TRC_WRITE(data, "[OUT] PAUSE requested by client");
|
||||
return Curl_xfer_pause_recv(data, TRUE);
|
||||
else if(nwritten != wlen) {
|
||||
failf(data, "Failure writing output to destination, "
|
||||
"passed %zu returned %zd", wlen, nwritten);
|
||||
return CURLE_WRITE_ERROR;
|
||||
}
|
||||
*pconsumed += nwritten;
|
||||
blen -= nwritten;
|
||||
buf += nwritten;
|
||||
}
|
||||
else if(CURL_WRITEFUNC_ERROR == nwritten) {
|
||||
failf(data, "client returned ERROR on write of %zu bytes", wlen);
|
||||
return CURLE_WRITE_ERROR;
|
||||
}
|
||||
else if(nwritten != wlen) {
|
||||
failf(data, "Failure writing output to destination, "
|
||||
"passed %zu returned %zd", wlen, nwritten);
|
||||
return CURLE_WRITE_ERROR;
|
||||
}
|
||||
*pconsumed += nwritten;
|
||||
blen -= nwritten;
|
||||
buf += nwritten;
|
||||
}
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
|
@ -413,7 +425,9 @@ static CURLcode cw_out_write(struct Curl_easy *data,
|
|||
|
||||
if((type & CLIENTWRITE_BODY) ||
|
||||
((type & CLIENTWRITE_HEADER) && data->set.include_header)) {
|
||||
result = cw_out_do_write(ctx, data, CW_OUT_BODY, flush_all, buf, blen);
|
||||
cw_out_type otype = (!blen && (type & CLIENTWRITE_0LEN)) ?
|
||||
CW_OUT_BODY_0LEN : CW_OUT_BODY;
|
||||
result = cw_out_do_write(ctx, data, otype, flush_all, buf, blen);
|
||||
if(result)
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@
|
|||
#define CLIENTWRITE_1XX (1<<5) /* a 1xx response related HEADER */
|
||||
#define CLIENTWRITE_TRAILER (1<<6) /* a trailer HEADER */
|
||||
#define CLIENTWRITE_EOS (1<<7) /* End Of transfer download Stream */
|
||||
#define CLIENTWRITE_0LEN (1<<8) /* write even 0-length buffers */
|
||||
|
||||
/**
|
||||
* Write `len` bytes at `prt` to the client. `type` indicates what
|
||||
|
|
|
|||
29
lib/ws.c
29
lib/ws.c
|
|
@ -474,7 +474,7 @@ static CURLcode ws_dec_read_head(struct ws_decoder *dec,
|
|||
static CURLcode ws_dec_pass_payload(struct ws_decoder *dec,
|
||||
struct Curl_easy *data,
|
||||
struct bufq *inraw,
|
||||
ws_write_payload *write_payload,
|
||||
ws_write_payload *write_cb,
|
||||
void *write_ctx)
|
||||
{
|
||||
const unsigned char *inbuf;
|
||||
|
|
@ -487,9 +487,9 @@ static CURLcode ws_dec_pass_payload(struct ws_decoder *dec,
|
|||
while(remain && Curl_bufq_peek(inraw, &inbuf, &inlen)) {
|
||||
if((curl_off_t)inlen > remain)
|
||||
inlen = (size_t)remain;
|
||||
nwritten = write_payload(inbuf, inlen, dec->frame_age, dec->frame_flags,
|
||||
dec->payload_offset, dec->payload_len,
|
||||
write_ctx, &result);
|
||||
nwritten = write_cb(inbuf, inlen, dec->frame_age, dec->frame_flags,
|
||||
dec->payload_offset, dec->payload_len,
|
||||
write_ctx, &result);
|
||||
if(nwritten < 0)
|
||||
return result;
|
||||
Curl_bufq_skip(inraw, (size_t)nwritten);
|
||||
|
|
@ -505,7 +505,7 @@ static CURLcode ws_dec_pass_payload(struct ws_decoder *dec,
|
|||
static CURLcode ws_dec_pass(struct ws_decoder *dec,
|
||||
struct Curl_easy *data,
|
||||
struct bufq *inraw,
|
||||
ws_write_payload *write_payload,
|
||||
ws_write_payload *write_cb,
|
||||
void *write_ctx)
|
||||
{
|
||||
CURLcode result;
|
||||
|
|
@ -535,8 +535,8 @@ static CURLcode ws_dec_pass(struct ws_decoder *dec,
|
|||
ssize_t nwritten;
|
||||
const unsigned char tmp = '\0';
|
||||
/* special case of a 0 length frame, need to write once */
|
||||
nwritten = write_payload(&tmp, 0, dec->frame_age, dec->frame_flags,
|
||||
0, 0, write_ctx, &result);
|
||||
nwritten = write_cb(&tmp, 0, dec->frame_age, dec->frame_flags,
|
||||
0, 0, write_ctx, &result);
|
||||
if(nwritten < 0)
|
||||
return result;
|
||||
dec->state = WS_DEC_INIT;
|
||||
|
|
@ -544,7 +544,7 @@ static CURLcode ws_dec_pass(struct ws_decoder *dec,
|
|||
}
|
||||
FALLTHROUGH();
|
||||
case WS_DEC_PAYLOAD:
|
||||
result = ws_dec_pass_payload(dec, data, inraw, write_payload, write_ctx);
|
||||
result = ws_dec_pass_payload(dec, data, inraw, write_cb, write_ctx);
|
||||
ws_dec_info(dec, data, "passing");
|
||||
if(result)
|
||||
return result;
|
||||
|
|
@ -631,7 +631,8 @@ static ssize_t ws_cw_dec_next(const unsigned char *buf, size_t buflen,
|
|||
update_meta(ws, frame_age, frame_flags, payload_offset,
|
||||
payload_len, buflen);
|
||||
|
||||
*err = Curl_cwriter_write(data, ctx->next_writer, ctx->cw_type,
|
||||
*err = Curl_cwriter_write(data, ctx->next_writer,
|
||||
(ctx->cw_type | CLIENTWRITE_0LEN),
|
||||
(const char *)buf, buflen);
|
||||
if(*err)
|
||||
return -1;
|
||||
|
|
@ -943,7 +944,11 @@ static CURLcode cr_ws_read(struct Curl_easy *data,
|
|||
return result;
|
||||
ctx->read_eos = eos;
|
||||
|
||||
if(!nread) {
|
||||
if(!Curl_bufq_is_empty(&ws->sendbuf)) {
|
||||
/* client_read started a new frame, we disregard any eos reported */
|
||||
ctx->read_eos = FALSE;
|
||||
}
|
||||
else if(!nread) {
|
||||
/* nothing to convert, return this right away */
|
||||
if(ctx->read_eos)
|
||||
ctx->eos = TRUE;
|
||||
|
|
@ -952,7 +957,7 @@ static CURLcode cr_ws_read(struct Curl_easy *data,
|
|||
goto out;
|
||||
}
|
||||
|
||||
if(!ws->enc.payload_remain) {
|
||||
if(!ws->enc.payload_remain && Curl_bufq_is_empty(&ws->sendbuf)) {
|
||||
/* encode the data as a new BINARY frame */
|
||||
result = ws_enc_write_head(data, &ws->enc, CURLWS_BINARY, nread,
|
||||
&ws->sendbuf);
|
||||
|
|
@ -1732,7 +1737,7 @@ CURL_EXTERN CURLcode curl_ws_start_frame(CURL *d,
|
|||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
|
||||
CURL_TRC_WS(data, "curl_start_frame(flags=%x, frame_len=%" FMT_OFF_T,
|
||||
CURL_TRC_WS(data, "curl_ws_start_frame(flags=%x, frame_len=%" FMT_OFF_T,
|
||||
flags, frame_len);
|
||||
|
||||
if(!data->conn) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue