master
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 * 'pingpong' is for generic back-and-forth support functions used by FTP,
24 * IMAP, POP3, SMTP and whatever more that likes them.
25 *
26 ***************************************************************************/
27#include "curl_setup.h"
29#include "urldata.h"
30#include "pingpong.h"
32#ifdef USE_PINGPONG
34#include "cfilters.h"
35#include "connect.h"
36#include "multiif.h"
37#include "sendf.h"
38#include "curl_trc.h"
39#include "select.h"
40#include "progress.h"
42timediff_t Curl_pp_state_timeleft_ms(struct Curl_easy *data,
43 struct pingpong *pp)
44{
45 timediff_t xfer_remain_ms;
46 timediff_t remain_ms = data->set.server_response_timeout ?
47 data->set.server_response_timeout : PINGPONG_TIMEOUT_MS;
49 /* If the overall transfer has less time remaining than pingpong
50 * has otherwise for the state, return that. */
51 remain_ms -= curlx_ptimediff_ms(Curl_pgrs_now(data), &pp->response);
52 /* transfer remaining time is 0, when it has no timeout. */
53 xfer_remain_ms = Curl_timeleft_ms(data);
54 if(xfer_remain_ms)
55 return CURLMIN(remain_ms, xfer_remain_ms);
56 return remain_ms;
57}
59/*
60 * Curl_pp_statemach()
61 */
62CURLcode Curl_pp_statemach(struct Curl_easy *data,
63 struct pingpong *pp, bool block,
64 bool disconnecting)
65{
66 struct connectdata *conn = data->conn;
67 curl_socket_t sock = conn->sock[FIRSTSOCKET];
68 int rc;
69 timediff_t interval_ms;
70 timediff_t timeout_ms = Curl_pp_state_timeleft_ms(data, pp);
71 CURLcode result = CURLE_OK;
73 if(timeout_ms <= 0) {
74 failf(data, "server response timeout");
75 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
76 }
78 if(block) {
79 interval_ms = 1000; /* use 1 second timeout intervals */
80 if(timeout_ms < interval_ms)
81 interval_ms = timeout_ms;
82 }
83 else {
84 interval_ms = 0; /* immediate */
85 /* Wake the multi socket API even when the server sends no response. */
86 Curl_expire(data, timeout_ms, EXPIRE_SERVER_RESPONSE);
87 }
89 if(Curl_conn_data_pending(data, FIRSTSOCKET))
90 rc = 1;
91 else if(pp->overflow)
92 /* We are receiving and there is data in the cache so read it */
93 rc = 1;
94 else if(!pp->sendleft && Curl_conn_data_pending(data, FIRSTSOCKET))
95 /* We are receiving and there is data ready in the SSL library */
96 rc = 1;
97 else {
98 rc = Curl_socket_check(pp->sendleft ? CURL_SOCKET_BAD : sock, /* reading */
99 CURL_SOCKET_BAD,
100 pp->sendleft ? sock : CURL_SOCKET_BAD, /* writing */
101 interval_ms);
102 }
104 if(block) {
105 /* if we did not wait, we do not have to spend time on this now */
106 result = Curl_pgrsCheck(data);
107 if(result)
108 return result;
109 }
111 if(rc == -1) {
112 failf(data, "select/poll error");
113 result = CURLE_OUT_OF_MEMORY;
114 }
115 else if(rc)
116 result = pp->statemachine(data, data->conn);
117 else if(disconnecting)
118 return CURLE_OPERATION_TIMEDOUT;
120 return result;
121}
123/* initialize stuff to prepare for reading a fresh new response */
124void Curl_pp_init(struct pingpong *pp, const struct curltime *pnow)
125{
126 DEBUGASSERT(!pp->initialized);
127 pp->nread_resp = 0;
128 pp->response = *pnow; /* start response time-out */
129 pp->pending_resp = TRUE;
130 curlx_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
131 curlx_dyn_init(&pp->recvbuf, DYN_PINGPPONG_CMD);
132 pp->initialized = TRUE;
133}
135CURLcode Curl_pp_vsendf(struct Curl_easy *data,
136 struct pingpong *pp,
137 const char *prefix,
138 const char *fmt,
139 va_list args)
140{
141 size_t bytes_written = 0;
142 size_t write_len;
143 char *s;
144 CURLcode result = CURLE_OK;
145 struct connectdata *conn = data->conn;
147 DEBUGASSERT(pp->sendleft == 0);
148 DEBUGASSERT(pp->sendsize == 0);
149 DEBUGASSERT(!pp->sendthis);
151 if(!conn)
152 /* cannot send without a connection! */
153 return CURLE_SEND_ERROR;
155 curlx_dyn_reset(&pp->sendbuf);
156 if(prefix) {
157 result = curlx_dyn_add(&pp->sendbuf, prefix);
158 if(!result)
159 result = curlx_dyn_addn(&pp->sendbuf, " ", 1);
160 }
161 if(!result)
162 result = curlx_dyn_vaddf(&pp->sendbuf, fmt, args);
163 if(result)
164 return result;
166 /* append CRLF */
167 result = curlx_dyn_addn(&pp->sendbuf, "\r\n", 2);
168 if(result)
169 return result;
171 pp->pending_resp = TRUE;
172 write_len = curlx_dyn_len(&pp->sendbuf);
173 s = curlx_dyn_ptr(&pp->sendbuf);
175 result = Curl_conn_send(data, FIRSTSOCKET, s, write_len, FALSE,
176 &bytes_written);
177 if(result == CURLE_AGAIN) {
178 bytes_written = 0;
179 }
180 else if(result)
181 return result;
183 Curl_debug(data, CURLINFO_HEADER_OUT, s, bytes_written);
185 if(bytes_written < write_len) {
186 /* the whole chunk was not sent, keep it around and adjust sizes */
187 pp->sendthis = s;
188 pp->sendsize = write_len;
189 pp->sendleft = write_len - bytes_written;
190 }
191 else {
192 pp->sendthis = NULL;
193 pp->sendleft = pp->sendsize = 0;
194 pp->response = *Curl_pgrs_now(data);
195 }
197 return CURLE_OK;
198}
200CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
201 const char *fmt, ...)
202{
203 CURLcode result;
204 va_list ap;
205 va_start(ap, fmt);
207 result = Curl_pp_vsendf(data, pp, NULL, fmt, ap);
209 va_end(ap);
211 return result;
212}
214static CURLcode pingpong_read(struct Curl_easy *data,
215 int8_t sockindex,
216 char *buffer,
217 size_t buflen,
218 size_t *nread)
219{
220 return Curl_conn_recv(data, sockindex, buffer, buflen, nread);
221}
223/*
224 * Curl_pp_readresp()
225 *
226 * Reads a piece of a server response.
227 */
228CURLcode Curl_pp_readresp(struct Curl_easy *data,
229 int8_t sockindex,
230 struct pingpong *pp,
231 int *code, /* return the server code if done */
232 size_t *size) /* size of the response */
233{
234 struct connectdata *conn = data->conn;
235 CURLcode result = CURLE_OK;
236 size_t gotbytes;
237 char buffer[900];
238 int maxloops = 10;
240 *code = 0; /* 0 for errors or not done */
241 *size = 0;
243 do {
244 gotbytes = 0;
245 if(pp->nfinal) {
246 /* a previous call left this many bytes in the beginning of the buffer as
247 that was the final line; now ditch that */
248 size_t full = curlx_dyn_len(&pp->recvbuf);
250 /* trim off the "final" leading part */
251 curlx_dyn_tail(&pp->recvbuf, full - pp->nfinal);
253 pp->nfinal = 0; /* now gone */
254 }
255 if(!pp->overflow) {
256 if(!maxloops--) /* don't get stuck */
257 return CURLE_OK;
259 result = pingpong_read(data, sockindex, buffer, sizeof(buffer),
260 &gotbytes);
261 if(result == CURLE_AGAIN)
262 return CURLE_OK;
264 if(result)
265 return result;
267 if(!gotbytes) {
268 failf(data, "response reading failed (errno: %d)", SOCKERRNO);
269 return CURLE_RECV_ERROR;
270 }
272 result = curlx_dyn_addn(&pp->recvbuf, buffer, gotbytes);
273 if(result)
274 return result;
276 data->req.headerbytecount += (unsigned int)gotbytes;
278 pp->nread_resp += gotbytes;
279 }
281 do {
282 const char *line = curlx_dyn_ptr(&pp->recvbuf);
283 const char *nl = memchr(line, '\n', curlx_dyn_len(&pp->recvbuf));
284 if(nl) {
285 /* a newline is CRLF in pp-talk, so the CR is ignored as
286 the line is not really terminated until the LF comes */
287 size_t length = nl - line + 1;
289 if(memchr(line, 0, length)) {
290 /* The response line is passed on as a "header" below, so reject an
291 embedded NUL byte the same way verify_header() does for HTTP. */
292 failf(data, "NUL byte in server response line");
293 return CURLE_WEIRD_SERVER_REPLY;
294 }
296 /* output debug output if that is requested */
297 Curl_debug(data, CURLINFO_HEADER_IN, line, length);
299 /*
300 * Pass all response-lines to the callback function registered for
301 * "headers". The response lines can be seen as a kind of headers.
302 */
303 result = Curl_client_write(data, CLIENTWRITE_INFO, line, length);
304 if(result)
305 return result;
307 if(pp->endofresp(data, conn, line, length, code)) {
308 /* When at "end of response", keep the endofresp line first in the
309 buffer since it will be accessed outside (by pingpong
310 parsers). Store the overflow counter to inform about additional
311 data in this buffer after the endofresp line. */
312 pp->nfinal = length;
313 if(curlx_dyn_len(&pp->recvbuf) > length)
314 pp->overflow = curlx_dyn_len(&pp->recvbuf) - length;
315 else
316 pp->overflow = 0;
317 *size = pp->nread_resp; /* size of the response */
318 pp->nread_resp = 0; /* restart */
319 gotbytes = 0; /* force break out of outer loop */
320 break;
321 }
322 if(curlx_dyn_len(&pp->recvbuf) > length)
323 /* keep the remaining piece */
324 curlx_dyn_tail((&pp->recvbuf), curlx_dyn_len(&pp->recvbuf) - length);
325 else
326 curlx_dyn_reset(&pp->recvbuf);
327 }
328 else {
329 /* without a newline, there is no overflow */
330 pp->overflow = 0;
331 break;
332 }
334 } while(1); /* while there is buffer left to scan */
336 } while(gotbytes == sizeof(buffer));
338 pp->pending_resp = FALSE;
340 return result;
341}
343CURLcode Curl_pp_pollset(struct Curl_easy *data,
344 struct pingpong *pp,
345 struct easy_pollset *ps)
346{
347 int flags = pp->sendleft ? CURL_POLL_OUT : CURL_POLL_IN;
348 return Curl_pollset_change(data, ps, data->conn->sock[FIRSTSOCKET],
349 flags, 0);
350}
352bool Curl_pp_needs_flush(struct Curl_easy *data,
353 struct pingpong *pp)
354{
355 (void)data;
356 return pp->sendleft > 0;
357}
359CURLcode Curl_pp_flushsend(struct Curl_easy *data,
360 struct pingpong *pp)
361{
362 /* we have a piece of a command still left to send */
363 size_t written;
364 CURLcode result;
366 if(!Curl_pp_needs_flush(data, pp))
367 return CURLE_OK;
369 result = Curl_conn_send(data, FIRSTSOCKET,
370 pp->sendthis + pp->sendsize - pp->sendleft,
371 pp->sendleft, FALSE, &written);
372 if(result == CURLE_AGAIN) {
373 result = CURLE_OK;
374 written = 0;
375 }
376 if(result)
377 return result;
379 if(written < pp->sendleft) {
380 /* only a fraction was sent */
381 pp->sendleft -= written;
382 }
383 else {
384 pp->sendthis = NULL;
385 pp->sendleft = pp->sendsize = 0;
386 pp->response = *Curl_pgrs_now(data);
387 }
388 return CURLE_OK;
389}
391CURLcode Curl_pp_disconnect(struct pingpong *pp)
392{
393 if(pp->initialized) {
394 curlx_dyn_free(&pp->sendbuf);
395 curlx_dyn_free(&pp->recvbuf);
396 memset(pp, 0, sizeof(*pp));
397 }
398 return CURLE_OK;
399}
401bool Curl_pp_moredata(struct pingpong *pp)
402{
403 return !pp->sendleft && curlx_dyn_len(&pp->recvbuf) > pp->nfinal;
404}
406#endif
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.0 KiB · hit22 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=22 git.response.bytes=4091 git.response.cache=HIT git.response.duration_ms=22 git.fetch.wants=cff9bf796bf08f7f52d960020c98de03e9ed3e41 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4091 git.pkt.payload.max=4070 git.sideband.band1.bytes=4069 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2d078a81defad
- Request
- 58fc7bf8…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format