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