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