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