mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-08 03:44:22 -04:00
I often need some very simple HTTP functionality, so this is a first stab
at integrating something really simple with HTTP. The interface is still evolving as I start messing with it. Not all the interfaces are properly exported yet. I am also trying to figure out how to intelligently hide the details about the different structures from users, so that that things can be changed around later. svn:r196
This commit is contained in:
parent
8af2db10d9
commit
a3bb4a035f
@ -23,7 +23,8 @@ 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
|
||||
libevent_la_SOURCES = event.c buffer.c evbuffer.c log.c event_tagging.c \
|
||||
http.c http.h
|
||||
libevent_la_LIBADD = @LTLIBOBJS@
|
||||
libevent_la_LDFLAGS = -release @VERSION@ -version-info 1:3:0
|
||||
|
||||
|
40
event.h
40
event.h
@ -270,7 +270,7 @@ int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
|
||||
void evbuffer_drain(struct evbuffer *, size_t);
|
||||
int evbuffer_write(struct evbuffer *, int);
|
||||
int evbuffer_read(struct evbuffer *, int, int);
|
||||
u_char *evbuffer_find(struct evbuffer *, u_char *, size_t);
|
||||
u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
|
||||
void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
|
||||
|
||||
/*
|
||||
@ -316,6 +316,44 @@ 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
|
||||
*/
|
||||
|
||||
/* 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);
|
||||
|
||||
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 };
|
||||
enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
|
||||
|
||||
struct evhttp_request *evhttp_request_new(
|
||||
void (*cb)(struct evhttp_request *, void *), void *arg);
|
||||
void evhttp_request_free(struct evhttp_request *req);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -58,7 +58,7 @@ class Struct:
|
||||
def PrintTags(self, file):
|
||||
"""Prints the tag definitions for a structure."""
|
||||
print >>file, '/* Tag definition for %s */' % self._name
|
||||
print >>file, 'enum {'
|
||||
print >>file, 'enum %s_ {' % self._name.lower()
|
||||
for entry in self._entries:
|
||||
print >>file, ' %s=%d,' % (self.EntryTagName(entry),
|
||||
entry.Tag())
|
||||
|
@ -13,7 +13,8 @@ test_init_SOURCES = test-init.c
|
||||
test_eof_SOURCES = test-eof.c
|
||||
test_weof_SOURCES = test-weof.c
|
||||
test_time_SOURCES = test-time.c
|
||||
regress_SOURCES = regress.c regress.gen.c regress.gen.h
|
||||
regress_SOURCES = regress.c regress.h regress_http.c \
|
||||
regress.gen.c regress.gen.h
|
||||
bench_SOURCES = bench.c
|
||||
|
||||
regress.gen.c regress.gen.h: regress.rpc
|
||||
|
@ -39,23 +39,28 @@
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <sys/queue.h>
|
||||
#ifndef WIN32
|
||||
#include <sys/socket.h>
|
||||
#include <sys/signal.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <netdb.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <event.h>
|
||||
#include "event.h"
|
||||
#include "log.h"
|
||||
#include "http.h"
|
||||
|
||||
#include "regress.h"
|
||||
#include "regress.gen.h"
|
||||
|
||||
static int pair[2];
|
||||
static int test_ok;
|
||||
int pair[2];
|
||||
int test_ok;
|
||||
static int called;
|
||||
static char wbuf[4096];
|
||||
static char rbuf[4096];
|
||||
@ -872,6 +877,8 @@ main (int argc, char **argv)
|
||||
/* Initalize the event library */
|
||||
event_base = event_init();
|
||||
|
||||
http_suite();
|
||||
|
||||
test_simpleread();
|
||||
|
||||
test_simplewrite();
|
||||
|
41
test/regress.h
Normal file
41
test/regress.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
|
||||
* 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 _REGRESS_H_
|
||||
#define _REGRESS_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void http_suite(void);
|
||||
void http_basic_test(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _REGRESS_H_ */
|
308
test/regress_http.c
Normal file
308
test/regress_http.c
Normal file
@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <sys/queue.h>
|
||||
#ifndef WIN32
|
||||
#include <sys/socket.h>
|
||||
#include <sys/signal.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <netdb.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "event.h"
|
||||
#include "log.h"
|
||||
#include "http.h"
|
||||
|
||||
extern int pair[];
|
||||
extern int test_ok;
|
||||
|
||||
static struct evhttp *http;
|
||||
|
||||
void http_basic_cb(struct evhttp_request *req, void *arg);
|
||||
|
||||
struct evhttp *
|
||||
http_setup(short *pport)
|
||||
{
|
||||
int i;
|
||||
struct evhttp *myhttp;
|
||||
short port = -1;
|
||||
|
||||
/* Try a few different ports */
|
||||
for (i = 0; i < 50; ++i) {
|
||||
myhttp = evhttp_start("127.0.0.1", 8080 + i);
|
||||
if (myhttp != NULL) {
|
||||
port = 8080 + i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (port == -1)
|
||||
event_errx(1, "Could not start web server");
|
||||
|
||||
evhttp_set_cb(myhttp, "/test", http_basic_cb, NULL);
|
||||
|
||||
*pport = port;
|
||||
return (myhttp);
|
||||
}
|
||||
|
||||
int
|
||||
http_connect(const char *address, u_short port)
|
||||
{
|
||||
/* Stupid code for connecting */
|
||||
struct addrinfo ai, *aitop;
|
||||
char strport[NI_MAXSERV];
|
||||
int fd;
|
||||
|
||||
memset(&ai, 0, sizeof (ai));
|
||||
ai.ai_family = AF_INET;
|
||||
ai.ai_socktype = SOCK_STREAM;
|
||||
snprintf(strport, sizeof (strport), "%d", port);
|
||||
if (getaddrinfo(address, strport, &ai, &aitop) != 0) {
|
||||
event_warn("getaddrinfo");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd == -1)
|
||||
event_err(1, "socket failed");
|
||||
|
||||
if (connect(fd, aitop->ai_addr, aitop->ai_addrlen) == -1)
|
||||
event_err(1, "connect failed");
|
||||
|
||||
freeaddrinfo(aitop);
|
||||
|
||||
return (fd);
|
||||
}
|
||||
|
||||
void
|
||||
http_readcb(struct bufferevent *bev, void *arg)
|
||||
{
|
||||
const char *what = "This is funny";
|
||||
|
||||
event_debug(("%s: %s\n", __func__, EVBUFFER_DATA(bev->input)));
|
||||
|
||||
if (evbuffer_find(bev->input, what, strlen(what)) != NULL) {
|
||||
struct evhttp_request *req = evhttp_request_new(NULL, NULL);
|
||||
req->kind = EVHTTP_RESPONSE;
|
||||
int done = evhttp_parse_lines(req, bev->input);
|
||||
|
||||
if (done == 1 &&
|
||||
evhttp_find_header(req->input_headers,
|
||||
"Content-Type") != NULL)
|
||||
test_ok++;
|
||||
evhttp_request_free(req);
|
||||
event_loopexit(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
http_writecb(struct bufferevent *bev, void *arg)
|
||||
{
|
||||
if (EVBUFFER_LENGTH(bev->output) == 0) {
|
||||
/* enable reading of the reply */
|
||||
bufferevent_enable(bev, EV_READ);
|
||||
test_ok++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
http_errorcb(struct bufferevent *bev, short what, void *arg)
|
||||
{
|
||||
test_ok = -2;
|
||||
event_loopexit(NULL);
|
||||
}
|
||||
|
||||
void
|
||||
http_basic_cb(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
event_debug((stderr, "%s: called\n", __func__));
|
||||
|
||||
struct evbuffer *evb = evbuffer_new();
|
||||
evbuffer_add_printf(evb, "This is funny");
|
||||
|
||||
evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
|
||||
|
||||
evbuffer_free(evb);
|
||||
}
|
||||
|
||||
void
|
||||
http_basic_test(void)
|
||||
{
|
||||
struct bufferevent *bev;
|
||||
int fd;
|
||||
char *http_request;
|
||||
short port = -1;
|
||||
|
||||
test_ok = 0;
|
||||
fprintf(stdout, "Testing Basic HTTP Server: ");
|
||||
|
||||
http = http_setup(&port);
|
||||
|
||||
fd = http_connect("127.0.0.1", port);
|
||||
|
||||
/* Stupid thing to send a request */
|
||||
bev = bufferevent_new(fd, http_readcb, http_writecb,
|
||||
http_errorcb, NULL);
|
||||
|
||||
http_request =
|
||||
"GET /test HTTP/1.1\r\n"
|
||||
"Host: somehost \r\n"
|
||||
"\r\n";
|
||||
|
||||
bufferevent_write(bev, http_request, strlen(http_request));
|
||||
|
||||
event_dispatch();
|
||||
|
||||
bufferevent_free(bev);
|
||||
close(fd);
|
||||
|
||||
evhttp_free(http);
|
||||
|
||||
if (test_ok != 2) {
|
||||
fprintf(stdout, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stdout, "OK\n");
|
||||
}
|
||||
|
||||
void http_connectcb(struct evhttp_connection *evcon, void *arg);
|
||||
|
||||
void
|
||||
http_connection_test(void)
|
||||
{
|
||||
short port = -1;
|
||||
struct evhttp_connection *evcon = NULL;
|
||||
|
||||
test_ok = 0;
|
||||
fprintf(stdout, "Testing Basic HTTP Connection: ");
|
||||
|
||||
http = http_setup(&port);
|
||||
|
||||
evcon = evhttp_connect("127.0.0.1", port, http_connectcb, NULL);
|
||||
if (evcon == NULL) {
|
||||
fprintf(stdout, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
event_dispatch();
|
||||
|
||||
/*
|
||||
* At this point, we want to schedule a request to the HTTP
|
||||
* server using our start request method.
|
||||
*/
|
||||
|
||||
evhttp_connection_free(evcon);
|
||||
evhttp_free(http);
|
||||
|
||||
if (test_ok != 1) {
|
||||
fprintf(stdout, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stdout, "OK\n");
|
||||
}
|
||||
|
||||
void http_request_done(struct evhttp_request *, void *);
|
||||
|
||||
void
|
||||
http_connectcb(struct evhttp_connection *evcon, void *arg)
|
||||
{
|
||||
struct evhttp_request *req = NULL;
|
||||
|
||||
if (evcon == NULL) {
|
||||
fprintf(stdout, "FAILED\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
req = evhttp_request_new(http_request_done, NULL);
|
||||
|
||||
/* Add the information that we care about */
|
||||
evhttp_add_header(req->output_headers, "Host", "somehost");
|
||||
|
||||
if (evhttp_start_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
|
||||
fprintf(stdout, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
http_request_done(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
const char *what = "This is funny";
|
||||
|
||||
if (req->response_code != HTTP_OK) {
|
||||
|
||||
fprintf(stderr, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (evhttp_find_header(req->input_headers,
|
||||
"Content-Type") == NULL) {
|
||||
fprintf(stderr, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (EVBUFFER_LENGTH(req->buffer) != strlen(what)) {
|
||||
fprintf(stderr, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (memcmp(EVBUFFER_DATA(req->buffer), what, strlen(what)) != 0) {
|
||||
fprintf(stderr, "FAILED\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
test_ok = 1;
|
||||
event_loopexit(NULL);
|
||||
}
|
||||
|
||||
void
|
||||
http_suite(void)
|
||||
{
|
||||
http_basic_test();
|
||||
http_connection_test();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user