From 6813af3f11c4fdffa68efce687d46023aa195773 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Sat, 10 Jun 2006 22:37:21 +0000 Subject: [PATCH] move http related prototypes to evhttp.h svn:r214 --- Makefile.am | 4 +- event.h | 58 ------------------------ evhttp.h | 104 ++++++++++++++++++++++++++++++++++++++++++++ http.c | 3 +- test/regress.c | 1 - test/regress_http.c | 1 + 6 files changed, 109 insertions(+), 62 deletions(-) create mode 100644 evhttp.h diff --git a/Makefile.am b/Makefile.am index 14ec3f66..625fb5d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,11 +24,11 @@ EXTRA_DIST = acconfig.h event.h event-internal.h log.h evsignal.h event.3 \ lib_LTLIBRARIES = libevent.la libevent_la_SOURCES = event.c buffer.c evbuffer.c log.c event_tagging.c \ - http.c http-internal.h + http.c evhttp.h http-internal.h libevent_la_LIBADD = @LTLIBOBJS@ libevent_la_LDFLAGS = -release @VERSION@ -version-info 1:3:0 -include_HEADERS = event.h +include_HEADERS = event.h evhttp.h INCLUDES = -Icompat diff --git a/event.h b/event.h index 4c1b38f0..304c0a49 100644 --- a/event.h +++ b/event.h @@ -332,64 +332,6 @@ int evtag_unmarshal_string(struct evbuffer *evbuf, u_int8_t need_tag, int evtag_unmarshal_timeval(struct evbuffer *evbuf, u_int8_t need_tag, struct timeval *ptv); -/* - * Basic support for HTTP serving. - * - * As libevent is a library for dealing with event notification and most - * interesting applications are networked today, I have often found the - * need to write HTTP code. The following prototypes and definitions provide - * an application with a minimal interface for making HTTP requests and for - * creating a very simple HTTP server. - */ - -/* Response codes */ -#define HTTP_OK 200 -#define HTTP_MOVEPERM 301 -#define HTTP_MOVETEMP 302 -#define HTTP_NOTFOUND 404 - -struct evhttp; -struct evhttp_request; - -/* Start an HTTP server on the specified address and port */ -struct evhttp *evhttp_start(const char *address, u_short port); - -/* - * Free the previously create HTTP server. Works only if no requests are - * currently being served. - */ -void evhttp_free(struct evhttp* http); - -/* Set a callback for a specified URI */ -void evhttp_set_cb(struct evhttp *, const char *, - void (*)(struct evhttp_request *, void *), void *); - -/* Set a callback for all requests that are not caught by specific callbacks */ -void evhttp_set_gencb(struct evhttp *, - void (*)(struct evhttp_request *, void *), void *); - -void evhttp_send_error(struct evhttp_request *, int, const char *); -void evhttp_send_reply(struct evhttp_request *, int, const char *, - struct evbuffer *); - -/* Interfaces for making requests */ -enum evhttp_cmd_type { EVHTTP_REQ_GET, EVHTTP_REQ_POST, EVHTTP_REQ_HEAD }; - -struct evhttp_request *evhttp_request_new( - void (*cb)(struct evhttp_request *, void *), void *arg); -void evhttp_request_free(struct evhttp_request *req); -const char *evhttp_request_uri(struct evhttp_request *req); - -/* Interfaces for dealing with HTTP headers */ - -const char *evhttp_find_header(struct evkeyvalq *, const char *); -int evhttp_remove_header(struct evkeyvalq *, const char *); -int evhttp_add_header(struct evkeyvalq *, const char *, const char *); -void evhttp_clear_headers(struct evkeyvalq *); - -/* Miscellaneous utility functions */ -void evhttp_parse_query(const char *uri, struct evkeyvalq *); -char *evhttp_htmlescape(const char *html); #ifdef __cplusplus } #endif diff --git a/evhttp.h b/evhttp.h new file mode 100644 index 00000000..ac1889e9 --- /dev/null +++ b/evhttp.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2000-2004 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVHTTP_H_ +#define _EVHTTP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +#endif + +/* + * Basic support for HTTP serving. + * + * As libevent is a library for dealing with event notification and most + * interesting applications are networked today, I have often found the + * need to write HTTP code. The following prototypes and definitions provide + * an application with a minimal interface for making HTTP requests and for + * creating a very simple HTTP server. + */ + +/* Response codes */ +#define HTTP_OK 200 +#define HTTP_MOVEPERM 301 +#define HTTP_MOVETEMP 302 +#define HTTP_NOTFOUND 404 + +struct evhttp; +struct evhttp_request; +struct evkeyvalq; + +/* Start an HTTP server on the specified address and port */ +struct evhttp *evhttp_start(const char *address, u_short port); + +/* + * Free the previously create HTTP server. Works only if no requests are + * currently being served. + */ +void evhttp_free(struct evhttp* http); + +/* Set a callback for a specified URI */ +void evhttp_set_cb(struct evhttp *, const char *, + void (*)(struct evhttp_request *, void *), void *); + +/* Set a callback for all requests that are not caught by specific callbacks */ +void evhttp_set_gencb(struct evhttp *, + void (*)(struct evhttp_request *, void *), void *); + +void evhttp_send_error(struct evhttp_request *, int, const char *); +void evhttp_send_reply(struct evhttp_request *, int, const char *, + struct evbuffer *); + +/* Interfaces for making requests */ +enum evhttp_cmd_type { EVHTTP_REQ_GET, EVHTTP_REQ_POST, EVHTTP_REQ_HEAD }; + +struct evhttp_request *evhttp_request_new( + void (*cb)(struct evhttp_request *, void *), void *arg); +void evhttp_request_free(struct evhttp_request *req); +const char *evhttp_request_uri(struct evhttp_request *req); + +/* Interfaces for dealing with HTTP headers */ + +const char *evhttp_find_header(struct evkeyvalq *, const char *); +int evhttp_remove_header(struct evkeyvalq *, const char *); +int evhttp_add_header(struct evkeyvalq *, const char *, const char *); +void evhttp_clear_headers(struct evkeyvalq *); + +/* Miscellaneous utility functions */ +void evhttp_parse_query(const char *uri, struct evkeyvalq *); +char *evhttp_htmlescape(const char *html); +#ifdef __cplusplus +} +#endif + +#endif /* _EVHTTP_H_ */ diff --git a/http.c b/http.c index 61f3b70f..fa591121 100644 --- a/http.c +++ b/http.c @@ -66,6 +66,7 @@ #undef timeout_initialized #include "event.h" +#include "evhttp.h" #include "log.h" #include "http-internal.h" @@ -224,7 +225,7 @@ evhttp_make_header_request(struct evbuffer *buf, struct evhttp_request *req) if (req->type == EVHTTP_REQ_POST && evhttp_find_header(req->output_headers, "Content-Length") == NULL){ char size[12]; - snprintf(size, sizeof(size), "%d", + snprintf(size, sizeof(size), "%ld", EVBUFFER_LENGTH(req->buffer)); evhttp_add_header(req->output_headers, "Content-Length", size); } diff --git a/test/regress.c b/test/regress.c index 32f2f2f6..2480d425 100644 --- a/test/regress.c +++ b/test/regress.c @@ -54,7 +54,6 @@ #include "event.h" #include "log.h" -#include "http-internal.h" #include "regress.h" #include "regress.gen.h" diff --git a/test/regress_http.c b/test/regress_http.c index 8801bb21..3b4001f2 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -53,6 +53,7 @@ #include #include "event.h" +#include "evhttp.h" #include "log.h" #include "http-internal.h"