http.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #ifndef HEADER_CURL_HTTP_H
  2. #define HEADER_CURL_HTTP_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2018, 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.haxx.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. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_HTTP
  26. #ifdef USE_NGHTTP2
  27. #include <nghttp2/nghttp2.h>
  28. #endif
  29. extern const struct Curl_handler Curl_handler_http;
  30. #ifdef USE_SSL
  31. extern const struct Curl_handler Curl_handler_https;
  32. #endif
  33. /* Header specific functions */
  34. bool Curl_compareheader(const char *headerline, /* line to check */
  35. const char *header, /* header keyword _with_ colon */
  36. const char *content); /* content string to find */
  37. char *Curl_copy_header_value(const char *header);
  38. char *Curl_checkProxyheaders(const struct connectdata *conn,
  39. const char *thisheader);
  40. /* ------------------------------------------------------------------------- */
  41. /*
  42. * The add_buffer series of functions are used to build one large memory chunk
  43. * from repeated function invokes. Used so that the entire HTTP request can
  44. * be sent in one go.
  45. */
  46. struct Curl_send_buffer {
  47. char *buffer;
  48. size_t size_max;
  49. size_t size_used;
  50. };
  51. typedef struct Curl_send_buffer Curl_send_buffer;
  52. Curl_send_buffer *Curl_add_buffer_init(void);
  53. void Curl_add_buffer_free(Curl_send_buffer **inp);
  54. CURLcode Curl_add_bufferf(Curl_send_buffer **inp, const char *fmt, ...)
  55. WARN_UNUSED_RESULT;
  56. CURLcode Curl_add_buffer(Curl_send_buffer **inp, const void *inptr,
  57. size_t size) WARN_UNUSED_RESULT;
  58. CURLcode Curl_add_buffer_send(Curl_send_buffer **inp,
  59. struct connectdata *conn,
  60. long *bytes_written,
  61. size_t included_body_bytes,
  62. int socketindex);
  63. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  64. Curl_send_buffer *buf);
  65. CURLcode Curl_add_custom_headers(struct connectdata *conn,
  66. bool is_connect,
  67. Curl_send_buffer *req_buffer);
  68. /* protocol-specific functions set up to be called by the main engine */
  69. CURLcode Curl_http(struct connectdata *conn, bool *done);
  70. CURLcode Curl_http_done(struct connectdata *, CURLcode, bool premature);
  71. CURLcode Curl_http_connect(struct connectdata *conn, bool *done);
  72. CURLcode Curl_http_setup_conn(struct connectdata *conn);
  73. /* The following functions are defined in http_chunks.c */
  74. void Curl_httpchunk_init(struct connectdata *conn);
  75. CHUNKcode Curl_httpchunk_read(struct connectdata *conn, char *datap,
  76. ssize_t length, ssize_t *wrote);
  77. /* These functions are in http.c */
  78. void Curl_http_auth_stage(struct Curl_easy *data, int stage);
  79. CURLcode Curl_http_input_auth(struct connectdata *conn, bool proxy,
  80. const char *auth);
  81. CURLcode Curl_http_auth_act(struct connectdata *conn);
  82. CURLcode Curl_http_perhapsrewind(struct connectdata *conn);
  83. /* If only the PICKNONE bit is set, there has been a round-trip and we
  84. selected to use no auth at all. Ie, we actively select no auth, as opposed
  85. to not having one selected. The other CURLAUTH_* defines are present in the
  86. public curl/curl.h header. */
  87. #define CURLAUTH_PICKNONE (1<<30) /* don't use auth */
  88. /* MAX_INITIAL_POST_SIZE indicates the number of bytes that will make the POST
  89. data get included in the initial data chunk sent to the server. If the
  90. data is larger than this, it will automatically get split up in multiple
  91. system calls.
  92. This value used to be fairly big (100K), but we must take into account that
  93. if the server rejects the POST due for authentication reasons, this data
  94. will always be unconditionally sent and thus it may not be larger than can
  95. always be afforded to send twice.
  96. It must not be greater than 64K to work on VMS.
  97. */
  98. #ifndef MAX_INITIAL_POST_SIZE
  99. #define MAX_INITIAL_POST_SIZE (64*1024)
  100. #endif
  101. /* EXPECT_100_THRESHOLD is the request body size limit for when libcurl will
  102. * automatically add an "Expect: 100-continue" header in HTTP requests. When
  103. * the size is unknown, it will always add it.
  104. *
  105. */
  106. #ifndef EXPECT_100_THRESHOLD
  107. #define EXPECT_100_THRESHOLD 1024
  108. #endif
  109. #endif /* CURL_DISABLE_HTTP */
  110. /****************************************************************************
  111. * HTTP unique setup
  112. ***************************************************************************/
  113. struct HTTP {
  114. curl_mimepart *sendit;
  115. curl_off_t postsize; /* off_t to handle large file sizes */
  116. const char *postdata;
  117. const char *p_pragma; /* Pragma: string */
  118. const char *p_accept; /* Accept: string */
  119. curl_off_t readbytecount;
  120. curl_off_t writebytecount;
  121. /* For FORM posting */
  122. curl_mimepart form;
  123. struct back {
  124. curl_read_callback fread_func; /* backup storage for fread pointer */
  125. void *fread_in; /* backup storage for fread_in pointer */
  126. const char *postdata;
  127. curl_off_t postsize;
  128. } backup;
  129. enum {
  130. HTTPSEND_NADA, /* init */
  131. HTTPSEND_REQUEST, /* sending a request */
  132. HTTPSEND_BODY, /* sending body */
  133. HTTPSEND_LAST /* never use this */
  134. } sending;
  135. #ifndef CURL_DISABLE_HTTP
  136. Curl_send_buffer *send_buffer; /* used if the request couldn't be sent in
  137. one chunk, points to an allocated
  138. send_buffer struct */
  139. #endif
  140. #ifdef USE_NGHTTP2
  141. /*********** for HTTP/2 we store stream-local data here *************/
  142. int32_t stream_id; /* stream we are interested in */
  143. bool bodystarted;
  144. /* We store non-final and final response headers here, per-stream */
  145. Curl_send_buffer *header_recvbuf;
  146. size_t nread_header_recvbuf; /* number of bytes in header_recvbuf fed into
  147. upper layer */
  148. Curl_send_buffer *trailer_recvbuf;
  149. int status_code; /* HTTP status code */
  150. const uint8_t *pausedata; /* pointer to data received in on_data_chunk */
  151. size_t pauselen; /* the number of bytes left in data */
  152. bool closed; /* TRUE on HTTP2 stream close */
  153. bool close_handled; /* TRUE if stream closure is handled by libcurl */
  154. char *mem; /* points to a buffer in memory to store received data */
  155. size_t len; /* size of the buffer 'mem' points to */
  156. size_t memlen; /* size of data copied to mem */
  157. const uint8_t *upload_mem; /* points to a buffer to read from */
  158. size_t upload_len; /* size of the buffer 'upload_mem' points to */
  159. curl_off_t upload_left; /* number of bytes left to upload */
  160. char **push_headers; /* allocated array */
  161. size_t push_headers_used; /* number of entries filled in */
  162. size_t push_headers_alloc; /* number of entries allocated */
  163. #endif
  164. };
  165. #ifdef USE_NGHTTP2
  166. /* h2 settings for this connection */
  167. struct h2settings {
  168. uint32_t max_concurrent_streams;
  169. bool enable_push;
  170. };
  171. #endif
  172. struct http_conn {
  173. #ifdef USE_NGHTTP2
  174. #define H2_BINSETTINGS_LEN 80
  175. nghttp2_session *h2;
  176. uint8_t binsettings[H2_BINSETTINGS_LEN];
  177. size_t binlen; /* length of the binsettings data */
  178. Curl_send *send_underlying; /* underlying send Curl_send callback */
  179. Curl_recv *recv_underlying; /* underlying recv Curl_recv callback */
  180. char *inbuf; /* buffer to receive data from underlying socket */
  181. size_t inbuflen; /* number of bytes filled in inbuf */
  182. size_t nread_inbuf; /* number of bytes read from in inbuf */
  183. /* We need separate buffer for transmission and reception because we
  184. may call nghttp2_session_send() after the
  185. nghttp2_session_mem_recv() but mem buffer is still not full. In
  186. this case, we wrongly sends the content of mem buffer if we share
  187. them for both cases. */
  188. int32_t pause_stream_id; /* stream ID which paused
  189. nghttp2_session_mem_recv */
  190. size_t drain_total; /* sum of all stream's UrlState.drain */
  191. /* this is a hash of all individual streams (Curl_easy structs) */
  192. struct h2settings settings;
  193. /* list of settings that will be sent */
  194. nghttp2_settings_entry local_settings[3];
  195. size_t local_settings_num;
  196. uint32_t error_code; /* HTTP/2 error code */
  197. #else
  198. int unused; /* prevent a compiler warning */
  199. #endif
  200. };
  201. CURLcode Curl_http_readwrite_headers(struct Curl_easy *data,
  202. struct connectdata *conn,
  203. ssize_t *nread,
  204. bool *stop_reading);
  205. /**
  206. * Curl_http_output_auth() setups the authentication headers for the
  207. * host/proxy and the correct authentication
  208. * method. conn->data->state.authdone is set to TRUE when authentication is
  209. * done.
  210. *
  211. * @param conn all information about the current connection
  212. * @param request pointer to the request keyword
  213. * @param path pointer to the requested path
  214. * @param proxytunnel boolean if this is the request setting up a "proxy
  215. * tunnel"
  216. *
  217. * @returns CURLcode
  218. */
  219. CURLcode
  220. Curl_http_output_auth(struct connectdata *conn,
  221. const char *request,
  222. const char *path,
  223. bool proxytunnel); /* TRUE if this is the request setting
  224. up the proxy tunnel */
  225. #endif /* HEADER_CURL_HTTP_H */