xref: /curl/lib/request.h (revision b2331f3e)
1 #ifndef HEADER_CURL_REQUEST_H
2 #define HEADER_CURL_REQUEST_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 /* This file is for lib internal stuff */
28 
29 #include "curl_setup.h"
30 
31 #include "bufq.h"
32 
33 /* forward declarations */
34 struct UserDefined;
35 #ifndef CURL_DISABLE_DOH
36 struct doh_probes;
37 #endif
38 
39 enum expect100 {
40   EXP100_SEND_DATA,           /* enough waiting, just send the body now */
41   EXP100_AWAITING_CONTINUE,   /* waiting for the 100 Continue header */
42   EXP100_SENDING_REQUEST,     /* still sending the request but will wait for
43                                  the 100 header once done with the request */
44   EXP100_FAILED               /* used on 417 Expectation Failed */
45 };
46 
47 enum upgrade101 {
48   UPGR101_INIT,               /* default state */
49   UPGR101_WS,                 /* upgrade to WebSockets requested */
50   UPGR101_H2,                 /* upgrade to HTTP/2 requested */
51   UPGR101_RECEIVED,           /* 101 response received */
52   UPGR101_WORKING             /* talking upgraded protocol */
53 };
54 
55 
56 /*
57  * Request specific data in the easy handle (Curl_easy). Previously,
58  * these members were on the connectdata struct but since a conn struct may
59  * now be shared between different Curl_easys, we store connection-specific
60  * data here. This struct only keeps stuff that is interesting for *this*
61  * request, as it will be cleared between multiple ones
62  */
63 struct SingleRequest {
64   curl_off_t size;        /* -1 if unknown at this point */
65   curl_off_t maxdownload; /* in bytes, the maximum amount of data to fetch,
66                              -1 means unlimited */
67   curl_off_t bytecount;         /* total number of bytes read */
68   curl_off_t writebytecount;    /* number of bytes written */
69 
70   struct curltime start;         /* transfer started at this time */
71   unsigned int headerbytecount;  /* received server headers (not CONNECT
72                                     headers) */
73   unsigned int allheadercount;   /* all received headers (server + CONNECT) */
74   unsigned int deductheadercount; /* this amount of bytes does not count when
75                                      we check if anything has been transferred
76                                      at the end of a connection. We use this
77                                      counter to make only a 100 reply (without
78                                      a following second response code) result
79                                      in a CURLE_GOT_NOTHING error code */
80   int headerline;               /* counts header lines to better track the
81                                    first one */
82   curl_off_t offset;            /* possible resume offset read from the
83                                    Content-Range: header */
84   int httpversion;              /* Version in response (09, 10, 11, etc.) */
85   int httpcode;                 /* error code from the 'HTTP/1.? XXX' or
86                                    'RTSP/1.? XXX' line */
87   int keepon;
88   enum upgrade101 upgr101;      /* 101 upgrade state */
89 
90   /* Client Writer stack, handles transfer- and content-encodings, protocol
91    * checks, pausing by client callbacks. */
92   struct Curl_cwriter *writer_stack;
93   /* Client Reader stack, handles transfer- and content-encodings, protocol
94    * checks, pausing by client callbacks. */
95   struct Curl_creader *reader_stack;
96   struct bufq sendbuf; /* data which needs to be send to the server */
97   size_t sendbuf_hds_len; /* amount of header bytes in sendbuf */
98   time_t timeofdoc;
99   char *location;   /* This points to an allocated version of the Location:
100                        header data */
101   char *newurl;     /* Set to the new URL to use when a redirect or a retry is
102                        wanted */
103 
104   /* Allocated protocol-specific data. Each protocol handler makes sure this
105      points to data it needs. */
106   union {
107     struct FILEPROTO *file;
108     struct FTP *ftp;
109     struct IMAP *imap;
110     struct ldapreqinfo *ldap;
111     struct MQTT *mqtt;
112     struct POP3 *pop3;
113     struct RTSP *rtsp;
114     struct smb_request *smb;
115     struct SMTP *smtp;
116     struct SSHPROTO *ssh;
117     struct TELNET *telnet;
118   } p;
119 #ifndef CURL_DISABLE_DOH
120   struct doh_probes *doh; /* DoH specific data for this request */
121 #endif
122 #ifndef CURL_DISABLE_COOKIES
123   unsigned char setcookies;
124 #endif
125   BIT(header);        /* incoming data has HTTP header */
126   BIT(done);          /* request is done, e.g. no more send/recv should
127                        * happen. This can be TRUE before `upload_done` or
128                        * `download_done` is TRUE. */
129   BIT(content_range); /* set TRUE if Content-Range: was found */
130   BIT(download_done); /* set to TRUE when download is complete */
131   BIT(eos_written);   /* iff EOS has been written to client */
132   BIT(eos_read);      /* iff EOS has been read from the client */
133   BIT(eos_sent);      /* iff EOS has been sent to the server */
134   BIT(rewind_read);   /* iff reader needs rewind at next start */
135   BIT(upload_done);   /* set to TRUE when all request data has been sent */
136   BIT(upload_aborted); /* set to TRUE when upload was aborted. Will also
137                         * show `upload_done` as TRUE. */
138   BIT(ignorebody);    /* we read a response-body but we ignore it! */
139   BIT(http_bodyless); /* HTTP response status code is between 100 and 199,
140                          204 or 304 */
141   BIT(chunk);         /* if set, this is a chunked transfer-encoding */
142   BIT(resp_trailer);  /* response carried 'Trailer:' header field */
143   BIT(ignore_cl);     /* ignore content-length */
144   BIT(upload_chunky); /* set TRUE if we are doing chunked transfer-encoding
145                          on upload */
146   BIT(getheader);    /* TRUE if header parsing is wanted */
147   BIT(no_body);      /* the response has no body */
148   BIT(authneg);      /* TRUE when the auth phase has started, which means
149                         that we are creating a request with an auth header,
150                         but it is not the final request in the auth
151                         negotiation. */
152   BIT(sendbuf_init); /* sendbuf is initialized */
153   BIT(shutdown);     /* request end will shutdown connection */
154   BIT(shutdown_err_ignore); /* errors in shutdown will not fail request */
155 #ifdef USE_HYPER
156   BIT(bodywritten);
157 #endif
158 };
159 
160 /**
161  * Initialize the state of the request for first use.
162  */
163 void Curl_req_init(struct SingleRequest *req);
164 
165 /**
166  * The request is about to start. Record time and do a soft reset.
167  */
168 CURLcode Curl_req_start(struct SingleRequest *req,
169                         struct Curl_easy *data);
170 
171 /**
172  * The request may continue with a follow up. Reset
173  * members, but keep start time for overall duration calc.
174  */
175 CURLcode Curl_req_soft_reset(struct SingleRequest *req,
176                              struct Curl_easy *data);
177 
178 /**
179  * The request is done. If not aborted, make sure that buffers are
180  * flushed to the client.
181  * @param req        the request
182  * @param data       the transfer
183  * @param aborted    TRUE iff the request was aborted/errored
184  */
185 CURLcode Curl_req_done(struct SingleRequest *req,
186                        struct Curl_easy *data, bool aborted);
187 
188 /**
189  * Free the state of the request, not usable afterwards.
190  */
191 void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data);
192 
193 /**
194  * Hard reset the state of the request to virgin state base on
195  * transfer settings.
196  */
197 void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data);
198 
199 #ifndef USE_HYPER
200 /**
201  * Send request headers. If not all could be sent
202  * they will be buffered. Use `Curl_req_flush()` to make sure
203  * bytes are really send.
204  * @param data      the transfer making the request
205  * @param buf       the complete header bytes, no body
206  * @return CURLE_OK (on blocking with *pnwritten == 0) or error.
207  */
208 CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *buf);
209 
210 #endif /* !USE_HYPER */
211 
212 /**
213  * TRUE iff the request has sent all request headers and data.
214  */
215 bool Curl_req_done_sending(struct Curl_easy *data);
216 
217 /*
218  * Read more from client and flush all buffered request bytes.
219  * @return CURLE_OK on success or the error on the sending.
220  *         Never returns CURLE_AGAIN.
221  */
222 CURLcode Curl_req_send_more(struct Curl_easy *data);
223 
224 /**
225  * TRUE iff the request wants to send, e.g. has buffered bytes.
226  */
227 bool Curl_req_want_send(struct Curl_easy *data);
228 
229 /**
230  * TRUE iff the request has no buffered bytes yet to send.
231  */
232 bool Curl_req_sendbuf_empty(struct Curl_easy *data);
233 
234 /**
235  * Stop sending any more request data to the server.
236  * Will clear the send buffer and mark request sending as done.
237  */
238 CURLcode Curl_req_abort_sending(struct Curl_easy *data);
239 
240 /**
241  * Stop sending and receiving any more request data.
242  * Will abort sending if not done.
243  */
244 CURLcode Curl_req_stop_send_recv(struct Curl_easy *data);
245 
246 /**
247  * Invoked when all request data has been uploaded.
248  */
249 CURLcode Curl_req_set_upload_done(struct Curl_easy *data);
250 
251 #endif /* HEADER_CURL_REQUEST_H */
252