lib: Curl_read/Curl_write clarifications

- replace `Curl_read()`, `Curl_write()` and `Curl_nwrite()` to
  clarify when and at what level they operate
- send/recv of transfer related data is now done via
  `Curl_xfer_send()/Curl_xfer_recv()` which no longer has
  socket/socketindex as parameter. It decides on the transfer
  setup of `conn->sockfd` and `conn->writesockfd` on which
  connection filter chain to operate.
- send/recv on a specific connection filter chain is done via
  `Curl_conn_send()/Curl_conn_recv()` which get the socket index
  as parameter.
- rename `Curl_setup_transfer()` to `Curl_xfer_setup()` for
  naming consistency
- clarify that the special CURLE_AGAIN hangling to return
  `CURLE_OK` with length 0 only applies to `Curl_xfer_send()`
  and CURLE_AGAIN is returned by all other send() variants.
- fix a bug in websocket `curl_ws_recv()` that mixed up data
  when it arrived in more than a single chunk (to be made
  into a sperate PR, also)

Added as documented [in
CLIENT-READER.md](5b1f31dfba/docs/CLIENT-READERS.md).

- old `Curl_buffer_send()` completely replaced by new `Curl_req_send()`
- old `Curl_fillreadbuffer()` replaced with `Curl_client_read()`
- HTTP chunked uploads are now formatted in a client reader added when
  needed.
- FTP line-end conversions are done in a client reader added when
  needed.
- when sending requests headers, remaining buffer space is filled with
  body data for sending in "one go". This is independent of the request
  body size. Resolves #12938 as now small and large requests have the
  same code path.

Changes done to test cases:

- test513: now fails before sending request headers as this initial
  "client read" triggers the setup fault. Behaves now the same as in
  hyper build
- test547, test555, test1620: fix the length check in the lib code to
  only fail for reads *smaller* than expected. This was a bug in the
  test code that never triggered in the old implementation.

Closes #12969
This commit is contained in:
Stefan Eissing 2024-02-15 16:22:53 +01:00 committed by Daniel Stenberg
parent 8d67c61c47
commit 9369c30cd8
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
26 changed files with 1191 additions and 1021 deletions

View file

@ -61,7 +61,7 @@ CURLcode Curl_client_write(struct Curl_easy *data, int type, const char *ptr,
/**
* Free all resources related to client writing.
*/
void Curl_cw_reset(struct Curl_easy *data);
void Curl_client_reset(struct Curl_easy *data);
/**
* Client Writers - a chain passing transfer BODY data to the client.
@ -175,4 +175,107 @@ void Curl_cwriter_def_close(struct Curl_easy *data,
struct Curl_cwriter *writer);
/* Client Reader Type, provides the implementation */
struct Curl_crtype {
const char *name; /* writer name. */
CURLcode (*do_init)(struct Curl_easy *data, struct Curl_creader *writer);
CURLcode (*do_read)(struct Curl_easy *data, struct Curl_creader *reader,
char *buf, size_t blen, size_t *nread, bool *eos);
void (*do_close)(struct Curl_easy *data, struct Curl_creader *reader);
bool (*needs_rewind)(struct Curl_easy *data, struct Curl_creader *reader);
size_t creader_size; /* sizeof() allocated struct Curl_creader */
};
/* Phase a reader operates at. */
typedef enum {
CURL_CR_NET, /* data send to the network (connection filters) */
CURL_CR_TRANSFER_ENCODE, /* add transfer-encodings */
CURL_CR_PROTOCOL, /* before transfer, but after content decoding */
CURL_CR_CONTENT_ENCODE, /* add content-encodings */
CURL_CR_CLIENT /* data read from client */
} Curl_creader_phase;
/* Client reader instance */
struct Curl_creader {
const struct Curl_crtype *crt; /* type implementation */
struct Curl_creader *next; /* Downstream reader. */
Curl_creader_phase phase; /* phase at which it operates */
};
/**
* Default implementations for do_init, do_write, do_close that
* do nothing and pass the data through.
*/
CURLcode Curl_creader_def_init(struct Curl_easy *data,
struct Curl_creader *reader);
void Curl_creader_def_close(struct Curl_easy *data,
struct Curl_creader *reader);
bool Curl_creader_def_needs_rewind(struct Curl_easy *data,
struct Curl_creader *reader);
/**
* Convenience method for calling `reader->do_read()` that
* checks for NULL reader.
*/
CURLcode Curl_creader_read(struct Curl_easy *data,
struct Curl_creader *reader,
char *buf, size_t blen, size_t *nread, bool *eos);
/**
* Create a new creader instance with given type and phase. Is not
* inserted into the writer chain by this call.
* Invokes `reader->do_init()`.
*/
CURLcode Curl_creader_create(struct Curl_creader **preader,
struct Curl_easy *data,
const struct Curl_crtype *cr_handler,
Curl_creader_phase phase);
/**
* Free a creader instance.
* Invokes `reader->do_close()`.
*/
void Curl_creader_free(struct Curl_easy *data, struct Curl_creader *reader);
/**
* Adds a reader to the transfer's reader chain.
* The readers `phase` determines where in the chain it is inserted.
*/
CURLcode Curl_creader_add(struct Curl_easy *data,
struct Curl_creader *reader);
/**
* Read at most `blen` bytes at `buf` from the client.
* @param date the transfer to read client bytes for
* @param buf the memory location to read to
* @param blen the amount of memory at `buf`
* @param nread on return the number of bytes read into `buf`
* @param eos TRUE iff bytes are the end of data from client
* @return CURLE_OK on successful read (even 0 length) or error
*/
CURLcode Curl_client_read(struct Curl_easy *data, char *buf, size_t blen,
size_t *nread, bool *eos) WARN_UNUSED_RESULT;
/**
* TRUE iff client reader needs rewing before it can be used for
* a retry request.
*/
bool Curl_client_read_needs_rewind(struct Curl_easy *data);
/**
* Set the client reader to provide 0 bytes, immediate EOS.
*/
CURLcode Client_reader_set_null(struct Curl_easy *data);
/**
* Set the client reader the reads from fread callback.
*/
CURLcode Client_reader_set_fread(struct Curl_easy *data, curl_off_t len);
/**
* Set the client reader the reads from the supplied buf (NOT COPIED).
*/
CURLcode Client_reader_set_buf(struct Curl_easy *data,
const char *buf, size_t blen);
#endif /* HEADER_CURL_SENDF_H */