blob: 80377ee60583bbd5de31e38b39cf85e281d94317 [file] [log] [blame]
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001/*
2 * Copyright 2008-2012 Niels Provos and Nick Mathewson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
14 *
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.
25 *
26 */
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#ifdef _WIN32
31#include <winsock2.h>
32#else
33#include <sys/socket.h>
34#include <sys/resource.h>
35#include <sys/time.h>
36#include <unistd.h>
37#endif
38#include <fcntl.h>
39#include <signal.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <errno.h>
44
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"
50
51static void http_basic_cb(struct evhttp_request *req, void *arg);
52
53static char *content;
54static size_t content_len = 0;
55
56static void
57http_basic_cb(struct evhttp_request *req, void *arg)
58{
59 struct evbuffer *evb = evbuffer_new();
60
61 evbuffer_add(evb, content, content_len);
62
63 /* allow sending of an empty reply */
64 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
65
66 evbuffer_free(evb);
67}
68
69#if LIBEVENT_VERSION_NUMBER >= 0x02000200
70static void
71http_ref_cb(struct evhttp_request *req, void *arg)
72{
73 struct evbuffer *evb = evbuffer_new();
74
75 evbuffer_add_reference(evb, content, content_len, NULL, NULL);
76
77 /* allow sending of an empty reply */
78 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
79
80 evbuffer_free(evb);
81}
82#endif
83
84int
85main(int argc, char **argv)
86{
87 struct event_config *cfg = event_config_new();
88 struct event_base *base;
89 struct evhttp *http;
90 int i;
91 int c;
92 int use_iocp = 0;
93 ev_uint16_t port = 8080;
94 char *endptr = NULL;
95
96#ifdef _WIN32
97 WSADATA WSAData;
98 WSAStartup(0x101, &WSAData);
99#else
100 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
101 return (1);
102#endif
103
Haibo Huangb2279672019-05-31 16:12:39 -0700104 setbuf(stdout, NULL);
105 setbuf(stderr, NULL);
106
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100107 for (i = 1; i < argc; ++i) {
108 if (*argv[i] != '-')
109 continue;
110
111 c = argv[i][1];
112
113 if ((c == 'p' || c == 'l') && i + 1 >= argc) {
114 fprintf(stderr, "-%c requires argument.\n", c);
115 exit(1);
116 }
117
118 switch (c) {
119 case 'p':
120 if (i+1 >= argc || !argv[i+1]) {
121 fprintf(stderr, "Missing port\n");
122 exit(1);
123 }
124 port = (int)strtol(argv[i+1], &endptr, 10);
125 if (*endptr != '\0') {
126 fprintf(stderr, "Bad port\n");
127 exit(1);
128 }
129 break;
130 case 'l':
131 if (i+1 >= argc || !argv[i+1]) {
132 fprintf(stderr, "Missing content length\n");
133 exit(1);
134 }
135 content_len = (size_t)strtol(argv[i+1], &endptr, 10);
136 if (*endptr != '\0' || content_len == 0) {
137 fprintf(stderr, "Bad content length\n");
138 exit(1);
139 }
140 break;
141#ifdef _WIN32
142 case 'i':
143 use_iocp = 1;
144#ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED
145 evthread_use_windows_threads();
146#endif
147 event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
148 break;
149#endif
150 default:
151 fprintf(stderr, "Illegal argument \"%c\"\n", c);
152 exit(1);
153 }
154 }
155
156 base = event_base_new_with_config(cfg);
157 if (!base) {
158 fprintf(stderr, "creating event_base failed. Exiting.\n");
159 return 1;
160 }
161
162 http = evhttp_new(base);
163
164 content = malloc(content_len);
165 if (content == NULL) {
166 fprintf(stderr, "Cannot allocate content\n");
167 exit(1);
168 } else {
169 int i = 0;
170 for (i = 0; i < (int)content_len; ++i)
171 content[i] = (i & 255);
172 }
173
174 evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
175 fprintf(stderr, "/ind - basic content (memory copy)\n");
176
177 evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
178 fprintf(stderr, "/ref - basic content (reference)\n");
179
180 fprintf(stderr, "Serving %d bytes on port %d using %s\n",
181 (int)content_len, port,
182 use_iocp? "IOCP" : event_base_get_method(base));
183
184 evhttp_bind_socket(http, "0.0.0.0", port);
185
186#ifdef _WIN32
187 if (use_iocp) {
188 struct timeval tv={99999999,0};
189 event_base_loopexit(base, &tv);
190 }
191#endif
192 event_base_dispatch(base);
193
194#ifdef _WIN32
195 WSACleanup();
196#endif
197
198 /* NOTREACHED */
199 return (0);
200}