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