Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/sendf.h
Line
Count
Source
1
#ifndef HEADER_CURL_SENDF_H
2
#define HEADER_CURL_SENDF_H
3
/***************************************************************************
4
 *                                  _   _ ____  _
5
 *  Project                     ___| | | |  _ \| |
6
 *                             / __| | | | |_) | |
7
 *                            | (__| |_| |  _ <| |___
8
 *                             \___|\___/|_| \_\_____|
9
 *
10
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11
 *
12
 * This software is licensed as described in the file COPYING, which
13
 * you should have received as part of this distribution. The terms
14
 * are also available at https://curl.se/docs/copyright.html.
15
 *
16
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
 * copies of the Software, and permit persons to whom the Software is
18
 * furnished to do so, under the terms of the COPYING file.
19
 *
20
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
 * KIND, either express or implied.
22
 *
23
 * SPDX-License-Identifier: curl
24
 *
25
 ***************************************************************************/
26
#include "curl_setup.h"
27
#include "cw-out.h"
28
29
/**
30
 * Type of data that is being written to the client (application)
31
 * - data written can be either BODY or META data
32
 * - META data is either INFO or HEADER
33
 * - INFO is meta information, e.g. not BODY, that cannot be interpreted
34
 *   as headers of a response. Example FTP/IMAP pingpong answers.
35
 * - HEADER can have additional bits set (more than one)
36
 *   - STATUS special "header", e.g. response status line in HTTP
37
 *   - CONNECT header was received during proxying the connection
38
 *   - 1XX header is part of an intermediate response, e.g. HTTP 1xx code
39
 *   - TRAILER header is trailing response data, e.g. HTTP trailers
40
 * BODY, INFO and HEADER should not be mixed, as this would lead to
41
 * confusion on how to interpret/format/convert the data.
42
 */
43
1.53M
#define CLIENTWRITE_BODY    (1 << 0) /* non-meta information, BODY */
44
467k
#define CLIENTWRITE_INFO    (1 << 1) /* meta information, not a HEADER */
45
1.37M
#define CLIENTWRITE_HEADER  (1 << 2) /* meta information, HEADER */
46
312k
#define CLIENTWRITE_STATUS  (1 << 3) /* a special status HEADER */
47
637k
#define CLIENTWRITE_CONNECT (1 << 4) /* a CONNECT related HEADER */
48
289k
#define CLIENTWRITE_1XX     (1 << 5) /* a 1xx response related HEADER */
49
287k
#define CLIENTWRITE_TRAILER (1 << 6) /* a trailer HEADER */
50
464k
#define CLIENTWRITE_EOS     (1 << 7) /* End Of transfer download Stream */
51
13.5k
#define CLIENTWRITE_0LEN    (1 << 8) /* write even 0-length buffers */
52
53
/* Forward declarations */
54
struct Curl_creader;
55
struct Curl_cwriter;
56
struct Curl_easy;
57
58
/**
59
 * Write `len` bytes at `buf` to the client. `type` indicates what
60
 * kind of data is being written.
61
 */
62
CURLcode Curl_client_write(struct Curl_easy *data, int type, const char *buf,
63
                           size_t len) WARN_UNUSED_RESULT;
64
65
/* Flush buffered data. Set eos when no more input will be written. */
66
CURLcode Curl_client_flush(struct Curl_easy *data, bool eos);
67
68
/**
69
 * Free all resources related to client writing.
70
 */
71
void Curl_client_cleanup(struct Curl_easy *data);
72
73
/**
74
 * Reset readers and writer chains, keep rewind information
75
 * when necessary.
76
 */
77
void Curl_client_reset(struct Curl_easy *data);
78
79
/**
80
 * A new request is starting, perform any ops like rewinding
81
 * previous readers when needed.
82
 */
83
CURLcode Curl_client_start(struct Curl_easy *data);
84
85
/**
86
 * Client Writers - a chain passing transfer BODY data to the client.
87
 * Main application: HTTP and related protocols
88
 * Other uses: monitoring of download progress
89
 *
90
 * Writers in the chain are order by their `phase`. First come all
91
 * writers in CURL_CW_RAW, followed by any in CURL_CW_TRANSFER_DECODE,
92
 * followed by any in CURL_CW_PROTOCOL, etc.
93
 *
94
 * When adding a writer, it is inserted as first in its phase. This means
95
 * the order of adding writers of the same phase matters, but writers for
96
 * different phases may be added in any order.
97
 *
98
 * Writers which do modify the BODY data written are expected to be of
99
 * phases TRANSFER_DECODE or CONTENT_DECODE. The other phases are intended
100
 * for monitoring writers. Which do *not* modify the data but gather
101
 * statistics or update progress reporting.
102
 */
