2006-06-10 22:28:21 +00:00
|
|
|
/*
|
2009-01-27 22:34:36 +00:00
|
|
|
* Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
|
2012-02-10 17:29:53 -05:00
|
|
|
* Copyright 2007-2012 Niels Provos and Nick Mathewson
|
2006-06-10 22:28:21 +00:00
|
|
|
*
|
|
|
|
* This header file contains definitions for dealing with HTTP requests
|
|
|
|
* that are internal to libevent. As user of the library, you should not
|
|
|
|
* need to know about these.
|
|
|
|
*/
|
|
|
|
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifndef HTTP_INTERNAL_H_INCLUDED_
|
|
|
|
#define HTTP_INTERNAL_H_INCLUDED_
|
2008-05-08 15:55:09 +00:00
|
|
|
|
|
|
|
#include "event2/event_struct.h"
|
2009-04-13 18:23:02 +00:00
|
|
|
#include "util-internal.h"
|
2010-10-25 21:53:15 -04:00
|
|
|
#include "defer-internal.h"
|
2006-06-10 22:28:21 +00:00
|
|
|
|
|
|
|
#define HTTP_CONNECT_TIMEOUT 45
|
|
|
|
#define HTTP_WRITE_TIMEOUT 50
|
|
|
|
#define HTTP_READ_TIMEOUT 50
|
|
|
|
|
|
|
|
#define HTTP_PREFIX "http://"
|
|
|
|
#define HTTP_DEFAULTPORT 80
|
|
|
|
|
2008-06-26 00:40:57 +00:00
|
|
|
enum message_read_status {
|
|
|
|
ALL_DATA_READ = 1,
|
|
|
|
MORE_DATA_EXPECTED = 0,
|
|
|
|
DATA_CORRUPTED = -1,
|
2009-11-04 20:17:32 +00:00
|
|
|
REQUEST_CANCELED = -2,
|
|
|
|
DATA_TOO_LONG = -3
|
2008-06-26 00:40:57 +00:00
|
|
|
};
|
|
|
|
|
2006-06-10 22:28:21 +00:00
|
|
|
struct evbuffer;
|
|
|
|
struct addrinfo;
|
2006-07-17 00:33:57 +00:00
|
|
|
struct evhttp_request;
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2010-11-04 12:39:41 -04:00
|
|
|
/* Indicates an unknown request method. */
|
2012-02-29 15:07:32 -05:00
|
|
|
#define EVHTTP_REQ_UNKNOWN_ (1<<15)
|
2010-11-04 12:39:41 -04:00
|
|
|
|
2006-07-17 00:33:57 +00:00
|
|
|
enum evhttp_connection_state {
|
2008-06-26 00:40:57 +00:00
|
|
|
EVCON_DISCONNECTED, /**< not currently connected not trying either*/
|
|
|
|
EVCON_CONNECTING, /**< tries to currently connect */
|
|
|
|
EVCON_IDLE, /**< connection is established */
|
|
|
|
EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
|
|
|
|
**< Status-Line (outgoing conn) */
|
|
|
|
EVCON_READING_HEADERS, /**< reading request/response headers */
|
|
|
|
EVCON_READING_BODY, /**< reading request/response body */
|
|
|
|
EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
|
|
|
|
EVCON_WRITING /**< writing request/response headers/body */
|
2006-07-17 00:33:57 +00:00
|
|
|
};
|
|
|
|
|
2007-08-19 02:41:23 +00:00
|
|
|
struct event_base;
|
|
|
|
|
2010-08-09 13:25:50 -04:00
|
|
|
/* A client or server connection. */
|
2006-06-10 22:28:21 +00:00
|
|
|
struct evhttp_connection {
|
2010-10-21 12:23:10 -04:00
|
|
|
/* we use this tailq only if this connection was created for an http
|
|
|
|
* server */
|
2010-08-13 17:08:59 -04:00
|
|
|
TAILQ_ENTRY(evhttp_connection) next;
|
2006-11-18 03:05:26 +00:00
|
|
|
|
2007-11-25 21:53:06 +00:00
|
|
|
evutil_socket_t fd;
|
2008-04-29 04:52:50 +00:00
|
|
|
struct bufferevent *bufev;
|
|
|
|
|
|
|
|
struct event retry_ev; /* for retrying connects */
|
2009-07-17 18:38:38 +00:00
|
|
|
|
2007-09-07 02:49:46 +00:00
|
|
|
char *bind_address; /* address to use for binding the src */
|
2014-12-13 19:42:42 +01:00
|
|
|
ev_uint16_t bind_port; /* local port for binding the src */
|
2007-09-07 02:49:46 +00:00
|
|
|
|
|
|
|
char *address; /* address to connect to */
|
2014-12-13 19:42:42 +01:00
|
|
|
ev_uint16_t port;
|
2006-07-17 00:33:57 +00:00
|
|
|
|
2009-11-04 20:17:32 +00:00
|
|
|
size_t max_headers_size;
|
2009-11-05 15:57:22 +00:00
|
|
|
ev_uint64_t max_body_size;
|
2009-11-04 20:17:32 +00:00
|
|
|
|
2006-07-17 00:33:57 +00:00
|
|
|
int flags;
|
2016-02-15 02:59:40 +03:00
|
|
|
#define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
|
|
|
|
#define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
|
|
|
|
#define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
|
|
|
|
/* set when we want to auto free the connection */
|
|
|
|
#define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END
|
2016-03-11 19:58:05 +03:00
|
|
|
/* Installed when attempt to read HTTP error after write failed, see
|
|
|
|
* EVHTTP_CON_READ_ON_WRITE_ERROR */
|
|
|
|
#define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1)
|
2006-11-22 06:54:28 +00:00
|
|
|
|
2011-02-22 17:52:50 -05:00
|
|
|
struct timeval timeout; /* timeout for events */
|
2006-12-09 01:41:57 +00:00
|
|
|
int retry_cnt; /* retry count */
|
|
|
|
int retry_max; /* maximum number of retries */
|
2012-01-24 14:34:04 -05:00
|
|
|
struct timeval initial_retry_timeout; /* Timeout for low long to wait
|
|
|
|
* after first failing attempt
|
|
|
|
* before retry */
|
2009-07-17 18:38:38 +00:00
|
|
|
|
2006-07-17 00:33:57 +00:00
|
|
|
enum evhttp_connection_state state;
|
|
|
|
|
2006-11-18 03:05:26 +00:00
|
|
|
/* for server connections, the http server they are connected with */
|
|
|
|
struct evhttp *http_server;
|
|
|
|
|
2006-07-17 00:33:57 +00:00
|
|
|
TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
|
2009-07-17 18:38:38 +00:00
|
|
|
|
2008-04-29 04:52:50 +00:00
|
|
|
void (*cb)(struct evhttp_connection *, void *);
|
2006-06-10 22:28:21 +00:00
|
|
|
void *cb_arg;
|
2009-07-17 18:38:38 +00:00
|
|
|
|
2006-12-09 02:58:12 +00:00
|
|
|
void (*closecb)(struct evhttp_connection *, void *);
|
|
|
|
void *closecb_arg;
|
2007-08-19 02:41:23 +00:00
|
|
|
|
2012-04-06 11:05:35 -04:00
|
|
|
struct event_callback read_more_deferred_cb;
|
2010-10-25 21:53:15 -04:00
|
|
|
|
2007-08-19 02:41:23 +00:00
|
|
|
struct event_base *base;
|
2009-12-30 00:11:27 -05:00
|
|
|
struct evdns_base *dns_base;
|
2014-03-21 17:32:09 +04:00
|
|
|
int ai_family;
|
2006-06-10 22:28:21 +00:00
|
|
|
};
|
|
|
|
|
2010-08-09 13:25:50 -04:00
|
|
|
/* A callback for an http server */
|
2006-06-10 22:28:21 +00:00
|
|
|
struct evhttp_cb {
|
|
|
|
TAILQ_ENTRY(evhttp_cb) next;
|
|
|
|
|
|
|
|
char *what;
|
|
|
|
|
|
|
|
void (*cb)(struct evhttp_request *req, void *);
|
|
|
|
void *cbarg;
|
|
|
|
};
|
|
|
|
|
2006-11-20 03:32:53 +00:00
|
|
|
/* both the http server as well as the rpc system need to queue connections */
|
|
|
|
TAILQ_HEAD(evconq, evhttp_connection);
|
|
|
|
|
2008-05-04 22:21:29 +00:00
|
|
|
/* each bound socket is stored in one of these */
|
|
|
|
struct evhttp_bound_socket {
|
2010-08-13 17:08:59 -04:00
|
|
|
TAILQ_ENTRY(evhttp_bound_socket) next;
|
2008-05-04 22:21:29 +00:00
|
|
|
|
2009-12-30 00:41:03 -05:00
|
|
|
struct evconnlistener *listener;
|
2008-05-04 22:21:29 +00:00
|
|
|
};
|
|
|
|
|
2010-11-29 07:18:32 -08:00
|
|
|
/* server alias list item. */
|
2010-11-05 11:17:07 -07:00
|
|
|
struct evhttp_server_alias {
|
|
|
|
TAILQ_ENTRY(evhttp_server_alias) next;
|
2010-11-29 07:18:32 -08:00
|
|
|
|
|
|
|
char *alias; /* the server alias. */
|
2010-11-05 11:17:07 -07:00
|
|
|
};
|
|
|
|
|
2006-06-10 22:28:21 +00:00
|
|
|
struct evhttp {
|
2010-08-09 13:25:50 -04:00
|
|
|
/* Next vhost, if this is a vhost. */
|
|
|
|
TAILQ_ENTRY(evhttp) next_vhost;
|
2008-05-05 07:17:05 +00:00
|
|
|
|
2010-08-09 13:25:50 -04:00
|
|
|
/* All listeners for this host */
|
2008-05-04 22:21:29 +00:00
|
|
|
TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
|
2006-06-10 22:28:21 +00:00
|
|
|
|
|
|
|
TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
|
2010-08-09 13:25:50 -04:00
|
|
|
|
|
|
|
/* All live connections on this host. */
|
2010-02-18 17:41:15 -05:00
|
|
|
struct evconq connections;
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2009-07-17 18:38:38 +00:00
|
|
|
TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
|
2008-05-05 07:17:05 +00:00
|
|
|
|
2010-11-05 11:17:07 -07:00
|
|
|
TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
|
|
|
|
|
2008-05-05 07:17:05 +00:00
|
|
|
/* NULL if this server is not a vhost */
|
2010-02-18 17:41:15 -05:00
|
|
|
char *vhost_pattern;
|
2008-05-05 07:17:05 +00:00
|
|
|
|
2011-02-22 17:52:50 -05:00
|
|
|
struct timeval timeout;
|
2006-11-23 06:32:20 +00:00
|
|
|
|
2009-11-04 20:17:32 +00:00
|
|
|
size_t default_max_headers_size;
|
2009-11-05 15:57:22 +00:00
|
|
|
ev_uint64_t default_max_body_size;
|
2016-02-15 00:12:54 +03:00
|
|
|
int flags;
|
2013-09-28 20:03:28 +02:00
|
|
|
const char *default_content_type;
|
2009-11-05 15:57:22 +00:00
|
|
|
|
2010-11-04 11:53:34 -04:00
|
|
|
/* Bitmask of all HTTP methods that we accept and pass to user
|
|
|
|
* callbacks. */
|
|
|
|
ev_uint16_t allowed_methods;
|
2010-11-04 11:25:35 -04:00
|
|
|
|
2010-08-09 13:25:50 -04:00
|
|
|
/* Fallback callback if all the other callbacks for this connection
|
|
|
|
don't match. */
|
2006-06-10 22:28:21 +00:00
|
|
|
void (*gencb)(struct evhttp_request *req, void *);
|
|
|
|
void *gencbarg;
|
2011-09-12 10:46:17 -04:00
|
|
|
struct bufferevent* (*bevcb)(struct event_base *, void *);
|
|
|
|
void *bevcbarg;
|
2017-12-01 01:29:32 +00:00
|
|
|
int (*newreqcb)(struct evhttp_request *req, void *);
|
|
|
|
void *newreqcbarg;
|
2007-08-19 02:41:23 +00:00
|
|
|
|
|
|
|
struct event_base *base;
|
2006-06-10 22:28:21 +00:00
|
|
|
};
|
|
|
|
|
2010-08-09 13:25:50 -04:00
|
|
|
/* XXX most of these functions could be static. */
|
|
|
|
|
2006-07-17 00:33:57 +00:00
|
|
|
/* resets the connection; can be reused for more requests */
|
2012-02-29 15:07:33 -05:00
|
|
|
void evhttp_connection_reset_(struct evhttp_connection *);
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2006-07-17 00:33:57 +00:00
|
|
|
/* connects if necessary */
|
2012-02-29 15:07:33 -05:00
|
|
|
int evhttp_connection_connect_(struct evhttp_connection *);
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2013-03-21 13:55:40 +04:00
|
|
|
enum evhttp_request_error;
|
2006-07-17 00:33:57 +00:00
|
|
|
/* notifies the current request that it failed; resets connection */
|
2017-03-12 20:50:35 +03:00
|
|
|
EVENT2_EXPORT_SYMBOL
|
2012-02-29 15:07:33 -05:00
|
|
|
void evhttp_connection_fail_(struct evhttp_connection *,
|
2013-03-21 13:55:40 +04:00
|
|
|
enum evhttp_request_error error);
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2009-04-23 05:40:06 +00:00
|
|
|
enum message_read_status;
|
|
|
|
|
2017-03-12 20:50:35 +03:00
|
|
|
EVENT2_EXPORT_SYMBOL
|
2012-02-29 15:07:33 -05:00
|
|
|
enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
|
2017-03-12 20:50:35 +03:00
|
|
|
EVENT2_EXPORT_SYMBOL
|
2012-02-29 15:07:33 -05:00
|
|
|
enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2012-02-29 15:07:33 -05:00
|
|
|
void evhttp_start_read_(struct evhttp_connection *);
|
2016-03-11 13:08:28 +03:00
|
|
|
void evhttp_start_write_(struct evhttp_connection *);
|
2006-06-10 22:28:21 +00:00
|
|
|
|
|
|
|
/* response sending HTML the data in the buffer */
|
2012-02-29 15:07:33 -05:00
|
|
|
void evhttp_response_code_(struct evhttp_request *, int, const char *);
|
|
|
|
void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
|
2006-06-10 22:28:21 +00:00
|
|
|
|
2017-03-12 20:50:35 +03:00
|
|
|
EVENT2_EXPORT_SYMBOL
|
2013-03-01 12:00:24 +04:00
|
|
|
int evhttp_decode_uri_internal(const char *uri, size_t length,
|
|
|
|
char *ret, int decode_plus);
|
|
|
|
|
2006-06-10 22:28:21 +00:00
|
|
|
#endif /* _HTTP_H */
|