xref: /curl/lib/request.c (revision 926fb004)
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 #include "urldata.h"
28 #include "cfilters.h"
29 #include "dynbuf.h"
30 #include "doh.h"
31 #include "multiif.h"
32 #include "progress.h"
33 #include "request.h"
34 #include "sendf.h"
35 #include "transfer.h"
36 #include "url.h"
37 
38 /* The last 3 #include files should be in this order */
39 #include "curl_printf.h"
40 #include "curl_memory.h"
41 #include "memdebug.h"
42 
Curl_req_init(struct SingleRequest * req)43 void Curl_req_init(struct SingleRequest *req)
44 {
45   memset(req, 0, sizeof(*req));
46 }
47 
Curl_req_soft_reset(struct SingleRequest * req,struct Curl_easy * data)48 CURLcode Curl_req_soft_reset(struct SingleRequest *req,
49                              struct Curl_easy *data)
50 {
51   CURLcode result;
52 
53   req->done = FALSE;
54   req->upload_done = FALSE;
55   req->download_done = FALSE;
56   req->ignorebody = FALSE;
57   req->bytecount = 0;
58   req->writebytecount = 0;
59   req->header = TRUE; /* assume header */
60   req->headerline = 0;
61   req->headerbytecount = 0;
62   req->allheadercount =  0;
63   req->deductheadercount = 0;
64 
65   result = Curl_client_start(data);
66   if(result)
67     return result;
68 
69   if(!req->sendbuf_init) {
70     Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
71                     BUFQ_OPT_SOFT_LIMIT);
72     req->sendbuf_init = TRUE;
73   }
74   else {
75     Curl_bufq_reset(&req->sendbuf);
76     if(data->set.upload_buffer_size != req->sendbuf.chunk_size) {
77       Curl_bufq_free(&req->sendbuf);
78       Curl_bufq_init2(&req->sendbuf, data->set.upload_buffer_size, 1,
79                       BUFQ_OPT_SOFT_LIMIT);
80     }
81   }
82 
83   return CURLE_OK;
84 }
85 
Curl_req_start(struct SingleRequest * req,struct Curl_easy * data)86 CURLcode Curl_req_start(struct SingleRequest *req,
87                         struct Curl_easy *data)
88 {
89   req->start = Curl_now();
90   return Curl_req_soft_reset(req, data);
91 }
92 
93 static CURLcode req_flush(struct Curl_easy *data);
94 
Curl_req_done(struct SingleRequest * req,struct Curl_easy * data,bool aborted)95 CURLcode Curl_req_done(struct SingleRequest *req,
96                        struct Curl_easy *data, bool aborted)
97 {
98   (void)req;
99   if(!aborted)
100     (void)req_flush(data);
101   Curl_client_reset(data);
102   return CURLE_OK;
103 }
104 
Curl_req_hard_reset(struct SingleRequest * req,struct Curl_easy * data)105 void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
106 {
107   struct curltime t0 = {0, 0};
108 
109   /* This is a bit ugly. `req->p` is a union and we assume we can
110    * free this safely without leaks. */
111   Curl_safefree(req->p.http);
112   Curl_safefree(req->newurl);
113   Curl_client_reset(data);
114   if(req->sendbuf_init)
115     Curl_bufq_reset(&req->sendbuf);
116 
117 #ifndef CURL_DISABLE_DOH
118   if(req->doh) {
119     Curl_close(&req->doh->probe[0].easy);
120     Curl_close(&req->doh->probe[1].easy);
121   }
122 #endif
123   /* Can no longer memset() this struct as we need to keep some state */
124   req->size = -1;
125   req->maxdownload = -1;
126   req->bytecount = 0;
127   req->writebytecount = 0;
128   req->start = t0;
129   req->headerbytecount = 0;
130   req->allheadercount =  0;
131   req->deductheadercount = 0;
132   req->headerline = 0;
133   req->offset = 0;
134   req->httpcode = 0;
135   req->keepon = 0;
136   req->upgr101 = UPGR101_INIT;
137   req->timeofdoc = 0;
138   req->bodywrites = 0;
139   req->location = NULL;
140   req->newurl = NULL;
141 #ifndef CURL_DISABLE_COOKIES
142   req->setcookies = 0;
143 #endif
144   req->header = FALSE;
145   req->content_range = FALSE;
146   req->download_done = FALSE;
147   req->eos_written = FALSE;
148   req->eos_read = FALSE;
149   req->upload_done = FALSE;
150   req->upload_aborted = FALSE;
151   req->ignorebody = FALSE;
152   req->http_bodyless = FALSE;
153   req->chunk = FALSE;
154   req->ignore_cl = FALSE;
155   req->upload_chunky = FALSE;
156   req->getheader = FALSE;
157   req->no_body = data->set.opt_no_body;
158   req->authneg = FALSE;
159 }
160 
Curl_req_free(struct SingleRequest * req,struct Curl_easy * data)161 void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
162 {
163   /* This is a bit ugly. `req->p` is a union and we assume we can
164    * free this safely without leaks. */
165   Curl_safefree(req->p.http);
166   Curl_safefree(req->newurl);
167   if(req->sendbuf_init)
168     Curl_bufq_free(&req->sendbuf);
169   Curl_client_cleanup(data);
170 
171 #ifndef CURL_DISABLE_DOH
172   if(req->doh) {
173     Curl_close(&req->doh->probe[0].easy);
174     Curl_close(&req->doh->probe[1].easy);
175     Curl_dyn_free(&req->doh->probe[0].serverdoh);
176     Curl_dyn_free(&req->doh->probe[1].serverdoh);
177     curl_slist_free_all(req->doh->headers);
178     Curl_safefree(req->doh);
179   }
180 #endif
181 }
182 
xfer_send(struct Curl_easy * data,const char * buf,size_t blen,size_t hds_len,size_t * pnwritten)183 static CURLcode xfer_send(struct Curl_easy *data,
184                           const char *buf, size_t blen,
185                           size_t hds_len, size_t *pnwritten)
186 {
187   CURLcode result = CURLE_OK;
188 
189   *pnwritten = 0;
190 #ifdef CURLDEBUG
191   {
192     /* Allow debug builds to override this logic to force short initial
193        sends
194      */
195     char *p = getenv("CURL_SMALLREQSEND");
196     if(p) {
197       size_t altsize = (size_t)strtoul(p, NULL, 10);
198       if(altsize && altsize < blen)
199         blen = altsize;
200     }
201   }
202 #endif
203   /* Make sure this doesn't send more body bytes than what the max send
204      speed says. The headers do not count to the max speed. */
205   if(data->set.max_send_speed) {
206     size_t body_bytes = blen - hds_len;
207     if((curl_off_t)body_bytes > data->set.max_send_speed)
208       blen = hds_len + (size_t)data->set.max_send_speed;
209   }
210 
211   result = Curl_xfer_send(data, buf, blen, pnwritten);
212   if(!result && *pnwritten) {
213     if(hds_len)
214       Curl_debug(data, CURLINFO_HEADER_OUT, (char *)buf,
215                  CURLMIN(hds_len, *pnwritten));
216     if(*pnwritten > hds_len) {
217       size_t body_len = *pnwritten - hds_len;
218       Curl_debug(data, CURLINFO_DATA_OUT, (char *)buf + hds_len, body_len);
219       data->req.writebytecount += body_len;
220       Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
221     }
222   }
223   return result;
224 }
225 
req_send_buffer_flush(struct Curl_easy * data)226 static CURLcode req_send_buffer_flush(struct Curl_easy *data)
227 {
228   CURLcode result = CURLE_OK;
229   const unsigned char *buf;
230   size_t blen;
231 
232   while(Curl_bufq_peek(&data->req.sendbuf, &buf, &blen)) {
233     size_t nwritten, hds_len = CURLMIN(data->req.sendbuf_hds_len, blen);
234     result = xfer_send(data, (const char *)buf, blen, hds_len, &nwritten);
235     if(result)
236       break;
237 
238     Curl_bufq_skip(&data->req.sendbuf, nwritten);
239     if(hds_len) {
240       data->req.sendbuf_hds_len -= CURLMIN(hds_len, nwritten);
241     }
242     /* leave if we could not send all. Maybe network blocking or
243      * speed limits on transfer */
244     if(nwritten < blen)
245       break;
246   }
247   return result;
248 }
249 
req_set_upload_done(struct Curl_easy * data)250 static CURLcode req_set_upload_done(struct Curl_easy *data)
251 {
252   DEBUGASSERT(!data->req.upload_done);
253   data->req.upload_done = TRUE;
254   data->req.keepon &= ~(KEEP_SEND|KEEP_SEND_TIMED); /* we're done sending */
255 
256   Curl_creader_done(data, data->req.upload_aborted);
257 
258   if(data->req.upload_aborted) {
259     if(data->req.writebytecount)
260       infof(data, "abort upload after having sent %" CURL_FORMAT_CURL_OFF_T
261             " bytes", data->req.writebytecount);
262     else
263       infof(data, "abort upload");
264   }
265   else if(data->req.writebytecount)
266     infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
267           " bytes", data->req.writebytecount);
268   else if(!data->req.download_done)
269     infof(data, Curl_creader_total_length(data)?
270                 "We are completely uploaded and fine" :
271                 "Request completely sent off");
272 
273   return Curl_xfer_send_close(data);
274 }
275 
req_flush(struct Curl_easy * data)276 static CURLcode req_flush(struct Curl_easy *data)
277 {
278   CURLcode result;
279 
280   if(!data || !data->conn)
281     return CURLE_FAILED_INIT;
282 
283   if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
284     result = req_send_buffer_flush(data);
285     if(result)
286       return result;
287     if(!Curl_bufq_is_empty(&data->req.sendbuf)) {
288       return CURLE_AGAIN;
289     }
290   }
291 
292   if(!data->req.upload_done && data->req.eos_read &&
293      Curl_bufq_is_empty(&data->req.sendbuf)) {
294     return req_set_upload_done(data);
295   }
296   return CURLE_OK;
297 }
298 
add_from_client(void * reader_ctx,unsigned char * buf,size_t buflen,CURLcode * err)299 static ssize_t add_from_client(void *reader_ctx,
300                                unsigned char *buf, size_t buflen,
301                                CURLcode *err)
302 {
303   struct Curl_easy *data = reader_ctx;
304   size_t nread;
305   bool eos;
306 
307   *err = Curl_client_read(data, (char *)buf, buflen, &nread, &eos);
308   if(*err)
309     return -1;
310   if(eos)
311     data->req.eos_read = TRUE;
312   return (ssize_t)nread;
313 }
314 
315 #ifndef USE_HYPER
316 
req_send_buffer_add(struct Curl_easy * data,const char * buf,size_t blen,size_t hds_len)317 static CURLcode req_send_buffer_add(struct Curl_easy *data,
318                                     const char *buf, size_t blen,
319                                     size_t hds_len)
320 {
321   CURLcode result = CURLE_OK;
322   ssize_t n;
323   n = Curl_bufq_write(&data->req.sendbuf,
324                       (const unsigned char *)buf, blen, &result);
325   if(n < 0)
326     return result;
327   /* We rely on a SOFTLIMIT on sendbuf, so it can take all data in */
328   DEBUGASSERT((size_t)n == blen);
329   data->req.sendbuf_hds_len += hds_len;
330   return CURLE_OK;
331 }
332 
Curl_req_send(struct Curl_easy * data,struct dynbuf * req)333 CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *req)
334 {
335   CURLcode result;
336   const char *buf;
337   size_t blen, nwritten;
338 
339   if(!data || !data->conn)
340     return CURLE_FAILED_INIT;
341 
342   buf = Curl_dyn_ptr(req);
343   blen = Curl_dyn_len(req);
344   if(!Curl_creader_total_length(data)) {
345     /* Request without body. Try to send directly from the buf given. */
346     data->req.eos_read = TRUE;
347     result = xfer_send(data, buf, blen, blen, &nwritten);
348     if(result)
349       return result;
350     buf += nwritten;
351     blen -= nwritten;
352   }
353 
354   if(blen) {
355     /* Either we have a request body, or we could not send the complete
356      * request in one go. Buffer the remainder and try to add as much
357      * body bytes as room is left in the buffer. Then flush. */
358     result = req_send_buffer_add(data, buf, blen, blen);
359     if(result)
360       return result;
361 
362     return Curl_req_send_more(data);
363   }
364   return CURLE_OK;
365 }
366 #endif /* !USE_HYPER */
367 
Curl_req_want_send(struct Curl_easy * data)368 bool Curl_req_want_send(struct Curl_easy *data)
369 {
370   return data->req.sendbuf_init && !Curl_bufq_is_empty(&data->req.sendbuf);
371 }
372 
Curl_req_done_sending(struct Curl_easy * data)373 bool Curl_req_done_sending(struct Curl_easy *data)
374 {
375   if(data->req.upload_done) {
376     DEBUGASSERT(Curl_bufq_is_empty(&data->req.sendbuf));
377     return TRUE;
378   }
379   return FALSE;
380 }
381 
Curl_req_send_more(struct Curl_easy * data)382 CURLcode Curl_req_send_more(struct Curl_easy *data)
383 {
384   CURLcode result;
385 
386   /* Fill our send buffer if more from client can be read. */
387   if(!data->req.eos_read && !Curl_bufq_is_full(&data->req.sendbuf)) {
388     ssize_t nread = Curl_bufq_sipn(&data->req.sendbuf, 0,
389                                    add_from_client, data, &result);
390     if(nread < 0 && result != CURLE_AGAIN)
391       return result;
392   }
393 
394   result = req_flush(data);
395   if(result == CURLE_AGAIN)
396     result = CURLE_OK;
397 
398   return result;
399 }
400 
Curl_req_abort_sending(struct Curl_easy * data)401 CURLcode Curl_req_abort_sending(struct Curl_easy *data)
402 {
403   if(!data->req.upload_done) {
404     Curl_bufq_reset(&data->req.sendbuf);
405     data->req.upload_aborted = TRUE;
406     return req_set_upload_done(data);
407   }
408   return CURLE_OK;
409 }
410