request.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, <[email protected]>, 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. /* This file is for lib internal stuff */
  27. #include "curl_setup.h"
  28. #include "bufq.h"
  29. /* forward declarations */
  30. struct UserDefined;
  31. enum expect100 {
  32. EXP100_SEND_DATA, /* enough waiting, just send the body now */
  33. EXP100_AWAITING_CONTINUE, /* waiting for the 100 Continue header */
  34. EXP100_SENDING_REQUEST, /* still sending the request but will wait for
  35. the 100 header once done with the request */
  36. EXP100_FAILED /* used on 417 Expectation Failed */
  37. };
  38. enum upgrade101 {
  39. UPGR101_NONE, /* default state */
  40. UPGR101_WS, /* upgrade to WebSocket requested */
  41. UPGR101_H2, /* upgrade to HTTP/2 requested */
  42. UPGR101_RECEIVED /* 101 response received */
  43. };
  44. /*
  45. * Request specific data in the easy handle (Curl_easy). Previously,
  46. * these members were on the connectdata struct but since a conn struct may
  47. * now be shared between different Curl_easys, we store connection-specific
  48. * data here. This struct only keeps stuff that is interesting for *this*
  49. * request, as it will be cleared between multiple ones
  50. */
  51. struct SingleRequest {
  52. curl_off_t size; /* -1 if unknown at this point */
  53. curl_off_t maxdownload; /* in bytes, the maximum amount of data to fetch,
  54. -1 means unlimited */
  55. curl_off_t bytecount; /* total number of bytes read */
  56. curl_off_t writebytecount; /* number of bytes written */
  57. struct curltime start; /* transfer started at this time */
  58. unsigned int headerbytecount; /* received server headers (not CONNECT
  59. headers) */
  60. unsigned int allheadercount; /* all received headers (server + CONNECT) */
  61. unsigned int deductheadercount; /* this amount of bytes does not count when
  62. we check if anything has been transferred
  63. at the end of a connection. We use this
  64. counter to make only a 100 reply (without
  65. a following second response code) result
  66. in a CURLE_GOT_NOTHING error code */
  67. int headerline; /* counts header lines to better track the
  68. first one */
  69. curl_off_t offset; /* possible resume offset read from the
  70. Content-Range: header */
  71. int httpcode; /* error code from the 'HTTP/1.? XXX' or
  72. 'RTSP/1.? XXX' line */
  73. int keepon;
  74. unsigned char httpversion_sent; /* Version in request (09, 10, 11, etc.) */
  75. unsigned char httpversion; /* Version in response (09, 10, 11, etc.) */
  76. enum upgrade101 upgr101; /* 101 upgrade state */
  77. /* Client Writer stack, handles transfer- and content-encodings, protocol
  78. * checks, pausing by client callbacks. */
  79. struct Curl_cwriter *writer_stack;
  80. /* Client Reader stack, handles transfer- and content-encodings, protocol
  81. * checks, pausing by client callbacks. */
  82. struct Curl_creader *reader_stack;
  83. struct bufq sendbuf; /* data which needs to be send to the server */
  84. size_t sendbuf_hds_len; /* amount of header bytes in sendbuf */
  85. time_t timeofdoc;
  86. char *location; /* This points to an allocated version of the Location:
  87. header data */
  88. char *newurl; /* Set to the new URL to use when a redirect or a retry is
  89. wanted */
  90. #ifndef CURL_DISABLE_COOKIES
  91. unsigned char setcookies;
  92. #endif
  93. BIT(header); /* incoming data has HTTP header */
  94. BIT(done); /* request is done, e.g. no more send/recv should
  95. * happen. This can be TRUE before `upload_done` or
  96. * `download_done` is TRUE. */
  97. BIT(content_range); /* set TRUE if Content-Range: was found */
  98. BIT(download_done); /* set to TRUE when download is complete */
  99. BIT(eos_written); /* iff EOS has been written to client */
  100. BIT(eos_read); /* iff EOS has been read from the client */
  101. BIT(eos_sent); /* iff EOS has been sent to the server */
  102. BIT(rewind_read); /* iff reader needs rewind at next start */
  103. BIT(upload_done); /* set to TRUE when all request data has been sent */
  104. BIT(upload_aborted); /* set to TRUE when upload was aborted. Will also
  105. * show `upload_done` as TRUE. */
  106. BIT(ignorebody); /* we read a response-body but we ignore it! */
  107. BIT(http_bodyless); /* HTTP response status code is between 100 and 199,
  108. 204 or 304 */
  109. BIT(chunk); /* if set, this is a chunked transfer-encoding */
  110. BIT(resp_trailer); /* response carried 'Trailer:' header field */
  111. BIT(ignore_cl); /* ignore content-length */
  112. BIT(upload_chunky); /* set TRUE if we are doing chunked transfer-encoding
  113. on upload */
  114. BIT(no_body); /* the response has no body */
  115. BIT(authneg); /* TRUE when the auth phase has started, which means
  116. that we are creating a request with an auth header,
  117. but it is not the final request in the auth
  118. negotiation. */
  119. BIT(sendbuf_init); /* sendbuf is initialized */
  120. BIT(shutdown); /* request end will shutdown connection */
  121. BIT(shutdown_err_ignore); /* errors in shutdown will not fail request */
  122. BIT(reader_started); /* client reads have started */
  123. };
  124. /**
  125. * Initialize the state of the request for first use.
  126. */
  127. void Curl_req_init(struct SingleRequest *req);
  128. /**
  129. * The request is about to start. Record time and do a soft reset.
  130. */
  131. CURLcode Curl_req_start(struct SingleRequest *req,
  132. struct Curl_easy *data);
  133. /**
  134. * The request may continue with a follow up. Reset
  135. * members, but keep start time for overall duration calc.
  136. */
  137. CURLcode Curl_req_soft_reset(struct SingleRequest *req,
  138. struct Curl_easy *data);
  139. /**
  140. * The request is done. If not aborted, make sure that buffers are
  141. * flushed to the client.
  142. * @param req the request
  143. * @param data the transfer
  144. * @param aborted TRUE iff the request was aborted/errored
  145. */
  146. CURLcode Curl_req_done(struct SingleRequest *req,
  147. struct Curl_easy *data, bool aborted);
  148. /**
  149. * Free the state of the request, not usable afterwards.
  150. */
  151. void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data);
  152. /**
  153. * Hard reset the state of the request to virgin state base on
  154. * transfer settings.
  155. */
  156. void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data);
  157. /**
  158. * Send request headers. If not all could be sent
  159. * they will be buffered. Use `Curl_req_flush()` to make sure
  160. * bytes are really send.
  161. * @param data the transfer making the request
  162. * @param buf the complete header bytes, no body
  163. * @param httpversion version used in request (09, 10, 11, etc.)
  164. * @return CURLE_OK (on blocking with *pnwritten == 0) or error.
  165. */
  166. CURLcode Curl_req_send(struct Curl_easy *data, struct dynbuf *buf,
  167. unsigned char httpversion);
  168. /**
  169. * TRUE iff the request has sent all request headers and data.
  170. */
  171. bool Curl_req_done_sending(struct Curl_easy *data);
  172. /*
  173. * Read more from client and flush all buffered request bytes.
  174. * @return CURLE_OK on success or the error on the sending.
  175. * Never returns CURLE_AGAIN.
  176. */
  177. CURLcode Curl_req_send_more(struct Curl_easy *data);
  178. /* TRUE if the request wants to send, e.g. is not done sending
  179. * and is not blocked. */
  180. bool Curl_req_want_send(struct Curl_easy *data);
  181. /* TRUE if the request wants to receive and is not blocked. */
  182. bool Curl_req_want_recv(struct Curl_easy *data);
  183. /**
  184. * TRUE iff the request has no buffered bytes yet to send.
  185. */
  186. bool Curl_req_sendbuf_empty(struct Curl_easy *data);
  187. /**
  188. * Stop sending any more request data to the server.
  189. * Will clear the send buffer and mark request sending as done.
  190. */
  191. CURLcode Curl_req_abort_sending(struct Curl_easy *data);
  192. /**
  193. * Stop sending and receiving any more request data.
  194. * Will abort sending if not done.
  195. */
  196. CURLcode Curl_req_stop_send_recv(struct Curl_easy *data);
  197. /**
  198. * Invoked when all request data has been uploaded.
  199. */
  200. CURLcode Curl_req_set_upload_done(struct Curl_easy *data);
  201. #endif /* HEADER_CURL_REQUEST_H */