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