Austin Schuh | a273376 | 2015-09-06 17:46:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2001-2007 Niels Provos <provos@citi.umich.edu> |
| 3 | * Copyright 2007-2012 Niels Provos and Nick Mathewson |
| 4 | * |
| 5 | * This header file contains definitions for dealing with HTTP requests |
| 6 | * that are internal to libevent. As user of the library, you should not |
| 7 | * need to know about these. |
| 8 | */ |
| 9 | |
| 10 | #ifndef _HTTP_INTERNAL_H_ |
| 11 | #define _HTTP_INTERNAL_H_ |
| 12 | |
| 13 | #include "event2/event_struct.h" |
| 14 | #include "util-internal.h" |
| 15 | #include "defer-internal.h" |
| 16 | |
| 17 | #define HTTP_CONNECT_TIMEOUT 45 |
| 18 | #define HTTP_WRITE_TIMEOUT 50 |
| 19 | #define HTTP_READ_TIMEOUT 50 |
| 20 | |
| 21 | #define HTTP_PREFIX "http://" |
| 22 | #define HTTP_DEFAULTPORT 80 |
| 23 | |
| 24 | enum message_read_status { |
| 25 | ALL_DATA_READ = 1, |
| 26 | MORE_DATA_EXPECTED = 0, |
| 27 | DATA_CORRUPTED = -1, |
| 28 | REQUEST_CANCELED = -2, |
| 29 | DATA_TOO_LONG = -3 |
| 30 | }; |
| 31 | |
| 32 | enum evhttp_connection_error { |
| 33 | EVCON_HTTP_TIMEOUT, |
| 34 | EVCON_HTTP_EOF, |
| 35 | EVCON_HTTP_INVALID_HEADER, |
| 36 | EVCON_HTTP_BUFFER_ERROR, |
| 37 | EVCON_HTTP_REQUEST_CANCEL |
| 38 | }; |
| 39 | |
| 40 | struct evbuffer; |
| 41 | struct addrinfo; |
| 42 | struct evhttp_request; |
| 43 | |
| 44 | /* Indicates an unknown request method. */ |
| 45 | #define _EVHTTP_REQ_UNKNOWN (1<<15) |
| 46 | |
| 47 | enum evhttp_connection_state { |
| 48 | EVCON_DISCONNECTED, /**< not currently connected not trying either*/ |
| 49 | EVCON_CONNECTING, /**< tries to currently connect */ |
| 50 | EVCON_IDLE, /**< connection is established */ |
| 51 | EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or |
| 52 | **< Status-Line (outgoing conn) */ |
| 53 | EVCON_READING_HEADERS, /**< reading request/response headers */ |
| 54 | EVCON_READING_BODY, /**< reading request/response body */ |
| 55 | EVCON_READING_TRAILER, /**< reading request/response chunked trailer */ |
| 56 | EVCON_WRITING /**< writing request/response headers/body */ |
| 57 | }; |
| 58 | |
| 59 | struct event_base; |
| 60 | |
| 61 | /* A client or server connection. */ |
| 62 | struct evhttp_connection { |
| 63 | /* we use this tailq only if this connection was created for an http |
| 64 | * server */ |
| 65 | TAILQ_ENTRY(evhttp_connection) next; |
| 66 | |
| 67 | evutil_socket_t fd; |
| 68 | struct bufferevent *bufev; |
| 69 | |
| 70 | struct event retry_ev; /* for retrying connects */ |
| 71 | |
| 72 | char *bind_address; /* address to use for binding the src */ |
| 73 | u_short bind_port; /* local port for binding the src */ |
| 74 | |
| 75 | char *address; /* address to connect to */ |
| 76 | u_short port; |
| 77 | |
| 78 | size_t max_headers_size; |
| 79 | ev_uint64_t max_body_size; |
| 80 | |
| 81 | int flags; |
| 82 | #define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */ |
| 83 | #define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */ |
| 84 | #define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */ |
| 85 | |
| 86 | int timeout; /* timeout in seconds for events */ |
| 87 | int retry_cnt; /* retry count */ |
| 88 | int retry_max; /* maximum number of retries */ |
| 89 | |
| 90 | enum evhttp_connection_state state; |
| 91 | |
| 92 | /* for server connections, the http server they are connected with */ |
| 93 | struct evhttp *http_server; |
| 94 | |
| 95 | TAILQ_HEAD(evcon_requestq, evhttp_request) requests; |
| 96 | |
| 97 | void (*cb)(struct evhttp_connection *, void *); |
| 98 | void *cb_arg; |
| 99 | |
| 100 | void (*closecb)(struct evhttp_connection *, void *); |
| 101 | void *closecb_arg; |
| 102 | |
| 103 | struct deferred_cb read_more_deferred_cb; |
| 104 | |
| 105 | struct event_base *base; |
| 106 | struct evdns_base *dns_base; |
| 107 | }; |
| 108 | |
| 109 | /* A callback for an http server */ |
| 110 | struct evhttp_cb { |
| 111 | TAILQ_ENTRY(evhttp_cb) next; |
| 112 | |
| 113 | char *what; |
| 114 | |
| 115 | void (*cb)(struct evhttp_request *req, void *); |
| 116 | void *cbarg; |
| 117 | }; |
| 118 | |
| 119 | /* both the http server as well as the rpc system need to queue connections */ |
| 120 | TAILQ_HEAD(evconq, evhttp_connection); |
| 121 | |
| 122 | /* each bound socket is stored in one of these */ |
| 123 | struct evhttp_bound_socket { |
| 124 | TAILQ_ENTRY(evhttp_bound_socket) next; |
| 125 | |
| 126 | struct evconnlistener *listener; |
| 127 | }; |
| 128 | |
| 129 | /* server alias list item. */ |
| 130 | struct evhttp_server_alias { |
| 131 | TAILQ_ENTRY(evhttp_server_alias) next; |
| 132 | |
| 133 | char *alias; /* the server alias. */ |
| 134 | }; |
| 135 | |
| 136 | struct evhttp { |
| 137 | /* Next vhost, if this is a vhost. */ |
| 138 | TAILQ_ENTRY(evhttp) next_vhost; |
| 139 | |
| 140 | /* All listeners for this host */ |
| 141 | TAILQ_HEAD(boundq, evhttp_bound_socket) sockets; |
| 142 | |
| 143 | TAILQ_HEAD(httpcbq, evhttp_cb) callbacks; |
| 144 | |
| 145 | /* All live connections on this host. */ |
| 146 | struct evconq connections; |
| 147 | |
| 148 | TAILQ_HEAD(vhostsq, evhttp) virtualhosts; |
| 149 | |
| 150 | TAILQ_HEAD(aliasq, evhttp_server_alias) aliases; |
| 151 | |
| 152 | /* NULL if this server is not a vhost */ |
| 153 | char *vhost_pattern; |
| 154 | |
| 155 | int timeout; |
| 156 | |
| 157 | size_t default_max_headers_size; |
| 158 | ev_uint64_t default_max_body_size; |
| 159 | |
| 160 | /* Bitmask of all HTTP methods that we accept and pass to user |
| 161 | * callbacks. */ |
| 162 | ev_uint16_t allowed_methods; |
| 163 | |
| 164 | /* Fallback callback if all the other callbacks for this connection |
| 165 | don't match. */ |
| 166 | void (*gencb)(struct evhttp_request *req, void *); |
| 167 | void *gencbarg; |
| 168 | |
| 169 | struct event_base *base; |
| 170 | }; |
| 171 | |
| 172 | /* XXX most of these functions could be static. */ |
| 173 | |
| 174 | /* resets the connection; can be reused for more requests */ |
| 175 | void evhttp_connection_reset(struct evhttp_connection *); |
| 176 | |
| 177 | /* connects if necessary */ |
| 178 | int evhttp_connection_connect(struct evhttp_connection *); |
| 179 | |
| 180 | /* notifies the current request that it failed; resets connection */ |
| 181 | void evhttp_connection_fail(struct evhttp_connection *, |
| 182 | enum evhttp_connection_error error); |
| 183 | |
| 184 | enum message_read_status; |
| 185 | |
| 186 | enum message_read_status evhttp_parse_firstline(struct evhttp_request *, struct evbuffer*); |
| 187 | enum message_read_status evhttp_parse_headers(struct evhttp_request *, struct evbuffer*); |
| 188 | |
| 189 | void evhttp_start_read(struct evhttp_connection *); |
| 190 | |
| 191 | /* response sending HTML the data in the buffer */ |
| 192 | void evhttp_response_code(struct evhttp_request *, int, const char *); |
| 193 | void evhttp_send_page(struct evhttp_request *, struct evbuffer *); |
| 194 | |
| 195 | #endif /* _HTTP_H */ |