ipc/chromium/src/third_party/libevent/test/bench_http.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial