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