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