xref: /minix3/external/bsd/libevent/dist/test/bench_http.c (revision e985b929927b5932e3b68f4b50587d458900107a)
1 /*	$NetBSD: bench_http.c,v 1.1.1.1 2013/04/11 16:43:33 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 	unsigned short 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 	for (i = 1; i < argc; ++i) {
106 		if (*argv[i] != '-')
107 			continue;
108 
109 		c = argv[i][1];
110 
111 		if ((c == 'p' || c == 'l') && i + 1 >= argc) {
112 			fprintf(stderr, "-%c requires argument.\n", c);
113 			exit(1);
114 		}
115 
116 		switch (c) {
117 		case 'p':
118 			if (i+1 >= argc || !argv[i+1]) {
119 				fprintf(stderr, "Missing port\n");
120 				exit(1);
121 			}
122 			port = (int)strtol(argv[i+1], &endptr, 10);
123 			if (*endptr != '\0') {
124 				fprintf(stderr, "Bad port\n");
125 				exit(1);
126 			}
127 			break;
128 		case 'l':
129 			if (i+1 >= argc || !argv[i+1]) {
130 				fprintf(stderr, "Missing content length\n");
131 				exit(1);
132 			}
133 			content_len = (size_t)strtol(argv[i+1], &endptr, 10);
134 			if (*endptr != '\0' || content_len == 0) {
135 				fprintf(stderr, "Bad content length\n");
136 				exit(1);
137 			}
138 			break;
139 #ifdef WIN32
140 		case 'i':
141 			use_iocp = 1;
142 			evthread_use_windows_threads();
143 			event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
144 			break;
145 #endif
146 		default:
147 			fprintf(stderr, "Illegal argument \"%c\"\n", c);
148 			exit(1);
149 		}
150 	}
151 
152 	base = event_base_new_with_config(cfg);
153 	if (!base) {
154 		fprintf(stderr, "creating event_base failed. Exiting.\n");
155 		return 1;
156 	}
157 
158 	http = evhttp_new(base);
159 
160 	content = malloc(content_len);
161 	if (content == NULL) {
162 		fprintf(stderr, "Cannot allocate content\n");
163 		exit(1);
164 	} else {
165 		int i = 0;
166 		for (i = 0; i < (int)content_len; ++i)
167 			content[i] = (i & 255);
168 	}
169 
170 	evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
171 	fprintf(stderr, "/ind - basic content (memory copy)\n");
172 
173 #ifdef _EVENT2_EVENT_H_
174 	evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
175 	fprintf(stderr, "/ref - basic content (reference)\n");
176 #endif
177 
178 	fprintf(stderr, "Serving %d bytes on port %d using %s\n",
179 	    (int)content_len, port,
180 	    use_iocp? "IOCP" : event_base_get_method(base));
181 
182 	evhttp_bind_socket(http, "0.0.0.0", port);
183 
184 	if (use_iocp) {
185 		struct timeval tv={99999999,0};
186 		event_base_loopexit(base, &tv);
187 	}
188 	event_base_dispatch(base);
189 
190 	/* NOTREACHED */
191 	return (0);
192 }
193