Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 12 | * are also available at https://curl.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
Elliott Hughes | 0128fe4 | 2018-02-27 14:57:55 -0800 | [diff] [blame] | 25 | #ifdef HAVE_NETINET_IN_H |
| 26 | #include <netinet/in.h> |
| 27 | #endif |
| 28 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 29 | #ifdef HAVE_LINUX_TCP_H |
| 30 | #include <linux/tcp.h> |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 31 | #elif defined(HAVE_NETINET_TCP_H) |
| 32 | #include <netinet/tcp.h> |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 33 | #endif |
| 34 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 35 | #include <curl/curl.h> |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 36 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 37 | #include "urldata.h" |
| 38 | #include "sendf.h" |
| 39 | #include "connect.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 40 | #include "vtls/vtls.h" |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 41 | #include "vssh/ssh.h" |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 42 | #include "easyif.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 43 | #include "multiif.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 44 | #include "non-ascii.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 45 | #include "strerror.h" |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 46 | #include "select.h" |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 47 | #include "strdup.h" |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 48 | #include "http2.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 49 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 50 | /* The last 3 #include files should be in this order */ |
| 51 | #include "curl_printf.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 52 | #include "curl_memory.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 53 | #include "memdebug.h" |
| 54 | |
| 55 | #ifdef CURL_DO_LINEEND_CONV |
| 56 | /* |
| 57 | * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF |
| 58 | * (\n), with special processing for CRLF sequences that are split between two |
| 59 | * blocks of data. Remaining, bare CRs are changed to LFs. The possibly new |
| 60 | * size of the data is returned. |
| 61 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 62 | static size_t convert_lineends(struct Curl_easy *data, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 63 | char *startPtr, size_t size) |
| 64 | { |
| 65 | char *inPtr, *outPtr; |
| 66 | |
| 67 | /* sanity check */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 68 | if(!startPtr || (size < 1)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 69 | return size; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 72 | if(data->state.prev_block_had_trailing_cr) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 73 | /* The previous block of incoming data |
| 74 | had a trailing CR, which was turned into a LF. */ |
| 75 | if(*startPtr == '\n') { |
| 76 | /* This block of incoming data starts with the |
| 77 | previous block's LF so get rid of it */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 78 | memmove(startPtr, startPtr + 1, size-1); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 79 | size--; |
| 80 | /* and it wasn't a bare CR but a CRLF conversion instead */ |
| 81 | data->state.crlf_conversions++; |
| 82 | } |
| 83 | data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */ |
| 84 | } |
| 85 | |
| 86 | /* find 1st CR, if any */ |
| 87 | inPtr = outPtr = memchr(startPtr, '\r', size); |
| 88 | if(inPtr) { |
| 89 | /* at least one CR, now look for CRLF */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 90 | while(inPtr < (startPtr + size-1)) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 91 | /* note that it's size-1, so we'll never look past the last byte */ |
| 92 | if(memcmp(inPtr, "\r\n", 2) == 0) { |
| 93 | /* CRLF found, bump past the CR and copy the NL */ |
| 94 | inPtr++; |
| 95 | *outPtr = *inPtr; |
| 96 | /* keep track of how many CRLFs we converted */ |
| 97 | data->state.crlf_conversions++; |
| 98 | } |
| 99 | else { |
| 100 | if(*inPtr == '\r') { |
| 101 | /* lone CR, move LF instead */ |
| 102 | *outPtr = '\n'; |
| 103 | } |
| 104 | else { |
| 105 | /* not a CRLF nor a CR, just copy whatever it is */ |
| 106 | *outPtr = *inPtr; |
| 107 | } |
| 108 | } |
| 109 | outPtr++; |
| 110 | inPtr++; |
| 111 | } /* end of while loop */ |
| 112 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 113 | if(inPtr < startPtr + size) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 114 | /* handle last byte */ |
| 115 | if(*inPtr == '\r') { |
| 116 | /* deal with a CR at the end of the buffer */ |
| 117 | *outPtr = '\n'; /* copy a NL instead */ |
| 118 | /* note that a CRLF might be split across two blocks */ |
| 119 | data->state.prev_block_had_trailing_cr = TRUE; |
| 120 | } |
| 121 | else { |
| 122 | /* copy last byte */ |
| 123 | *outPtr = *inPtr; |
| 124 | } |
| 125 | outPtr++; |
| 126 | } |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 127 | if(outPtr < startPtr + size) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 128 | /* tidy up by null terminating the now shorter data */ |
| 129 | *outPtr = '\0'; |
| 130 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 131 | return (outPtr - startPtr); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 132 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 133 | return size; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 134 | } |
| 135 | #endif /* CURL_DO_LINEEND_CONV */ |
| 136 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 137 | #ifdef USE_RECV_BEFORE_SEND_WORKAROUND |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 138 | bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex) |
| 139 | { |
| 140 | struct postponed_data * const psnd = &(conn->postponed[sockindex]); |
| 141 | return psnd->buffer && psnd->allocated_size && |
| 142 | psnd->recv_size > psnd->recv_processed; |
| 143 | } |
| 144 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 145 | static CURLcode pre_receive_plain(struct Curl_easy *data, |
| 146 | struct connectdata *conn, int num) |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 147 | { |
| 148 | const curl_socket_t sockfd = conn->sock[num]; |
| 149 | struct postponed_data * const psnd = &(conn->postponed[num]); |
| 150 | size_t bytestorecv = psnd->allocated_size - psnd->recv_size; |
| 151 | /* WinSock will destroy unread received data if send() is |
| 152 | failed. |
| 153 | To avoid lossage of received data, recv() must be |
| 154 | performed before every send() if any incoming data is |
| 155 | available. However, skip this, if buffer is already full. */ |
| 156 | if((conn->handler->protocol&PROTO_FAMILY_HTTP) != 0 && |
| 157 | conn->recv[num] == Curl_recv_plain && |
| 158 | (!psnd->buffer || bytestorecv)) { |
| 159 | const int readymask = Curl_socket_check(sockfd, CURL_SOCKET_BAD, |
| 160 | CURL_SOCKET_BAD, 0); |
| 161 | if(readymask != -1 && (readymask & CURL_CSELECT_IN) != 0) { |
| 162 | /* Have some incoming data */ |
| 163 | if(!psnd->buffer) { |
| 164 | /* Use buffer double default size for intermediate buffer */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 165 | psnd->allocated_size = 2 * data->set.buffer_size; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 166 | psnd->buffer = malloc(psnd->allocated_size); |
Haibo Huang | b5a52b9 | 2020-10-28 22:18:23 -0700 | [diff] [blame] | 167 | if(!psnd->buffer) |
| 168 | return CURLE_OUT_OF_MEMORY; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 169 | psnd->recv_size = 0; |
| 170 | psnd->recv_processed = 0; |
| 171 | #ifdef DEBUGBUILD |
| 172 | psnd->bindsock = sockfd; /* Used only for DEBUGASSERT */ |
| 173 | #endif /* DEBUGBUILD */ |
| 174 | bytestorecv = psnd->allocated_size; |
| 175 | } |
| 176 | if(psnd->buffer) { |
| 177 | ssize_t recvedbytes; |
| 178 | DEBUGASSERT(psnd->bindsock == sockfd); |
| 179 | recvedbytes = sread(sockfd, psnd->buffer + psnd->recv_size, |
| 180 | bytestorecv); |
| 181 | if(recvedbytes > 0) |
| 182 | psnd->recv_size += recvedbytes; |
| 183 | } |
| 184 | else |
| 185 | psnd->allocated_size = 0; |
| 186 | } |
| 187 | } |
Haibo Huang | b5a52b9 | 2020-10-28 22:18:23 -0700 | [diff] [blame] | 188 | return CURLE_OK; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static ssize_t get_pre_recved(struct connectdata *conn, int num, char *buf, |
| 192 | size_t len) |
| 193 | { |
| 194 | struct postponed_data * const psnd = &(conn->postponed[num]); |
| 195 | size_t copysize; |
| 196 | if(!psnd->buffer) |
| 197 | return 0; |
| 198 | |
| 199 | DEBUGASSERT(psnd->allocated_size > 0); |
| 200 | DEBUGASSERT(psnd->recv_size <= psnd->allocated_size); |
| 201 | DEBUGASSERT(psnd->recv_processed <= psnd->recv_size); |
| 202 | /* Check and process data that already received and storied in internal |
| 203 | intermediate buffer */ |
| 204 | if(psnd->recv_size > psnd->recv_processed) { |
| 205 | DEBUGASSERT(psnd->bindsock == conn->sock[num]); |
| 206 | copysize = CURLMIN(len, psnd->recv_size - psnd->recv_processed); |
| 207 | memcpy(buf, psnd->buffer + psnd->recv_processed, copysize); |
| 208 | psnd->recv_processed += copysize; |
| 209 | } |
| 210 | else |
| 211 | copysize = 0; /* buffer was allocated, but nothing was received */ |
| 212 | |
| 213 | /* Free intermediate buffer if it has no unprocessed data */ |
| 214 | if(psnd->recv_processed == psnd->recv_size) { |
| 215 | free(psnd->buffer); |
| 216 | psnd->buffer = NULL; |
| 217 | psnd->allocated_size = 0; |
| 218 | psnd->recv_size = 0; |
| 219 | psnd->recv_processed = 0; |
| 220 | #ifdef DEBUGBUILD |
| 221 | psnd->bindsock = CURL_SOCKET_BAD; |
| 222 | #endif /* DEBUGBUILD */ |
| 223 | } |
| 224 | return (ssize_t)copysize; |
| 225 | } |
| 226 | #else /* ! USE_RECV_BEFORE_SEND_WORKAROUND */ |
| 227 | /* Use "do-nothing" macros instead of functions when workaround not used */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 228 | bool Curl_recv_has_postponed_data(struct connectdata *conn, int sockindex) |
| 229 | { |
| 230 | (void)conn; |
| 231 | (void)sockindex; |
| 232 | return false; |
| 233 | } |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 234 | #define pre_receive_plain(d,c,n) CURLE_OK |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 235 | #define get_pre_recved(c,n,b,l) 0 |
| 236 | #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */ |
| 237 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 238 | /* Curl_infof() is for info message along the way */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 239 | #define MAXINFO 2048 |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 240 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 241 | void Curl_infof(struct Curl_easy *data, const char *fmt, ...) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 242 | { |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 243 | DEBUGASSERT(!strchr(fmt, '\n')); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 244 | if(data && data->set.verbose) { |
| 245 | va_list ap; |
| 246 | size_t len; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 247 | char buffer[MAXINFO + 2]; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 248 | va_start(ap, fmt); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 249 | len = mvsnprintf(buffer, MAXINFO, fmt, ap); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 250 | va_end(ap); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 251 | buffer[len++] = '\n'; |
| 252 | buffer[len] = '\0'; |
| 253 | Curl_debug(data, CURLINFO_TEXT, buffer, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /* Curl_failf() is for messages stating why we failed. |
| 258 | * The message SHALL NOT include any LF or CR. |
| 259 | */ |
| 260 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 261 | void Curl_failf(struct Curl_easy *data, const char *fmt, ...) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 262 | { |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 263 | DEBUGASSERT(!strchr(fmt, '\n')); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 264 | if(data->set.verbose || data->set.errorbuffer) { |
| 265 | va_list ap; |
| 266 | size_t len; |
| 267 | char error[CURL_ERROR_SIZE + 2]; |
| 268 | va_start(ap, fmt); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 269 | len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 270 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 271 | if(data->set.errorbuffer && !data->state.errorbuf) { |
| 272 | strcpy(data->set.errorbuffer, error); |
| 273 | data->state.errorbuf = TRUE; /* wrote error string */ |
| 274 | } |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 275 | error[len++] = '\n'; |
| 276 | error[len] = '\0'; |
| 277 | Curl_debug(data, CURLINFO_TEXT, error, len); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 278 | va_end(ap); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 279 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 282 | /* |
| 283 | * Curl_write() is an internal write function that sends data to the |
| 284 | * server. Works with plain sockets, SCP, SSL or kerberos. |
| 285 | * |
| 286 | * If the write would block (CURLE_AGAIN), we return CURLE_OK and |
| 287 | * (*written == 0). Otherwise we return regular CURLcode value. |
| 288 | */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 289 | CURLcode Curl_write(struct Curl_easy *data, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 290 | curl_socket_t sockfd, |
| 291 | const void *mem, |
| 292 | size_t len, |
| 293 | ssize_t *written) |
| 294 | { |
| 295 | ssize_t bytes_written; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 296 | CURLcode result = CURLE_OK; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 297 | struct connectdata *conn; |
| 298 | int num; |
| 299 | DEBUGASSERT(data); |
| 300 | DEBUGASSERT(data->conn); |
| 301 | conn = data->conn; |
| 302 | num = (sockfd == conn->sock[SECONDARYSOCKET]); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 303 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 304 | #ifdef CURLDEBUG |
| 305 | { |
| 306 | /* Allow debug builds to override this logic to force short sends |
| 307 | */ |
| 308 | char *p = getenv("CURL_SMALLSENDS"); |
| 309 | if(p) { |
| 310 | size_t altsize = (size_t)strtoul(p, NULL, 10); |
| 311 | if(altsize) |
| 312 | len = CURLMIN(len, altsize); |
| 313 | } |
| 314 | } |
| 315 | #endif |
| 316 | bytes_written = conn->send[num](data, num, mem, len, &result); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 317 | |
| 318 | *written = bytes_written; |
| 319 | if(bytes_written >= 0) |
| 320 | /* we completely ignore the curlcode value when subzero is not returned */ |
| 321 | return CURLE_OK; |
| 322 | |
| 323 | /* handle CURLE_AGAIN or a send failure */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 324 | switch(result) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 325 | case CURLE_AGAIN: |
| 326 | *written = 0; |
| 327 | return CURLE_OK; |
| 328 | |
| 329 | case CURLE_OK: |
| 330 | /* general send failure */ |
| 331 | return CURLE_SEND_ERROR; |
| 332 | |
| 333 | default: |
| 334 | /* we got a specific curlcode, forward it */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 335 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 339 | ssize_t Curl_send_plain(struct Curl_easy *data, int num, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 340 | const void *mem, size_t len, CURLcode *code) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 341 | { |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 342 | struct connectdata *conn; |
| 343 | curl_socket_t sockfd; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 344 | ssize_t bytes_written; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 345 | |
| 346 | DEBUGASSERT(data); |
| 347 | DEBUGASSERT(data->conn); |
| 348 | conn = data->conn; |
| 349 | sockfd = conn->sock[num]; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 350 | /* WinSock will destroy unread received data if send() is |
| 351 | failed. |
| 352 | To avoid lossage of received data, recv() must be |
| 353 | performed before every send() if any incoming data is |
| 354 | available. */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 355 | if(pre_receive_plain(data, conn, num)) { |
Haibo Huang | b5a52b9 | 2020-10-28 22:18:23 -0700 | [diff] [blame] | 356 | *code = CURLE_OUT_OF_MEMORY; |
| 357 | return -1; |
| 358 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 359 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 360 | #if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT) /* Linux */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 361 | if(conn->bits.tcp_fastopen) { |
| 362 | bytes_written = sendto(sockfd, mem, len, MSG_FASTOPEN, |
| 363 | conn->ip_addr->ai_addr, conn->ip_addr->ai_addrlen); |
| 364 | conn->bits.tcp_fastopen = FALSE; |
| 365 | } |
| 366 | else |
| 367 | #endif |
| 368 | bytes_written = swrite(sockfd, mem, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 369 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 370 | *code = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 371 | if(-1 == bytes_written) { |
| 372 | int err = SOCKERRNO; |
| 373 | |
| 374 | if( |
| 375 | #ifdef WSAEWOULDBLOCK |
| 376 | /* This is how Windows does it */ |
| 377 | (WSAEWOULDBLOCK == err) |
| 378 | #else |
| 379 | /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 380 | due to its inability to send off data without blocking. We therefore |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 381 | treat both error codes the same here */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 382 | (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err) || |
| 383 | (EINPROGRESS == err) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 384 | #endif |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 385 | ) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 386 | /* this is just a case of EWOULDBLOCK */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 387 | bytes_written = 0; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 388 | *code = CURLE_AGAIN; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 389 | } |
| 390 | else { |
Haibo Huang | 65021c7 | 2019-03-27 15:37:23 -0700 | [diff] [blame] | 391 | char buffer[STRERROR_LEN]; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 392 | failf(data, "Send failure: %s", |
Haibo Huang | 65021c7 | 2019-03-27 15:37:23 -0700 | [diff] [blame] | 393 | Curl_strerror(err, buffer, sizeof(buffer))); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 394 | data->state.os_errno = err; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 395 | *code = CURLE_SEND_ERROR; |
| 396 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 397 | } |
| 398 | return bytes_written; |
| 399 | } |
| 400 | |
| 401 | /* |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 402 | * Curl_write_plain() is an internal write function that sends data to the |
| 403 | * server using plain sockets only. Otherwise meant to have the exact same |
| 404 | * proto as Curl_write() |
| 405 | */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 406 | CURLcode Curl_write_plain(struct Curl_easy *data, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 407 | curl_socket_t sockfd, |
| 408 | const void *mem, |
| 409 | size_t len, |
| 410 | ssize_t *written) |
| 411 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 412 | CURLcode result; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 413 | struct connectdata *conn = data->conn; |
| 414 | int num; |
| 415 | DEBUGASSERT(conn); |
| 416 | num = (sockfd == conn->sock[SECONDARYSOCKET]); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 417 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 418 | *written = Curl_send_plain(data, num, mem, len, &result); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 419 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 420 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 423 | ssize_t Curl_recv_plain(struct Curl_easy *data, int num, char *buf, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 424 | size_t len, CURLcode *code) |
| 425 | { |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 426 | struct connectdata *conn; |
| 427 | curl_socket_t sockfd; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 428 | ssize_t nread; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 429 | DEBUGASSERT(data); |
| 430 | DEBUGASSERT(data->conn); |
| 431 | conn = data->conn; |
| 432 | sockfd = conn->sock[num]; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 433 | /* Check and return data that already received and storied in internal |
| 434 | intermediate buffer */ |
| 435 | nread = get_pre_recved(conn, num, buf, len); |
| 436 | if(nread > 0) { |
| 437 | *code = CURLE_OK; |
| 438 | return nread; |
| 439 | } |
| 440 | |
| 441 | nread = sread(sockfd, buf, len); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 442 | |
| 443 | *code = CURLE_OK; |
| 444 | if(-1 == nread) { |
| 445 | int err = SOCKERRNO; |
| 446 | |
| 447 | if( |
| 448 | #ifdef WSAEWOULDBLOCK |
| 449 | /* This is how Windows does it */ |
| 450 | (WSAEWOULDBLOCK == err) |
| 451 | #else |
| 452 | /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 453 | due to its inability to send off data without blocking. We therefore |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 454 | treat both error codes the same here */ |
| 455 | (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err) |
| 456 | #endif |
| 457 | ) { |
| 458 | /* this is just a case of EWOULDBLOCK */ |
| 459 | *code = CURLE_AGAIN; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 460 | } |
| 461 | else { |
Haibo Huang | 65021c7 | 2019-03-27 15:37:23 -0700 | [diff] [blame] | 462 | char buffer[STRERROR_LEN]; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 463 | failf(data, "Recv failure: %s", |
Haibo Huang | 65021c7 | 2019-03-27 15:37:23 -0700 | [diff] [blame] | 464 | Curl_strerror(err, buffer, sizeof(buffer))); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 465 | data->state.os_errno = err; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 466 | *code = CURLE_RECV_ERROR; |
| 467 | } |
| 468 | } |
| 469 | return nread; |
| 470 | } |
| 471 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 472 | static CURLcode pausewrite(struct Curl_easy *data, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 473 | int type, /* what type of data */ |
| 474 | const char *ptr, |
| 475 | size_t len) |
| 476 | { |
| 477 | /* signalled to pause sending on this connection, but since we have data |
| 478 | we want to send we need to dup it to save a copy for when the sending |
| 479 | is again enabled */ |
| 480 | struct SingleRequest *k = &data->req; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 481 | struct UrlState *s = &data->state; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 482 | unsigned int i; |
| 483 | bool newtype = TRUE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 484 | |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 485 | /* If this transfers over HTTP/2, pause the stream! */ |
| 486 | Curl_http2_stream_pause(data, TRUE); |
| 487 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 488 | if(s->tempcount) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 489 | for(i = 0; i< s->tempcount; i++) { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 490 | if(s->tempwrite[i].type == type) { |
| 491 | /* data for this type exists */ |
| 492 | newtype = FALSE; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | DEBUGASSERT(i < 3); |
| 497 | } |
| 498 | else |
| 499 | i = 0; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 500 | |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 501 | if(newtype) { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 502 | /* store this information in the state struct for later use */ |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 503 | Curl_dyn_init(&s->tempwrite[i].b, DYN_PAUSE_BUFFER); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 504 | s->tempwrite[i].type = type; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 505 | s->tempcount++; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 506 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 507 | |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 508 | if(Curl_dyn_addn(&s->tempwrite[i].b, (unsigned char *)ptr, len)) |
| 509 | return CURLE_OUT_OF_MEMORY; |
| 510 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 511 | /* mark the connection as RECV paused */ |
| 512 | k->keepon |= KEEP_RECV_PAUSE; |
| 513 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 514 | return CURLE_OK; |
| 515 | } |
| 516 | |
| 517 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 518 | /* chop_write() writes chunks of data not larger than CURL_MAX_WRITE_SIZE via |
| 519 | * client write callback(s) and takes care of pause requests from the |
| 520 | * callbacks. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 521 | */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 522 | static CURLcode chop_write(struct Curl_easy *data, |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 523 | int type, |
| 524 | char *optr, |
| 525 | size_t olen) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 526 | { |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 527 | struct connectdata *conn = data->conn; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 528 | curl_write_callback writeheader = NULL; |
| 529 | curl_write_callback writebody = NULL; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 530 | char *ptr = optr; |
| 531 | size_t len = olen; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 532 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 533 | if(!len) |
| 534 | return CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 535 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 536 | /* If reading is paused, append this data to the already held data for this |
| 537 | type. */ |
| 538 | if(data->req.keepon & KEEP_RECV_PAUSE) |
| 539 | return pausewrite(data, type, ptr, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 540 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 541 | /* Determine the callback(s) to use. */ |
| 542 | if(type & CLIENTWRITE_BODY) |
| 543 | writebody = data->set.fwrite_func; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 544 | if((type & CLIENTWRITE_HEADER) && |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 545 | (data->set.fwrite_header || data->set.writeheader)) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 546 | /* |
| 547 | * Write headers to the same callback or to the especially setup |
| 548 | * header callback function (added after version 7.7.1). |
| 549 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 550 | writeheader = |
| 551 | data->set.fwrite_header? data->set.fwrite_header: data->set.fwrite_func; |
| 552 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 553 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 554 | /* Chop data, write chunks. */ |
| 555 | while(len) { |
| 556 | size_t chunklen = len <= CURL_MAX_WRITE_SIZE? len: CURL_MAX_WRITE_SIZE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 557 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 558 | if(writebody) { |
Haibo Huang | 34ab346 | 2019-05-22 00:50:27 -0700 | [diff] [blame] | 559 | size_t wrote; |
| 560 | Curl_set_in_callback(data, true); |
| 561 | wrote = writebody(ptr, 1, chunklen, data->set.out); |
| 562 | Curl_set_in_callback(data, false); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 563 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 564 | if(CURL_WRITEFUNC_PAUSE == wrote) { |
| 565 | if(conn->handler->flags & PROTOPT_NONETWORK) { |
| 566 | /* Protocols that work without network cannot be paused. This is |
| 567 | actually only FILE:// just now, and it can't pause since the |
| 568 | transfer isn't done using the "normal" procedure. */ |
| 569 | failf(data, "Write callback asked for PAUSE when not supported!"); |
| 570 | return CURLE_WRITE_ERROR; |
| 571 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 572 | return pausewrite(data, type, ptr, len); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 573 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 574 | if(wrote != chunklen) { |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 575 | failf(data, "Failure writing output to destination"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 576 | return CURLE_WRITE_ERROR; |
| 577 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 578 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 579 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 580 | ptr += chunklen; |
| 581 | len -= chunklen; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 584 | if(writeheader) { |
| 585 | size_t wrote; |
| 586 | ptr = optr; |
| 587 | len = olen; |
| 588 | Curl_set_in_callback(data, true); |
| 589 | wrote = writeheader(ptr, 1, len, data->set.writeheader); |
| 590 | Curl_set_in_callback(data, false); |
| 591 | |
| 592 | if(CURL_WRITEFUNC_PAUSE == wrote) |
| 593 | /* here we pass in the HEADER bit only since if this was body as well |
| 594 | then it was passed already and clearly that didn't trigger the |
| 595 | pause, so this is saved for later with the HEADER bit only */ |
| 596 | return pausewrite(data, CLIENTWRITE_HEADER, ptr, len); |
| 597 | |
| 598 | if(wrote != len) { |
| 599 | failf(data, "Failed writing header"); |
| 600 | return CURLE_WRITE_ERROR; |
| 601 | } |
| 602 | } |
| 603 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 604 | return CURLE_OK; |
| 605 | } |
| 606 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 607 | |
| 608 | /* Curl_client_write() sends data to the write callback(s) |
| 609 | |
| 610 | The bit pattern defines to what "streams" to write to. Body and/or header. |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 611 | The defines are in sendf.h of course. "len" is not allowed to be 0. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 612 | |
| 613 | If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the |
| 614 | local character encoding. This is a problem and should be changed in |
| 615 | the future to leave the original data alone. |
| 616 | */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 617 | CURLcode Curl_client_write(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 618 | int type, |
| 619 | char *ptr, |
| 620 | size_t len) |
| 621 | { |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 622 | struct connectdata *conn = data->conn; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 623 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 624 | DEBUGASSERT(len); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 625 | DEBUGASSERT(type <= 3); |
| 626 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 627 | /* FTP data may need conversion. */ |
| 628 | if((type & CLIENTWRITE_BODY) && |
| 629 | (conn->handler->protocol & PROTO_FAMILY_FTP) && |
| 630 | conn->proto.ftpc.transfertype == 'A') { |
| 631 | /* convert from the network encoding */ |
| 632 | CURLcode result = Curl_convert_from_network(data, ptr, len); |
| 633 | /* Curl_convert_from_network calls failf if unsuccessful */ |
| 634 | if(result) |
| 635 | return result; |
| 636 | |
| 637 | #ifdef CURL_DO_LINEEND_CONV |
| 638 | /* convert end-of-line markers */ |
| 639 | len = convert_lineends(data, ptr, len); |
| 640 | #endif /* CURL_DO_LINEEND_CONV */ |
| 641 | } |
| 642 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 643 | return chop_write(data, type, ptr, len); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 646 | CURLcode Curl_read_plain(curl_socket_t sockfd, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 647 | char *buf, |
| 648 | size_t bytesfromsocket, |
| 649 | ssize_t *n) |
| 650 | { |
| 651 | ssize_t nread = sread(sockfd, buf, bytesfromsocket); |
| 652 | |
| 653 | if(-1 == nread) { |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 654 | const int err = SOCKERRNO; |
| 655 | const bool return_error = |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 656 | #ifdef USE_WINSOCK |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 657 | WSAEWOULDBLOCK == err |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 658 | #else |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 659 | EWOULDBLOCK == err || EAGAIN == err || EINTR == err |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 660 | #endif |
Haibo Huang | b51266f | 2020-03-04 02:22:48 -0800 | [diff] [blame] | 661 | ; |
| 662 | *n = 0; /* no data returned */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 663 | if(return_error) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 664 | return CURLE_AGAIN; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 665 | return CURLE_RECV_ERROR; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 668 | *n = nread; |
| 669 | return CURLE_OK; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Internal read-from-socket function. This is meant to deal with plain |
| 674 | * sockets, SSL sockets and kerberos sockets. |
| 675 | * |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 676 | * Returns a regular CURLcode value. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 677 | */ |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 678 | CURLcode Curl_read(struct Curl_easy *data, /* transfer */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 679 | curl_socket_t sockfd, /* read from this socket */ |
| 680 | char *buf, /* store read data here */ |
| 681 | size_t sizerequested, /* max amount to read */ |
| 682 | ssize_t *n) /* amount bytes read */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 683 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 684 | CURLcode result = CURLE_RECV_ERROR; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 685 | ssize_t nread = 0; |
| 686 | size_t bytesfromsocket = 0; |
| 687 | char *buffertofill = NULL; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 688 | struct connectdata *conn = data->conn; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 689 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 690 | /* Set 'num' to 0 or 1, depending on which socket that has been sent here. |
| 691 | If it is the second socket, we set num to 1. Otherwise to 0. This lets |
| 692 | us use the correct ssl handle. */ |
| 693 | int num = (sockfd == conn->sock[SECONDARYSOCKET]); |
| 694 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 695 | *n = 0; /* reset amount to zero */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 696 | |
Haibo Huang | 34ab346 | 2019-05-22 00:50:27 -0700 | [diff] [blame] | 697 | bytesfromsocket = CURLMIN(sizerequested, (size_t)data->set.buffer_size); |
| 698 | buffertofill = buf; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 699 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 700 | nread = conn->recv[num](data, num, buffertofill, bytesfromsocket, &result); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 701 | if(nread < 0) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 702 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 703 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 704 | *n += nread; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 705 | |
| 706 | return CURLE_OK; |
| 707 | } |
| 708 | |
| 709 | /* return 0 on success */ |
Elliott Hughes | 72d948d | 2018-08-03 14:37:21 -0700 | [diff] [blame] | 710 | int Curl_debug(struct Curl_easy *data, curl_infotype type, |
| 711 | char *ptr, size_t size) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 712 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 713 | int rc = 0; |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 714 | if(data->set.verbose) { |
| 715 | static const char s_infotype[CURLINFO_END][3] = { |
| 716 | "* ", "< ", "> ", "{ ", "} ", "{ ", "} " }; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 717 | |
| 718 | #ifdef CURL_DOES_CONVERSIONS |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 719 | char *buf = NULL; |
| 720 | size_t conv_size = 0; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 721 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 722 | switch(type) { |
| 723 | case CURLINFO_HEADER_OUT: |
| 724 | buf = Curl_memdup(ptr, size); |
| 725 | if(!buf) |
| 726 | return 1; |
| 727 | conv_size = size; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 728 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 729 | /* Special processing is needed for this block if it |
| 730 | * contains both headers and data (separated by CRLFCRLF). |
| 731 | * We want to convert just the headers, leaving the data as-is. |
| 732 | */ |
| 733 | if(size > 4) { |
| 734 | size_t i; |
| 735 | for(i = 0; i < size-4; i++) { |
| 736 | if(memcmp(&buf[i], "\x0d\x0a\x0d\x0a", 4) == 0) { |
| 737 | /* convert everything through this CRLFCRLF but no further */ |
| 738 | conv_size = i + 4; |
| 739 | break; |
| 740 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 743 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 744 | Curl_convert_from_network(data, buf, conv_size); |
| 745 | /* Curl_convert_from_network calls failf if unsuccessful */ |
| 746 | /* we might as well continue even if it fails... */ |
| 747 | ptr = buf; /* switch pointer to use my buffer instead */ |
| 748 | break; |
| 749 | default: |
| 750 | /* leave everything else as-is */ |
| 751 | break; |
| 752 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 753 | #endif /* CURL_DOES_CONVERSIONS */ |
| 754 | |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 755 | if(data->set.fdebug) { |
| 756 | Curl_set_in_callback(data, true); |
| 757 | rc = (*data->set.fdebug)(data, type, ptr, size, data->set.debugdata); |
| 758 | Curl_set_in_callback(data, false); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 759 | } |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 760 | else { |
| 761 | switch(type) { |
| 762 | case CURLINFO_TEXT: |
| 763 | case CURLINFO_HEADER_OUT: |
| 764 | case CURLINFO_HEADER_IN: |
| 765 | fwrite(s_infotype[type], 2, 1, data->set.err); |
| 766 | fwrite(ptr, size, 1, data->set.err); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 767 | #ifdef CURL_DOES_CONVERSIONS |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 768 | if(size != conv_size) { |
| 769 | /* we had untranslated data so we need an explicit newline */ |
| 770 | fwrite("\n", 1, 1, data->set.err); |
| 771 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 772 | #endif |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 773 | break; |
| 774 | default: /* nada */ |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | #ifdef CURL_DOES_CONVERSIONS |
| 779 | free(buf); |
| 780 | #endif |
| 781 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 782 | return rc; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 783 | } |