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