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 ***************************************************************************/
24#include "curl_setup.h"
26#include "urldata.h"
27#include "cfilters.h"
28#include "curlx/dynbuf.h"
29#include "progress.h"
30#include "request.h"
31#include "sendf.h"
32#include "curl_trc.h"
33#include "transfer.h"
34#include "url.h"
35#include "curlx/strparse.h"
37void Curl_req_init(struct SingleRequest *req)
38{
39 memset(req, 0, sizeof(*req));
40}
42CURLcode Curl_req_soft_reset(struct SingleRequest *req,
43 struct Curl_easy *data)
44{
45 CURLcode result;
47 req->done = FALSE;
48 req->upload_done = FALSE;
49 req->upload_aborted = FALSE;
50 req->download_done = FALSE;
51 req->eos_written = FALSE;
52 req->eos_read = FALSE;
53 req->eos_sent = FALSE;
54 req->ignorebody = FALSE;
55 req->shutdown = FALSE;
56 req->bytecount = 0;
57 req->writebytecount = 0;
58 req->header = FALSE;
59 req->headerline = 0;
60 req->headerbytecount = 0;
61 req->allheadercount = 0;
62 req->deductheadercount = 0;
63 req->httpversion_sent = 0;
64 req->httpversion = 0;
65 req->sendbuf_hds_len = 0;
67 curlx_safefree(req->hd_auth);
68#ifndef CURL_DISABLE_PROXY
69 curlx_safefree(req->hd_proxy_auth);
70#endif
72 result = Curl_client_start(data);
73 if(result)
74 return result;
76 if(!req->sendbuf_init) {
77 Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
78 BUFQ_OPT_SOFT_LIMIT);
79 req->sendbuf_init = TRUE;
80 }
81 else {
82 Curl_bufq_reset(&req->sendbuf);
83 if(data->set.upload_buffer_size != req->sendbuf.chunk_size) {
84 Curl_bufq_free(&req->sendbuf);
85 Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
86 BUFQ_OPT_SOFT_LIMIT);
87 }
88 }
90 return CURLE_OK;
91}
93CURLcode Curl_req_start(struct SingleRequest *req,
94 struct Curl_easy *data)
95{
96 req->start = *Curl_pgrs_now(data);
97 return Curl_req_soft_reset(req, data);
98}
100static CURLcode req_flush(struct Curl_easy *data);
102CURLcode Curl_req_done(struct SingleRequest *req,
103 struct Curl_easy *data, bool aborted)
104{
105 (void)req;
106 if(!aborted)
107 (void)req_flush(data);
108 Curl_client_reset(data);
109 return CURLE_OK;
110}
112void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
113{
114 struct curltime t0 = { 0, 0 };
116 curlx_safefree(req->newurl);
117 curlx_safefree(req->hd_auth);
118#ifndef CURL_DISABLE_PROXY
119 curlx_safefree(req->hd_proxy_auth);
120#endif
121#ifndef CURL_DISABLE_COOKIES
122 curlx_safefree(req->cookiehost);
123#endif
124 Curl_client_reset(data);
125 if(req->sendbuf_init)
126 Curl_bufq_reset(&req->sendbuf);
128 /* clear any resolve data */
129 Curl_resolv_destroy_all(data);
130 /* Can no longer memset() this struct as we need to keep some state */
131 req->size = -1;
132 req->maxdownload = -1;
133 req->bytecount = 0;
134 req->writebytecount = 0;
135 req->start = t0;
136 req->headerbytecount = 0;
137 req->allheadercount = 0;
138 req->deductheadercount = 0;
139 req->headerline = 0;
140 req->offset = 0;
141 req->httpcode = 0;
142 req->io_flags = 0;
143 req->upgr101 = UPGR101_NONE;
144 req->sendbuf_hds_len = 0;
145 req->timeofdoc = 0;
146 req->location = NULL;
147 req->newurl = NULL;
148#ifndef CURL_DISABLE_COOKIES
149 req->setcookies = 0;
150#endif
151 req->header = FALSE;
152 req->content_range = FALSE;
153 req->download_done = FALSE;
154 req->eos_written = FALSE;
155 req->eos_read = FALSE;
156 req->eos_sent = FALSE;
157 req->rewind_read = FALSE;
158 req->upload_done = FALSE;
159 req->upload_aborted = FALSE;
160 req->ignorebody = FALSE;
161 req->http_bodyless = FALSE;
162 req->chunk = FALSE;
163 req->resp_trailer = FALSE;
164 req->ignore_cl = FALSE;
165 req->upload_chunky = FALSE;
166 req->no_body = data->set.opt_no_body;
167 req->authneg = FALSE;
168 req->shutdown = FALSE;
169 /* Unpause all directions */
170 Curl_rlimit_block(&data->progress.dl.rlimit, FALSE, &t0);
171 Curl_rlimit_block(&data->progress.ul.rlimit, FALSE, &t0);
172}
174void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
175{
176 curlx_safefree(req->newurl);
177 curlx_safefree(req->hd_auth);
178#ifndef CURL_DISABLE_PROXY
179 curlx_safefree(req->hd_proxy_auth);
180#endif
181 if(req->sendbuf_init)
182 Curl_bufq_free(&req->sendbuf);
183 Curl_client_cleanup(data);
184}
186static CURLcode xfer_send(struct Curl_easy *data,
187 const char *buf, size_t blen,
188 size_t hds_len, size_t *pnwritten)
189{
190 CURLcode result = CURLE_OK;
191 bool eos = FALSE;
193 *pnwritten = 0;
194 DEBUGASSERT(hds_len <= blen);
195#ifdef DEBUGBUILD
196 {
197 /* Allow debug builds to override this logic to force short initial
198 sends */
199 size_t body_len = blen - hds_len;
200 if(body_len) {
201 const char *p = getenv("CURL_SMALLREQSEND");
202 if(p) {
203 curl_off_t body_small;
204 if(!curlx_str_number(&p, &body_small, body_len))
205 blen = hds_len + (size_t)body_small;
206 }
207 }
208 }
209#endif
210 /* Make sure this does not send more body bytes than what the max send
211 speed says. The headers do not count to the max speed. */
212 if(data->set.max_send_speed) {
213 size_t body_bytes = blen - hds_len;
214 if((curl_off_t)body_bytes > data->set.max_send_speed)
215 blen = hds_len + (size_t)data->set.max_send_speed;
216 }
218 if(data->req.eos_read &&
219 (Curl_bufq_is_empty(&data->req.sendbuf) ||
220 Curl_bufq_len(&data->req.sendbuf) == blen)) {
221 DEBUGF(infof(data, "sending last upload chunk of %zu bytes", blen));
222 eos = TRUE;
223 }
224 result = Curl_xfer_send(data, buf, blen, eos, pnwritten);
225 if(!result) {
226 if(eos && (blen == *pnwritten))
227 data->req.eos_sent = TRUE;
228 if(*pnwritten) {
229 if(hds_len)
230 Curl_debug(data, CURLINFO_HEADER_OUT, buf,
231 CURLMIN(hds_len, *pnwritten));
232 if(*pnwritten > hds_len) {
233 size_t body_len = *pnwritten - hds_len;
234 Curl_debug(data, CURLINFO_DATA_OUT, buf + hds_len, body_len);
235 data->req.writebytecount += body_len;
236 Curl_pgrs_upload_inc(data, body_len);
237 }
238 }
239 }
240 return result;
241}
243static CURLcode req_send_buffer_flush(struct Curl_easy *data)
244{
245 CURLcode result = CURLE_OK;
246 const unsigned char *buf;
247 size_t blen;
249 while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
250 size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
251 result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
252 if(result)
253 break;
255 Curl_bufq_skip(&data->req.sendbuf, nwritten);
256 if(hds_len) {
257 data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
258 }
259 /* leave if we could not send all. Maybe network blocking or
260 * speed limits on transfer */
261 if(nwritten < blen)
262 break;
263 }
264 return result;
265}
267static CURLcode req_set_upload_done(struct Curl_easy *data)
268{
269 DEBUGASSERT(!data->req.upload_done);
270 data->req.upload_done = TRUE;
271 CURL_REQ_CLEAR_SEND(data);
273 if(data->mstate >= MSTATE_DID)
274 Curl_pgrsTime(data, TIMER_POSTRANSFER);
275 Curl_creader_done(data, data->req.upload_aborted);
277 if(data->req.upload_aborted) {
278 Curl_bufq_reset(&data->req.sendbuf);
279 if(data->req.writebytecount)
280 infof(data, "abort upload after having sent %" FMT_OFF_T " bytes",
281 data->req.writebytecount);
282 else
283 infof(data, "abort upload");
284 }
285 else if(data->req.writebytecount)
286 infof(data, "upload completely sent off: %" FMT_OFF_T " bytes",
287 data->req.writebytecount);
288 else if(!data->req.download_done) {
289 DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
290 infof(data, Curl_creader_total_length(data) ?
291 "We are completely uploaded and fine" :
292 "Request completely sent off");
293 }
295 return Curl_xfer_send_close(data);
296}
298static CURLcode req_flush(struct Curl_easy *data)
299{
300 CURLcode result;
302 if(!data || !data->conn)
303 return CURLE_FAILED_INIT;
305 if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
306 result = req_send_buffer_flush(data);
307 if(result)
308 return result;
309 if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
310 DEBUGF(infof(data, "Curl_req_flush(len=%zu) -> EAGAIN",
311 Curl_bufq_len(&data->req.sendbuf)));
312 return CURLE_AGAIN;
313 }
314 }
315 else if(Curl_xfer_needs_flush(data)) {
316 DEBUGF(infof(data, "Curl_req_flush(), xfer send_pending"));
317 return Curl_xfer_flush(data);
318 }
320 if(data->req.eos_read && !data->req.eos_sent) {
321 char tmp = 0;
322 size_t nwritten;
323 result = xfer_send(data, &tmp, 0, 0, &nwritten);
324 if(result)
325 return result;
326 DEBUGASSERT(data->req.eos_sent);
327 }
329 if(!data->req.upload_done && data->req.eos_read && data->req.eos_sent) {
330 DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
331 if(data->req.shutdown) {
332 bool done;
333 result = Curl_xfer_send_shutdown(data, &done);
334 if(result && data->req.shutdown_err_ignore) {
335 infof(data, "Shutdown send direction error: %d. Broken server? "
336 "Proceeding as if everything is ok.", (int)result);
337 result = CURLE_OK;
338 done = TRUE;
339 }
341 if(result)
342 return result;
343 if(!done)
344 return CURLE_AGAIN;
345 }
346 return req_set_upload_done(data);
347 }
348 return CURLE_OK;
349}
351static CURLcode add_from_client(void *reader_ctx,
352 unsigned char *buf, size_t buflen,
353 size_t *pnread)
354{
355 struct Curl_easy *data = reader_ctx;
356 CURLcode result;
357 bool eos;
359 result = Curl_client_read(data, (char *)buf, buflen, pnread, &eos);
360 if(!result && eos)
361 data->req.eos_read = TRUE;
362 return result;
363}
365static CURLcode req_send_buffer_add(struct Curl_easy *data,
366 const char *buf, size_t blen,
367 size_t hds_len)
368{
369 CURLcode result = CURLE_OK;
370 size_t n;
371 result = Curl_bufq_cwrite(&data->req.sendbuf, buf, blen, &n);
372 if(result)
373 return result;
374 /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
375 DEBUGASSERT(n == blen);
376 data->req.sendbuf_hds_len += hds_len;
377 return CURLE_OK;
378}
380CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req,
381 unsigned char httpversion)
382{
383 CURLcode result;
384 const char *buf;
385 size_t blen, nwritten;
387 if(!data || !data->conn)
388 return CURLE_FAILED_INIT;
390 data->req.httpversion_sent = httpversion;
391 buf = curlx_dyn_ptr(req);
392 blen = curlx_dyn_len(req);
393 /* if the sendbuf is empty and the request without body and
394 * the length to send fits info a sendbuf chunk, we send it directly.
395 * If `blen` is larger than `chunk_size`, we can not. Because we
396 * might have to retry a blocked send later from sendbuf and that
397 * would result in retry sends with a shrunken length. That is trouble. */
398 if(Curl_bufq_is_empty(&data->req.sendbuf) &&
399 !Curl_creader_total_length(data) &&
400 (blen <= data->req.sendbuf.chunk_size)) {
401 data->req.eos_read = TRUE;
402 result = xfer_send(data, buf, blen, blen, &nwritten);
403 if(result)
404 return result;
405 buf += nwritten;
406 blen -= nwritten;
407 if(!blen) {
408 result = req_set_upload_done(data);
409 if(result)
410 return result;
411 }
412 }
414 if(blen) {
415 /* Either we have a request body, or we could not send the complete
416 * request in one go. Buffer the remainder and try to add as much
417 * body bytes as room is left in the buffer. Then flush. */
418 result = req_send_buffer_add(data, buf, blen, blen);
419 if(result)
420 return result;
422 return Curl_req_send_more(data);
423 }
424 return CURLE_OK;
425}
427bool Curl_req_sendbuf_empty(struct Curl_easy *data)
428{
429 return !data->req.sendbuf_init || Curl_bufq_is_empty(&data->req.sendbuf);
430}
432bool Curl_req_want_send(struct Curl_easy *data)
433{
434 /* Not done and upload not blocked and either one of
435 * - REQ_IO_SEND
436 * - request has buffered data to send
437 * - connection has pending data to send */
438 return !data->req.done &&
439 !Curl_rlimit_is_blocked(&data->progress.ul.rlimit) &&
440 (CURL_REQ_WANT_SEND(data) ||
441 !Curl_req_sendbuf_empty(data) ||
442 Curl_xfer_needs_flush(data));
443}
445bool Curl_req_want_recv(struct Curl_easy *data)
446{
447 /* Not done and download not blocked and want RECV */
448 return !data->req.done &&
449 !Curl_rlimit_is_blocked(&data->progress.dl.rlimit) &&
450 CURL_REQ_WANT_RECV(data);
451}
453bool Curl_req_done_sending(struct Curl_easy *data)
454{
455 return data->req.upload_done && !Curl_req_want_send(data);
456}
458CURLcode Curl_req_send_more(struct Curl_easy *data)
459{
460 CURLcode result;
462 /* Fill our send buffer if more from client can be read. */
463 if(!data->req.upload_aborted &&
464 !data->req.eos_read &&
465 !Curl_xfer_send_is_paused(data) &&
466 !Curl_bufq_is_full(&data->req.sendbuf)) {
467 size_t nread;
468 result = Curl_bufq_sipn(&data->req.sendbuf, 0,
469 add_from_client, data, &nread);
470 if(result && result != CURLE_AGAIN)
471 return result;
472 }
474 result = req_flush(data);
475 if(result == CURLE_AGAIN)
476 result = CURLE_OK;
478 return result;
479}
481CURLcode Curl_req_abort_sending(struct Curl_easy *data)
482{
483 if(!data->req.upload_done) {
484 Curl_bufq_reset(&data->req.sendbuf);
485 data->req.upload_aborted = TRUE;
486 CURL_REQ_CLEAR_SEND(data);
487 return req_set_upload_done(data);
488 }
489 return CURLE_OK;
490}
492CURLcode Curl_req_stop_send_recv(struct Curl_easy *data)
493{
494 /* stop receiving and ALL sending as well, including PAUSE and HOLD.
495 * We might still be paused on receive client writes though, so
496 * keep those bits around. */
497 CURLcode result = CURLE_OK;
498 if(CURL_REQ_WANT_SEND(data))
499 result = Curl_req_abort_sending(data);
500 CURL_REQ_CLEAR_IO(data);
501 return result;
502}
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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 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.3 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=4442 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=51672bfe00cb037850773564fc9315584f32de3a 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=4442 git.pkt.payload.max=4421 git.sideband.band1.bytes=4420 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
- a5e2a0e6c8d91293
- Request
- f59eab22…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format