mirror of
https://github.com/curl/curl.git
synced 2026-08-25 06:53:31 +03:00
lib: replace readwrite with write_resp
This clarifies the handling of server responses by folding the code for
the complicated protocols into their protocol handlers. This concerns
mainly HTTP and its bastard sibling RTSP.
The terms "read" and "write" are often used without clear context if
they refer to the connect or the client/application side of a
transfer. This PR uses "read/write" for operations on the client side
and "send/receive" for the connection, e.g. server side. If this is
considered useful, we can revisit renaming of further methods in another
PR.
Curl's protocol handler `readwrite()` method been changed:
```diff
- CURLcode (*readwrite)(struct Curl_easy *data, struct connectdata *conn,
- const char *buf, size_t blen,
- size_t *pconsumed, bool *readmore);
+ CURLcode (*write_resp)(struct Curl_easy *data, const char *buf, size_t blen,
+ bool is_eos, bool *done);
```
The name was changed to clarify that this writes reponse data to the
client side. The parameter changes are:
* `conn` removed as it always operates on `data->conn`
* `pconsumed` removed as the method needs to handle all data on success
* `readmore` removed as no longer necessary
* `is_eos` as indicator that this is the last call for the transfer
response (end-of-stream).
* `done` TRUE on return iff the transfer response is to be treated as
finished
This change affects many files only because of updated comments in
handlers that provide no implementation. The real change is that the
HTTP protocol handlers now provide an implementation.
The HTTP protocol handlers `write_resp()` implementation will get passed
**all** raw data of a server response for the transfer. The HTTP/1.x
formatted status and headers, as well as the undecoded response
body. `Curl_http_write_resp_hds()` is used internally to parse the
response headers and pass them on. This method is public as the RTSP
protocol handler also uses it.
HTTP/1.1 "chunked" transport encoding is now part of the general
*content encoding* writer stack, just like other encodings. A new flag
`CLIENTWRITE_EOS` was added for the last client write. This allows
writers to verify that they are in a valid end state. The chunked
decoder will check if it indeed has seen the last chunk.
The general response handling in `transfer.c:466` happens in function
`readwrite_data()`. This mainly operates now like:
```
static CURLcode readwrite_data(data, ...)
{
do {
Curl_xfer_recv_resp(data, buf)
...
Curl_xfer_write_resp(data, buf)
...
} while(interested);
...
}
```
All the response data handling is implemented in
`Curl_xfer_write_resp()`. It calls the protocol handler's `write_resp()`
implementation if available, or does the default behaviour.
All raw response data needs to pass through this function. Which also
means that anyone in possession of such data may call
`Curl_xfer_write_resp()`.
Closes #12480
This commit is contained in:
parent
d587ab422d
commit
d7b6ce64ce
34 changed files with 765 additions and 614 deletions
|
|
@ -835,8 +835,8 @@ static const struct Curl_cwtype identity_encoding = {
|
|||
};
|
||||
|
||||
|
||||
/* supported content encodings table. */
|
||||
static const struct Curl_cwtype * const encodings[] = {
|
||||
/* supported general content decoders. */
|
||||
static const struct Curl_cwtype * const general_unencoders[] = {
|
||||
&identity_encoding,
|
||||
#ifdef HAVE_LIBZ
|
||||
&deflate_encoding,
|
||||
|
|
@ -851,6 +851,13 @@ static const struct Curl_cwtype * const encodings[] = {
|
|||
NULL
|
||||
};
|
||||
|
||||
/* supported content decoders only for transfer encodings */
|
||||
static const struct Curl_cwtype * const transfer_unencoders[] = {
|
||||
#ifndef CURL_DISABLE_HTTP
|
||||
&Curl_httpchunk_unencoder,
|
||||
#endif
|
||||
NULL
|
||||
};
|
||||
|
||||
/* Provide a list of comma-separated names of supported encodings.
|
||||
*/
|
||||
|
|
@ -864,7 +871,7 @@ void Curl_all_content_encodings(char *buf, size_t blen)
|
|||
DEBUGASSERT(blen);
|
||||
buf[0] = 0;
|
||||
|
||||
for(cep = encodings; *cep; cep++) {
|
||||
for(cep = general_unencoders; *cep; cep++) {
|
||||
ce = *cep;
|
||||
if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT))
|
||||
len += strlen(ce->name) + 2;
|
||||
|
|
@ -876,7 +883,7 @@ void Curl_all_content_encodings(char *buf, size_t blen)
|
|||
}
|
||||
else if(blen > len) {
|
||||
char *p = buf;
|
||||
for(cep = encodings; *cep; cep++) {
|
||||
for(cep = general_unencoders; *cep; cep++) {
|
||||
ce = *cep;
|
||||
if(!strcasecompare(ce->name, CONTENT_ENCODING_DEFAULT)) {
|
||||
strcpy(p, ce->name);
|
||||
|
|
@ -934,12 +941,23 @@ static const struct Curl_cwtype error_writer = {
|
|||
};
|
||||
|
||||
/* Find the content encoding by name. */
|
||||
static const struct Curl_cwtype *find_encoding(const char *name,
|
||||
size_t len)
|
||||
static const struct Curl_cwtype *find_unencode_writer(const char *name,
|
||||
size_t len,
|
||||
Curl_cwriter_phase phase)
|
||||
{
|
||||
const struct Curl_cwtype * const *cep;
|
||||
|
||||
for(cep = encodings; *cep; cep++) {
|
||||
if(phase == CURL_CW_TRANSFER_DECODE) {
|
||||
for(cep = transfer_unencoders; *cep; cep++) {
|
||||
const struct Curl_cwtype *ce = *cep;
|
||||
if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
|
||||
(ce->alias && strncasecompare(name, ce->alias, len)
|
||||
&& !ce->alias[len]))
|
||||
return ce;
|
||||
}
|
||||
}
|
||||
/* look among the general decoders */
|
||||
for(cep = general_unencoders; *cep; cep++) {
|
||||
const struct Curl_cwtype *ce = *cep;
|
||||
if((strncasecompare(name, ce->name, len) && !ce->name[len]) ||
|
||||
(ce->alias && strncasecompare(name, ce->alias, len) && !ce->alias[len]))
|
||||
|
|
@ -953,7 +971,6 @@ static const struct Curl_cwtype *find_encoding(const char *name,
|
|||
CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
|
||||
const char *enclist, int is_transfer)
|
||||
{
|
||||
struct SingleRequest *k = &data->req;
|
||||
Curl_cwriter_phase phase = is_transfer?
|
||||
CURL_CW_TRANSFER_DECODE:CURL_CW_CONTENT_DECODE;
|
||||
CURLcode result;
|
||||
|
|
@ -972,16 +989,14 @@ CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
|
|||
if(!ISSPACE(*enclist))
|
||||
namelen = enclist - name + 1;
|
||||
|
||||
/* Special case: chunked encoding is handled at the reader level. */
|
||||
if(is_transfer && namelen == 7 && strncasecompare(name, "chunked", 7)) {
|
||||
k->chunk = TRUE; /* chunks coming our way. */
|
||||
Curl_httpchunk_init(data); /* init our chunky engine. */
|
||||
}
|
||||
else if(namelen) {
|
||||
if(namelen) {
|
||||
const struct Curl_cwtype *cwt;
|
||||
struct Curl_cwriter *writer;
|
||||
|
||||
if((is_transfer && !data->set.http_transfer_encoding) ||
|
||||
/* if we skip the decoding in this phase, do not look further.
|
||||
* Exception is "chunked" transfer-encoding which always must happen */
|
||||
if((is_transfer && !data->set.http_transfer_encoding &&
|
||||
(namelen != 7 || !strncasecompare(name, "chunked", 7))) ||
|
||||
(!is_transfer && data->set.http_ce_skip)) {
|
||||
/* not requested, ignore */
|
||||
return CURLE_OK;
|
||||
|
|
@ -993,7 +1008,7 @@ CURLcode Curl_build_unencoding_stack(struct Curl_easy *data,
|
|||
return CURLE_BAD_CONTENT_ENCODING;
|
||||
}
|
||||
|
||||
cwt = find_encoding(name, namelen);
|
||||
cwt = find_unencode_writer(name, namelen, phase);
|
||||
if(!cwt)
|
||||
cwt = &error_writer; /* Defer error at use. */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue