1 /* $NetBSD: bench_http.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $ */
2 /*
3 * Copyright 2008-2012 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #ifdef _WIN32
32 #include <winsock2.h>
33 #else
34 #include <sys/socket.h>
35 #include <sys/resource.h>
36 #include <sys/time.h>
37 #include <unistd.h>
38 #endif
39 #include <fcntl.h>
40 #include <signal.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45
46 #include "event2/event.h"
47 #include "event2/buffer.h"
48 #include "event2/util.h"
49 #include "event2/http.h"
50 #include "event2/thread.h"
51
52 static void http_basic_cb(struct evhttp_request *req, void *arg);
53
54 static char *content;
55 static size_t content_len = 0;
56
57 static void
http_basic_cb(struct evhttp_request * req,void * arg)58 http_basic_cb(struct evhttp_request *req, void *arg)
59 {
60 struct evbuffer *evb = evbuffer_new();
61
62 evbuffer_add(evb, content, content_len);
63
64 /* allow sending of an empty reply */
65 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
66
67 evbuffer_free(evb);
68 }
69
70 #if LIBEVENT_VERSION_NUMBER >= 0x02000200
71 static void
http_ref_cb(struct evhttp_request * req,void * arg)72 http_ref_cb(struct evhttp_request *req, void *arg)
73 {
74 struct evbuffer *evb = evbuffer_new();
75
76 evbuffer_add_reference(evb, content, content_len, NULL, NULL);
77
78 /* allow sending of an empty reply */
79 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
80
81 evbuffer_free(evb);
82 }
83 #endif
84
85 int
main(int argc,char ** argv)86 main(int argc, char **argv)
87 {
88 struct event_config *cfg = event_config_new();
89 struct event_base *base;
90 struct evhttp *http;
91 int i;
92 int c;
93 int use_iocp = 0;
94 ev_uint16_t port = 8080;
95 char *endptr = NULL;
96
97 #ifdef _WIN32
98 WSADATA WSAData;
99 WSAStartup(0x101, &WSAData);
100 #else
101 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
102 return (1);
103 #endif
104
105 setbuf(stdout, NULL);
106 setbuf(stderr, NULL);
107
108 for (i = 1; i < argc; ++i) {
109 if (*argv[i] != '-')
110 continue;
111
112 c = argv[i][1];
113
114 if ((c == 'p' || c == 'l') && i + 1 >= argc) {
115 fprintf(stderr, "-%c requires argument.\n", c);
116 exit(1);
117 }
118
119 switch (c) {
120 case 'p':
121 if (i+1 >= argc || !argv[i+1]) {
122 fprintf(stderr, "Missing port\n");
123 exit(1);
124 }
125 port = (int)strtol(argv[i+1], &endptr, 10);
126 if (*endptr != '\0') {
127 fprintf(stderr, "Bad port\n");
128 exit(1);
129 }
130 break;
131 case 'l':
132 if (i+1 >= argc || !argv[i+1]) {
133 fprintf(stderr, "Missing content length\n");
134 exit(1);
135 }
136 content_len = (size_t)strtol(argv[i+1], &endptr, 10);
137 if (*endptr != '\0' || content_len == 0) {
138 fprintf(stderr, "Bad content length\n");
139 exit(1);
140 }
141 break;
142 #ifdef _WIN32
143 case 'i':
144 use_iocp = 1;
145 #ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED
146 evthread_use_windows_threads();
147 #endif
148 event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
149 break;
150 #endif
151 default:
152 fprintf(stderr, "Illegal argument \"%c\"\n", c);
153 exit(1);
154 }
155 }
156
157 base = event_base_new_with_config(cfg);
158 if (!base) {
159 fprintf(stderr, "creating event_base failed. Exiting.\n");
160 return 1;
161 }
162
163 http = evhttp_new(base);
164
165 content = malloc(content_len);
166 if (content == NULL) {
167 fprintf(stderr, "Cannot allocate content\n");
168 exit(1);
169 } else {
170 int i = 0;
171 for (i = 0; i < (int)content_len; ++i)
172 content[i] = (i & 255);
173 }
174
175 evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
176 fprintf(stderr, "/ind - basic content (memory copy)\n");
177
178 evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
179 fprintf(stderr, "/ref - basic content (reference)\n");
180
181 fprintf(stderr, "Serving %d bytes on port %d using %s\n",
182 (int)content_len, port,
183 use_iocp? "IOCP" : event_base_get_method(base));
184
185 evhttp_bind_socket(http, "0.0.0.0", port);
186
187 #ifdef _WIN32
188 if (use_iocp) {
189 struct timeval tv={99999999,0};
190 event_base_loopexit(base, &tv);
191 }
192 #endif
193 event_base_dispatch(base);
194
195 #ifdef _WIN32
196 WSACleanup();
197 #endif
198
199 /* NOTREACHED */
200 return (0);
201 }
202