103
104
/* Phase a writer operates at. */
105
typedef enum {
106
  CURL_CW_RAW,  /* raw data written, before any decoding */
107
  CURL_CW_TRANSFER_DECODE, /* remove transfer-encodings */
108
  CURL_CW_PROTOCOL, /* after transfer, but before content decoding */
109
  CURL_CW_BEFORE_DECODE, /* after protocol, but before content decoding */
110
  CURL_CW_CONTENT_DECODE, /* remove content-encodings */
111
  CURL_CW_CLIENT  /* data written to client */
112
} Curl_cwriter_phase;
113
114
/* writer may blow up size of write data, e.g. zip bombs */
115
41.0k
#define CURL_CW_FLAG_BLOWUP     (1U << 0)
116
117
/* Client Writer Type, provides the implementation */
118
struct Curl_cwtype {
119
  const char *name;        /* writer name. */
120
  const char *alias;       /* writer name alias, maybe NULL. */
121
  uint8_t flags;           /* flags for writer behaviour */
122
  CURLcode (*do_init)(struct Curl_easy *data,
123
                      struct Curl_cwriter *writer);
124
  CURLcode (*do_write)(struct Curl_easy *data,
125
                       struct Curl_cwriter *writer, int type,
126
                       const char *buf, size_t nbytes);
127
  CURLcode (*do_flush)(struct Curl_easy *data,
128
                       struct Curl_cwriter *writer, bool eos);
129
  void (*do_close)(struct Curl_easy *data,
130
                   struct Curl_cwriter *writer);
131
  size_t cwriter_size;  /* sizeof() allocated struct Curl_cwriter */
132
};
133
134
/* Client writer instance, allocated on creation.
135
 * `void *ctx` is the pointer from the allocation of
136
 * the `struct Curl_cwriter` itself. This is suitable for "downcasting"
137
 * by the writers implementation. See https://github.com/curl/curl/pull/13054
138
 * for the alignment problems that arise otherwise.
139
 */
140
struct Curl_cwriter {
141
  const struct Curl_cwtype *cwt;  /* type implementation */
142
  struct Curl_cwriter *next;  /* Downstream writer. */
143
  void *ctx;                  /* allocated instance pointer */
144
  Curl_cwriter_phase phase; /* phase at which it operates */
145
};
146
147
/**
148
 * Create a new cwriter instance with given type and phase. Is not
149
 * inserted into the writer chain by this call.
150
 * Invokes `writer->do_init()`.
151
 */
152
CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
153
                             struct Curl_easy *data,
154
                             const struct Curl_cwtype *cwt,
155
                             Curl_cwriter_phase phase);
156
157
/**
158
 * Free a cwriter instance.
159
 * Invokes `writer->do_close()`.
160
 */
161
void Curl_cwriter_free(struct Curl_easy *data,
162
                       struct Curl_cwriter *writer);
163
164
/**
165
 * Count the number of writers installed of the given phase.
166
 */
167
size_t Curl_cwriter_count(struct Curl_easy *data, Curl_cwriter_phase phase);
168
169
/**
170
 * Adds a writer to the transfer's writer chain.
171
 * The writers `phase` determines where in the chain it is inserted.
172
 */
173
CURLcode Curl_cwriter_add(struct Curl_easy *data,
174
                          struct Curl_cwriter *writer);
175
176
/**
177
 * Look up an installed client writer on `data` by its type.
178
 * @return first writer with that type or NULL
179
 */
180
struct Curl_cwriter *Curl_cwriter_get_by_type(struct Curl_easy *data,
181
                                              const struct Curl_cwtype *cwt);
182
183
struct Curl_cwriter *Curl_cwriter_get_by_name(struct Curl_easy *data,
184
                                              const char *name);
185
186
/* Convenience method for calling `writer->do_write()` that
187
 * checks for NULL writer. */
188
#define Curl_cwriter_write(d, w, t, b, n) \
189
1.50M
  ((w) ? (w)->cwt->do_write((d), (w), (t), (b), (n)) : CURLE_WRITE_ERROR)
190
191
#define Curl_cwriter_flush(d, w, eos) \
192
152k
  ((w) ? (w)->cwt->do_flush((d), (w), (eos)) : CURLE_WRITE_ERROR)
193
194
/* TRUE if client writer is paused. */
195
59.5k
#define Curl_cwriter_is_paused(d)     ((bool)(d)->req.writer.paused)
196
197
/**
198
 * Unpause client writer and flush any buffered date to the client.
199
 */
200
CURLcode Curl_cwriter_unpause(struct Curl_easy *data, bool flush);
201
202
/**
203
 * Default implementations for do_init, do_write, do_close that
204
 * do nothing and pass the data through.
205
 */
