Coverage Report

Created: 2026-12-01 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/cf-recvbuf.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_WEBSOCKETS
27
/* only used for this protocol, so far */
28
29
#include "urldata.h"
30
#include "bufq.h"
31
#include "cfilters.h"
32
#include "cf-recvbuf.h"
33
#include "curl_trc.h"
34
35
1.19k
#define CURL_CF_RECVBUF_CHUNK     (16 * 1024)
36
37
struct cf_recvbuf_ctx {
38
  struct bufq recvbuf;
39
};
40
41
static void cf_recvbuf_destroy(struct Curl_cfilter *cf,
42
                               struct Curl_easy *data)
43
592
{
44
592
  struct cf_recvbuf_ctx *ctx = cf->ctx;
45
592
  (void)data;
46
592
  if(ctx) {
47
592
    Curl_bufq_free(&ctx->recvbuf);
48
592
    curlx_free(ctx);
49
592
  }
50
592
}
51
52
static CURLcode cf_recvbuf_recv(struct Curl_cfilter *cf,
53
                                struct Curl_easy *data,
54
                                char *buf, size_t len,
55
                                size_t *pnread)
56
3.36k
{
57
3.36k
  struct cf_recvbuf_ctx *ctx = cf->ctx;
58
59
3.36k
  if(!Curl_bufq_is_empty(&ctx->recvbuf)) {
60
591
    return Curl_bufq_cread(&ctx->recvbuf, buf, len, pnread);
61
591
  }
62
63
2.77k
  if(cf->next)
64
2.77k
    return cf->next->cft->do_recv(cf->next, data, buf, len, pnread);
65
0
  *pnread = 0;
66
0
  return CURLE_RECV_ERROR;
67
2.77k
}
68
69
static bool cf_recvbuf_data_pending(struct Curl_cfilter *cf,
70
                                    const struct Curl_easy *data)
71
0
{
72
0
  struct cf_recvbuf_ctx *ctx = cf->ctx;
73
74
0
  if(!Curl_bufq_is_empty(&ctx->recvbuf))
75
0
    return TRUE;
76
77
0
  return cf->next ?
78
0
    cf->next->cft->has_data_pending(cf->next, data) : FALSE;
79
0
}
80
81
struct Curl_cftype Curl_cft_recvbuf = {
82
  "RECVBUF",
83
  0,
84
  CURL_LOG_LVL_NONE,
85
  cf_recvbuf_destroy,
86
  Curl_cf_def_connect,
87
  Curl_cf_def_shutdown,
88
  Curl_cf_def_adjust_pollset,
89
  cf_recvbuf_data_pending,
90
  Curl_cf_def_send,
91
  cf_recvbuf_recv,
92
  Curl_cf_def_cntrl,
93
  Curl_cf_def_conn_is_alive,
94
  Curl_cf_def_conn_keep_alive,
95
  Curl_cf_def_query,
96
};
97
98
static CURLcode cf_recvbuf_create(struct Curl_cfilter **pcf,
99
                                  struct Curl_easy *data,
100
                                  const uint8_t *buf, size_t blen)
101
597
{
102
597
  struct Curl_cfilter *cf = NULL;
103
597
  struct cf_recvbuf_ctx *ctx;
104
597
  CURLcode result = CURLE_OK;
105
597
  size_t nwritten = 0;
106
107
597
  (void)data;
108
597
  ctx = curlx_calloc(1, sizeof(*ctx));
109
597
  if(!ctx) {
110
0
    result = CURLE_OUT_OF_MEMORY;
111
0
    goto out;
112
0
  }
113
597
  Curl_bufq_init2(&ctx->recvbuf, CURL_CF_RECVBUF_CHUNK,
114
597
                  (blen / CURL_CF_RECVBUF_CHUNK) + 1,
115
597
                  (BUFQ_OPT_SOFT_LIMIT | BUFQ_OPT_NO_SPARES));
116
597
  result = Curl_bufq_write(&ctx->recvbuf, buf, blen, &nwritten);
117
597
  if(result)
118
1
    goto out;
119
596
  if(nwritten != blen) {
120
0
    result = CURLE_FAILED_INIT;
121
0
    goto out;
122
0
  }
123
124
596
  result = Curl_cf_create(&cf, &Curl_cft_recvbuf, ctx);
125
596
  if(result)
126
4
    goto out;
127
592
  ctx = NULL;
128
129
597
out:
130
597
  *pcf = result ? NULL : cf;
131
597
  if(ctx) {
132
5
    Curl_bufq_free(&ctx->recvbuf);
133
5
    curlx_free(ctx);
134
5
  }
135
597
  return result;
136
592
}
137
138
CURLcode Curl_cf_recvbuf_add(struct Curl_easy *data,
139
                             const uint8_t *buf, size_t blen)
140
597
{
141
597
  struct connectdata *conn = data->conn;
142
597
  struct Curl_cfilter *cf;
143
597
  CURLcode result = CURLE_OK;
144
145
597
  DEBUGASSERT(data);
146
597
  result = cf_recvbuf_create(&cf, data, buf, blen);
147
597
  if(result)
148
5
    goto out;
149
150
592
  cf->connected = Curl_conn_is_connected(conn);
151
592
  Curl_cfchain_add(&conn->chain, cf);
152
597
out:
153
597
  return result;
154
592
}
155
156
#endif /* !CURL_DISABLE_WEBSOCKETS */