1 /* $NetBSD: bench_http.c,v 1.1.1.2 2017/01/31 21:14:53 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 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 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 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 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 #ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 143 evthread_use_windows_threads(); 144 #endif 145 event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP); 146 break; 147 #endif 148 default: 149 fprintf(stderr, "Illegal argument \"%c\"\n", c); 150 exit(1); 151 } 152 } 153 154 base = event_base_new_with_config(cfg); 155 if (!base) { 156 fprintf(stderr, "creating event_base failed. Exiting.\n"); 157 return 1; 158 } 159 160 http = evhttp_new(base); 161 162 content = malloc(content_len); 163 if (content == NULL) { 164 fprintf(stderr, "Cannot allocate content\n"); 165 exit(1); 166 } else { 167 int i = 0; 168 for (i = 0; i < (int)content_len; ++i) 169 content[i] = (i & 255); 170 } 171 172 evhttp_set_cb(http, "/ind", http_basic_cb, NULL); 173 fprintf(stderr, "/ind - basic content (memory copy)\n"); 174 175 evhttp_set_cb(http, "/ref", http_ref_cb, NULL); 176 fprintf(stderr, "/ref - basic content (reference)\n"); 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 #ifdef _WIN32 185 if (use_iocp) { 186 struct timeval tv={99999999,0}; 187 event_base_loopexit(base, &tv); 188 } 189 #endif 190 event_base_dispatch(base); 191 192 #ifdef _WIN32 193 WSACleanup(); 194 #endif 195 196 /* NOTREACHED */ 197 return (0); 198 } 199