206
CURLcode Curl_cwriter_def_init(struct Curl_easy *data,
207
                               struct Curl_cwriter *writer);
208
CURLcode Curl_cwriter_def_write(struct Curl_easy *data,
209
                                struct Curl_cwriter *writer, int type,
210
                                const char *buf, size_t nbytes);
211
CURLcode Curl_cwriter_def_flush(struct Curl_easy *data,
212
                                struct Curl_cwriter *writer, bool eos);
213
void Curl_cwriter_def_close(struct Curl_easy *data,
214
                            struct Curl_cwriter *writer);
215
216
typedef enum {
217
  CURL_CRCNTRL_REWIND,
218
  CURL_CRCNTRL_UNPAUSE,
219
  CURL_CRCNTRL_CLEAR_EOS
220
} Curl_creader_cntrl;
221
222
/* Client Reader Type, provides the implementation */
223
struct Curl_crtype {
224
  const char *name;        /* writer name. */
225
  CURLcode (*do_init)(struct Curl_easy *data, struct Curl_creader *reader);
226
  CURLcode (*do_read)(struct Curl_easy *data, struct Curl_creader *reader,
227
                      char *buf, size_t blen, size_t *nread, bool *eos);
228
  void (*do_close)(struct Curl_easy *data, struct Curl_creader *reader);
229
  bool (*needs_rewind)(struct Curl_easy *data, struct Curl_creader *reader);
230
  curl_off_t (*total_length)(struct Curl_easy *data,
231
                             struct Curl_creader *reader);
232
  CURLcode (*resume_from)(struct Curl_easy *data,
233
                          struct Curl_creader *reader, curl_off_t offset);
234
  CURLcode (*cntrl)(struct Curl_easy *data, struct Curl_creader *reader,
235
                    Curl_creader_cntrl opcode);
236
  bool (*is_paused)(struct Curl_easy *data, struct Curl_creader *reader);
237
  void (*done)(struct Curl_easy *data,
238
               struct Curl_creader *reader, int premature);
239
  size_t creader_size;  /* sizeof() allocated struct Curl_creader */
240
};
241
242
/* Phase a reader operates at. */
243
typedef enum {
244
  CURL_CR_NET,  /* data send to the network (connection filters) */
245
  CURL_CR_TRANSFER_ENCODE, /* add transfer-encodings */
246
  CURL_CR_PROTOCOL, /* before transfer, but after content decoding */
247
  CURL_CR_CONTENT_ENCODE, /* add content-encodings */
248
  CURL_CR_CLIENT  /* data read from client */
249
} Curl_creader_phase;
250
251
/* Client reader instance, allocated on creation.
252
 * `void *ctx` is the pointer from the allocation of
253
 * the `struct Curl_cwriter` itself. This is suitable for "downcasting"
254
 * by the writers implementation. See https://github.com/curl/curl/pull/13054
255
 * for the alignment problems that arise otherwise.
256
 */
257
struct Curl_creader {
258
  const struct Curl_crtype *crt;  /* type implementation */
259
  struct Curl_creader *next;  /* Downstream reader. */
260
  void *ctx;
261
  Curl_creader_phase phase; /* phase at which it operates */
262
};
263
264
/**
265
 * Default implementations for do_init, do_write, do_close that
266
 * do nothing and pass the data through.
267
 */
268
CURLcode Curl_creader_def_init(struct Curl_easy *data,
269
                               struct Curl_creader *reader);
270
void Curl_creader_def_close(struct Curl_easy *data,
271
                            struct Curl_creader *reader);
272
CURLcode Curl_creader_def_read(struct Curl_easy *data,
273
                               struct Curl_creader *reader,
274
                               char *buf, size_t blen,
275
                               size_t *nread, bool *eos);
276
bool Curl_creader_def_needs_rewind(struct Curl_easy *data,
277
                                   struct Curl_creader *reader);
278
curl_off_t Curl_creader_def_total_length(struct Curl_easy *data,
279
                                         struct Curl_creader *reader);
280
CURLcode Curl_creader_def_resume_from(struct Curl_easy *data,
281
                                      struct Curl_creader *reader,
282
                                      curl_off_t offset);
283
CURLcode Curl_creader_def_cntrl(struct Curl_easy *data,
284
                                struct Curl_creader *reader,
285
                                Curl_creader_cntrl opcode);
286
bool Curl_creader_def_is_paused(struct Curl_easy *data,
287
                                struct Curl_creader *reader);
288
void Curl_creader_def_done(struct Curl_easy *data,
289
                           struct Curl_creader *reader, int premature);
