/src/PROJ/curl/lib/transfer.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
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 |
12 | | * are also available at https://curl.se/docs/copyright.html. |
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 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #ifdef HAVE_NETINET_IN_H |
28 | | #include <netinet/in.h> |
29 | | #endif |
30 | | #ifdef HAVE_NETDB_H |
31 | | #include <netdb.h> |
32 | | #endif |
33 | | #ifdef HAVE_ARPA_INET_H |
34 | | #include <arpa/inet.h> |
35 | | #endif |
36 | | #ifdef HAVE_NET_IF_H |
37 | | #include <net/if.h> |
38 | | #endif |
39 | | #ifdef HAVE_SYS_IOCTL_H |
40 | | #include <sys/ioctl.h> |
41 | | #endif |
42 | | #ifndef UNDER_CE |
43 | | #include <signal.h> |
44 | | #endif |
45 | | |
46 | | #ifdef HAVE_SYS_PARAM_H |
47 | | #include <sys/param.h> |
48 | | #endif |
49 | | |
50 | | #ifdef HAVE_SYS_SELECT_H |
51 | | #include <sys/select.h> |
52 | | #elif defined(HAVE_UNISTD_H) |
53 | | #include <unistd.h> |
54 | | #endif |
55 | | |
56 | | #ifndef HAVE_SOCKET |
57 | | #error "We cannot compile without socket() support!" |
58 | | #endif |
59 | | |
60 | | #include "urldata.h" |
61 | | #include <curl/curl.h> |
62 | | #include "netrc.h" |
63 | | |
64 | | #include "content_encoding.h" |
65 | | #include "hostip.h" |
66 | | #include "cfilters.h" |
67 | | #include "cw-out.h" |
68 | | #include "transfer.h" |
69 | | #include "sendf.h" |
70 | | #include "speedcheck.h" |
71 | | #include "progress.h" |
72 | | #include "http.h" |
73 | | #include "url.h" |
74 | | #include "getinfo.h" |
75 | | #include "vtls/vtls.h" |
76 | | #include "vquic/vquic.h" |
77 | | #include "select.h" |
78 | | #include "multiif.h" |
79 | | #include "connect.h" |
80 | | #include "http2.h" |
81 | | #include "mime.h" |
82 | | #include "hsts.h" |
83 | | #include "setopt.h" |
84 | | #include "headers.h" |
85 | | |
86 | | /* The last 2 #include files should be in this order */ |
87 | | #include "curl_memory.h" |
88 | | #include "memdebug.h" |
89 | | |
90 | | #if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_SMTP) || \ |
91 | | !defined(CURL_DISABLE_IMAP) |
92 | | /* |
93 | | * checkheaders() checks the linked list of custom headers for a |
94 | | * particular header (prefix). Provide the prefix without colon! |
95 | | * |
96 | | * Returns a pointer to the first matching header or NULL if none matched. |
97 | | */ |
98 | | char *Curl_checkheaders(const struct Curl_easy *data, |
99 | | const char *thisheader, |
100 | | const size_t thislen) |
101 | 0 | { |
102 | 0 | struct curl_slist *head; |
103 | 0 | DEBUGASSERT(thislen); |
104 | 0 | DEBUGASSERT(thisheader[thislen-1] != ':'); |
105 | |
|
106 | 0 | for(head = data->set.headers; head; head = head->next) { |
107 | 0 | if(curl_strnequal(head->data, thisheader, thislen) && |
108 | 0 | Curl_headersep(head->data[thislen]) ) |
109 | 0 | return head->data; |
110 | 0 | } |
111 | | |
112 | 0 | return NULL; |
113 | 0 | } |
114 | | #endif |
115 | | |
116 | | static int data_pending(struct Curl_easy *data, bool rcvd_eagain) |
117 | 0 | { |
118 | 0 | struct connectdata *conn = data->conn; |
119 | |
|
120 | 0 | if(conn->handler->protocol&PROTO_FAMILY_FTP) |
121 | 0 | return Curl_conn_data_pending(data, SECONDARYSOCKET); |
122 | | |
123 | | /* in the case of libssh2, we can never be really sure that we have emptied |
124 | | its internal buffers so we MUST always try until we get EAGAIN back */ |
125 | 0 | return (!rcvd_eagain && |
126 | 0 | conn->handler->protocol&(CURLPROTO_SCP|CURLPROTO_SFTP)) || |
127 | 0 | Curl_conn_data_pending(data, FIRSTSOCKET); |
128 | 0 | } |
129 | | |
130 | | /* |
131 | | * Check to see if CURLOPT_TIMECONDITION was met by comparing the time of the |
132 | | * remote document with the time provided by CURLOPT_TIMEVAL |
133 | | */ |
134 | | bool Curl_meets_timecondition(struct Curl_easy *data, time_t timeofdoc) |
135 | 0 | { |
136 | 0 | if((timeofdoc == 0) || (data->set.timevalue == 0)) |
137 | 0 | return TRUE; |
138 | | |
139 | 0 | switch(data->set.timecondition) { |
140 | 0 | case CURL_TIMECOND_IFMODSINCE: |
141 | 0 | default: |
142 | 0 | if(timeofdoc <= data->set.timevalue) { |
143 | 0 | infof(data, |
144 | 0 | "The requested document is not new enough"); |
145 | 0 | data->info.timecond = TRUE; |
146 | 0 | return FALSE; |
147 | 0 | } |
148 | 0 | break; |
149 | 0 | case CURL_TIMECOND_IFUNMODSINCE: |
150 | 0 | if(timeofdoc >= data->set.timevalue) { |
151 | 0 | infof(data, |
152 | 0 | "The requested document is not old enough"); |
153 | 0 | data->info.timecond = TRUE; |
154 | 0 | return FALSE; |
155 | 0 | } |
156 | 0 | break; |
157 | 0 | } |
158 | | |
159 | 0 | return TRUE; |
160 | 0 | } |
161 | | |
162 | | static CURLcode xfer_recv_shutdown(struct Curl_easy *data, bool *done) |
163 | 0 | { |
164 | 0 | if(!data || !data->conn) |
165 | 0 | return CURLE_FAILED_INIT; |
166 | 0 | return Curl_conn_shutdown(data, data->conn->recv_idx, done); |
167 | 0 | } |
168 | | |
169 | | static bool xfer_recv_shutdown_started(struct Curl_easy *data) |
170 | 0 | { |
171 | 0 | if(!data || !data->conn) |
172 | 0 | return FALSE; |
173 | 0 | return Curl_shutdown_started(data, data->conn->recv_idx); |
174 | 0 | } |
175 | | |
176 | | CURLcode Curl_xfer_send_shutdown(struct Curl_easy *data, bool *done) |
177 | 0 | { |
178 | 0 | if(!data || !data->conn) |
179 | 0 | return CURLE_FAILED_INIT; |
180 | 0 | return Curl_conn_shutdown(data, data->conn->send_idx, done); |
181 | 0 | } |
182 | | |
183 | | /** |
184 | | * Receive raw response data for the transfer. |
185 | | * @param data the transfer |
186 | | * @param buf buffer to keep response data received |
187 | | * @param blen length of `buf` |
188 | | * @param eos_reliable if EOS detection in underlying connection is reliable |
189 | | * @param err error code in case of -1 return |
190 | | * @return number of bytes read or -1 for error |
191 | | */ |
192 | | static ssize_t xfer_recv_resp(struct Curl_easy *data, |
193 | | char *buf, size_t blen, |
194 | | bool eos_reliable, |
195 | | CURLcode *err) |
196 | 0 | { |
197 | 0 | size_t nread; |
198 | |
|
199 | 0 | DEBUGASSERT(blen > 0); |
200 | | /* If we are reading BODY data and the connection does NOT handle EOF |
201 | | * and we know the size of the BODY data, limit the read amount */ |
202 | 0 | if(!eos_reliable && !data->req.header && data->req.size != -1) { |
203 | 0 | curl_off_t totalleft = data->req.size - data->req.bytecount; |
204 | 0 | if(totalleft <= 0) |
205 | 0 | blen = 0; |
206 | 0 | else if(totalleft < (curl_off_t)blen) |
207 | 0 | blen = (size_t)totalleft; |
208 | 0 | } |
209 | 0 | else if(xfer_recv_shutdown_started(data)) { |
210 | | /* we already received everything. Do not try more. */ |
211 | 0 | blen = 0; |
212 | 0 | } |
213 | |
|
214 | 0 | if(!blen) { |
215 | | /* want nothing more */ |
216 | 0 | *err = CURLE_OK; |
217 | 0 | nread = 0; |
218 | 0 | } |
219 | 0 | else { |
220 | 0 | *err = Curl_xfer_recv(data, buf, blen, &nread); |
221 | 0 | } |
222 | |
|
223 | 0 | if(*err) |
224 | 0 | return -1; |
225 | 0 | if(nread == 0) { |
226 | 0 | if(data->req.shutdown) { |
227 | 0 | bool done; |
228 | 0 | *err = xfer_recv_shutdown(data, &done); |
229 | 0 | if(*err) |
230 | 0 | return -1; |
231 | 0 | if(!done) { |
232 | 0 | *err = CURLE_AGAIN; |
233 | 0 | return -1; |
234 | 0 | } |
235 | 0 | } |
236 | 0 | DEBUGF(infof(data, "sendrecv_dl: we are done")); |
237 | 0 | } |
238 | 0 | return (ssize_t)nread; |
239 | 0 | } |
240 | | |
241 | | /* |
242 | | * Go ahead and do a read if we have a readable socket or if |
243 | | * the stream was rewound (in which case we have data in a |
244 | | * buffer) |
245 | | */ |
246 | | static CURLcode sendrecv_dl(struct Curl_easy *data, |
247 | | struct SingleRequest *k) |
248 | 0 | { |
249 | 0 | struct connectdata *conn = data->conn; |
250 | 0 | CURLcode result = CURLE_OK; |
251 | 0 | char *buf, *xfer_buf; |
252 | 0 | size_t blen, xfer_blen; |
253 | 0 | int maxloops = 10; |
254 | 0 | curl_off_t total_received = 0; |
255 | 0 | bool is_multiplex = FALSE; |
256 | 0 | bool rcvd_eagain = FALSE; |
257 | 0 | bool is_eos = FALSE; |
258 | |
|
259 | 0 | result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen); |
260 | 0 | if(result) |
261 | 0 | goto out; |
262 | | |
263 | | /* This is where we loop until we have read everything there is to |
264 | | read or we get a CURLE_AGAIN */ |
265 | 0 | do { |
266 | 0 | size_t bytestoread; |
267 | 0 | ssize_t nread; |
268 | |
|
269 | 0 | if(!is_multiplex) { |
270 | | /* Multiplexed connection have inherent handling of EOF and we do not |
271 | | * have to carefully restrict the amount we try to read. |
272 | | * Multiplexed changes only in one direction. */ |
273 | 0 | is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET); |
274 | 0 | } |
275 | |
|
276 | 0 | buf = xfer_buf; |
277 | 0 | bytestoread = xfer_blen; |
278 | |
|
279 | 0 | if(bytestoread && data->set.max_recv_speed > 0) { |
280 | | /* In case of speed limit on receiving: if this loop already got |
281 | | * a quarter of the quota, break out. We want to stutter a bit |
282 | | * to keep in the limit, but too small receives will just cost |
283 | | * cpu unnecessarily. */ |
284 | 0 | if(total_received && (total_received >= (data->set.max_recv_speed / 4))) |
285 | 0 | break; |
286 | 0 | if(data->set.max_recv_speed < (curl_off_t)bytestoread) |
287 | 0 | bytestoread = (size_t)data->set.max_recv_speed; |
288 | 0 | } |
289 | | |
290 | 0 | rcvd_eagain = FALSE; |
291 | 0 | nread = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result); |
292 | 0 | if(nread < 0) { |
293 | 0 | if(CURLE_AGAIN != result) |
294 | 0 | goto out; /* real error */ |
295 | 0 | rcvd_eagain = TRUE; |
296 | 0 | result = CURLE_OK; |
297 | 0 | if(data->req.download_done && data->req.no_body && |
298 | 0 | !data->req.resp_trailer) { |
299 | 0 | DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, " |
300 | 0 | "not waiting for EOS")); |
301 | 0 | nread = 0; |
302 | | /* continue as if we received the EOS */ |
303 | 0 | } |
304 | 0 | else |
305 | 0 | break; /* get out of loop */ |
306 | 0 | } |
307 | | |
308 | | /* We only get a 0-length receive at the end of the response */ |
309 | 0 | blen = (size_t)nread; |
310 | 0 | is_eos = (blen == 0); |
311 | |
|
312 | 0 | if(!blen && (conn->recv[FIRSTSOCKET] == Curl_cf_recv)) { |
313 | | /* if we receive 0 or less here and the protocol handler did not |
314 | | replace the connection's `recv` callback, either the data transfer |
315 | | is done or the server closed the connection and |
316 | | we bail out from this! |
317 | | With a `recv` replacement, we assume the protocol handler knows |
318 | | what it is doing and a 0-length receive is fine. For example, |
319 | | SFTP downloads of an empty file would show this. See #19165. */ |
320 | 0 | if(is_multiplex) |
321 | 0 | DEBUGF(infof(data, "nread == 0, stream closed, bailing")); |
322 | 0 | else |
323 | 0 | DEBUGF(infof(data, "nread <= 0, server closed connection, bailing")); |
324 | 0 | result = Curl_req_stop_send_recv(data); |
325 | 0 | if(result) |
326 | 0 | goto out; |
327 | 0 | if(k->eos_written) /* already did write this to client, leave */ |
328 | 0 | break; |
329 | 0 | } |
330 | 0 | total_received += blen; |
331 | |
|
332 | 0 | result = Curl_xfer_write_resp(data, buf, blen, is_eos); |
333 | 0 | if(result || data->req.done) |
334 | 0 | goto out; |
335 | | |
336 | | /* if we are done, we stop receiving. On multiplexed connections, |
337 | | * we should read the EOS. Which may arrive as meta data after |
338 | | * the bytes. Not taking it in might lead to RST of streams. */ |
339 | 0 | if((!is_multiplex && data->req.download_done) || is_eos) { |
340 | 0 | data->req.keepon &= ~KEEP_RECV; |
341 | 0 | } |
342 | | /* if we are PAUSEd or stopped receiving, leave the loop */ |
343 | 0 | if((k->keepon & KEEP_RECV_PAUSE) || !(k->keepon & KEEP_RECV)) |
344 | 0 | break; |
345 | |
|
346 | 0 | } while(maxloops--); |
347 | | |
348 | 0 | if(!is_eos && !Curl_xfer_is_blocked(data) && |
349 | 0 | (!rcvd_eagain || data_pending(data, rcvd_eagain))) { |
350 | | /* Did not read until EAGAIN/EOS or there is still data pending |
351 | | * in buffers. Mark as read-again via simulated SELECT results. */ |
352 | 0 | Curl_multi_mark_dirty(data); |
353 | 0 | CURL_TRC_M(data, "sendrecv_dl() no EAGAIN/pending data, mark as dirty"); |
354 | 0 | } |
355 | |
|
356 | 0 | if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) && |
357 | 0 | (conn->bits.close || is_multiplex)) { |
358 | | /* When we have read the entire thing and the close bit is set, the server |
359 | | may now close the connection. If there is now any kind of sending going |
360 | | on from our side, we need to stop that immediately. */ |
361 | 0 | infof(data, "we are done reading and this is set to close, stop send"); |
362 | 0 | Curl_req_abort_sending(data); |
363 | 0 | } |
364 | |
|
365 | 0 | out: |
366 | 0 | Curl_multi_xfer_buf_release(data, xfer_buf); |
367 | 0 | if(result) |
368 | 0 | DEBUGF(infof(data, "sendrecv_dl() -> %d", result)); |
369 | 0 | return result; |
370 | 0 | } |
371 | | |
372 | | /* |
373 | | * Send data to upload to the server, when the socket is writable. |
374 | | */ |
375 | | static CURLcode sendrecv_ul(struct Curl_easy *data) |
376 | 0 | { |
377 | | /* We should not get here when the sending is already done. It |
378 | | * probably means that someone set `data-req.keepon |= KEEP_SEND` |
379 | | * when it should not. */ |
380 | 0 | DEBUGASSERT(!Curl_req_done_sending(data)); |
381 | |
|
382 | 0 | if(!Curl_req_done_sending(data)) |
383 | 0 | return Curl_req_send_more(data); |
384 | 0 | return CURLE_OK; |
385 | 0 | } |
386 | | |
387 | | /* |
388 | | * Curl_sendrecv() is the low-level function to be called when data is to |
389 | | * be read and written to/from the connection. |
390 | | */ |
391 | | CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp) |
392 | 0 | { |
393 | 0 | struct SingleRequest *k = &data->req; |
394 | 0 | CURLcode result = CURLE_OK; |
395 | |
|
396 | 0 | DEBUGASSERT(nowp); |
397 | 0 | if(Curl_xfer_is_blocked(data)) { |
398 | 0 | result = CURLE_OK; |
399 | 0 | goto out; |
400 | 0 | } |
401 | | |
402 | | /* We go ahead and do a read if we have a readable socket or if the stream |
403 | | was rewound (in which case we have data in a buffer) */ |
404 | 0 | if(k->keepon & KEEP_RECV) { |
405 | 0 | result = sendrecv_dl(data, k); |
406 | 0 | if(result || data->req.done) |
407 | 0 | goto out; |
408 | 0 | } |
409 | | |
410 | | /* If we still have writing to do, we check if we have a writable socket. */ |
411 | 0 | if(Curl_req_want_send(data) || (data->req.keepon & KEEP_SEND_TIMED)) { |
412 | 0 | result = sendrecv_ul(data); |
413 | 0 | if(result) |
414 | 0 | goto out; |
415 | 0 | } |
416 | | |
417 | 0 | if(Curl_pgrsUpdate(data)) |
418 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
419 | 0 | else |
420 | 0 | result = Curl_speedcheck(data, *nowp); |
421 | 0 | if(result) |
422 | 0 | goto out; |
423 | | |
424 | 0 | if(k->keepon) { |
425 | 0 | if(0 > Curl_timeleft(data, nowp, FALSE)) { |
426 | 0 | if(k->size != -1) { |
427 | 0 | failf(data, "Operation timed out after %" FMT_TIMEDIFF_T |
428 | 0 | " milliseconds with %" FMT_OFF_T " out of %" |
429 | 0 | FMT_OFF_T " bytes received", |
430 | 0 | curlx_timediff(*nowp, data->progress.t_startsingle), |
431 | 0 | k->bytecount, k->size); |
432 | 0 | } |
433 | 0 | else { |
434 | 0 | failf(data, "Operation timed out after %" FMT_TIMEDIFF_T |
435 | 0 | " milliseconds with %" FMT_OFF_T " bytes received", |
436 | 0 | curlx_timediff(*nowp, data->progress.t_startsingle), |
437 | 0 | k->bytecount); |
438 | 0 | } |
439 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
440 | 0 | goto out; |
441 | 0 | } |
442 | 0 | } |
443 | 0 | else { |
444 | | /* |
445 | | * The transfer has been performed. Just make some general checks before |
446 | | * returning. |
447 | | */ |
448 | 0 | if(!(data->req.no_body) && (k->size != -1) && |
449 | 0 | (k->bytecount != k->size) && !k->newurl) { |
450 | 0 | failf(data, "transfer closed with %" FMT_OFF_T |
451 | 0 | " bytes remaining to read", k->size - k->bytecount); |
452 | 0 | result = CURLE_PARTIAL_FILE; |
453 | 0 | goto out; |
454 | 0 | } |
455 | 0 | if(Curl_pgrsUpdate(data)) { |
456 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
457 | 0 | goto out; |
458 | 0 | } |
459 | 0 | } |
460 | | |
461 | | /* If there is nothing more to send/recv, the request is done */ |
462 | 0 | if((k->keepon & (KEEP_RECVBITS|KEEP_SENDBITS)) == 0) |
463 | 0 | data->req.done = TRUE; |
464 | |
|
465 | 0 | out: |
466 | 0 | if(result) |
467 | 0 | DEBUGF(infof(data, "Curl_sendrecv() -> %d", result)); |
468 | 0 | return result; |
469 | 0 | } |
470 | | |
471 | | /* Curl_init_CONNECT() gets called each time the handle switches to CONNECT |
472 | | which means this gets called once for each subsequent redirect etc */ |
473 | | void Curl_init_CONNECT(struct Curl_easy *data) |
474 | 0 | { |
475 | 0 | data->state.fread_func = data->set.fread_func_set; |
476 | 0 | data->state.in = data->set.in_set; |
477 | 0 | data->state.upload = (data->state.httpreq == HTTPREQ_PUT); |
478 | 0 | } |
479 | | |
480 | | /* |
481 | | * Curl_pretransfer() is called immediately before a transfer starts, and only |
482 | | * once for one transfer no matter if it has redirects or do multi-pass |
483 | | * authentication etc. |
484 | | */ |
485 | | CURLcode Curl_pretransfer(struct Curl_easy *data) |
486 | 0 | { |
487 | 0 | CURLcode result = CURLE_OK; |
488 | | |
489 | | /* Reset the retry count at the start of each request. |
490 | | * If the retry count is not reset, when the connection drops, |
491 | | * it will not enter the retry mechanism on CONN_MAX_RETRIES + 1 attempts |
492 | | * and will immediately throw |
493 | | * "Connection died, tried CONN_MAX_RETRIES times before giving up". |
494 | | * By resetting it here, we ensure each new request starts fresh. */ |
495 | 0 | data->state.retrycount = 0; |
496 | |
|
497 | 0 | if(!data->set.str[STRING_SET_URL] && !data->set.uh) { |
498 | | /* we cannot do anything without URL */ |
499 | 0 | failf(data, "No URL set"); |
500 | 0 | return CURLE_URL_MALFORMAT; |
501 | 0 | } |
502 | | |
503 | | /* CURLOPT_CURLU overrides CURLOPT_URL and the contents of the CURLU handle |
504 | | is allowed to be changed by the user between transfers */ |
505 | 0 | if(data->set.uh) { |
506 | 0 | CURLUcode uc; |
507 | 0 | free(data->set.str[STRING_SET_URL]); |
508 | 0 | uc = curl_url_get(data->set.uh, |
509 | 0 | CURLUPART_URL, &data->set.str[STRING_SET_URL], 0); |
510 | 0 | if(uc) { |
511 | 0 | failf(data, "No URL set"); |
512 | 0 | return CURLE_URL_MALFORMAT; |
513 | 0 | } |
514 | 0 | } |
515 | | |
516 | | /* since the URL may have been redirected in a previous use of this handle */ |
517 | 0 | if(data->state.url_alloc) { |
518 | 0 | Curl_safefree(data->state.url); |
519 | 0 | data->state.url_alloc = FALSE; |
520 | 0 | } |
521 | |
|
522 | 0 | data->state.url = data->set.str[STRING_SET_URL]; |
523 | |
|
524 | 0 | if(data->set.postfields && data->set.set_resume_from) { |
525 | | /* we cannot */ |
526 | 0 | failf(data, "cannot mix POSTFIELDS with RESUME_FROM"); |
527 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
528 | 0 | } |
529 | | |
530 | 0 | data->state.prefer_ascii = data->set.prefer_ascii; |
531 | 0 | #ifdef CURL_LIST_ONLY_PROTOCOL |
532 | 0 | data->state.list_only = data->set.list_only; |
533 | 0 | #endif |
534 | 0 | data->state.httpreq = data->set.method; |
535 | |
|
536 | 0 | data->state.requests = 0; |
537 | 0 | data->state.followlocation = 0; /* reset the location-follow counter */ |
538 | 0 | data->state.this_is_a_follow = FALSE; /* reset this */ |
539 | 0 | data->state.errorbuf = FALSE; /* no error has occurred */ |
540 | 0 | #ifndef CURL_DISABLE_HTTP |
541 | 0 | Curl_http_neg_init(data, &data->state.http_neg); |
542 | 0 | #endif |
543 | 0 | data->state.authproblem = FALSE; |
544 | 0 | data->state.authhost.want = data->set.httpauth; |
545 | 0 | data->state.authproxy.want = data->set.proxyauth; |
546 | 0 | Curl_safefree(data->info.wouldredirect); |
547 | 0 | Curl_data_priority_clear_state(data); |
548 | |
|
549 | 0 | if(data->state.httpreq == HTTPREQ_PUT) |
550 | 0 | data->state.infilesize = data->set.filesize; |
551 | 0 | else if((data->state.httpreq != HTTPREQ_GET) && |
552 | 0 | (data->state.httpreq != HTTPREQ_HEAD)) { |
553 | 0 | data->state.infilesize = data->set.postfieldsize; |
554 | 0 | if(data->set.postfields && (data->state.infilesize == -1)) |
555 | 0 | data->state.infilesize = (curl_off_t)strlen(data->set.postfields); |
556 | 0 | } |
557 | 0 | else |
558 | 0 | data->state.infilesize = 0; |
559 | | |
560 | | /* If there is a list of cookie files to read, do it now! */ |
561 | 0 | Curl_cookie_loadfiles(data); |
562 | | |
563 | | /* If there is a list of host pairs to deal with */ |
564 | 0 | if(data->state.resolve) |
565 | 0 | result = Curl_loadhostpairs(data); |
566 | | |
567 | | /* If there is a list of hsts files to read */ |
568 | 0 | Curl_hsts_loadfiles(data); |
569 | |
|
570 | 0 | if(!result) { |
571 | | /* Allow data->set.use_port to set which port to use. This needs to be |
572 | | * disabled for example when we follow Location: headers to URLs using |
573 | | * different ports! */ |
574 | 0 | data->state.allow_port = TRUE; |
575 | |
|
576 | | #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL) |
577 | | /************************************************************* |
578 | | * Tell signal handler to ignore SIGPIPE |
579 | | *************************************************************/ |
580 | | if(!data->set.no_signal) |
581 | | data->state.prev_signal = signal(SIGPIPE, SIG_IGN); |
582 | | #endif |
583 | |
|
584 | 0 | Curl_initinfo(data); /* reset session-specific information "variables" */ |
585 | 0 | Curl_pgrsResetTransferSizes(data); |
586 | 0 | Curl_pgrsStartNow(data); |
587 | | |
588 | | /* In case the handle is reused and an authentication method was picked |
589 | | in the session we need to make sure we only use the one(s) we now |
590 | | consider to be fine */ |
591 | 0 | data->state.authhost.picked &= data->state.authhost.want; |
592 | 0 | data->state.authproxy.picked &= data->state.authproxy.want; |
593 | |
|
594 | 0 | #ifndef CURL_DISABLE_FTP |
595 | 0 | data->state.wildcardmatch = data->set.wildcard_enabled; |
596 | 0 | if(data->state.wildcardmatch) { |
597 | 0 | struct WildcardData *wc; |
598 | 0 | if(!data->wildcard) { |
599 | 0 | data->wildcard = calloc(1, sizeof(struct WildcardData)); |
600 | 0 | if(!data->wildcard) |
601 | 0 | return CURLE_OUT_OF_MEMORY; |
602 | 0 | } |
603 | 0 | wc = data->wildcard; |
604 | 0 | if(wc->state < CURLWC_INIT) { |
605 | 0 | if(wc->ftpwc) |
606 | 0 | wc->dtor(wc->ftpwc); |
607 | 0 | Curl_safefree(wc->pattern); |
608 | 0 | Curl_safefree(wc->path); |
609 | 0 | result = Curl_wildcard_init(wc); /* init wildcard structures */ |
610 | 0 | if(result) |
611 | 0 | return CURLE_OUT_OF_MEMORY; |
612 | 0 | } |
613 | 0 | } |
614 | 0 | #endif |
615 | 0 | result = Curl_hsts_loadcb(data, data->hsts); |
616 | 0 | } |
617 | | |
618 | | /* |
619 | | * Set user-agent. Used for HTTP, but since we can attempt to tunnel |
620 | | * basically anything through an HTTP proxy we cannot limit this based on |
621 | | * protocol. |
622 | | */ |
623 | 0 | if(data->set.str[STRING_USERAGENT]) { |
624 | 0 | free(data->state.aptr.uagent); |
625 | 0 | data->state.aptr.uagent = |
626 | 0 | curl_maprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]); |
627 | 0 | if(!data->state.aptr.uagent) |
628 | 0 | return CURLE_OUT_OF_MEMORY; |
629 | 0 | } |
630 | | |
631 | 0 | if(data->set.str[STRING_USERNAME] || |
632 | 0 | data->set.str[STRING_PASSWORD]) |
633 | 0 | data->state.creds_from = CREDS_OPTION; |
634 | 0 | if(!result) |
635 | 0 | result = Curl_setstropt(&data->state.aptr.user, |
636 | 0 | data->set.str[STRING_USERNAME]); |
637 | 0 | if(!result) |
638 | 0 | result = Curl_setstropt(&data->state.aptr.passwd, |
639 | 0 | data->set.str[STRING_PASSWORD]); |
640 | 0 | #ifndef CURL_DISABLE_PROXY |
641 | 0 | if(!result) |
642 | 0 | result = Curl_setstropt(&data->state.aptr.proxyuser, |
643 | 0 | data->set.str[STRING_PROXYUSERNAME]); |
644 | 0 | if(!result) |
645 | 0 | result = Curl_setstropt(&data->state.aptr.proxypasswd, |
646 | 0 | data->set.str[STRING_PROXYPASSWORD]); |
647 | 0 | #endif |
648 | |
|
649 | 0 | data->req.headerbytecount = 0; |
650 | 0 | Curl_headers_cleanup(data); |
651 | 0 | return result; |
652 | 0 | } |
653 | | |
654 | | /* Returns CURLE_OK *and* sets '*url' if a request retry is wanted. |
655 | | |
656 | | NOTE: that the *url is malloc()ed. */ |
657 | | CURLcode Curl_retry_request(struct Curl_easy *data, char **url) |
658 | 0 | { |
659 | 0 | struct connectdata *conn = data->conn; |
660 | 0 | bool retry = FALSE; |
661 | 0 | *url = NULL; |
662 | | |
663 | | /* if we are talking upload, we cannot do the checks below, unless the |
664 | | protocol is HTTP as when uploading over HTTP we will still get a |
665 | | response */ |
666 | 0 | if(data->state.upload && |
667 | 0 | !(conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_RTSP))) |
668 | 0 | return CURLE_OK; |
669 | | |
670 | 0 | if(conn->bits.reuse && |
671 | 0 | (data->req.bytecount + data->req.headerbytecount == 0) && |
672 | 0 | ((!data->req.no_body && !data->req.done) || |
673 | 0 | (conn->handler->protocol & PROTO_FAMILY_HTTP)) |
674 | 0 | #ifndef CURL_DISABLE_RTSP |
675 | 0 | && (data->set.rtspreq != RTSPREQ_RECEIVE) |
676 | 0 | #endif |
677 | 0 | ) |
678 | | /* We got no data, we attempted to reuse a connection. For HTTP this |
679 | | can be a retry so we try again regardless if we expected a body. |
680 | | For other protocols we only try again only if we expected a body. |
681 | | |
682 | | This might happen if the connection was left alive when we were |
683 | | done using it before, but that was closed when we wanted to read from |
684 | | it again. Bad luck. Retry the same request on a fresh connect! */ |
685 | 0 | retry = TRUE; |
686 | 0 | else if(data->state.refused_stream && |
687 | 0 | (data->req.bytecount + data->req.headerbytecount == 0) ) { |
688 | | /* This was sent on a refused stream, safe to rerun. A refused stream |
689 | | error can typically only happen on HTTP/2 level if the stream is safe |
690 | | to issue again, but the nghttp2 API can deliver the message to other |
691 | | streams as well, which is why this adds the check the data counters |
692 | | too. */ |
693 | 0 | infof(data, "REFUSED_STREAM, retrying a fresh connect"); |
694 | 0 | data->state.refused_stream = FALSE; /* clear again */ |
695 | 0 | retry = TRUE; |
696 | 0 | } |
697 | 0 | if(retry) { |
698 | 0 | #define CONN_MAX_RETRIES 5 |
699 | 0 | if(data->state.retrycount++ >= CONN_MAX_RETRIES) { |
700 | 0 | failf(data, "Connection died, tried %d times before giving up", |
701 | 0 | CONN_MAX_RETRIES); |
702 | 0 | data->state.retrycount = 0; |
703 | 0 | return CURLE_SEND_ERROR; |
704 | 0 | } |
705 | 0 | infof(data, "Connection died, retrying a fresh connect (retry count: %d)", |
706 | 0 | data->state.retrycount); |
707 | 0 | *url = strdup(data->state.url); |
708 | 0 | if(!*url) |
709 | 0 | return CURLE_OUT_OF_MEMORY; |
710 | | |
711 | 0 | connclose(conn, "retry"); /* close this connection */ |
712 | 0 | conn->bits.retry = TRUE; /* mark this as a connection we are about |
713 | | to retry. Marking it this way should |
714 | | prevent i.e HTTP transfers to return |
715 | | error just because nothing has been |
716 | | transferred! */ |
717 | 0 | Curl_creader_set_rewind(data, TRUE); |
718 | 0 | } |
719 | 0 | return CURLE_OK; |
720 | 0 | } |
721 | | |
722 | | static void xfer_setup( |
723 | | struct Curl_easy *data, /* transfer */ |
724 | | int send_idx, /* sockindex to send on or -1 */ |
725 | | int recv_idx, /* sockindex to receive on or -1 */ |
726 | | curl_off_t recv_size /* how much to receive, -1 if unknown */ |
727 | | ) |
728 | 0 | { |
729 | 0 | struct SingleRequest *k = &data->req; |
730 | 0 | struct connectdata *conn = data->conn; |
731 | |
|
732 | 0 | DEBUGASSERT(conn != NULL); |
733 | | /* indexes are in range */ |
734 | 0 | DEBUGASSERT((send_idx <= 1) && (send_idx >= -1)); |
735 | 0 | DEBUGASSERT((recv_idx <= 1) && (recv_idx >= -1)); |
736 | | /* if request wants to send, switching off the send direction is wrong */ |
737 | 0 | DEBUGASSERT((send_idx >= 0) || !Curl_req_want_send(data)); |
738 | |
|
739 | 0 | conn->send_idx = send_idx; |
740 | 0 | conn->recv_idx = recv_idx; |
741 | | |
742 | | /* without receiving, there should be not recv_size */ |
743 | 0 | DEBUGASSERT((conn->recv_idx >= 0) || (recv_size == -1)); |
744 | 0 | k->size = recv_size; |
745 | 0 | k->header = !!conn->handler->write_resp_hd; |
746 | | /* by default, we do not shutdown at the end of the transfer */ |
747 | 0 | k->shutdown = FALSE; |
748 | 0 | k->shutdown_err_ignore = FALSE; |
749 | | |
750 | | /* The code sequence below is placed in this function just because all |
751 | | necessary input is not always known in do_complete() as this function may |
752 | | be called after that */ |
753 | 0 | if(!k->header && (recv_size > 0)) |
754 | 0 | Curl_pgrsSetDownloadSize(data, recv_size); |
755 | | |
756 | | /* we want header and/or body, if neither then do not do this! */ |
757 | 0 | if(conn->handler->write_resp_hd || !data->req.no_body) { |
758 | |
|
759 | 0 | if(conn->recv_idx != -1) |
760 | 0 | k->keepon |= KEEP_RECV; |
761 | |
|
762 | 0 | if(conn->send_idx != -1) |
763 | 0 | k->keepon |= KEEP_SEND; |
764 | 0 | } |
765 | |
|
766 | 0 | CURL_TRC_M(data, "xfer_setup: recv_idx=%d, send_idx=%d", |
767 | 0 | conn->recv_idx, conn->send_idx); |
768 | 0 | } |
769 | | |
770 | | void Curl_xfer_setup_nop(struct Curl_easy *data) |
771 | 0 | { |
772 | 0 | xfer_setup(data, -1, -1, -1); |
773 | 0 | } |
774 | | |
775 | | void Curl_xfer_setup_sendrecv(struct Curl_easy *data, |
776 | | int sockindex, |
777 | | curl_off_t recv_size) |
778 | 0 | { |
779 | 0 | xfer_setup(data, sockindex, sockindex, recv_size); |
780 | 0 | } |
781 | | |
782 | | void Curl_xfer_setup_send(struct Curl_easy *data, |
783 | | int sockindex) |
784 | 0 | { |
785 | 0 | xfer_setup(data, sockindex, -1, -1); |
786 | 0 | } |
787 | | |
788 | | void Curl_xfer_setup_recv(struct Curl_easy *data, |
789 | | int sockindex, |
790 | | curl_off_t recv_size) |
791 | 0 | { |
792 | 0 | xfer_setup(data, -1, sockindex, recv_size); |
793 | 0 | } |
794 | | |
795 | | void Curl_xfer_set_shutdown(struct Curl_easy *data, |
796 | | bool shutdown, |
797 | | bool ignore_errors) |
798 | 0 | { |
799 | | /* Shutdown should only be set when the transfer only sends or receives. */ |
800 | 0 | DEBUGASSERT(!shutdown || |
801 | 0 | (data->conn->send_idx < 0) || (data->conn->recv_idx < 0)); |
802 | 0 | data->req.shutdown = shutdown; |
803 | 0 | data->req.shutdown_err_ignore = ignore_errors; |
804 | 0 | } |
805 | | |
806 | | CURLcode Curl_xfer_write_resp(struct Curl_easy *data, |
807 | | const char *buf, size_t blen, |
808 | | bool is_eos) |
809 | 0 | { |
810 | 0 | CURLcode result = CURLE_OK; |
811 | |
|
812 | 0 | if(data->conn->handler->write_resp) { |
813 | | /* protocol handlers offering this function take full responsibility |
814 | | * for writing all received download data to the client. */ |
815 | 0 | result = data->conn->handler->write_resp(data, buf, blen, is_eos); |
816 | 0 | } |
817 | 0 | else { |
818 | | /* No special handling by protocol handler, write all received data |
819 | | * as BODY to the client. */ |
820 | 0 | if(blen || is_eos) { |
821 | 0 | int cwtype = CLIENTWRITE_BODY; |
822 | 0 | if(is_eos) |
823 | 0 | cwtype |= CLIENTWRITE_EOS; |
824 | 0 | result = Curl_client_write(data, cwtype, buf, blen); |
825 | 0 | } |
826 | 0 | } |
827 | |
|
828 | 0 | if(!result && is_eos) { |
829 | | /* If we wrote the EOS, we are definitely done */ |
830 | 0 | data->req.eos_written = TRUE; |
831 | 0 | data->req.download_done = TRUE; |
832 | 0 | } |
833 | 0 | CURL_TRC_WRITE(data, "xfer_write_resp(len=%zu, eos=%d) -> %d", |
834 | 0 | blen, is_eos, result); |
835 | 0 | return result; |
836 | 0 | } |
837 | | |
838 | | bool Curl_xfer_write_is_paused(struct Curl_easy *data) |
839 | 0 | { |
840 | 0 | return Curl_cwriter_is_paused(data); |
841 | 0 | } |
842 | | |
843 | | CURLcode Curl_xfer_write_resp_hd(struct Curl_easy *data, |
844 | | const char *hd0, size_t hdlen, bool is_eos) |
845 | 0 | { |
846 | 0 | if(data->conn->handler->write_resp_hd) { |
847 | | /* protocol handlers offering this function take full responsibility |
848 | | * for writing all received download data to the client. */ |
849 | 0 | return data->conn->handler->write_resp_hd(data, hd0, hdlen, is_eos); |
850 | 0 | } |
851 | | /* No special handling by protocol handler, write as response bytes */ |
852 | 0 | return Curl_xfer_write_resp(data, hd0, hdlen, is_eos); |
853 | 0 | } |
854 | | |
855 | | CURLcode Curl_xfer_write_done(struct Curl_easy *data, bool premature) |
856 | 0 | { |
857 | 0 | (void)premature; |
858 | 0 | return Curl_cw_out_done(data); |
859 | 0 | } |
860 | | |
861 | | bool Curl_xfer_needs_flush(struct Curl_easy *data) |
862 | 0 | { |
863 | 0 | return Curl_conn_needs_flush(data, data->conn->send_idx); |
864 | 0 | } |
865 | | |
866 | | CURLcode Curl_xfer_flush(struct Curl_easy *data) |
867 | 0 | { |
868 | 0 | return Curl_conn_flush(data, data->conn->send_idx); |
869 | 0 | } |
870 | | |
871 | | CURLcode Curl_xfer_send(struct Curl_easy *data, |
872 | | const void *buf, size_t blen, bool eos, |
873 | | size_t *pnwritten) |
874 | 0 | { |
875 | 0 | CURLcode result; |
876 | |
|
877 | 0 | DEBUGASSERT(data); |
878 | 0 | DEBUGASSERT(data->conn); |
879 | |
|
880 | 0 | result = Curl_conn_send(data, data->conn->send_idx, |
881 | 0 | buf, blen, eos, pnwritten); |
882 | 0 | if(result == CURLE_AGAIN) { |
883 | 0 | result = CURLE_OK; |
884 | 0 | *pnwritten = 0; |
885 | 0 | } |
886 | 0 | else if(!result && *pnwritten) |
887 | 0 | data->info.request_size += *pnwritten; |
888 | |
|
889 | 0 | DEBUGF(infof(data, "Curl_xfer_send(len=%zu, eos=%d) -> %d, %zu", |
890 | 0 | blen, eos, result, *pnwritten)); |
891 | 0 | return result; |
892 | 0 | } |
893 | | |
894 | | CURLcode Curl_xfer_recv(struct Curl_easy *data, |
895 | | char *buf, size_t blen, |
896 | | size_t *pnrcvd) |
897 | 0 | { |
898 | 0 | DEBUGASSERT(data); |
899 | 0 | DEBUGASSERT(data->conn); |
900 | 0 | DEBUGASSERT(data->set.buffer_size > 0); |
901 | |
|
902 | 0 | if((size_t)data->set.buffer_size < blen) |
903 | 0 | blen = (size_t)data->set.buffer_size; |
904 | 0 | return Curl_conn_recv(data, data->conn->recv_idx, buf, blen, pnrcvd); |
905 | 0 | } |
906 | | |
907 | | CURLcode Curl_xfer_send_close(struct Curl_easy *data) |
908 | 0 | { |
909 | 0 | Curl_conn_ev_data_done_send(data); |
910 | 0 | return CURLE_OK; |
911 | 0 | } |
912 | | |
913 | | bool Curl_xfer_is_blocked(struct Curl_easy *data) |
914 | 0 | { |
915 | 0 | bool want_send = ((data)->req.keepon & KEEP_SEND); |
916 | 0 | bool want_recv = ((data)->req.keepon & KEEP_RECV); |
917 | 0 | if(!want_send) |
918 | 0 | return want_recv && Curl_xfer_recv_is_paused(data); |
919 | 0 | else if(!want_recv) |
920 | 0 | return want_send && Curl_xfer_send_is_paused(data); |
921 | 0 | else |
922 | 0 | return Curl_xfer_recv_is_paused(data) && Curl_xfer_send_is_paused(data); |
923 | 0 | } |
924 | | |
925 | | bool Curl_xfer_send_is_paused(struct Curl_easy *data) |
926 | 0 | { |
927 | 0 | return (data->req.keepon & KEEP_SEND_PAUSE); |
928 | 0 | } |
929 | | |
930 | | bool Curl_xfer_recv_is_paused(struct Curl_easy *data) |
931 | 0 | { |
932 | 0 | return (data->req.keepon & KEEP_RECV_PAUSE); |
933 | 0 | } |
934 | | |
935 | | CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable) |
936 | 0 | { |
937 | 0 | CURLcode result = CURLE_OK; |
938 | 0 | if(enable) { |
939 | 0 | data->req.keepon |= KEEP_SEND_PAUSE; |
940 | 0 | } |
941 | 0 | else { |
942 | 0 | data->req.keepon &= ~KEEP_SEND_PAUSE; |
943 | 0 | if(Curl_creader_is_paused(data)) |
944 | 0 | result = Curl_creader_unpause(data); |
945 | 0 | } |
946 | 0 | return result; |
947 | 0 | } |
948 | | |
949 | | CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable) |
950 | 0 | { |
951 | 0 | CURLcode result = CURLE_OK; |
952 | 0 | if(enable) { |
953 | 0 | data->req.keepon |= KEEP_RECV_PAUSE; |
954 | 0 | } |
955 | 0 | else { |
956 | 0 | data->req.keepon &= ~KEEP_RECV_PAUSE; |
957 | 0 | if(Curl_cwriter_is_paused(data)) |
958 | 0 | result = Curl_cwriter_unpause(data); |
959 | 0 | } |
960 | 0 | Curl_conn_ev_data_pause(data, enable); |
961 | 0 | return result; |
962 | 0 | } |
963 | | |
964 | | bool Curl_xfer_is_too_fast(struct Curl_easy *data) |
965 | 0 | { |
966 | 0 | struct Curl_llist_node *e = Curl_llist_head(&data->state.timeoutlist); |
967 | 0 | while(e) { |
968 | 0 | struct time_node *n = Curl_node_elem(e); |
969 | 0 | e = Curl_node_next(e); |
970 | 0 | if(n->eid == EXPIRE_TOOFAST) |
971 | 0 | return TRUE; |
972 | 0 | } |
973 | 0 | return FALSE; |
974 | 0 | } |