Skip to content

Commit

Permalink
lib: replace readwrite with write_resp
Browse files Browse the repository at this point in the history
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 curl#12480
  • Loading branch information
icing authored and bagder committed Jan 13, 2024
1 parent d587ab4 commit d7b6ce6
Show file tree
Hide file tree
Showing 34 changed files with 767 additions and 616 deletions.
3 changes: 3 additions & 0 deletions lib/c-hyper.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ static CURLcode empty_header(struct Curl_easy *data)
CURLE_WRITE_ERROR : CURLE_OK;
if(result)
failf(data, "hyperstream: couldn't pass blank header");
/* Hyper does chunked decoding itself. If it was added during
* response header processing, remove it again. */
Curl_cwriter_remove_by_name(data, "chunked");
}
return result;
}
Expand Down
65 changes: 23 additions & 42 deletions lib/cf-h1-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct h1_tunnel_state {
struct dynbuf request_data;
size_t nsent;
size_t headerlines;
struct Curl_chunker ch;
enum keeponval {
KEEPON_DONE,
KEEPON_CONNECT,
Expand Down Expand Up @@ -133,6 +134,7 @@ static CURLcode tunnel_init(struct Curl_cfilter *cf,

Curl_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
Curl_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
Curl_httpchunk_init(data, &ts->ch, TRUE);

*pts = ts;
connkeep(cf->conn, "HTTP proxy CONNECT");
Expand All @@ -146,14 +148,6 @@ static void h1_tunnel_go_state(struct Curl_cfilter *cf,
{
if(ts->tunnel_state == new_state)
return;
/* leaving this one */
switch(ts->tunnel_state) {
case H1_TUNNEL_CONNECT:
data->req.ignorebody = FALSE;
break;
default:
break;
}
/* entering this one */
switch(new_state) {
case H1_TUNNEL_INIT:
Expand Down Expand Up @@ -212,6 +206,7 @@ static void tunnel_free(struct Curl_cfilter *cf,
h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
Curl_dyn_free(&ts->rcvbuf);
Curl_dyn_free(&ts->request_data);
Curl_httpchunk_free(data, &ts->ch);
free(ts);
cf->ctx = NULL;
}
Expand Down Expand Up @@ -344,8 +339,8 @@ static CURLcode on_resp_header(struct Curl_cfilter *cf,
STRCONST("chunked"))) {
infof(data, "CONNECT responded chunked");
ts->chunked_encoding = TRUE;
/* init our chunky engine */
Curl_httpchunk_init(data);
/* reset our chunky engine */
Curl_httpchunk_reset(data, &ts->ch, TRUE);
}
}
else if(Curl_compareheader(header,
Expand Down Expand Up @@ -373,7 +368,7 @@ static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
struct SingleRequest *k = &data->req;
curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
char *linep;
size_t perline;
size_t line_len;
int error, writetype;

#define SELECT_OK 0
Expand Down Expand Up @@ -432,17 +427,17 @@ static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
break;
}
}
else {
else if(ts->chunked_encoding) {
/* chunked-encoded body, so we need to do the chunked dance
properly to know when the end of the body is reached */
CHUNKcode r;
CURLcode extra;
size_t consumed = 0;

/* now parse the chunked piece of data so that we can
properly tell when the stream ends */
r = Curl_httpchunk_read(data, &byte, 1, &consumed, &extra);
if(r == CHUNKE_STOP) {
result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
if(result)
return result;
if(Curl_httpchunk_is_done(data, &ts->ch)) {
/* we're done reading chunks! */
infof(data, "chunk reading DONE");
ts->keepon = KEEPON_DONE;
Expand All @@ -462,19 +457,19 @@ static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,

ts->headerlines++;
linep = Curl_dyn_ptr(&ts->rcvbuf);
perline = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
line_len = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */

/* output debug if that is requested */
Curl_debug(data, CURLINFO_HEADER_IN, linep, perline);
Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);

/* send the header to the callback */
writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
(ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
result = Curl_client_write(data, writetype, linep, perline);
result = Curl_client_write(data, writetype, linep, line_len);
if(result)
return result;

result = Curl_bump_headersize(data, perline, TRUE);
result = Curl_bump_headersize(data, line_len, TRUE);
if(result)
return result;

Expand All @@ -497,29 +492,7 @@ static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
" bytes of response-body", ts->cl);
}
else if(ts->chunked_encoding) {
CHUNKcode r;
CURLcode extra;
size_t consumed = 0;

infof(data, "Ignore chunked response-body");

/* We set ignorebody true here since the chunked decoder
function will acknowledge that. Pay attention so that this is
cleared again when this function returns! */
k->ignorebody = TRUE;

if(linep[1] == '\n')
/* this can only be a LF if the letter at index 0 was a CR */
linep++;

/* now parse the chunked piece of data so that we can properly
tell when the stream ends */
r = Curl_httpchunk_read(data, linep + 1, 1, &consumed, &extra);
if(r == CHUNKE_STOP) {
/* we're done reading chunks! */
infof(data, "chunk reading DONE");
ts->keepon = KEEPON_DONE;
}
}
else {
/* without content-length or chunked encoding, we
Expand Down Expand Up @@ -1030,6 +1003,14 @@ static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
*done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
if(*done) {
cf->connected = TRUE;
/* Restore `data->req` fields that may habe been touched */
data->req.header = TRUE; /* assume header */
data->req.bytecount = 0;
data->req.ignorebody = FALSE;
Curl_client_cleanup(data);
Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0);

tunnel_free(cf, data);
}
return result;
Expand Down
47 changes: 31 additions & 16 deletions lib/content_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
*/
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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]))
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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. */

Expand Down
12 changes: 6 additions & 6 deletions lib/curl_rtmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const struct Curl_handler Curl_handler_rtmp = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_RTMP, /* defport */
Expand All @@ -102,7 +102,7 @@ const struct Curl_handler Curl_handler_rtmpt = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_RTMPT, /* defport */
Expand All @@ -125,7 +125,7 @@ const struct Curl_handler Curl_handler_rtmpe = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_RTMP, /* defport */
Expand All @@ -148,7 +148,7 @@ const struct Curl_handler Curl_handler_rtmpte = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_RTMPT, /* defport */
Expand All @@ -171,7 +171,7 @@ const struct Curl_handler Curl_handler_rtmps = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_RTMPS, /* defport */
Expand All @@ -194,7 +194,7 @@ const struct Curl_handler Curl_handler_rtmpts = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_RTMPS, /* defport */
Expand Down
2 changes: 1 addition & 1 deletion lib/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const struct Curl_handler Curl_handler_dict = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
ZERO_NULL, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_DICT, /* defport */
Expand Down
2 changes: 1 addition & 1 deletion lib/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const struct Curl_handler Curl_handler_file = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
file_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
0, /* defport */
Expand Down
4 changes: 2 additions & 2 deletions lib/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const struct Curl_handler Curl_handler_ftp = {
ftp_domore_getsock, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
ftp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_FTP, /* defport */
Expand Down Expand Up @@ -199,7 +199,7 @@ const struct Curl_handler Curl_handler_ftps = {
ftp_domore_getsock, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
ftp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_FTPS, /* defport */
Expand Down
4 changes: 2 additions & 2 deletions lib/gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const struct Curl_handler Curl_handler_gopher = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
ZERO_NULL, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_GOPHER, /* defport */
Expand All @@ -99,7 +99,7 @@ const struct Curl_handler Curl_handler_gophers = {
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
ZERO_NULL, /* disconnect */
ZERO_NULL, /* readwrite */
ZERO_NULL, /* write_resp */
ZERO_NULL, /* connection_check */
ZERO_NULL, /* attach connection */
PORT_GOPHER, /* defport */
Expand Down
Loading

0 comments on commit d7b6ce6

Please sign in to comment.