evhttp.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 #ifndef _EVHTTP_H_
00028 #define _EVHTTP_H_
00029 
00030 #include <event.h>
00031 
00032 #ifdef __cplusplus
00033 extern "C" {
00034 #endif
00035 
00036 #ifdef WIN32
00037 #define WIN32_LEAN_AND_MEAN
00038 #include <windows.h>
00039 #include <winsock2.h>
00040 #undef WIN32_LEAN_AND_MEAN
00041 #endif
00042 
00054 /* Response codes */
00055 #define HTTP_OK                 200
00056 #define HTTP_NOCONTENT          204
00057 #define HTTP_MOVEPERM           301
00058 #define HTTP_MOVETEMP           302
00059 #define HTTP_NOTMODIFIED        304
00060 #define HTTP_BADREQUEST         400
00061 #define HTTP_NOTFOUND           404
00062 #define HTTP_SERVUNAVAIL        503
00063 
00064 struct evhttp;
00065 struct evhttp_request;
00066 struct evkeyvalq;
00067 
00073 struct evhttp *evhttp_new(struct event_base *base);
00074 
00086 int evhttp_bind_socket(struct evhttp *http, const char *address, u_short port);
00087 
00096 void evhttp_free(struct evhttp* http);
00097 
00099 void evhttp_set_cb(struct evhttp *, const char *,
00100     void (*)(struct evhttp_request *, void *), void *);
00101 
00103 int evhttp_del_cb(struct evhttp *, const char *);
00104 
00107 void evhttp_set_gencb(struct evhttp *,
00108     void (*)(struct evhttp_request *, void *), void *);
00109 
00116 void evhttp_set_timeout(struct evhttp *, int timeout_in_secs);
00117 
00118 /* Request/Response functionality */
00119 
00127 void evhttp_send_error(struct evhttp_request *req, int error,
00128     const char *reason);
00129 
00138 void evhttp_send_reply(struct evhttp_request *req, int code,
00139     const char *reason, struct evbuffer *databuf);
00140 
00141 /* Low-level response interface, for streaming/chunked replies */
00142 void evhttp_send_reply_start(struct evhttp_request *, int, const char *);
00143 void evhttp_send_reply_chunk(struct evhttp_request *, struct evbuffer *);
00144 void evhttp_send_reply_end(struct evhttp_request *);
00145 
00154 struct evhttp *evhttp_start(const char *address, u_short port);
00155 
00156 /*
00157  * Interfaces for making requests
00158  */
00159 enum evhttp_cmd_type { EVHTTP_REQ_GET, EVHTTP_REQ_POST, EVHTTP_REQ_HEAD };
00160 
00161 enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
00162 
00168 struct evhttp_request {
00169 #if defined(TAILQ_ENTRY)
00170         TAILQ_ENTRY(evhttp_request) next;
00171 #else
00172 struct {
00173         struct evhttp_request *tqe_next;
00174         struct evhttp_request **tqe_prev;
00175 }       next;
00176 #endif
00177 
00178         /* the connection object that this request belongs to */
00179         struct evhttp_connection *evcon;
00180         int flags;
00181 #define EVHTTP_REQ_OWN_CONNECTION       0x0001
00182 #define EVHTTP_PROXY_REQUEST            0x0002
00183 
00184         struct evkeyvalq *input_headers;
00185         struct evkeyvalq *output_headers;
00186 
00187         /* address of the remote host and the port connection came from */
00188         char *remote_host;
00189         u_short remote_port;
00190 
00191         enum evhttp_request_kind kind;
00192         enum evhttp_cmd_type type;
00193 
00194         char *uri;                      /* uri after HTTP request was parsed */
00195 
00196         char major;                     /* HTTP Major number */
00197         char minor;                     /* HTTP Minor number */
00198 
00199         int got_firstline;
00200         int response_code;              /* HTTP Response code */
00201         char *response_code_line;       /* Readable response */
00202 
00203         struct evbuffer *input_buffer;  /* read data */
00204         int ntoread;
00205         int chunked;
00206 
00207         struct evbuffer *output_buffer; /* outgoing post or data */
00208 
00209         /* Callback */
00210         void (*cb)(struct evhttp_request *, void *);
00211         void *cb_arg;
00212 
00213         /*
00214          * Chunked data callback - call for each completed chunk if
00215          * specified.  If not specified, all the data is delivered via
00216          * the regular callback.
00217          */
00218         void (*chunk_cb)(struct evhttp_request *, void *);
00219 };
00220 
00226 struct evhttp_request *evhttp_request_new(
00227         void (*cb)(struct evhttp_request *, void *), void *arg);
00228 
00230 void evhttp_request_set_chunked_cb(struct evhttp_request *,
00231     void (*cb)(struct evhttp_request *, void *));
00232 
00234 void evhttp_request_free(struct evhttp_request *req);
00235 
00241 struct evhttp_connection *evhttp_connection_new(
00242         const char *address, unsigned short port);
00243 
00245 void evhttp_connection_free(struct evhttp_connection *evcon);
00246 
00248 void evhttp_connection_set_local_address(struct evhttp_connection *evcon,
00249     const char *address);
00250 
00252 void evhttp_connection_set_timeout(struct evhttp_connection *evcon,
00253     int timeout_in_secs);
00254 
00256 void evhttp_connection_set_retries(struct evhttp_connection *evcon,
00257     int retry_max);
00258 
00260 void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
00261     void (*)(struct evhttp_connection *, void *), void *);
00262 
00267 void evhttp_connection_set_base(struct evhttp_connection *evcon,
00268     struct event_base *base);
00269 
00271 void evhttp_connection_get_peer(struct evhttp_connection *evcon,
00272     char **address, u_short *port);
00273 
00275 int evhttp_make_request(struct evhttp_connection *evcon,
00276     struct evhttp_request *req,
00277     enum evhttp_cmd_type type, const char *uri);
00278 
00279 const char *evhttp_request_uri(struct evhttp_request *req);
00280 
00281 /* Interfaces for dealing with HTTP headers */
00282 
00283 const char *evhttp_find_header(const struct evkeyvalq *, const char *);
00284 int evhttp_remove_header(struct evkeyvalq *, const char *);
00285 int evhttp_add_header(struct evkeyvalq *, const char *, const char *);
00286 void evhttp_clear_headers(struct evkeyvalq *);
00287 
00288 /* Miscellaneous utility functions */
00289 
00290 
00299 char *evhttp_encode_uri(const char *uri);
00300 
00301 
00310 char *evhttp_decode_uri(const char *uri);
00311 
00312 
00318 void evhttp_parse_query(const char *uri, struct evkeyvalq *);
00319 
00320 
00332 char *evhttp_htmlescape(const char *html);
00333 
00334 #ifdef __cplusplus
00335 }
00336 #endif
00337 
00338 #endif /* _EVHTTP_H_ */

Generated on Mon Nov 12 08:47:55 2007 for libevent by  doxygen 1.5.3