1 /*
2  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
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_H_
11 #define _HTTP_H_
12 
13 #define HTTP_CONNECT_TIMEOUT	45
14 #define HTTP_WRITE_TIMEOUT	50
15 #define HTTP_READ_TIMEOUT	50
16 
17 #define HTTP_PREFIX		"http://"
18 #define HTTP_DEFAULTPORT	80
19 
20 enum message_read_status {
21 	ALL_DATA_READ = 1,
22 	MORE_DATA_EXPECTED = 0,
23 	DATA_CORRUPTED = -1,
24 	REQUEST_CANCELED = -2
25 };
26 
27 enum evhttp_connection_error {
28 	EVCON_HTTP_TIMEOUT,
29 	EVCON_HTTP_EOF,
30 	EVCON_HTTP_INVALID_HEADER
31 };
32 
33 struct evbuffer;
34 struct evhttp_request;
35 
36 /* A stupid connection object - maybe make this a bufferevent later */
37 
38 enum evhttp_connection_state {
39 	EVCON_DISCONNECTED,	/**< not currently connected not trying either*/
40 	EVCON_CONNECTING,	/**< tries to currently connect */
41 	EVCON_IDLE,		/**< connection is established */
42 	EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
43 				 **< Status-Line (outgoing conn) */
44 	EVCON_READING_HEADERS,	/**< reading request/response headers */
45 	EVCON_READING_BODY,	/**< reading request/response body */
46 	EVCON_READING_TRAILER,	/**< reading request/response chunked trailer */
47 	EVCON_WRITING		/**< writing request/response headers/body */
48 };
49 
50 struct event_base;
51 
52 struct evhttp_connection {
53 	/* we use tailq only if they were created for an http server */
54 	TAILQ_ENTRY(evhttp_connection) (next);
55 
56 	int fd;
57 	struct event ev;
58 	struct event close_ev;
59 	struct evbuffer *input_buffer;
60 	struct evbuffer *output_buffer;
61 
62 	char *bind_address;		/* address to use for binding the src */
63 	u_short bind_port;		/* local port for binding the src */
64 
65 	char *address;			/* address to connect to */
66 	u_short port;
67 
68 	int flags;
69 #define EVHTTP_CON_INCOMING	0x0001	/* only one request on it ever */
70 #define EVHTTP_CON_OUTGOING	0x0002  /* multiple requests possible */
71 #define EVHTTP_CON_CLOSEDETECT  0x0004  /* detecting if persistent close */
72 
73 	int timeout;			/* timeout in seconds for events */
74 	int retry_cnt;			/* retry count */
75 	int retry_max;			/* maximum number of retries */
76 
77 	enum evhttp_connection_state state;
78 
79 	/* for server connections, the http server they are connected with */
80 	struct evhttp *http_server;
81 
82 	TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
83 
84 						   void (*cb)(struct evhttp_connection *, void *);
85 	void *cb_arg;
86 
87 	void (*closecb)(struct evhttp_connection *, void *);
88 	void *closecb_arg;
89 
90 	struct event_base *base;
91 };
92 
93 struct evhttp_cb {
94 	TAILQ_ENTRY(evhttp_cb) next;
95 
96 	char *what;
97 
98 	void (*cb)(struct evhttp_request *req, void *);
99 	void *cbarg;
100 };
101 
102 /* both the http server as well as the rpc system need to queue connections */
103 TAILQ_HEAD(evconq, evhttp_connection);
104 
105 /* each bound socket is stored in one of these */
106 struct evhttp_bound_socket {
107 	TAILQ_ENTRY(evhttp_bound_socket) (next);
108 
109 	struct event  bind_ev;
110 };
111 
112 struct evhttp {
113 	TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
114 
115 	TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
116         struct evconq connections;
117 
118         int timeout;
119 
120 	void (*gencb)(struct evhttp_request *req, void *);
121 	void *gencbarg;
122 
123 	struct event_base *base;
124 };
125 
126 /* resets the connection; can be reused for more requests */
127 void evhttp_connection_reset(struct evhttp_connection *);
128 
129 /* connects if necessary */
130 int evhttp_connection_connect(struct evhttp_connection *);
131 
132 /* notifies the current request that it failed; resets connection */
133 void evhttp_connection_fail(struct evhttp_connection *,
134     enum evhttp_connection_error error);
135 
136 void evhttp_get_request(struct evhttp *, int, struct sockaddr *, socklen_t);
137 
138 int evhttp_hostportfile(char *, char **, u_short *, char **);
139 
140 int evhttp_parse_firstline(struct evhttp_request *, struct evbuffer*);
141 int evhttp_parse_headers(struct evhttp_request *, struct evbuffer*);
142 
143 void evhttp_start_read(struct evhttp_connection *);
144 void evhttp_make_header(struct evhttp_connection *, struct evhttp_request *);
145 
146 void evhttp_write_buffer(struct evhttp_connection *,
147     void (*)(struct evhttp_connection *, void *), void *);
148 
149 /* response sending HTML the data in the buffer */
150 void evhttp_response_code(struct evhttp_request *, int, const char *);
151 void evhttp_send_page(struct evhttp_request *, struct evbuffer *);
152 
153 #endif /* _HTTP_H */
154