VirtualBox

source: vbox/trunk/src/libs/curl-7.83.1/lib/pingpong.c@ 97122

最後變更 在這個檔案從97122是 95312,由 vboxsync 提交於 3 年 前

libs/{curl,libxml2}: OSE export fixes, bugref:8515

  • 屬性 svn:eol-style 設為 native
檔案大小: 15.1 KB
 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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 * 'pingpong' is for generic back-and-forth support functions used by FTP,
22 * IMAP, POP3, SMTP and whatever more that likes them.
23 *
24 ***************************************************************************/
25
26#include "curl_setup.h"
27
28#include "urldata.h"
29#include "sendf.h"
30#include "select.h"
31#include "progress.h"
32#include "speedcheck.h"
33#include "pingpong.h"
34#include "multiif.h"
35#include "vtls/vtls.h"
36
37/* The last 3 #include files should be in this order */
38#include "curl_printf.h"
39#include "curl_memory.h"
40#include "memdebug.h"
41
42#ifdef USE_PINGPONG
43
44/* Returns timeout in ms. 0 or negative number means the timeout has already
45 triggered */
46timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
47 struct pingpong *pp, bool disconnecting)
48{
49 struct connectdata *conn = data->conn;
50 timediff_t timeout_ms; /* in milliseconds */
51 timediff_t response_time = (data->set.server_response_timeout)?
52 data->set.server_response_timeout: pp->response_time;
53
54 /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
55 remaining time, or use pp->response because SERVER_RESPONSE_TIMEOUT is
56 supposed to govern the response for any given server response, not for
57 the time from connect to the given server response. */
58
59 /* Without a requested timeout, we only wait 'response_time' seconds for the
60 full response to arrive before we bail out */
61 timeout_ms = response_time -
62 Curl_timediff(Curl_now(), pp->response); /* spent time */
63
64 if(data->set.timeout && !disconnecting) {
65 /* if timeout is requested, find out how much remaining time we have */
66 timediff_t timeout2_ms = data->set.timeout - /* timeout time */
67 Curl_timediff(Curl_now(), conn->now); /* spent time */
68
69 /* pick the lowest number */
70 timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
71 }
72
73 return timeout_ms;
74}
75
76/*
77 * Curl_pp_statemach()
78 */
79CURLcode Curl_pp_statemach(struct Curl_easy *data,
80 struct pingpong *pp, bool block,
81 bool disconnecting)
82{
83 struct connectdata *conn = data->conn;
84 curl_socket_t sock = conn->sock[FIRSTSOCKET];
85 int rc;
86 timediff_t interval_ms;
87 timediff_t timeout_ms = Curl_pp_state_timeout(data, pp, disconnecting);
88 CURLcode result = CURLE_OK;
89
90 if(timeout_ms <= 0) {
91 failf(data, "server response timeout");
92 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
93 }
94
95 if(block) {
96 interval_ms = 1000; /* use 1 second timeout intervals */
97 if(timeout_ms < interval_ms)
98 interval_ms = timeout_ms;
99 }
100 else
101 interval_ms = 0; /* immediate */
102
103 if(Curl_ssl_data_pending(conn, FIRSTSOCKET))
104 rc = 1;
105 else if(Curl_pp_moredata(pp))
106 /* We are receiving and there is data in the cache so just read it */
107 rc = 1;
108 else if(!pp->sendleft && Curl_ssl_data_pending(conn, FIRSTSOCKET))
109 /* We are receiving and there is data ready in the SSL library */
110 rc = 1;
111 else
112 rc = Curl_socket_check(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
113 CURL_SOCKET_BAD,
114 pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
115 interval_ms);
116
117 if(block) {
118 /* if we didn't wait, we don't have to spend time on this now */
119 if(Curl_pgrsUpdate(data))
120 result = CURLE_ABORTED_BY_CALLBACK;
121 else
122 result = Curl_speedcheck(data, Curl_now());
123
124 if(result)
125 return result;
126 }
127
128 if(rc == -1) {
129 failf(data, "select/poll error");
130 result = CURLE_OUT_OF_MEMORY;
131 }
132 else if(rc)
133 result = pp->statemachine(data, data->conn);
134
135 return result;
136}
137
138/* initialize stuff to prepare for reading a fresh new response */
139void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp)
140{
141 DEBUGASSERT(data);
142 pp->nread_resp = 0;
143 pp->linestart_resp = data->state.buffer;
144 pp->pending_resp = TRUE;
145 pp->response = Curl_now(); /* start response time-out now! */
146}
147
148/* setup for the coming transfer */
149void Curl_pp_setup(struct pingpong *pp)
150{
151 Curl_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
152}
153
154/***********************************************************************
155 *
156 * Curl_pp_vsendf()
157 *
158 * Send the formatted string as a command to a pingpong server. Note that
159 * the string should not have any CRLF appended, as this function will
160 * append the necessary things itself.
161 *
162 * made to never block
163 */
164CURLcode Curl_pp_vsendf(struct Curl_easy *data,
165 struct pingpong *pp,
166 const char *fmt,
167 va_list args)
168{
169 ssize_t bytes_written = 0;
170 size_t write_len;
171 char *s;
172 CURLcode result;
173 struct connectdata *conn = data->conn;
174
175#ifdef HAVE_GSSAPI
176 enum protection_level data_sec;
177#endif
178
179 DEBUGASSERT(pp->sendleft == 0);
180 DEBUGASSERT(pp->sendsize == 0);
181 DEBUGASSERT(pp->sendthis == NULL);
182
183 if(!conn)
184 /* can't send without a connection! */
185 return CURLE_SEND_ERROR;
186
187 Curl_dyn_reset(&pp->sendbuf);
188 result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
189 if(result)
190 return result;
191
192 /* append CRLF */
193 result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
194 if(result)
195 return result;
196
197 write_len = Curl_dyn_len(&pp->sendbuf);
198 s = Curl_dyn_ptr(&pp->sendbuf);
199 Curl_pp_init(data, pp);
200
201#ifdef HAVE_GSSAPI
202 conn->data_prot = PROT_CMD;
203#endif
204 result = Curl_write(data, conn->sock[FIRSTSOCKET], s, write_len,
205 &bytes_written);
206 if(result)
207 return result;
208#ifdef HAVE_GSSAPI
209 data_sec = conn->data_prot;
210 DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
211 conn->data_prot = data_sec;
212#endif
213
214 Curl_debug(data, CURLINFO_HEADER_OUT, s, (size_t)bytes_written);
215
216 if(bytes_written != (ssize_t)write_len) {
217 /* the whole chunk was not sent, keep it around and adjust sizes */
218 pp->sendthis = s;
219 pp->sendsize = write_len;
220 pp->sendleft = write_len - bytes_written;
221 }
222 else {
223 pp->sendthis = NULL;
224 pp->sendleft = pp->sendsize = 0;
225 pp->response = Curl_now();
226 }
227
228 return CURLE_OK;
229}
230
231
232/***********************************************************************
233 *
234 * Curl_pp_sendf()
235 *
236 * Send the formatted string as a command to a pingpong server. Note that
237 * the string should not have any CRLF appended, as this function will
238 * append the necessary things itself.
239 *
240 * made to never block
241 */
242CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
243 const char *fmt, ...)
244{
245 CURLcode result;
246 va_list ap;
247 va_start(ap, fmt);
248
249 result = Curl_pp_vsendf(data, pp, fmt, ap);
250
251 va_end(ap);
252
253 return result;
254}
255
256/*
257 * Curl_pp_readresp()
258 *
259 * Reads a piece of a server response.
260 */
261CURLcode Curl_pp_readresp(struct Curl_easy *data,
262 curl_socket_t sockfd,
263 struct pingpong *pp,
264 int *code, /* return the server code if done */
265 size_t *size) /* size of the response */
266{
267 ssize_t perline; /* count bytes per line */
268 bool keepon = TRUE;
269 ssize_t gotbytes;
270 char *ptr;
271 struct connectdata *conn = data->conn;
272 char * const buf = data->state.buffer;
273 CURLcode result = CURLE_OK;
274
275 *code = 0; /* 0 for errors or not done */
276 *size = 0;
277
278 ptr = buf + pp->nread_resp;
279
280 /* number of bytes in the current line, so far */
281 perline = (ssize_t)(ptr-pp->linestart_resp);
282
283 while((pp->nread_resp < (size_t)data->set.buffer_size) &&
284 (keepon && !result)) {
285
286 if(pp->cache) {
287 /* we had data in the "cache", copy that instead of doing an actual
288 * read
289 *
290 * pp->cache_size is cast to ssize_t here. This should be safe, because
291 * it would have been populated with something of size int to begin
292 * with, even though its datatype may be larger than an int.
293 */
294 if((ptr + pp->cache_size) > (buf + data->set.buffer_size + 1)) {
295 failf(data, "cached response data too big to handle");
296 return CURLE_WEIRD_SERVER_REPLY;
297 }
298 memcpy(ptr, pp->cache, pp->cache_size);
299 gotbytes = (ssize_t)pp->cache_size;
300 free(pp->cache); /* free the cache */
301 pp->cache = NULL; /* clear the pointer */
302 pp->cache_size = 0; /* zero the size just in case */
303 }
304 else {
305#ifdef HAVE_GSSAPI
306 enum protection_level prot = conn->data_prot;
307 conn->data_prot = PROT_CLEAR;
308#endif
309 DEBUGASSERT((ptr + data->set.buffer_size - pp->nread_resp) <=
310 (buf + data->set.buffer_size + 1));
311 result = Curl_read(data, sockfd, ptr,
312 data->set.buffer_size - pp->nread_resp,
313 &gotbytes);
314#ifdef HAVE_GSSAPI
315 DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
316 conn->data_prot = prot;
317#endif
318 if(result == CURLE_AGAIN)
319 return CURLE_OK; /* return */
320
321 if(result)
322 /* Set outer result variable to this error. */
323 keepon = FALSE;
324 }
325
326 if(!keepon)
327 ;
328 else if(gotbytes <= 0) {
329 keepon = FALSE;
330 result = CURLE_RECV_ERROR;
331 failf(data, "response reading failed");
332 }
333 else {
334 /* we got a whole chunk of data, which can be anything from one
335 * byte to a set of lines and possible just a piece of the last
336 * line */
337 ssize_t i;
338 ssize_t clipamount = 0;
339 bool restart = FALSE;
340
341 data->req.headerbytecount += (long)gotbytes;
342
343 pp->nread_resp += gotbytes;
344 for(i = 0; i < gotbytes; ptr++, i++) {
345 perline++;
346 if(*ptr == '\n') {
347 /* a newline is CRLF in pp-talk, so the CR is ignored as
348 the line isn't really terminated until the LF comes */
349
350 /* output debug output if that is requested */
351#ifdef HAVE_GSSAPI
352 if(!conn->sec_complete)
353#endif
354 Curl_debug(data, CURLINFO_HEADER_IN,
355 pp->linestart_resp, (size_t)perline);
356
357 /*
358 * We pass all response-lines to the callback function registered
359 * for "headers". The response lines can be seen as a kind of
360 * headers.
361 */
362 result = Curl_client_write(data, CLIENTWRITE_HEADER,
363 pp->linestart_resp, perline);
364 if(result)
365 return result;
366
367 if(pp->endofresp(data, conn, pp->linestart_resp, perline, code)) {
368 /* This is the end of the last line, copy the last line to the
369 start of the buffer and null-terminate, for old times sake */
370 size_t n = ptr - pp->linestart_resp;
371 memmove(buf, pp->linestart_resp, n);
372 buf[n] = 0; /* null-terminate */
373 keepon = FALSE;
374 pp->linestart_resp = ptr + 1; /* advance pointer */
375 i++; /* skip this before getting out */
376
377 *size = pp->nread_resp; /* size of the response */
378 pp->nread_resp = 0; /* restart */
379 break;
380 }
381 perline = 0; /* line starts over here */
382 pp->linestart_resp = ptr + 1;
383 }
384 }
385
386 if(!keepon && (i != gotbytes)) {
387 /* We found the end of the response lines, but we didn't parse the
388 full chunk of data we have read from the server. We therefore need
389 to store the rest of the data to be checked on the next invoke as
390 it may actually contain another end of response already! */
391 clipamount = gotbytes - i;
392 restart = TRUE;
393 DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
394 "server response left",
395 (int)clipamount));
396 }
397 else if(keepon) {
398
399 if((perline == gotbytes) && (gotbytes > data->set.buffer_size/2)) {
400 /* We got an excessive line without newlines and we need to deal
401 with it. We keep the first bytes of the line then we throw
402 away the rest. */
403 infof(data, "Excessive server response line length received, "
404 "%zd bytes. Stripping", gotbytes);
405 restart = TRUE;
406
407 /* we keep 40 bytes since all our pingpong protocols are only
408 interested in the first piece */
409 clipamount = 40;
410 }
411 else if(pp->nread_resp > (size_t)data->set.buffer_size/2) {
412 /* We got a large chunk of data and there's potentially still
413 trailing data to take care of, so we put any such part in the
414 "cache", clear the buffer to make space and restart. */
415 clipamount = perline;
416 restart = TRUE;
417 }
418 }
419 else if(i == gotbytes)
420 restart = TRUE;
421
422 if(clipamount) {
423 pp->cache_size = clipamount;
424 pp->cache = malloc(pp->cache_size);
425 if(pp->cache)
426 memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
427 else
428 return CURLE_OUT_OF_MEMORY;
429 }
430 if(restart) {
431 /* now reset a few variables to start over nicely from the start of
432 the big buffer */
433 pp->nread_resp = 0; /* start over from scratch in the buffer */
434 ptr = pp->linestart_resp = buf;
435 perline = 0;
436 }
437
438 } /* there was data */
439
440 } /* while there's buffer left and loop is requested */
441
442 pp->pending_resp = FALSE;
443
444 return result;
445}
446
447int Curl_pp_getsock(struct Curl_easy *data,
448 struct pingpong *pp, curl_socket_t *socks)
449{
450 struct connectdata *conn = data->conn;
451 socks[0] = conn->sock[FIRSTSOCKET];
452
453 if(pp->sendleft) {
454 /* write mode */
455 return GETSOCK_WRITESOCK(0);
456 }
457
458 /* read mode */
459 return GETSOCK_READSOCK(0);
460}
461
462CURLcode Curl_pp_flushsend(struct Curl_easy *data,
463 struct pingpong *pp)
464{
465 /* we have a piece of a command still left to send */
466 struct connectdata *conn = data->conn;
467 ssize_t written;
468 curl_socket_t sock = conn->sock[FIRSTSOCKET];
469 CURLcode result = Curl_write(data, sock, pp->sendthis + pp->sendsize -
470 pp->sendleft, pp->sendleft, &written);
471 if(result)
472 return result;
473
474 if(written != (ssize_t)pp->sendleft) {
475 /* only a fraction was sent */
476 pp->sendleft -= written;
477 }
478 else {
479 pp->sendthis = NULL;
480 pp->sendleft = pp->sendsize = 0;
481 pp->response = Curl_now();
482 }
483 return CURLE_OK;
484}
485
486CURLcode Curl_pp_disconnect(struct pingpong *pp)
487{
488 Curl_dyn_free(&pp->sendbuf);
489 Curl_safefree(pp->cache);
490 return CURLE_OK;
491}
492
493bool Curl_pp_moredata(struct pingpong *pp)
494{
495 return (!pp->sendleft && pp->cache && pp->nread_resp < pp->cache_size) ?
496 TRUE : FALSE;
497}
498
499#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette