This source file includes following definitions.
- http_basic_cb
- http_ref_cb
- main
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 #include <sys/types.h>
  29 #include <sys/stat.h>
  30 #ifdef WIN32
  31 #include <winsock2.h>
  32 #else
  33 #include <sys/socket.h>
  34 #include <sys/resource.h>
  35 #include <sys/time.h>
  36 #include <unistd.h>
  37 #endif
  38 #include <fcntl.h>
  39 #include <signal.h>
  40 #include <stdlib.h>
  41 #include <stdio.h>
  42 #include <string.h>
  43 #include <errno.h>
  44 
  45 #include "event2/event.h"
  46 #include "event2/buffer.h"
  47 #include "event2/util.h"
  48 #include "event2/http.h"
  49 #include "event2/thread.h"
  50 
  51 static void http_basic_cb(struct evhttp_request *req, void *arg);
  52 
  53 static char *content;
  54 static size_t content_len = 0;
  55 
  56 static void
  57 http_basic_cb(struct evhttp_request *req, void *arg)
  58 {
  59         struct evbuffer *evb = evbuffer_new();
  60 
  61         evbuffer_add(evb, content, content_len);
  62 
  63         
  64         evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
  65 
  66         evbuffer_free(evb);
  67 }
  68 
  69 #if LIBEVENT_VERSION_NUMBER >= 0x02000200
  70 static void
  71 http_ref_cb(struct evhttp_request *req, void *arg)
  72 {
  73         struct evbuffer *evb = evbuffer_new();
  74 
  75         evbuffer_add_reference(evb, content, content_len, NULL, NULL);
  76 
  77         
  78         evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
  79 
  80         evbuffer_free(evb);
  81 }
  82 #endif
  83 
  84 int
  85 main(int argc, char **argv)
  86 {
  87         struct event_config *cfg = event_config_new();
  88         struct event_base *base;
  89         struct evhttp *http;
  90         int i;
  91         int c;
  92         int use_iocp = 0;
  93         unsigned short port = 8080;
  94         char *endptr = NULL;
  95 
  96 #ifdef WIN32
  97         WSADATA WSAData;
  98         WSAStartup(0x101, &WSAData);
  99 #else
 100         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
 101                 return (1);
 102 #endif
 103 
 104         for (i = 1; i < argc; ++i) {
 105                 if (*argv[i] != '-')
 106                         continue;
 107 
 108                 c = argv[i][1];
 109 
 110                 if ((c == 'p' || c == 'l') && i + 1 >= argc) {
 111                         fprintf(stderr, "-%c requires argument.\n", c);
 112                         exit(1);
 113                 }
 114 
 115                 switch (c) {
 116                 case 'p':
 117                         if (i+1 >= argc || !argv[i+1]) {
 118                                 fprintf(stderr, "Missing port\n");
 119                                 exit(1);
 120                         }
 121                         port = (int)strtol(argv[i+1], &endptr, 10);
 122                         if (*endptr != '\0') {
 123                                 fprintf(stderr, "Bad port\n");
 124                                 exit(1);
 125                         }
 126                         break;
 127                 case 'l':
 128                         if (i+1 >= argc || !argv[i+1]) {
 129                                 fprintf(stderr, "Missing content length\n");
 130                                 exit(1);
 131                         }
 132                         content_len = (size_t)strtol(argv[i+1], &endptr, 10);
 133                         if (*endptr != '\0' || content_len == 0) {
 134                                 fprintf(stderr, "Bad content length\n");
 135                                 exit(1);
 136                         }
 137                         break;
 138 #ifdef WIN32
 139                 case 'i':
 140                         use_iocp = 1;
 141                         evthread_use_windows_threads();
 142                         event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
 143                         break;
 144 #endif
 145                 default:
 146                         fprintf(stderr, "Illegal argument \"%c\"\n", c);
 147                         exit(1);
 148                 }
 149         }
 150 
 151         base = event_base_new_with_config(cfg);
 152         if (!base) {
 153                 fprintf(stderr, "creating event_base failed. Exiting.\n");
 154                 return 1;
 155         }
 156 
 157         http = evhttp_new(base);
 158 
 159         content = malloc(content_len);
 160         if (content == NULL) {
 161                 fprintf(stderr, "Cannot allocate content\n");
 162                 exit(1);
 163         } else {
 164                 int i = 0;
 165                 for (i = 0; i < (int)content_len; ++i)
 166                         content[i] = (i & 255);
 167         }
 168 
 169         evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
 170         fprintf(stderr, "/ind - basic content (memory copy)\n");
 171 
 172 #ifdef _EVENT2_EVENT_H_
 173         evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
 174         fprintf(stderr, "/ref - basic content (reference)\n");
 175 #endif
 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         if (use_iocp) {
 184                 struct timeval tv={99999999,0};
 185                 event_base_loopexit(base, &tv);
 186         }
 187         event_base_dispatch(base);
 188 
 189         
 190         return (0);
 191 }