diff options
author | Daniel Stenberg <daniel@haxx.se> | 2021-01-08 17:58:15 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2021-01-17 23:56:09 +0100 |
commit | 215db086e09665ee7af9b646ad6c4d6e281001ac (patch) | |
tree | 50c74f8456df87e86de6d72e90190a3563083a1a /lib/sendf.c | |
parent | 0d26ab9ed3ac29da2a383d313e93df3e9f5295a2 (diff) | |
download | curl-215db086e09665ee7af9b646ad6c4d6e281001ac.tar.gz |
lib: pass in 'struct Curl_easy *' to most functions
... in most cases instead of 'struct connectdata *' but in some cases in
addition to.
- We mostly operate on transfers and not connections.
- We need the transfer handle to log, store data and more. Everything in
libcurl is driven by a transfer (the CURL * in the public API).
- This work clarifies and separates the transfers from the connections
better.
- We should avoid "conn->data". Since individual connections can be used
by many transfers when multiplexing, making sure that conn->data
points to the current and correct transfer at all times is difficult
and has been notoriously error-prone over the years. The goal is to
ultimately remove the conn->data pointer for this reason.
Closes #6425
Diffstat (limited to 'lib/sendf.c')
-rw-r--r-- | lib/sendf.c | 65 |
1 files changed, 39 insertions, 26 deletions
diff --git a/lib/sendf.c b/lib/sendf.c index adcee89f8..e756f1ca1 100644 --- a/lib/sendf.c +++ b/lib/sendf.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -293,7 +293,7 @@ void Curl_failf(struct Curl_easy *data, const char *fmt, ...) * If the write would block (CURLE_AGAIN), we return CURLE_OK and * (*written == 0). Otherwise we return regular CURLcode value. */ -CURLcode Curl_write(struct connectdata *conn, +CURLcode Curl_write(struct Curl_easy *data, curl_socket_t sockfd, const void *mem, size_t len, @@ -301,9 +301,14 @@ CURLcode Curl_write(struct connectdata *conn, { ssize_t bytes_written; CURLcode result = CURLE_OK; - int num = (sockfd == conn->sock[SECONDARYSOCKET]); + struct connectdata *conn; + int num; + DEBUGASSERT(data); + DEBUGASSERT(data->conn); + conn = data->conn; + num = (sockfd == conn->sock[SECONDARYSOCKET]); - bytes_written = conn->send[num](conn, num, mem, len, &result); + bytes_written = conn->send[num](data, num, mem, len, &result); *written = bytes_written; if(bytes_written >= 0) @@ -326,11 +331,17 @@ CURLcode Curl_write(struct connectdata *conn, } } -ssize_t Curl_send_plain(struct connectdata *conn, int num, +ssize_t Curl_send_plain(struct Curl_easy *data, int num, const void *mem, size_t len, CURLcode *code) { - curl_socket_t sockfd = conn->sock[num]; + struct connectdata *conn; + curl_socket_t sockfd; ssize_t bytes_written; + + DEBUGASSERT(data); + DEBUGASSERT(data->conn); + conn = data->conn; + sockfd = conn->sock[num]; /* WinSock will destroy unread received data if send() is failed. To avoid lossage of received data, recv() must be @@ -373,9 +384,9 @@ ssize_t Curl_send_plain(struct connectdata *conn, int num, } else { char buffer[STRERROR_LEN]; - failf(conn->data, "Send failure: %s", + failf(data, "Send failure: %s", Curl_strerror(err, buffer, sizeof(buffer))); - conn->data->state.os_errno = err; + data->state.os_errno = err; *code = CURLE_SEND_ERROR; } } @@ -387,28 +398,31 @@ ssize_t Curl_send_plain(struct connectdata *conn, int num, * server using plain sockets only. Otherwise meant to have the exact same * proto as Curl_write() */ -CURLcode Curl_write_plain(struct connectdata *conn, +CURLcode Curl_write_plain(struct Curl_easy *data, curl_socket_t sockfd, const void *mem, size_t len, ssize_t *written) { - ssize_t bytes_written; CURLcode result; + struct connectdata *conn = data->conn; int num = (sockfd == conn->sock[SECONDARYSOCKET]); - bytes_written = Curl_send_plain(conn, num, mem, len, &result); - - *written = bytes_written; + *written = Curl_send_plain(data, num, mem, len, &result); return result; } -ssize_t Curl_recv_plain(struct connectdata *conn, int num, char *buf, +ssize_t Curl_recv_plain(struct Curl_easy *data, int num, char *buf, size_t len, CURLcode *code) { - curl_socket_t sockfd = conn->sock[num]; + struct connectdata *conn; + curl_socket_t sockfd; ssize_t nread; + DEBUGASSERT(data); + DEBUGASSERT(data->conn); + conn = data->conn; + sockfd = conn->sock[num]; /* Check and return data that already received and storied in internal intermediate buffer */ nread = get_pre_recved(conn, num, buf, len); @@ -439,9 +453,9 @@ ssize_t Curl_recv_plain(struct connectdata *conn, int num, char *buf, } else { char buffer[STRERROR_LEN]; - failf(conn->data, "Recv failure: %s", + failf(data, "Recv failure: %s", Curl_strerror(err, buffer, sizeof(buffer))); - conn->data->state.os_errno = err; + data->state.os_errno = err; *code = CURLE_RECV_ERROR; } } @@ -500,12 +514,12 @@ static CURLcode pausewrite(struct Curl_easy *data, * client write callback(s) and takes care of pause requests from the * callbacks. */ -static CURLcode chop_write(struct connectdata *conn, +static CURLcode chop_write(struct Curl_easy *data, int type, char *optr, size_t olen) { - struct Curl_easy *data = conn->data; + struct connectdata *conn = data->conn; curl_write_callback writeheader = NULL; curl_write_callback writebody = NULL; char *ptr = optr; @@ -595,13 +609,12 @@ static CURLcode chop_write(struct connectdata *conn, local character encoding. This is a problem and should be changed in the future to leave the original data alone. */ -CURLcode Curl_client_write(struct connectdata *conn, +CURLcode Curl_client_write(struct Curl_easy *data, int type, char *ptr, size_t len) { - struct Curl_easy *data = conn->data; - + struct connectdata *conn = data->conn; if(0 == len) len = strlen(ptr); @@ -623,7 +636,7 @@ CURLcode Curl_client_write(struct connectdata *conn, #endif /* CURL_DO_LINEEND_CONV */ } - return chop_write(conn, type, ptr, len); + return chop_write(data, type, ptr, len); } CURLcode Curl_read_plain(curl_socket_t sockfd, @@ -658,7 +671,7 @@ CURLcode Curl_read_plain(curl_socket_t sockfd, * * Returns a regular CURLcode value. */ -CURLcode Curl_read(struct connectdata *conn, /* connection data */ +CURLcode Curl_read(struct Curl_easy *data, /* transfer */ curl_socket_t sockfd, /* read from this socket */ char *buf, /* store read data here */ size_t sizerequested, /* max amount to read */ @@ -668,7 +681,7 @@ CURLcode Curl_read(struct connectdata *conn, /* connection data */ ssize_t nread = 0; size_t bytesfromsocket = 0; char *buffertofill = NULL; - struct Curl_easy *data = conn->data; + struct connectdata *conn = data->conn; /* Set 'num' to 0 or 1, depending on which socket that has been sent here. If it is the second socket, we set num to 1. Otherwise to 0. This lets @@ -680,7 +693,7 @@ CURLcode Curl_read(struct connectdata *conn, /* connection data */ bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size); buffertofill = buf; - nread = conn->recv[num](conn, num, buffertofill, bytesfromsocket, &result); + nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result); if(nread < 0) return result; |