290
291
/**
292
 * Convenience method for calling `reader->do_read()` that
293
 * checks for NULL reader.
294
 */
295
CURLcode Curl_creader_read(struct Curl_easy *data,
296
                           struct Curl_creader *reader,
297
                           char *buf, size_t blen, size_t *nread, bool *eos);
298
299
/* Tell the reader and all below that any EOS state is to be cleared */
300
void Curl_creader_clear_eos(struct Curl_easy *data,
301
                            struct Curl_creader *reader);
302
303
/**
304
 * Create a new creader instance with given type and phase. Is not
305
 * inserted into the writer chain by this call.
306
 * Invokes `reader->do_init()`.
307
 */
308
CURLcode Curl_creader_create(struct Curl_creader **preader,
309
                             struct Curl_easy *data,
310
                             const struct Curl_crtype *crt,
311
                             Curl_creader_phase phase);
312
313
/**
314
 * Free a creader instance.
315
 * Invokes `reader->do_close()`.
316
 */
317
void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader);
318
319
/**
320
 * Adds a reader to the transfer's reader chain.
321
 * The readers `phase` determines where in the chain it is inserted.
322
 */
323
CURLcode Curl_creader_add(struct Curl_easy *data,
324
                          struct Curl_creader *reader);
325
326
/**
327
 * Set the given reader, which needs to be of type CURL_CR_CLIENT,
328
 * as the new first reader. Discard any installed readers and init
329
 * the reader chain anew.
330
 * The function takes ownership of `r`.
331
 */
332
CURLcode Curl_creader_set(struct Curl_easy *data, struct Curl_creader *r);
333
334
/**
335
 * Read at most `blen` bytes at `buf` from the client.
336
 * @param data    the transfer to read client bytes for
337
 * @param buf     the memory location to read to
338
 * @param blen    the amount of memory at `buf`
339
 * @param nread   on return the number of bytes read into `buf`
340
 * @param eos     TRUE iff bytes are the end of data from client
341
 * @return CURLE_OK on successful read (even 0 length) or error
342
 */
343
CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen,
344
                          size_t *nread, bool *eos) WARN_UNUSED_RESULT;
345
346
/**
347
 * TRUE iff client reader needs rewing before it can be used for
348
 * a retry request.
349
 */
350
bool Curl_creader_needs_rewind(struct Curl_easy *data);
351
352
/**
353
 * TRUE iff client reader will rewind at next start
354
 */
355
bool Curl_creader_will_rewind(struct Curl_easy *data);
356
357
/**
358
 * En-/disable rewind of client reader at next start.
359
 */
360
void Curl_creader_set_rewind(struct Curl_easy *data, bool enable);
361
362
/**
363
 * Get the total length of bytes provided by the installed readers.
364
 * This is independent of the amount already delivered and is calculated
365
 * by all readers in the stack. If a reader like "chunked" or
366
 * "crlf conversion" is installed, the returned length will be -1.
367
 * @return -1 if length is indeterminate
368
 */
369
curl_off_t Curl_creader_total_length(struct Curl_easy *data);
370
371
/**
372
 * Get the total length of bytes provided by the reader at phase
373
 * CURL_CR_CLIENT. This may not match the amount of bytes read
374
 * for a request, depending if other, encoding readers are also installed.
375
 * However it allows for rough estimation of the overall length.
376
 * @return -1 if length is indeterminate
377
 */
378
curl_off_t Curl_creader_client_length(struct Curl_easy *data);
379
380
/**
381
 * Unpause all installed readers.
382
 */
383
CURLcode Curl_creader_unpause(struct Curl_easy *data);
384
385
/**
386
 * Return TRUE iff any of the installed readers is paused.
387
 */
388
bool Curl_creader_is_paused(struct Curl_easy *data);
389
390
/**
391
 * Tell all client readers that they are done.
392
 */
393
void Curl_creader_done(struct Curl_easy *data, int premature);
394
395
/**
396
 * Look up an installed client reader on `data` by its type.
397
 * @return first reader with that type or NULL
398
 */
399
struct Curl_creader *Curl_creader_get_by_type(struct Curl_easy *data,
400
                                              const struct Curl_crtype *crt);
401
402
/**
403
 * Set the client reader to provide 0 bytes, immediate EOS.
404
 */
405
CURLcode Curl_creader_set_null(struct Curl_easy *data);
406
407
/**
408
 * Set the client reader the reads from fread callback.
409
 */
410
CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len);
411
412
/**
413
 * Set the client reader the reads from the supplied buf (NOT COPIED).
414
 */
415
CURLcode Curl_creader_set_buf(struct Curl_easy *data,
416
                              const char *buf, size_t blen);
417
418
#endif /* HEADER_CURL_SENDF_H */