Home | History | Annotate | Download | only in curl
      1 #ifndef __CURL_MULTI_H
      2 #define __CURL_MULTI_H
      3 /***************************************************************************
      4  *                                  _   _ ____  _
      5  *  Project                     ___| | | |  _ \| |
      6  *                             / __| | | | |_) | |
      7  *                            | (__| |_| |  _ <| |___
      8  *                             \___|\___/|_| \_\_____|
      9  *
     10  * Copyright (C) 1998 - 2017, 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 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 /*
     25   This is an "external" header file. Don't give away any internals here!
     26 
     27   GOALS
     28 
     29   o Enable a "pull" interface. The application that uses libcurl decides where
     30     and when to ask libcurl to get/send data.
     31 
     32   o Enable multiple simultaneous transfers in the same thread without making it
     33     complicated for the application.
     34 
     35   o Enable the application to select() on its own file descriptors and curl's
     36     file descriptors simultaneous easily.
     37 
     38 */
     39 
     40 /*
     41  * This header file should not really need to include "curl.h" since curl.h
     42  * itself includes this file and we expect user applications to do #include
     43  * <curl/curl.h> without the need for especially including multi.h.
     44  *
     45  * For some reason we added this include here at one point, and rather than to
     46  * break existing (wrongly written) libcurl applications, we leave it as-is
     47  * but with this warning attached.
     48  */
     49 #include "curl.h"
     50 
     51 #ifdef  __cplusplus
     52 extern "C" {
     53 #endif
     54 
     55 #if defined(BUILDING_LIBCURL) || defined(CURL_STRICTER)
     56 typedef struct Curl_multi CURLM;
     57 #else
     58 typedef void CURLM;
     59 #endif
     60 
     61 typedef enum {
     62   CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or
     63                                     curl_multi_socket*() soon */
     64   CURLM_OK,
     65   CURLM_BAD_HANDLE,      /* the passed-in handle is not a valid CURLM handle */
     66   CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
     67   CURLM_OUT_OF_MEMORY,   /* if you ever get this, you're in deep sh*t */
     68   CURLM_INTERNAL_ERROR,  /* this is a libcurl bug */
     69   CURLM_BAD_SOCKET,      /* the passed in socket argument did not match */
     70   CURLM_UNKNOWN_OPTION,  /* curl_multi_setopt() with unsupported option */
     71   CURLM_ADDED_ALREADY,   /* an easy handle already added to a multi handle was
     72                             attempted to get added - again */
     73   CURLM_LAST
     74 } CURLMcode;
     75 
     76 /* just to make code nicer when using curl_multi_socket() you can now check
     77    for CURLM_CALL_MULTI_SOCKET too in the same style it works for
     78    curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
     79 #define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
     80 
     81 /* bitmask bits for CURLMOPT_PIPELINING */
     82 #define CURLPIPE_NOTHING   0L
     83 #define CURLPIPE_HTTP1     1L
     84 #define CURLPIPE_MULTIPLEX 2L
     85 
     86 typedef enum {
     87   CURLMSG_NONE, /* first, not used */
     88   CURLMSG_DONE, /* This easy handle has completed. 'result' contains
     89                    the CURLcode of the transfer */
     90   CURLMSG_LAST /* last, not used */
     91 } CURLMSG;
     92 
     93 struct CURLMsg {
     94   CURLMSG msg;       /* what this message means */
     95   CURL *easy_handle; /* the handle it concerns */
     96   union {
     97     void *whatever;    /* message-specific data */
     98     CURLcode result;   /* return code for transfer */
     99   } data;
    100 };
    101 typedef struct CURLMsg CURLMsg;
    102 
    103 /* Based on poll(2) structure and values.
    104  * We don't use pollfd and POLL* constants explicitly
    105  * to cover platforms without poll(). */
    106 #define CURL_WAIT_POLLIN    0x0001
    107 #define CURL_WAIT_POLLPRI   0x0002
    108 #define CURL_WAIT_POLLOUT   0x0004
    109 
    110 struct curl_waitfd {
    111   curl_socket_t fd;
    112   short events;
    113   short revents; /* not supported yet */
    114 };
    115 
    116 /*
    117  * Name:    curl_multi_init()
    118  *
    119  * Desc:    inititalize multi-style curl usage
    120  *
    121  * Returns: a new CURLM handle to use in all 'curl_multi' functions.
    122  */
    123 CURL_EXTERN CURLM *curl_multi_init(void);
    124 
    125 /*
    126  * Name:    curl_multi_add_handle()
    127  *
    128  * Desc:    add a standard curl handle to the multi stack
    129  *
    130  * Returns: CURLMcode type, general multi error code.
    131  */
    132 CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
    133                                             CURL *curl_handle);
    134 
    135  /*
    136   * Name:    curl_multi_remove_handle()
    137   *
    138   * Desc:    removes a curl handle from the multi stack again
    139   *
    140   * Returns: CURLMcode type, general multi error code.
    141   */
    142 CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
    143                                                CURL *curl_handle);
    144 
    145  /*
    146   * Name:    curl_multi_fdset()
    147   *
    148   * Desc:    Ask curl for its fd_set sets. The app can use these to select() or
    149   *          poll() on. We want curl_multi_perform() called as soon as one of
    150   *          them are ready.
    151   *
    152   * Returns: CURLMcode type, general multi error code.
    153   */
    154 CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
    155                                        fd_set *read_fd_set,
    156                                        fd_set *write_fd_set,
    157                                        fd_set *exc_fd_set,
    158                                        int *max_fd);
    159 
    160 /*
    161  * Name:     curl_multi_wait()
    162  *
    163  * Desc:     Poll on all fds within a CURLM set as well as any
    164  *           additional fds passed to the function.
    165  *
    166  * Returns:  CURLMcode type, general multi error code.
    167  */
    168 CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
    169                                       struct curl_waitfd extra_fds[],
    170                                       unsigned int extra_nfds,
    171                                       int timeout_ms,
    172                                       int *ret);
    173 
    174  /*
    175   * Name:    curl_multi_perform()
    176   *
    177   * Desc:    When the app thinks there's data available for curl it calls this
    178   *          function to read/write whatever there is right now. This returns
    179   *          as soon as the reads and writes are done. This function does not
    180   *          require that there actually is data available for reading or that
    181   *          data can be written, it can be called just in case. It returns
    182   *          the number of handles that still transfer data in the second
    183   *          argument's integer-pointer.
    184   *
    185   * Returns: CURLMcode type, general multi error code. *NOTE* that this only
    186   *          returns errors etc regarding the whole multi stack. There might
    187   *          still have occurred problems on invidual transfers even when this
    188   *          returns OK.
    189   */
    190 CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
    191                                          int *running_handles);
    192 
    193  /*
    194   * Name:    curl_multi_cleanup()
    195   *
    196   * Desc:    Cleans up and removes a whole multi stack. It does not free or
    197   *          touch any individual easy handles in any way. We need to define
    198   *          in what state those handles will be if this function is called
    199   *          in the middle of a transfer.
    200   *
    201   * Returns: CURLMcode type, general multi error code.
    202   */
    203 CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
    204 
    205 /*
    206  * Name:    curl_multi_info_read()
    207  *
    208  * Desc:    Ask the multi handle if there's any messages/informationals from
    209  *          the individual transfers. Messages include informationals such as
    210  *          error code from the transfer or just the fact that a transfer is
    211  *          completed. More details on these should be written down as well.
    212  *
    213  *          Repeated calls to this function will return a new struct each
    214  *          time, until a special "end of msgs" struct is returned as a signal
    215  *          that there is no more to get at this point.
    216  *
    217  *          The data the returned pointer points to will not survive calling
    218  *          curl_multi_cleanup().
    219  *
    220  *          The 'CURLMsg' struct is meant to be very simple and only contain
    221  *          very basic information. If more involved information is wanted,
    222  *          we will provide the particular "transfer handle" in that struct
    223  *          and that should/could/would be used in subsequent
    224  *          curl_easy_getinfo() calls (or similar). The point being that we
    225  *          must never expose complex structs to applications, as then we'll
    226  *          undoubtably get backwards compatibility problems in the future.
    227  *
    228  * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
    229  *          of structs. It also writes the number of messages left in the
    230  *          queue (after this read) in the integer the second argument points
    231  *          to.
    232  */
    233 CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
    234                                           int *msgs_in_queue);
    235 
    236 /*
    237  * Name:    curl_multi_strerror()
    238  *
    239  * Desc:    The curl_multi_strerror function may be used to turn a CURLMcode
    240  *          value into the equivalent human readable error string.  This is
    241  *          useful for printing meaningful error messages.
    242  *
    243  * Returns: A pointer to a zero-terminated error message.
    244  */
    245 CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
    246 
    247 /*
    248  * Name:    curl_multi_socket() and
    249  *          curl_multi_socket_all()
    250  *
    251  * Desc:    An alternative version of curl_multi_perform() that allows the
    252  *          application to pass in one of the file descriptors that have been
    253  *          detected to have "action" on them and let libcurl perform.
    254  *          See man page for details.
    255  */
    256 #define CURL_POLL_NONE   0
    257 #define CURL_POLL_IN     1
    258 #define CURL_POLL_OUT    2
    259 #define CURL_POLL_INOUT  3
    260 #define CURL_POLL_REMOVE 4
    261 
    262 #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
    263 
    264 #define CURL_CSELECT_IN   0x01
    265 #define CURL_CSELECT_OUT  0x02
    266 #define CURL_CSELECT_ERR  0x04
    267 
    268 typedef int (*curl_socket_callback)(CURL *easy,      /* easy handle */
    269                                     curl_socket_t s, /* socket */
    270                                     int what,        /* see above */
    271                                     void *userp,     /* private callback
    272                                                         pointer */
    273                                     void *socketp);  /* private socket
    274                                                         pointer */
    275 /*
    276  * Name:    curl_multi_timer_callback
    277  *
    278  * Desc:    Called by libcurl whenever the library detects a change in the
    279  *          maximum number of milliseconds the app is allowed to wait before
    280  *          curl_multi_socket() or curl_multi_perform() must be called
    281  *          (to allow libcurl's timed events to take place).
    282  *
    283  * Returns: The callback should return zero.
    284  */
    285 typedef int (*curl_multi_timer_callback)(CURLM *multi,    /* multi handle */
    286                                          long timeout_ms, /* see above */
    287                                          void *userp);    /* private callback
    288                                                              pointer */
    289 
    290 CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
    291                                         int *running_handles);
    292 
    293 CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
    294                                                curl_socket_t s,
    295                                                int ev_bitmask,
    296                                                int *running_handles);
    297 
    298 CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,
    299                                             int *running_handles);
    300 
    301 #ifndef CURL_ALLOW_OLD_MULTI_SOCKET
    302 /* This macro below was added in 7.16.3 to push users who recompile to use
    303    the new curl_multi_socket_action() instead of the old curl_multi_socket()
    304 */
    305 #define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
    306 #endif
    307 
    308 /*
    309  * Name:    curl_multi_timeout()
    310  *
    311  * Desc:    Returns the maximum number of milliseconds the app is allowed to
    312  *          wait before curl_multi_socket() or curl_multi_perform() must be
    313  *          called (to allow libcurl's timed events to take place).
    314  *
    315  * Returns: CURLM error code.
    316  */
    317 CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
    318                                          long *milliseconds);
    319 
    320 #undef CINIT /* re-using the same name as in curl.h */
    321 
    322 #ifdef CURL_ISOCPP
    323 #define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num
    324 #else
    325 /* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */
    326 #define LONG          CURLOPTTYPE_LONG
    327 #define OBJECTPOINT   CURLOPTTYPE_OBJECTPOINT
    328 #define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT
    329 #define OFF_T         CURLOPTTYPE_OFF_T
    330 #define CINIT(name,type,number) CURLMOPT_/**/name = type + number
    331 #endif
    332 
    333 typedef enum {
    334   /* This is the socket callback function pointer */
    335   CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),
    336 
    337   /* This is the argument passed to the socket callback */
    338   CINIT(SOCKETDATA, OBJECTPOINT, 2),
    339 
    340     /* set to 1 to enable pipelining for this multi handle */
    341   CINIT(PIPELINING, LONG, 3),
    342 
    343    /* This is the timer callback function pointer */
    344   CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4),
    345 
    346   /* This is the argument passed to the timer callback */
    347   CINIT(TIMERDATA, OBJECTPOINT, 5),
    348 
    349   /* maximum number of entries in the connection cache */
    350   CINIT(MAXCONNECTS, LONG, 6),
    351 
    352   /* maximum number of (pipelining) connections to one host */
    353   CINIT(MAX_HOST_CONNECTIONS, LONG, 7),
    354 
    355   /* maximum number of requests in a pipeline */
    356   CINIT(MAX_PIPELINE_LENGTH, LONG, 8),
    357 
    358   /* a connection with a content-length longer than this
    359      will not be considered for pipelining */
    360   CINIT(CONTENT_LENGTH_PENALTY_SIZE, OFF_T, 9),
    361 
    362   /* a connection with a chunk length longer than this
    363      will not be considered for pipelining */
    364   CINIT(CHUNK_LENGTH_PENALTY_SIZE, OFF_T, 10),
    365 
    366   /* a list of site names(+port) that are blacklisted from
    367      pipelining */
    368   CINIT(PIPELINING_SITE_BL, OBJECTPOINT, 11),
    369 
    370   /* a list of server types that are blacklisted from
    371      pipelining */
    372   CINIT(PIPELINING_SERVER_BL, OBJECTPOINT, 12),
    373 
    374   /* maximum number of open connections in total */
    375   CINIT(MAX_TOTAL_CONNECTIONS, LONG, 13),
    376 
    377    /* This is the server push callback function pointer */
    378   CINIT(PUSHFUNCTION, FUNCTIONPOINT, 14),
    379 
    380   /* This is the argument passed to the server push callback */
    381   CINIT(PUSHDATA, OBJECTPOINT, 15),
    382 
    383   CURLMOPT_LASTENTRY /* the last unused */
    384 } CURLMoption;
    385 
    386 
    387 /*
    388  * Name:    curl_multi_setopt()
    389  *
    390  * Desc:    Sets options for the multi handle.
    391  *
    392  * Returns: CURLM error code.
    393  */
    394 CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
    395                                         CURLMoption option, ...);
    396 
    397 
    398 /*
    399  * Name:    curl_multi_assign()
    400  *
    401  * Desc:    This function sets an association in the multi handle between the
    402  *          given socket and a private pointer of the application. This is
    403  *          (only) useful for curl_multi_socket uses.
    404  *
    405  * Returns: CURLM error code.
    406  */
    407 CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
    408                                         curl_socket_t sockfd, void *sockp);
    409 
    410 
    411 /*
    412  * Name: curl_push_callback
    413  *
    414  * Desc: This callback gets called when a new stream is being pushed by the
    415  *       server. It approves or denies the new stream.
    416  *
    417  * Returns: CURL_PUSH_OK or CURL_PUSH_DENY.
    418  */
    419 #define CURL_PUSH_OK   0
    420 #define CURL_PUSH_DENY 1
    421 
    422 struct curl_pushheaders;  /* forward declaration only */
    423 
    424 CURL_EXTERN char *curl_pushheader_bynum(struct curl_pushheaders *h,
    425                                         size_t num);
    426 CURL_EXTERN char *curl_pushheader_byname(struct curl_pushheaders *h,
    427                                          const char *name);
    428 
    429 typedef int (*curl_push_callback)(CURL *parent,
    430                                   CURL *easy,
    431                                   size_t num_headers,
    432                                   struct curl_pushheaders *headers,
    433                                   void *userp);
    434 
    435 #ifdef __cplusplus
    436 } /* end of extern "C" */
    437 #endif
    438 
    439 #endif
    440