2 * Copyright 2008-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 4. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/resource.h>
45 #include "event2/event.h"
46 #include "event2/buffer.h"
47 #include "event2/util.h"
48 #include "event2/http.h"
49 #include "event2/thread.h"
51 static void http_basic_cb(struct evhttp_request *req, void *arg);
54 static size_t content_len = 0;
57 http_basic_cb(struct evhttp_request *req, void *arg)
59 struct evbuffer *evb = evbuffer_new();
61 evbuffer_add(evb, content, content_len);
63 /* allow sending of an empty reply */
64 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
69 #if LIBEVENT_VERSION_NUMBER >= 0x02000200
71 http_ref_cb(struct evhttp_request *req, void *arg)
73 struct evbuffer *evb = evbuffer_new();
75 evbuffer_add_reference(evb, content, content_len, NULL, NULL);
77 /* allow sending of an empty reply */
78 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
85 main(int argc, char **argv)
87 struct event_config *cfg = event_config_new();
88 struct event_base *base;
93 unsigned short port = 8080;
98 WSAStartup(0x101, &WSAData);
100 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
104 for (i = 1; i < argc; ++i) {
110 if ((c == 'p' || c == 'l') && i + 1 >= argc) {
111 fprintf(stderr, "-%c requires argument.\n", c);
117 if (i+1 >= argc || !argv[i+1]) {
118 fprintf(stderr, "Missing port\n");
121 port = (int)strtol(argv[i+1], &endptr, 10);
122 if (*endptr != '\0') {
123 fprintf(stderr, "Bad port\n");
128 if (i+1 >= argc || !argv[i+1]) {
129 fprintf(stderr, "Missing content length\n");
132 content_len = (size_t)strtol(argv[i+1], &endptr, 10);
133 if (*endptr != '\0' || content_len == 0) {
134 fprintf(stderr, "Bad content length\n");
141 evthread_use_windows_threads();
142 event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
146 fprintf(stderr, "Illegal argument \"%c\"\n", c);
151 base = event_base_new_with_config(cfg);
153 fprintf(stderr, "creating event_base failed. Exiting.\n");
157 http = evhttp_new(base);
159 content = malloc(content_len);
160 if (content == NULL) {
161 fprintf(stderr, "Cannot allocate content\n");
165 for (i = 0; i < (int)content_len; ++i)
166 content[i] = (i & 255);
169 evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
170 fprintf(stderr, "/ind - basic content (memory copy)\n");
172 evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
173 fprintf(stderr, "/ref - basic content (reference)\n");
175 fprintf(stderr, "Serving %d bytes on port %d using %s\n",
176 (int)content_len, port,
177 use_iocp? "IOCP" : event_base_get_method(base));
179 evhttp_bind_socket(http, "0.0.0.0", port);
183 struct timeval tv={99999999,0};
184 event_base_loopexit(base, &tv);
187 event_base_dispatch(base);