]> granicus.if.org Git - apache/blob - support/ab.c
Support compilation against libssl built with OPENSSL_NO_SSL3,
[apache] / support / ab.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18    ** This program is based on ZeusBench V1.0 written by Adam Twiss
19    ** which is Copyright (c) 1996 by Zeus Technology Ltd. http://www.zeustech.net/
20    **
21    ** This software is provided "as is" and any express or implied waranties,
22    ** including but not limited to, the implied warranties of merchantability and
23    ** fitness for a particular purpose are disclaimed.  In no event shall
24    ** Zeus Technology Ltd. be liable for any direct, indirect, incidental, special,
25    ** exemplary, or consequential damaged (including, but not limited to,
26    ** procurement of substitute good or services; loss of use, data, or profits;
27    ** or business interruption) however caused and on theory of liability.  Whether
28    ** in contract, strict liability or tort (including negligence or otherwise)
29    ** arising in any way out of the use of this software, even if advised of the
30    ** possibility of such damage.
31    **
32  */
33
34 /*
35    ** HISTORY:
36    **    - Originally written by Adam Twiss <adam@zeus.co.uk>, March 1996
37    **      with input from Mike Belshe <mbelshe@netscape.com> and
38    **      Michael Campanella <campanella@stevms.enet.dec.com>
39    **    - Enhanced by Dean Gaudet <dgaudet@apache.org>, November 1997
40    **    - Cleaned up by Ralf S. Engelschall <rse@apache.org>, March 1998
41    **    - POST and verbosity by Kurt Sussman <kls@merlot.com>, August 1998
42    **    - HTML table output added by David N. Welton <davidw@prosa.it>, January 1999
43    **    - Added Cookie, Arbitrary header and auth support. <dirkx@webweaving.org>, April 1999
44    ** Version 1.3d
45    **    - Increased version number - as some of the socket/error handling has
46    **      fundamentally changed - and will give fundamentally different results
47    **      in situations where a server is dropping requests. Therefore you can
48    **      no longer compare results of AB as easily. Hence the inc of the version.
49    **      They should be closer to the truth though. Sander & <dirkx@covalent.net>, End 2000.
50    **    - Fixed proxy functionality, added median/mean statistics, added gnuplot
51    **      output option, added _experimental/rudimentary_ SSL support. Added
52    **      confidence guestimators and warnings. Sander & <dirkx@covalent.net>, End 2000
53    **    - Fixed serious int overflow issues which would cause realistic (longer
54    **      than a few minutes) run's to have wrong (but believable) results. Added
55    **      trapping of connection errors which influenced measurements.
56    **      Contributed by Sander Temme, Early 2001
57    ** Version 1.3e
58    **    - Changed timeout behavour during write to work whilst the sockets
59    **      are filling up and apr_write() does writes a few - but not all.
60    **      This will potentially change results. <dirkx@webweaving.org>, April 2001
61    ** Version 2.0.36-dev
62    **    Improvements to concurrent processing:
63    **      - Enabled non-blocking connect()s.
64    **      - Prevent blocking calls to apr_socket_recv() (thereby allowing AB to
65    **        manage its entire set of socket descriptors).
66    **      - Any error returned from apr_socket_recv() that is not EAGAIN or EOF
67    **        is now treated as fatal.
68    **      Contributed by Aaron Bannert, April 24, 2002
69    **
70    ** Version 2.0.36-2
71    **     Internalized the version string - this string is part
72    **     of the Agent: header and the result output.
73    **
74    ** Version 2.0.37-dev
75    **     Adopted SSL code by Madhu Mathihalli <madhusudan_mathihalli@hp.com>
76    **     [PATCH] ab with SSL support  Posted Wed, 15 Aug 2001 20:55:06 GMT
77    **     Introduces four 'if (int == value)' tests per non-ssl request.
78    **
79    ** Version 2.0.40-dev
80    **     Switched to the new abstract pollset API, allowing ab to
81    **     take advantage of future apr_pollset_t scalability improvements.
82    **     Contributed by Brian Pane, August 31, 2002
83    **
84    ** Version 2.3
85    **     SIGINT now triggers output_results().
86    **     Contributed by colm, March 30, 2006
87    **/
88
89 /* Note: this version string should start with \d+[\d\.]* and be a valid
90  * string for an HTTP Agent: header when prefixed with 'ApacheBench/'.
91  * It should reflect the version of AB - and not that of the apache server
92  * it happens to accompany. And it should be updated or changed whenever
93  * the results are no longer fundamentally comparable to the results of
94  * a previous version of ab. Either due to a change in the logic of
95  * ab - or to due to a change in the distribution it is compiled with
96  * (such as an APR change in for example blocking).
97  */
98 #define AP_AB_BASEREVISION "2.3"
99
100 /*
101  * BUGS:
102  *
103  * - uses strcpy/etc.
104  * - has various other poor buffer attacks related to the lazy parsing of
105  *   response headers from the server
106  * - doesn't implement much of HTTP/1.x, only accepts certain forms of
107  *   responses
108  * - (performance problem) heavy use of strstr shows up top in profile
109  *   only an issue for loopback usage
110  */
111
112 /*  -------------------------------------------------------------------- */
113
114 #if 'A' != 0x41
115 /* Hmmm... This source code isn't being compiled in ASCII.
116  * In order for data that flows over the network to make
117  * sense, we need to translate to/from ASCII.
118  */
119 #define NOT_ASCII
120 #endif
121
122 /* affects include files on Solaris */
123 #define BSD_COMP
124
125 #include "apr.h"
126 #include "apr_signal.h"
127 #include "apr_strings.h"
128 #include "apr_network_io.h"
129 #include "apr_file_io.h"
130 #include "apr_time.h"
131 #include "apr_getopt.h"
132 #include "apr_general.h"
133 #include "apr_lib.h"
134 #include "apr_portable.h"
135 #include "ap_release.h"
136 #include "apr_poll.h"
137
138 #define APR_WANT_STRFUNC
139 #include "apr_want.h"
140
141 #include "apr_base64.h"
142 #ifdef NOT_ASCII
143 #include "apr_xlate.h"
144 #endif
145 #if APR_HAVE_STDIO_H
146 #include <stdio.h>
147 #endif
148 #if APR_HAVE_STDLIB_H
149 #include <stdlib.h>
150 #endif
151 #if APR_HAVE_UNISTD_H
152 #include <unistd.h> /* for getpid() */
153 #endif
154
155 #if !defined(WIN32) && !defined(NETWARE)
156 #include "ap_config_auto.h"
157 #endif
158
159 #if defined(HAVE_OPENSSL)
160
161 #include <openssl/rsa.h>
162 #include <openssl/crypto.h>
163 #include <openssl/x509.h>
164 #include <openssl/pem.h>
165 #include <openssl/err.h>
166 #include <openssl/ssl.h>
167 #include <openssl/rand.h>
168 #define USE_SSL
169 #define SK_NUM(x) sk_X509_num(x)
170 #define SK_VALUE(x,y) sk_X509_value(x,y)
171 typedef STACK_OF(X509) X509_STACK_TYPE;
172
173 #endif
174
175 #if defined(USE_SSL)
176 #if (OPENSSL_VERSION_NUMBER >= 0x00909000)
177 #define AB_SSL_METHOD_CONST const
178 #else
179 #define AB_SSL_METHOD_CONST
180 #endif
181 #if (OPENSSL_VERSION_NUMBER >= 0x0090707f)
182 #define AB_SSL_CIPHER_CONST const
183 #else
184 #define AB_SSL_CIPHER_CONST
185 #endif
186 #ifdef SSL_OP_NO_TLSv1_2
187 #define HAVE_TLSV1_X
188 #endif
189 #endif
190
191 #include <math.h>
192 #if APR_HAVE_CTYPE_H
193 #include <ctype.h>
194 #endif
195 #if APR_HAVE_LIMITS_H
196 #include <limits.h>
197 #endif
198
199 /* ------------------- DEFINITIONS -------------------------- */
200
201 #ifndef LLONG_MAX
202 #define AB_MAX APR_INT64_C(0x7fffffffffffffff)
203 #else
204 #define AB_MAX LLONG_MAX
205 #endif
206
207 /* maximum number of requests on a time limited test */
208 #define MAX_REQUESTS (INT_MAX > 50000 ? 50000 : INT_MAX)
209
210 /* connection state
211  * don't add enums or rearrange or otherwise change values without
212  * visiting set_conn_state()
213  */
214 typedef enum {
215     STATE_UNCONNECTED = 0,
216     STATE_CONNECTING,           /* TCP connect initiated, but we don't
217                                  * know if it worked yet
218                                  */
219     STATE_CONNECTED,            /* we know TCP connect completed */
220     STATE_READ
221 } connect_state_e;
222
223 #define CBUFFSIZE (8192)
224
225 struct connection {
226     apr_pool_t *ctx;
227     apr_socket_t *aprsock;
228     apr_pollfd_t pollfd;
229     int state;
230     apr_size_t read;            /* amount of bytes read */
231     apr_size_t bread;           /* amount of body read */
232     apr_size_t rwrite, rwrote;  /* keep pointers in what we write - across
233                                  * EAGAINs */
234     apr_size_t length;          /* Content-Length value used for keep-alive */
235     char cbuff[CBUFFSIZE];      /* a buffer to store server response header */
236     int cbx;                    /* offset in cbuffer */
237     int keepalive;              /* non-zero if a keep-alive request */
238     int gotheader;              /* non-zero if we have the entire header in
239                                  * cbuff */
240     apr_time_t start,           /* Start of connection */
241                connect,         /* Connected, start writing */
242                endwrite,        /* Request written */
243                beginread,       /* First byte of input */
244                done;            /* Connection closed */
245
246     int socknum;
247 #ifdef USE_SSL
248     SSL *ssl;
249 #endif
250 };
251
252 struct data {
253     apr_time_t starttime;         /* start time of connection */
254     apr_interval_time_t waittime; /* between request and reading response */
255     apr_interval_time_t ctime;    /* time to connect */
256     apr_interval_time_t time;     /* time for connection */
257 };
258
259 #define ap_min(a,b) (((a)<(b))?(a):(b))
260 #define ap_max(a,b) (((a)>(b))?(a):(b))
261 #define ap_round_ms(a) ((apr_time_t)((a) + 500)/1000)
262 #define ap_double_ms(a) ((double)(a)/1000.0)
263 #define MAX_CONCURRENCY 20000
264
265 /* --------------------- GLOBALS ---------------------------- */
266
267 int verbosity = 0;      /* no verbosity by default */
268 int recverrok = 0;      /* ok to proceed after socket receive errors */
269 enum {NO_METH = 0, GET, HEAD, PUT, POST, CUSTOM_METHOD} method = NO_METH;
270 const char *method_str[] = {"bug", "GET", "HEAD", "PUT", "POST", ""};
271 int send_body = 0;      /* non-zero if sending body with request */
272 int requests = 1;       /* Number of requests to make */
273 int heartbeatres = 100; /* How often do we say we're alive */
274 int concurrency = 1;    /* Number of multiple requests to make */
275 int percentile = 1;     /* Show percentile served */
276 int nolength = 0;       /* Accept variable document length */
277 int confidence = 1;     /* Show confidence estimator and warnings */
278 int tlimit = 0;         /* time limit in secs */
279 int keepalive = 0;      /* try and do keepalive connections */
280 int windowsize = 0;     /* we use the OS default window size */
281 char servername[1024];  /* name that server reports */
282 char *hostname;         /* host name from URL */
283 const char *host_field;       /* value of "Host:" header field */
284 const char *path;             /* path name */
285 char *postdata;         /* *buffer containing data from postfile */
286 apr_size_t postlen = 0; /* length of data to be POSTed */
287 char *content_type = NULL;     /* content type to put in POST header */
288 const char *cookie,           /* optional cookie line */
289            *auth,             /* optional (basic/uuencoded) auhentication */
290            *hdrs;             /* optional arbitrary headers */
291 apr_port_t port;        /* port number */
292 char *proxyhost = NULL; /* proxy host name */
293 int proxyport = 0;      /* proxy port */
294 const char *connecthost;
295 const char *myhost;
296 apr_port_t connectport;
297 const char *gnuplot;          /* GNUplot file */
298 const char *csvperc;          /* CSV Percentile file */
299 const char *fullurl;
300 const char *colonhost;
301 int isproxy = 0;
302 apr_interval_time_t aprtimeout = apr_time_from_sec(30); /* timeout value */
303
304 /* overrides for ab-generated common headers */
305 int opt_host = 0;       /* was an optional "Host:" header specified? */
306 int opt_useragent = 0;  /* was an optional "User-Agent:" header specified? */
307 int opt_accept = 0;     /* was an optional "Accept:" header specified? */
308  /*
309   * XXX - this is now a per read/write transact type of value
310   */
311
312 int use_html = 0;       /* use html in the report */
313 const char *tablestring;
314 const char *trstring;
315 const char *tdstring;
316
317 apr_size_t doclen = 0;     /* the length the document should be */
318 apr_int64_t totalread = 0;    /* total number of bytes read */
319 apr_int64_t totalbread = 0;   /* totoal amount of entity body read */
320 apr_int64_t totalposted = 0;  /* total number of bytes posted, inc. headers */
321 int started = 0;           /* number of requests started, so no excess */
322 int done = 0;              /* number of requests we have done */
323 int doneka = 0;            /* number of keep alive connections done */
324 int good = 0, bad = 0;     /* number of good and bad requests */
325 int epipe = 0;             /* number of broken pipe writes */
326 int err_length = 0;        /* requests failed due to response length */
327 int err_conn = 0;          /* requests failed due to connection drop */
328 int err_recv = 0;          /* requests failed due to broken read */
329 int err_except = 0;        /* requests failed due to exception */
330 int err_response = 0;      /* requests with invalid or non-200 response */
331
332 #ifdef USE_SSL
333 int is_ssl;
334 SSL_CTX *ssl_ctx;
335 char *ssl_cipher = NULL;
336 char *ssl_info = NULL;
337 BIO *bio_out,*bio_err;
338 #endif
339
340 apr_time_t start, lasttime, stoptime;
341
342 /* global request (and its length) */
343 char _request[8192];
344 char *request = _request;
345 apr_size_t reqlen;
346 int requests_initialized = 0;
347
348 /* one global throw-away buffer to read stuff into */
349 char buffer[8192];
350
351 /* interesting percentiles */
352 int percs[] = {50, 66, 75, 80, 90, 95, 98, 99, 100};
353
354 struct connection *con;     /* connection array */
355 struct data *stats;         /* data for each request */
356 apr_pool_t *cntxt;
357
358 apr_pollset_t *readbits;
359
360 apr_sockaddr_t *mysa;
361 apr_sockaddr_t *destsa;
362
363 #ifdef NOT_ASCII
364 apr_xlate_t *from_ascii, *to_ascii;
365 #endif
366
367 static void write_request(struct connection * c);
368 static void close_connection(struct connection * c);
369
370 /* --------------------------------------------------------- */
371
372 /* simple little function to write an error string and exit */
373
374 static void err(const char *s)
375 {
376     fprintf(stderr, "%s\n", s);
377     if (done)
378         printf("Total of %d requests completed\n" , done);
379     exit(1);
380 }
381
382 /* simple little function to write an APR error string and exit */
383
384 static void apr_err(const char *s, apr_status_t rv)
385 {
386     char buf[120];
387
388     fprintf(stderr,
389         "%s: %s (%d)\n",
390         s, apr_strerror(rv, buf, sizeof buf), rv);
391     if (done)
392         printf("Total of %d requests completed\n" , done);
393     exit(rv);
394 }
395
396 static void *xmalloc(size_t size)
397 {
398     void *ret = malloc(size);
399     if (ret == NULL) {
400         fprintf(stderr, "Could not allocate memory (%"
401                 APR_SIZE_T_FMT" bytes)\n", size);
402         exit(1);
403     }
404     return ret;
405 }
406
407 static void *xcalloc(size_t num, size_t size)
408 {
409     void *ret = calloc(num, size);
410     if (ret == NULL) {
411         fprintf(stderr, "Could not allocate memory (%"
412                 APR_SIZE_T_FMT" bytes)\n", size*num);
413         exit(1);
414     }
415     return ret;
416 }
417
418 static char *xstrdup(const char *s)
419 {
420     char *ret = strdup(s);
421     if (ret == NULL) {
422         fprintf(stderr, "Could not allocate memory (%"
423                 APR_SIZE_T_FMT " bytes)\n", strlen(s));
424         exit(1);
425     }
426     return ret;
427 }
428
429 /* pool abort function */
430 static int abort_on_oom(int retcode)
431 {
432     fprintf(stderr, "Could not allocate memory\n");
433     exit(1);
434     /* not reached */
435     return retcode;
436 }
437
438 static void set_polled_events(struct connection *c, apr_int16_t new_reqevents)
439 {
440     apr_status_t rv;
441
442     if (c->pollfd.reqevents != new_reqevents) {
443         if (c->pollfd.reqevents != 0) {
444             rv = apr_pollset_remove(readbits, &c->pollfd);
445             if (rv != APR_SUCCESS) {
446                 apr_err("apr_pollset_remove()", rv);
447             }
448         }
449
450         if (new_reqevents != 0) {
451             c->pollfd.reqevents = new_reqevents;
452             rv = apr_pollset_add(readbits, &c->pollfd);
453             if (rv != APR_SUCCESS) {
454                 apr_err("apr_pollset_add()", rv);
455             }
456         }
457     }
458 }
459
460 static void set_conn_state(struct connection *c, connect_state_e new_state)
461 {
462     apr_int16_t events_by_state[] = {
463         0,           /* for STATE_UNCONNECTED */
464         APR_POLLOUT, /* for STATE_CONNECTING */
465         APR_POLLIN,  /* for STATE_CONNECTED; we don't poll in this state,
466                       * so prepare for polling in the following state --
467                       * STATE_READ
468                       */
469         APR_POLLIN   /* for STATE_READ */
470     };
471
472     c->state = new_state;
473
474     set_polled_events(c, events_by_state[new_state]);
475 }
476
477 /* --------------------------------------------------------- */
478 /* write out request to a connection - assumes we can write
479  * (small) request out in one go into our new socket buffer
480  *
481  */
482 #ifdef USE_SSL
483 static long ssl_print_cb(BIO *bio,int cmd,const char *argp,int argi,long argl,long ret)
484 {
485     BIO *out;
486
487     out=(BIO *)BIO_get_callback_arg(bio);
488     if (out == NULL) return(ret);
489
490     if (cmd == (BIO_CB_READ|BIO_CB_RETURN)) {
491         BIO_printf(out,"read from %p [%p] (%d bytes => %ld (0x%lX))\n",
492                    bio, argp, argi, ret, ret);
493         BIO_dump(out,(char *)argp,(int)ret);
494         return(ret);
495     }
496     else if (cmd == (BIO_CB_WRITE|BIO_CB_RETURN)) {
497         BIO_printf(out,"write to %p [%p] (%d bytes => %ld (0x%lX))\n",
498                    bio, argp, argi, ret, ret);
499         BIO_dump(out,(char *)argp,(int)ret);
500     }
501     return ret;
502 }
503
504 static void ssl_state_cb(const SSL *s, int w, int r)
505 {
506     if (w & SSL_CB_ALERT) {
507         BIO_printf(bio_err, "SSL/TLS Alert [%s] %s:%s\n",
508                    (w & SSL_CB_READ ? "read" : "write"),
509                    SSL_alert_type_string_long(r),
510                    SSL_alert_desc_string_long(r));
511     } else if (w & SSL_CB_LOOP) {
512         BIO_printf(bio_err, "SSL/TLS State [%s] %s\n",
513                    (SSL_in_connect_init((SSL*)s) ? "connect" : "-"),
514                    SSL_state_string_long(s));
515     } else if (w & (SSL_CB_HANDSHAKE_START|SSL_CB_HANDSHAKE_DONE)) {
516         BIO_printf(bio_err, "SSL/TLS Handshake [%s] %s\n",
517                    (w & SSL_CB_HANDSHAKE_START ? "Start" : "Done"),
518                    SSL_state_string_long(s));
519     }
520 }
521
522 #ifndef RAND_MAX
523 #define RAND_MAX INT_MAX
524 #endif
525
526 static int ssl_rand_choosenum(int l, int h)
527 {
528     int i;
529     char buf[50];
530
531     srand((unsigned int)time(NULL));
532     apr_snprintf(buf, sizeof(buf), "%.0f",
533                  (((double)(rand()%RAND_MAX)/RAND_MAX)*(h-l)));
534     i = atoi(buf)+1;
535     if (i < l) i = l;
536     if (i > h) i = h;
537     return i;
538 }
539
540 static void ssl_rand_seed(void)
541 {
542     int n, l;
543     time_t t;
544     pid_t pid;
545     unsigned char stackdata[256];
546
547     /*
548      * seed in the current time (usually just 4 bytes)
549      */
550     t = time(NULL);
551     l = sizeof(time_t);
552     RAND_seed((unsigned char *)&t, l);
553
554     /*
555      * seed in the current process id (usually just 4 bytes)
556      */
557     pid = getpid();
558     l = sizeof(pid_t);
559     RAND_seed((unsigned char *)&pid, l);
560
561     /*
562      * seed in some current state of the run-time stack (128 bytes)
563      */
564     n = ssl_rand_choosenum(0, sizeof(stackdata)-128-1);
565     RAND_seed(stackdata+n, 128);
566 }
567
568 static int ssl_print_connection_info(BIO *bio, SSL *ssl)
569 {
570     AB_SSL_CIPHER_CONST SSL_CIPHER *c;
571     int alg_bits,bits;
572
573     BIO_printf(bio,"Transport Protocol      :%s\n", SSL_get_version(ssl));
574
575     c = SSL_get_current_cipher(ssl);
576     BIO_printf(bio,"Cipher Suite Protocol   :%s\n", SSL_CIPHER_get_version(c));
577     BIO_printf(bio,"Cipher Suite Name       :%s\n",SSL_CIPHER_get_name(c));
578
579     bits = SSL_CIPHER_get_bits(c,&alg_bits);
580     BIO_printf(bio,"Cipher Suite Cipher Bits:%d (%d)\n",bits,alg_bits);
581
582     return(1);
583 }
584
585 static void ssl_print_cert_info(BIO *bio, X509 *cert)
586 {
587     X509_NAME *dn;
588     EVP_PKEY *pk;
589     char buf[1024];
590
591     BIO_printf(bio, "Certificate version: %ld\n", X509_get_version(cert)+1);
592     BIO_printf(bio,"Valid from: ");
593     ASN1_UTCTIME_print(bio, X509_get_notBefore(cert));
594     BIO_printf(bio,"\n");
595
596     BIO_printf(bio,"Valid to  : ");
597     ASN1_UTCTIME_print(bio, X509_get_notAfter(cert));
598     BIO_printf(bio,"\n");
599
600     pk = X509_get_pubkey(cert);
601     BIO_printf(bio,"Public key is %d bits\n",
602                EVP_PKEY_bits(pk));
603     EVP_PKEY_free(pk);
604
605     dn = X509_get_issuer_name(cert);
606     X509_NAME_oneline(dn, buf, sizeof(buf));
607     BIO_printf(bio,"The issuer name is %s\n", buf);
608
609     dn=X509_get_subject_name(cert);
610     X509_NAME_oneline(dn, buf, sizeof(buf));
611     BIO_printf(bio,"The subject name is %s\n", buf);
612
613     /* dump the extension list too */
614     BIO_printf(bio, "Extension Count: %d\n", X509_get_ext_count(cert));
615 }
616
617 static void ssl_print_info(struct connection *c)
618 {
619     X509_STACK_TYPE *sk;
620     X509 *cert;
621     int count;
622
623     BIO_printf(bio_err, "\n");
624     sk = SSL_get_peer_cert_chain(c->ssl);
625     if ((count = SK_NUM(sk)) > 0) {
626         int i;
627         for (i=1; i<count; i++) {
628             cert = (X509 *)SK_VALUE(sk, i);
629             ssl_print_cert_info(bio_out, cert);
630     }
631     }
632     cert = SSL_get_peer_certificate(c->ssl);
633     if (cert == NULL) {
634         BIO_printf(bio_out, "Anon DH\n");
635     } else {
636         BIO_printf(bio_out, "Peer certificate\n");
637         ssl_print_cert_info(bio_out, cert);
638         X509_free(cert);
639     }
640     ssl_print_connection_info(bio_err,c->ssl);
641     SSL_SESSION_print(bio_err, SSL_get_session(c->ssl));
642     }
643
644 static void ssl_proceed_handshake(struct connection *c)
645 {
646     int do_next = 1;
647
648     while (do_next) {
649         int ret, ecode;
650
651         ret = SSL_do_handshake(c->ssl);
652         ecode = SSL_get_error(c->ssl, ret);
653
654         switch (ecode) {
655         case SSL_ERROR_NONE:
656             if (verbosity >= 2)
657                 ssl_print_info(c);
658             if (ssl_info == NULL) {
659                 AB_SSL_CIPHER_CONST SSL_CIPHER *ci;
660                 X509 *cert;
661                 int sk_bits, pk_bits, swork;
662
663                 ci = SSL_get_current_cipher(c->ssl);
664                 sk_bits = SSL_CIPHER_get_bits(ci, &swork);
665                 cert = SSL_get_peer_certificate(c->ssl);
666                 if (cert)
667                     pk_bits = EVP_PKEY_bits(X509_get_pubkey(cert));
668                 else
669                     pk_bits = 0;  /* Anon DH */
670
671                 ssl_info = xmalloc(128);
672                 apr_snprintf(ssl_info, 128, "%s,%s,%d,%d",
673                              SSL_get_version(c->ssl),
674                              SSL_CIPHER_get_name(ci),
675                              pk_bits, sk_bits);
676             }
677             write_request(c);
678             do_next = 0;
679             break;
680         case SSL_ERROR_WANT_READ:
681             set_polled_events(c, APR_POLLIN);
682             do_next = 0;
683             break;
684         case SSL_ERROR_WANT_WRITE:
685             /* Try again */
686             do_next = 1;
687             break;
688         case SSL_ERROR_WANT_CONNECT:
689         case SSL_ERROR_SSL:
690         case SSL_ERROR_SYSCALL:
691             /* Unexpected result */
692             BIO_printf(bio_err, "SSL handshake failed (%d).\n", ecode);
693             ERR_print_errors(bio_err);
694             close_connection(c);
695             do_next = 0;
696             break;
697         }
698     }
699 }
700
701 #endif /* USE_SSL */
702
703 static void write_request(struct connection * c)
704 {
705     if (started >= requests) {
706         return;
707     }
708
709     do {
710         apr_time_t tnow;
711         apr_size_t l = c->rwrite;
712         apr_status_t e = APR_SUCCESS; /* prevent gcc warning */
713
714         tnow = lasttime = apr_time_now();
715
716         /*
717          * First time round ?
718          */
719         if (c->rwrite == 0) {
720             apr_socket_timeout_set(c->aprsock, 0);
721             c->connect = tnow;
722             c->rwrote = 0;
723             c->rwrite = reqlen;
724             if (send_body)
725                 c->rwrite += postlen;
726             l = c->rwrite;
727         }
728         else if (tnow > c->connect + aprtimeout) {
729             printf("Send request timed out!\n");
730             close_connection(c);
731             return;
732         }
733
734 #ifdef USE_SSL
735         if (c->ssl) {
736             apr_size_t e_ssl;
737             e_ssl = SSL_write(c->ssl,request + c->rwrote, l);
738             if (e_ssl != l) {
739                 BIO_printf(bio_err, "SSL write failed - closing connection\n");
740                 ERR_print_errors(bio_err);
741                 close_connection (c);
742                 return;
743             }
744             l = e_ssl;
745             e = APR_SUCCESS;
746         }
747         else
748 #endif
749             e = apr_socket_send(c->aprsock, request + c->rwrote, &l);
750
751         if (e != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(e)) {
752             epipe++;
753             printf("Send request failed!\n");
754             close_connection(c);
755             return;
756         }
757         totalposted += l;
758         c->rwrote += l;
759         c->rwrite -= l;
760     } while (c->rwrite);
761
762     c->endwrite = lasttime = apr_time_now();
763     started++;
764     set_conn_state(c, STATE_READ);
765 }
766
767 /* --------------------------------------------------------- */
768
769 /* calculate and output results */
770
771 static int compradre(struct data * a, struct data * b)
772 {
773     if ((a->ctime) < (b->ctime))
774         return -1;
775     if ((a->ctime) > (b->ctime))
776         return +1;
777     return 0;
778 }
779
780 static int comprando(struct data * a, struct data * b)
781 {
782     if ((a->time) < (b->time))
783         return -1;
784     if ((a->time) > (b->time))
785         return +1;
786     return 0;
787 }
788
789 static int compri(struct data * a, struct data * b)
790 {
791     apr_interval_time_t p = a->time - a->ctime;
792     apr_interval_time_t q = b->time - b->ctime;
793     if (p < q)
794         return -1;
795     if (p > q)
796         return +1;
797     return 0;
798 }
799
800 static int compwait(struct data * a, struct data * b)
801 {
802     if ((a->waittime) < (b->waittime))
803         return -1;
804     if ((a->waittime) > (b->waittime))
805         return 1;
806     return 0;
807 }
808
809 static void output_results(int sig)
810 {
811     double timetaken;
812
813     if (sig) {
814         lasttime = apr_time_now();  /* record final time if interrupted */
815     }
816     timetaken = (double) (lasttime - start) / APR_USEC_PER_SEC;
817
818     printf("\n\n");
819     printf("Server Software:        %s\n", servername);
820     printf("Server Hostname:        %s\n", hostname);
821     printf("Server Port:            %hu\n", port);
822 #ifdef USE_SSL
823     if (is_ssl && ssl_info) {
824         printf("SSL/TLS Protocol:       %s\n", ssl_info);
825     }
826 #endif
827     printf("\n");
828     printf("Document Path:          %s\n", path);
829     if (nolength)
830         printf("Document Length:        Variable\n");
831     else
832         printf("Document Length:        %" APR_SIZE_T_FMT " bytes\n", doclen);
833     printf("\n");
834     printf("Concurrency Level:      %d\n", concurrency);
835     printf("Time taken for tests:   %.3f seconds\n", timetaken);
836     printf("Complete requests:      %d\n", done);
837     printf("Failed requests:        %d\n", bad);
838     if (bad)
839         printf("   (Connect: %d, Receive: %d, Length: %d, Exceptions: %d)\n",
840             err_conn, err_recv, err_length, err_except);
841     if (epipe)
842         printf("Write errors:           %d\n", epipe);
843     if (err_response)
844         printf("Non-2xx responses:      %d\n", err_response);
845     if (keepalive)
846         printf("Keep-Alive requests:    %d\n", doneka);
847     printf("Total transferred:      %" APR_INT64_T_FMT " bytes\n", totalread);
848     if (send_body)
849         printf("Total body sent:        %" APR_INT64_T_FMT "\n",
850                totalposted);
851     printf("HTML transferred:       %" APR_INT64_T_FMT " bytes\n", totalbread);
852
853     /* avoid divide by zero */
854     if (timetaken && done) {
855         printf("Requests per second:    %.2f [#/sec] (mean)\n",
856                (double) done / timetaken);
857         printf("Time per request:       %.3f [ms] (mean)\n",
858                (double) concurrency * timetaken * 1000 / done);
859         printf("Time per request:       %.3f [ms] (mean, across all concurrent requests)\n",
860                (double) timetaken * 1000 / done);
861         printf("Transfer rate:          %.2f [Kbytes/sec] received\n",
862                (double) totalread / 1024 / timetaken);
863         if (send_body) {
864             printf("                        %.2f kb/s sent\n",
865                (double) totalposted / 1024 / timetaken);
866             printf("                        %.2f kb/s total\n",
867                (double) (totalread + totalposted) / 1024 / timetaken);
868         }
869     }
870
871     if (done > 0) {
872         /* work out connection times */
873         int i;
874         apr_time_t totalcon = 0, total = 0, totald = 0, totalwait = 0;
875         apr_time_t meancon, meantot, meand, meanwait;
876         apr_interval_time_t mincon = AB_MAX, mintot = AB_MAX, mind = AB_MAX,
877                             minwait = AB_MAX;
878         apr_interval_time_t maxcon = 0, maxtot = 0, maxd = 0, maxwait = 0;
879         apr_interval_time_t mediancon = 0, mediantot = 0, mediand = 0, medianwait = 0;
880         double sdtot = 0, sdcon = 0, sdd = 0, sdwait = 0;
881
882         for (i = 0; i < done; i++) {
883             struct data *s = &stats[i];
884             mincon = ap_min(mincon, s->ctime);
885             mintot = ap_min(mintot, s->time);
886             mind = ap_min(mind, s->time - s->ctime);
887             minwait = ap_min(minwait, s->waittime);
888
889             maxcon = ap_max(maxcon, s->ctime);
890             maxtot = ap_max(maxtot, s->time);
891             maxd = ap_max(maxd, s->time - s->ctime);
892             maxwait = ap_max(maxwait, s->waittime);
893
894             totalcon += s->ctime;
895             total += s->time;
896             totald += s->time - s->ctime;
897             totalwait += s->waittime;
898         }
899         meancon = totalcon / done;
900         meantot = total / done;
901         meand = totald / done;
902         meanwait = totalwait / done;
903
904         /* calculating the sample variance: the sum of the squared deviations, divided by n-1 */
905         for (i = 0; i < done; i++) {
906             struct data *s = &stats[i];
907             double a;
908             a = ((double)s->time - meantot);
909             sdtot += a * a;
910             a = ((double)s->ctime - meancon);
911             sdcon += a * a;
912             a = ((double)s->time - (double)s->ctime - meand);
913             sdd += a * a;
914             a = ((double)s->waittime - meanwait);
915             sdwait += a * a;
916         }
917
918         sdtot = (done > 1) ? sqrt(sdtot / (done - 1)) : 0;
919         sdcon = (done > 1) ? sqrt(sdcon / (done - 1)) : 0;
920         sdd = (done > 1) ? sqrt(sdd / (done - 1)) : 0;
921         sdwait = (done > 1) ? sqrt(sdwait / (done - 1)) : 0;
922
923         /*
924          * XXX: what is better; this hideous cast of the compradre function; or
925          * the four warnings during compile ? dirkx just does not know and
926          * hates both/
927          */
928         qsort(stats, done, sizeof(struct data),
929               (int (*) (const void *, const void *)) compradre);
930         if ((done > 1) && (done % 2))
931             mediancon = (stats[done / 2].ctime + stats[done / 2 + 1].ctime) / 2;
932         else
933             mediancon = stats[done / 2].ctime;
934
935         qsort(stats, done, sizeof(struct data),
936               (int (*) (const void *, const void *)) compri);
937         if ((done > 1) && (done % 2))
938             mediand = (stats[done / 2].time + stats[done / 2 + 1].time \
939             -stats[done / 2].ctime - stats[done / 2 + 1].ctime) / 2;
940         else
941             mediand = stats[done / 2].time - stats[done / 2].ctime;
942
943         qsort(stats, done, sizeof(struct data),
944               (int (*) (const void *, const void *)) compwait);
945         if ((done > 1) && (done % 2))
946             medianwait = (stats[done / 2].waittime + stats[done / 2 + 1].waittime) / 2;
947         else
948             medianwait = stats[done / 2].waittime;
949
950         qsort(stats, done, sizeof(struct data),
951               (int (*) (const void *, const void *)) comprando);
952         if ((done > 1) && (done % 2))
953             mediantot = (stats[done / 2].time + stats[done / 2 + 1].time) / 2;
954         else
955             mediantot = stats[done / 2].time;
956
957         printf("\nConnection Times (ms)\n");
958         /*
959          * Reduce stats from apr time to milliseconds
960          */
961         mincon     = ap_round_ms(mincon);
962         mind       = ap_round_ms(mind);
963         minwait    = ap_round_ms(minwait);
964         mintot     = ap_round_ms(mintot);
965         meancon    = ap_round_ms(meancon);
966         meand      = ap_round_ms(meand);
967         meanwait   = ap_round_ms(meanwait);
968         meantot    = ap_round_ms(meantot);
969         mediancon  = ap_round_ms(mediancon);
970         mediand    = ap_round_ms(mediand);
971         medianwait = ap_round_ms(medianwait);
972         mediantot  = ap_round_ms(mediantot);
973         maxcon     = ap_round_ms(maxcon);
974         maxd       = ap_round_ms(maxd);
975         maxwait    = ap_round_ms(maxwait);
976         maxtot     = ap_round_ms(maxtot);
977         sdcon      = ap_double_ms(sdcon);
978         sdd        = ap_double_ms(sdd);
979         sdwait     = ap_double_ms(sdwait);
980         sdtot      = ap_double_ms(sdtot);
981
982         if (confidence) {
983 #define CONF_FMT_STRING "%5" APR_TIME_T_FMT " %4" APR_TIME_T_FMT " %5.1f %6" APR_TIME_T_FMT " %7" APR_TIME_T_FMT "\n"
984             printf("              min  mean[+/-sd] median   max\n");
985             printf("Connect:    " CONF_FMT_STRING,
986                    mincon, meancon, sdcon, mediancon, maxcon);
987             printf("Processing: " CONF_FMT_STRING,
988                    mind, meand, sdd, mediand, maxd);
989             printf("Waiting:    " CONF_FMT_STRING,
990                    minwait, meanwait, sdwait, medianwait, maxwait);
991             printf("Total:      " CONF_FMT_STRING,
992                    mintot, meantot, sdtot, mediantot, maxtot);
993 #undef CONF_FMT_STRING
994
995 #define     SANE(what,mean,median,sd) \
996               { \
997                 double d = (double)mean - median; \
998                 if (d < 0) d = -d; \
999                 if (d > 2 * sd ) \
1000                     printf("ERROR: The median and mean for " what " are more than twice the standard\n" \
1001                            "       deviation apart. These results are NOT reliable.\n"); \
1002                 else if (d > sd ) \
1003                     printf("WARNING: The median and mean for " what " are not within a normal deviation\n" \
1004                            "        These results are probably not that reliable.\n"); \
1005             }
1006             SANE("the initial connection time", meancon, mediancon, sdcon);
1007             SANE("the processing time", meand, mediand, sdd);
1008             SANE("the waiting time", meanwait, medianwait, sdwait);
1009             SANE("the total time", meantot, mediantot, sdtot);
1010         }
1011         else {
1012             printf("              min   avg   max\n");
1013 #define CONF_FMT_STRING "%5" APR_TIME_T_FMT " %5" APR_TIME_T_FMT "%5" APR_TIME_T_FMT "\n"
1014             printf("Connect:    " CONF_FMT_STRING, mincon, meancon, maxcon);
1015             printf("Processing: " CONF_FMT_STRING, mind, meand, maxd);
1016             printf("Waiting:    " CONF_FMT_STRING, minwait, meanwait, maxwait);
1017             printf("Total:      " CONF_FMT_STRING, mintot, meantot, maxtot);
1018 #undef CONF_FMT_STRING
1019         }
1020
1021
1022         /* Sorted on total connect times */
1023         if (percentile && (done > 1)) {
1024             printf("\nPercentage of the requests served within a certain time (ms)\n");
1025             for (i = 0; i < sizeof(percs) / sizeof(int); i++) {
1026                 if (percs[i] <= 0)
1027                     printf(" 0%%  <0> (never)\n");
1028                 else if (percs[i] >= 100)
1029                     printf(" 100%%  %5" APR_TIME_T_FMT " (longest request)\n",
1030                            ap_round_ms(stats[done - 1].time));
1031                 else
1032                     printf("  %d%%  %5" APR_TIME_T_FMT "\n", percs[i],
1033                            ap_round_ms(stats[(unsigned long)done * percs[i] / 100].time));
1034             }
1035         }
1036         if (csvperc) {
1037             FILE *out = fopen(csvperc, "w");
1038             if (!out) {
1039                 perror("Cannot open CSV output file");
1040                 exit(1);
1041             }
1042             fprintf(out, "" "Percentage served" "," "Time in ms" "\n");
1043             for (i = 0; i <= 100; i++) {
1044                 double t;
1045                 if (i == 0)
1046                     t = ap_double_ms(stats[0].time);
1047                 else if (i == 100)
1048                     t = ap_double_ms(stats[done - 1].time);
1049                 else
1050                     t = ap_double_ms(stats[(unsigned long) (0.5 + (double)done * i / 100.0)].time);
1051                 fprintf(out, "%d,%.3f\n", i, t);
1052             }
1053             fclose(out);
1054         }
1055         if (gnuplot) {
1056             FILE *out = fopen(gnuplot, "w");
1057             char tmstring[APR_CTIME_LEN];
1058             if (!out) {
1059                 perror("Cannot open gnuplot output file");
1060                 exit(1);
1061             }
1062             fprintf(out, "starttime\tseconds\tctime\tdtime\tttime\twait\n");
1063             for (i = 0; i < done; i++) {
1064                 (void) apr_ctime(tmstring, stats[i].starttime);
1065                 fprintf(out, "%s\t%" APR_TIME_T_FMT "\t%" APR_TIME_T_FMT
1066                                "\t%" APR_TIME_T_FMT "\t%" APR_TIME_T_FMT
1067                                "\t%" APR_TIME_T_FMT "\n", tmstring,
1068                         apr_time_sec(stats[i].starttime),
1069                         ap_round_ms(stats[i].ctime),
1070                         ap_round_ms(stats[i].time - stats[i].ctime),
1071                         ap_round_ms(stats[i].time),
1072                         ap_round_ms(stats[i].waittime));
1073             }
1074             fclose(out);
1075         }
1076     }
1077
1078     if (sig) {
1079         exit(1);
1080     }
1081 }
1082
1083 /* --------------------------------------------------------- */
1084
1085 /* calculate and output results in HTML  */
1086
1087 static void output_html_results(void)
1088 {
1089     double timetaken = (double) (lasttime - start) / APR_USEC_PER_SEC;
1090
1091     printf("\n\n<table %s>\n", tablestring);
1092     printf("<tr %s><th colspan=2 %s>Server Software:</th>"
1093        "<td colspan=2 %s>%s</td></tr>\n",
1094        trstring, tdstring, tdstring, servername);
1095     printf("<tr %s><th colspan=2 %s>Server Hostname:</th>"
1096        "<td colspan=2 %s>%s</td></tr>\n",
1097        trstring, tdstring, tdstring, hostname);
1098     printf("<tr %s><th colspan=2 %s>Server Port:</th>"
1099        "<td colspan=2 %s>%hu</td></tr>\n",
1100        trstring, tdstring, tdstring, port);
1101     printf("<tr %s><th colspan=2 %s>Document Path:</th>"
1102        "<td colspan=2 %s>%s</td></tr>\n",
1103        trstring, tdstring, tdstring, path);
1104     if (nolength)
1105         printf("<tr %s><th colspan=2 %s>Document Length:</th>"
1106             "<td colspan=2 %s>Variable</td></tr>\n",
1107             trstring, tdstring, tdstring);
1108     else
1109         printf("<tr %s><th colspan=2 %s>Document Length:</th>"
1110             "<td colspan=2 %s>%" APR_SIZE_T_FMT " bytes</td></tr>\n",
1111             trstring, tdstring, tdstring, doclen);
1112     printf("<tr %s><th colspan=2 %s>Concurrency Level:</th>"
1113        "<td colspan=2 %s>%d</td></tr>\n",
1114        trstring, tdstring, tdstring, concurrency);
1115     printf("<tr %s><th colspan=2 %s>Time taken for tests:</th>"
1116        "<td colspan=2 %s>%.3f seconds</td></tr>\n",
1117        trstring, tdstring, tdstring, timetaken);
1118     printf("<tr %s><th colspan=2 %s>Complete requests:</th>"
1119        "<td colspan=2 %s>%d</td></tr>\n",
1120        trstring, tdstring, tdstring, done);
1121     printf("<tr %s><th colspan=2 %s>Failed requests:</th>"
1122        "<td colspan=2 %s>%d</td></tr>\n",
1123        trstring, tdstring, tdstring, bad);
1124     if (bad)
1125         printf("<tr %s><td colspan=4 %s >   (Connect: %d, Length: %d, Exceptions: %d)</td></tr>\n",
1126            trstring, tdstring, err_conn, err_length, err_except);
1127     if (err_response)
1128         printf("<tr %s><th colspan=2 %s>Non-2xx responses:</th>"
1129            "<td colspan=2 %s>%d</td></tr>\n",
1130            trstring, tdstring, tdstring, err_response);
1131     if (keepalive)
1132         printf("<tr %s><th colspan=2 %s>Keep-Alive requests:</th>"
1133            "<td colspan=2 %s>%d</td></tr>\n",
1134            trstring, tdstring, tdstring, doneka);
1135     printf("<tr %s><th colspan=2 %s>Total transferred:</th>"
1136        "<td colspan=2 %s>%" APR_INT64_T_FMT " bytes</td></tr>\n",
1137        trstring, tdstring, tdstring, totalread);
1138     if (send_body)
1139         printf("<tr %s><th colspan=2 %s>Total body sent:</th>"
1140            "<td colspan=2 %s>%" APR_INT64_T_FMT "</td></tr>\n",
1141            trstring, tdstring,
1142            tdstring, totalposted);
1143     printf("<tr %s><th colspan=2 %s>HTML transferred:</th>"
1144        "<td colspan=2 %s>%" APR_INT64_T_FMT " bytes</td></tr>\n",
1145        trstring, tdstring, tdstring, totalbread);
1146
1147     /* avoid divide by zero */
1148     if (timetaken) {
1149         printf("<tr %s><th colspan=2 %s>Requests per second:</th>"
1150            "<td colspan=2 %s>%.2f</td></tr>\n",
1151            trstring, tdstring, tdstring, (double) done / timetaken);
1152         printf("<tr %s><th colspan=2 %s>Transfer rate:</th>"
1153            "<td colspan=2 %s>%.2f kb/s received</td></tr>\n",
1154            trstring, tdstring, tdstring, (double) totalread / 1024 / timetaken);
1155         if (send_body) {
1156             printf("<tr %s><td colspan=2 %s>&nbsp;</td>"
1157                "<td colspan=2 %s>%.2f kb/s sent</td></tr>\n",
1158                trstring, tdstring, tdstring,
1159                (double) totalposted / 1024 / timetaken);
1160             printf("<tr %s><td colspan=2 %s>&nbsp;</td>"
1161                "<td colspan=2 %s>%.2f kb/s total</td></tr>\n",
1162                trstring, tdstring, tdstring,
1163                (double) (totalread + totalposted) / 1024 / timetaken);
1164         }
1165     }
1166     {
1167         /* work out connection times */
1168         int i;
1169         apr_interval_time_t totalcon = 0, total = 0;
1170         apr_interval_time_t mincon = AB_MAX, mintot = AB_MAX;
1171         apr_interval_time_t maxcon = 0, maxtot = 0;
1172
1173         for (i = 0; i < done; i++) {
1174             struct data *s = &stats[i];
1175             mincon = ap_min(mincon, s->ctime);
1176             mintot = ap_min(mintot, s->time);
1177             maxcon = ap_max(maxcon, s->ctime);
1178             maxtot = ap_max(maxtot, s->time);
1179             totalcon += s->ctime;
1180             total    += s->time;
1181         }
1182         /*
1183          * Reduce stats from apr time to milliseconds
1184          */
1185         mincon   = ap_round_ms(mincon);
1186         mintot   = ap_round_ms(mintot);
1187         maxcon   = ap_round_ms(maxcon);
1188         maxtot   = ap_round_ms(maxtot);
1189         totalcon = ap_round_ms(totalcon);
1190         total    = ap_round_ms(total);
1191
1192         if (done > 0) { /* avoid division by zero (if 0 done) */
1193             printf("<tr %s><th %s colspan=4>Connnection Times (ms)</th></tr>\n",
1194                trstring, tdstring);
1195             printf("<tr %s><th %s>&nbsp;</th> <th %s>min</th>   <th %s>avg</th>   <th %s>max</th></tr>\n",
1196                trstring, tdstring, tdstring, tdstring, tdstring);
1197             printf("<tr %s><th %s>Connect:</th>"
1198                "<td %s>%5" APR_TIME_T_FMT "</td>"
1199                "<td %s>%5" APR_TIME_T_FMT "</td>"
1200                "<td %s>%5" APR_TIME_T_FMT "</td></tr>\n",
1201                trstring, tdstring, tdstring, mincon, tdstring, totalcon / done, tdstring, maxcon);
1202             printf("<tr %s><th %s>Processing:</th>"
1203                "<td %s>%5" APR_TIME_T_FMT "</td>"
1204                "<td %s>%5" APR_TIME_T_FMT "</td>"
1205                "<td %s>%5" APR_TIME_T_FMT "</td></tr>\n",
1206                trstring, tdstring, tdstring, mintot - mincon, tdstring,
1207                (total / done) - (totalcon / done), tdstring, maxtot - maxcon);
1208             printf("<tr %s><th %s>Total:</th>"
1209                "<td %s>%5" APR_TIME_T_FMT "</td>"
1210                "<td %s>%5" APR_TIME_T_FMT "</td>"
1211                "<td %s>%5" APR_TIME_T_FMT "</td></tr>\n",
1212                trstring, tdstring, tdstring, mintot, tdstring, total / done, tdstring, maxtot);
1213         }
1214         printf("</table>\n");
1215     }
1216 }
1217
1218 /* --------------------------------------------------------- */
1219
1220 /* start asnchronous non-blocking connection */
1221
1222 static void start_connect(struct connection * c)
1223 {
1224     apr_status_t rv;
1225
1226     if (!(started < requests))
1227         return;
1228
1229     c->read = 0;
1230     c->bread = 0;
1231     c->keepalive = 0;
1232     c->cbx = 0;
1233     c->gotheader = 0;
1234     c->rwrite = 0;
1235     if (c->ctx)
1236         apr_pool_clear(c->ctx);
1237     else
1238         apr_pool_create(&c->ctx, cntxt);
1239
1240     if ((rv = apr_socket_create(&c->aprsock, destsa->family,
1241                 SOCK_STREAM, 0, c->ctx)) != APR_SUCCESS) {
1242     apr_err("socket", rv);
1243     }
1244
1245     if (myhost) {
1246         if ((rv = apr_socket_bind(c->aprsock, mysa)) != APR_SUCCESS) {
1247             apr_err("bind", rv);
1248         }
1249     }
1250
1251     c->pollfd.desc_type = APR_POLL_SOCKET;
1252     c->pollfd.desc.s = c->aprsock;
1253     c->pollfd.reqevents = 0;
1254     c->pollfd.client_data = c;
1255
1256     if ((rv = apr_socket_opt_set(c->aprsock, APR_SO_NONBLOCK, 1))
1257          != APR_SUCCESS) {
1258         apr_err("socket nonblock", rv);
1259     }
1260
1261     if (windowsize != 0) {
1262         rv = apr_socket_opt_set(c->aprsock, APR_SO_SNDBUF,
1263                                 windowsize);
1264         if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
1265             apr_err("socket send buffer", rv);
1266         }
1267         rv = apr_socket_opt_set(c->aprsock, APR_SO_RCVBUF,
1268                                 windowsize);
1269         if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
1270             apr_err("socket receive buffer", rv);
1271         }
1272     }
1273
1274     c->start = lasttime = apr_time_now();
1275 #ifdef USE_SSL
1276     if (is_ssl) {
1277         BIO *bio;
1278         apr_os_sock_t fd;
1279
1280         if ((c->ssl = SSL_new(ssl_ctx)) == NULL) {
1281             BIO_printf(bio_err, "SSL_new failed.\n");
1282             ERR_print_errors(bio_err);
1283             exit(1);
1284         }
1285         ssl_rand_seed();
1286         apr_os_sock_get(&fd, c->aprsock);
1287         bio = BIO_new_socket(fd, BIO_NOCLOSE);
1288         SSL_set_bio(c->ssl, bio, bio);
1289         SSL_set_connect_state(c->ssl);
1290         if (verbosity >= 4) {
1291             BIO_set_callback(bio, ssl_print_cb);
1292             BIO_set_callback_arg(bio, (void *)bio_err);
1293         }
1294     } else {
1295         c->ssl = NULL;
1296     }
1297 #endif
1298     if ((rv = apr_socket_connect(c->aprsock, destsa)) != APR_SUCCESS) {
1299         if (APR_STATUS_IS_EINPROGRESS(rv)) {
1300             set_conn_state(c, STATE_CONNECTING);
1301             c->rwrite = 0;
1302             return;
1303         }
1304         else {
1305             set_conn_state(c, STATE_UNCONNECTED);
1306             apr_socket_close(c->aprsock);
1307             if (good == 0 && destsa->next) {
1308                 destsa = destsa->next;
1309                 err_conn = 0;
1310             }
1311             else if (bad++ > 10) {
1312                 fprintf(stderr,
1313                    "\nTest aborted after 10 failures\n\n");
1314                 apr_err("apr_socket_connect()", rv);
1315             }
1316             else {
1317                 err_conn++;
1318             }
1319
1320             start_connect(c);
1321             return;
1322         }
1323     }
1324
1325     /* connected first time */
1326     set_conn_state(c, STATE_CONNECTED);
1327 #ifdef USE_SSL
1328     if (c->ssl) {
1329         ssl_proceed_handshake(c);
1330     } else
1331 #endif
1332     {
1333         write_request(c);
1334     }
1335 }
1336
1337 /* --------------------------------------------------------- */
1338
1339 /* close down connection and save stats */
1340
1341 static void close_connection(struct connection * c)
1342 {
1343     if (c->read == 0 && c->keepalive) {
1344         /*
1345          * server has legitimately shut down an idle keep alive request
1346          */
1347         if (good)
1348             good--;     /* connection never happened */
1349     }
1350     else {
1351         if (good == 1) {
1352             /* first time here */
1353             doclen = c->bread;
1354         }
1355         else if ((c->bread != doclen) && !nolength) {
1356             bad++;
1357             err_length++;
1358         }
1359         /* save out time */
1360         if (done < requests) {
1361             struct data *s = &stats[done++];
1362             c->done      = lasttime = apr_time_now();
1363             s->starttime = c->start;
1364             s->ctime     = ap_max(0, c->connect - c->start);
1365             s->time      = ap_max(0, c->done - c->start);
1366             s->waittime  = ap_max(0, c->beginread - c->endwrite);
1367             if (heartbeatres && !(done % heartbeatres)) {
1368                 fprintf(stderr, "Completed %d requests\n", done);
1369                 fflush(stderr);
1370             }
1371         }
1372     }
1373
1374     set_conn_state(c, STATE_UNCONNECTED);
1375 #ifdef USE_SSL
1376     if (c->ssl) {
1377         SSL_shutdown(c->ssl);
1378         SSL_free(c->ssl);
1379         c->ssl = NULL;
1380     }
1381 #endif
1382     apr_socket_close(c->aprsock);
1383
1384     /* connect again */
1385     start_connect(c);
1386     return;
1387 }
1388
1389 /* --------------------------------------------------------- */
1390
1391 /* read data from connection */
1392
1393 static void read_connection(struct connection * c)
1394 {
1395     apr_size_t r;
1396     apr_status_t status;
1397     char *part;
1398     char respcode[4];       /* 3 digits and null */
1399     int i;
1400
1401     r = sizeof(buffer);
1402 #ifdef USE_SSL
1403     if (c->ssl) {
1404         status = SSL_read(c->ssl, buffer, r);
1405         if (status <= 0) {
1406             int scode = SSL_get_error(c->ssl, status);
1407
1408             if (scode == SSL_ERROR_ZERO_RETURN) {
1409                 /* connection closed cleanly: */
1410                 good++;
1411                 close_connection(c);
1412             }
1413             else if (scode == SSL_ERROR_SYSCALL
1414                      && status == 0
1415                      && c->read != 0) {
1416                 /* connection closed, but in violation of the protocol, after
1417                  * some data has already been read; this commonly happens, so
1418                  * let the length check catch any response errors
1419                  */
1420                 good++;
1421                 close_connection(c);
1422             }
1423             else if (scode == SSL_ERROR_SYSCALL 
1424                      && c->read == 0
1425                      && destsa->next
1426                      && c->state == STATE_CONNECTING
1427                      && good == 0) {
1428                 return;
1429             }
1430             else if (scode != SSL_ERROR_WANT_WRITE
1431                      && scode != SSL_ERROR_WANT_READ) {
1432                 /* some fatal error: */
1433                 c->read = 0;
1434                 BIO_printf(bio_err, "SSL read failed (%d) - closing connection\n", scode);
1435                 ERR_print_errors(bio_err);
1436                 close_connection(c);
1437             }
1438             return;
1439         }
1440         r = status;
1441     }
1442     else
1443 #endif
1444     {
1445         status = apr_socket_recv(c->aprsock, buffer, &r);
1446         if (APR_STATUS_IS_EAGAIN(status))
1447             return;
1448         else if (r == 0 && APR_STATUS_IS_EOF(status)) {
1449             good++;
1450             close_connection(c);
1451             return;
1452         }
1453         /* catch legitimate fatal apr_socket_recv errors */
1454         else if (status != APR_SUCCESS) {
1455             if (recverrok) {
1456                 err_recv++;
1457                 bad++;
1458                 close_connection(c);
1459                 if (verbosity >= 1) {
1460                     char buf[120];
1461                     fprintf(stderr,"%s: %s (%d)\n", "apr_socket_recv", apr_strerror(status, buf, sizeof buf), status);
1462                 }
1463                 return;
1464             } else if (destsa->next && c->state == STATE_CONNECTING
1465                        && c->read == 0 && good == 0) {
1466                 return;
1467             }
1468             else {
1469                 err_recv++;
1470                 apr_err("apr_socket_recv", status);
1471             }
1472         }
1473     }
1474
1475     totalread += r;
1476     if (c->read == 0) {
1477         c->beginread = apr_time_now();
1478     }
1479     c->read += r;
1480
1481
1482     if (!c->gotheader) {
1483         char *s;
1484         int l = 4;
1485         apr_size_t space = CBUFFSIZE - c->cbx - 1; /* -1 allows for \0 term */
1486         int tocopy = (space < r) ? space : r;
1487 #ifdef NOT_ASCII
1488         apr_size_t inbytes_left = space, outbytes_left = space;
1489
1490         status = apr_xlate_conv_buffer(from_ascii, buffer, &inbytes_left,
1491                            c->cbuff + c->cbx, &outbytes_left);
1492         if (status || inbytes_left || outbytes_left) {
1493             fprintf(stderr, "only simple translation is supported (%d/%" APR_SIZE_T_FMT
1494                             "/%" APR_SIZE_T_FMT ")\n", status, inbytes_left, outbytes_left);
1495             exit(1);
1496         }
1497 #else
1498         memcpy(c->cbuff + c->cbx, buffer, space);
1499 #endif              /* NOT_ASCII */
1500         c->cbx += tocopy;
1501         space -= tocopy;
1502         c->cbuff[c->cbx] = 0;   /* terminate for benefit of strstr */
1503         if (verbosity >= 2) {
1504             printf("LOG: header received:\n%s\n", c->cbuff);
1505         }
1506         s = strstr(c->cbuff, "\r\n\r\n");
1507         /*
1508          * this next line is so that we talk to NCSA 1.5 which blatantly
1509          * breaks the http specifaction
1510          */
1511         if (!s) {
1512             s = strstr(c->cbuff, "\n\n");
1513             l = 2;
1514         }
1515
1516         if (!s) {
1517             /* read rest next time */
1518             if (space) {
1519                 return;
1520             }
1521             else {
1522             /* header is in invalid or too big - close connection */
1523                 set_conn_state(c, STATE_UNCONNECTED);
1524                 apr_socket_close(c->aprsock);
1525                 err_response++;
1526                 if (bad++ > 10) {
1527                     err("\nTest aborted after 10 failures\n\n");
1528                 }
1529                 start_connect(c);
1530             }
1531         }
1532         else {
1533             /* have full header */
1534             if (!good) {
1535                 /*
1536                  * this is first time, extract some interesting info
1537                  */
1538                 char *p, *q;
1539                 size_t len = 0;
1540                 p = strstr(c->cbuff, "Server:");
1541                 q = servername;
1542                 if (p) {
1543                     p += 8;
1544                     /* -1 to not overwrite last '\0' byte */
1545                     while (*p > 32 && len++ < sizeof(servername) - 1)
1546                         *q++ = *p++;
1547                 }
1548                 *q = 0;
1549             }
1550             /*
1551              * XXX: this parsing isn't even remotely HTTP compliant... but in
1552              * the interest of speed it doesn't totally have to be, it just
1553              * needs to be extended to handle whatever servers folks want to
1554              * test against. -djg
1555              */
1556
1557             /* check response code */
1558             part = strstr(c->cbuff, "HTTP");    /* really HTTP/1.x_ */
1559             if (part && strlen(part) > strlen("HTTP/1.x_")) {
1560                 strncpy(respcode, (part + strlen("HTTP/1.x_")), 3);
1561                 respcode[3] = '\0';
1562             }
1563             else {
1564                 strcpy(respcode, "500");
1565             }
1566
1567             if (respcode[0] != '2') {
1568                 err_response++;
1569                 if (verbosity >= 2)
1570                     printf("WARNING: Response code not 2xx (%s)\n", respcode);
1571             }
1572             else if (verbosity >= 3) {
1573                 printf("LOG: Response code = %s\n", respcode);
1574             }
1575             c->gotheader = 1;
1576             *s = 0;     /* terminate at end of header */
1577             if (keepalive &&
1578             (strstr(c->cbuff, "Keep-Alive")
1579              || strstr(c->cbuff, "keep-alive"))) {  /* for benefit of MSIIS */
1580                 char *cl;
1581                 cl = strstr(c->cbuff, "Content-Length:");
1582                 /* handle NCSA, which sends Content-length: */
1583                 if (!cl)
1584                     cl = strstr(c->cbuff, "Content-length:");
1585                 if (cl) {
1586                     c->keepalive = 1;
1587                     /* response to HEAD doesn't have entity body */
1588                     c->length = method != HEAD ? atoi(cl + 16) : 0;
1589                 }
1590                 /* The response may not have a Content-Length header */
1591                 if (!cl) {
1592                     c->keepalive = 1;
1593                     c->length = 0;
1594                 }
1595             }
1596             c->bread += c->cbx - (s + l - c->cbuff) + r - tocopy;
1597             totalbread += c->bread;
1598
1599             /* We have received the header, so we know this destination socket
1600              * address is working, so initialize all remaining requests. */
1601             if (!requests_initialized) {
1602                 for (i = 1; i < concurrency; i++) {
1603                     con[i].socknum = i;
1604                     start_connect(&con[i]);
1605                 }
1606                 requests_initialized = 1;
1607             }
1608         }
1609     }
1610     else {
1611         /* outside header, everything we have read is entity body */
1612         c->bread += r;
1613         totalbread += r;
1614     }
1615
1616     if (c->keepalive && (c->bread >= c->length)) {
1617         /* finished a keep-alive connection */
1618         good++;
1619         /* save out time */
1620         if (good == 1) {
1621             /* first time here */
1622             doclen = c->bread;
1623         }
1624         else if ((c->bread != doclen) && !nolength) {
1625             bad++;
1626             err_length++;
1627         }
1628         if (done < requests) {
1629             struct data *s = &stats[done++];
1630             doneka++;
1631             c->done      = apr_time_now();
1632             s->starttime = c->start;
1633             s->ctime     = ap_max(0, c->connect - c->start);
1634             s->time      = ap_max(0, c->done - c->start);
1635             s->waittime  = ap_max(0, c->beginread - c->endwrite);
1636             if (heartbeatres && !(done % heartbeatres)) {
1637                 fprintf(stderr, "Completed %d requests\n", done);
1638                 fflush(stderr);
1639             }
1640         }
1641         c->keepalive = 0;
1642         c->length = 0;
1643         c->gotheader = 0;
1644         c->cbx = 0;
1645         c->read = c->bread = 0;
1646         /* zero connect time with keep-alive */
1647         c->start = c->connect = lasttime = apr_time_now();
1648         write_request(c);
1649     }
1650 }
1651
1652 /* --------------------------------------------------------- */
1653
1654 /* run the tests */
1655
1656 static void test(void)
1657 {
1658     apr_time_t stoptime;
1659     apr_int16_t rtnev;
1660     apr_status_t rv;
1661     int i;
1662     apr_status_t status;
1663     int snprintf_res = 0;
1664 #ifdef NOT_ASCII
1665     apr_size_t inbytes_left, outbytes_left;
1666 #endif
1667
1668     if (isproxy) {
1669         connecthost = apr_pstrdup(cntxt, proxyhost);
1670         connectport = proxyport;
1671     }
1672     else {
1673         connecthost = apr_pstrdup(cntxt, hostname);
1674         connectport = port;
1675     }
1676
1677     if (!use_html) {
1678         printf("Benchmarking %s ", hostname);
1679     if (isproxy)
1680         printf("[through %s:%d] ", proxyhost, proxyport);
1681     printf("(be patient)%s",
1682            (heartbeatres ? "\n" : "..."));
1683     fflush(stdout);
1684     }
1685
1686     con = xcalloc(concurrency, sizeof(struct connection));
1687
1688     /*
1689      * XXX: a way to calculate the stats without requiring O(requests) memory
1690      * XXX: would be nice.
1691      */
1692     stats = xcalloc(requests, sizeof(struct data));
1693
1694     if ((status = apr_pollset_create(&readbits, concurrency, cntxt,
1695                                      APR_POLLSET_NOCOPY)) != APR_SUCCESS) {
1696         apr_err("apr_pollset_create failed", status);
1697     }
1698
1699     /* add default headers if necessary */
1700     if (!opt_host) {
1701         /* Host: header not overridden, add default value to hdrs */
1702         hdrs = apr_pstrcat(cntxt, hdrs, "Host: ", host_field, colonhost, "\r\n", NULL);
1703     }
1704     else {
1705         /* Header overridden, no need to add, as it is already in hdrs */
1706     }
1707
1708     if (!opt_useragent) {
1709         /* User-Agent: header not overridden, add default value to hdrs */
1710         hdrs = apr_pstrcat(cntxt, hdrs, "User-Agent: ApacheBench/", AP_AB_BASEREVISION, "\r\n", NULL);
1711     }
1712     else {
1713         /* Header overridden, no need to add, as it is already in hdrs */
1714     }
1715
1716     if (!opt_accept) {
1717         /* Accept: header not overridden, add default value to hdrs */
1718         hdrs = apr_pstrcat(cntxt, hdrs, "Accept: */*\r\n", NULL);
1719     }
1720     else {
1721         /* Header overridden, no need to add, as it is already in hdrs */
1722     }
1723
1724     /* setup request */
1725     if (!send_body) {
1726         snprintf_res = apr_snprintf(request, sizeof(_request),
1727             "%s %s HTTP/1.0\r\n"
1728             "%s" "%s" "%s"
1729             "%s" "\r\n",
1730             method_str[method],
1731             (isproxy) ? fullurl : path,
1732             keepalive ? "Connection: Keep-Alive\r\n" : "",
1733             cookie, auth, hdrs);
1734     }
1735     else {
1736         snprintf_res = apr_snprintf(request,  sizeof(_request),
1737             "%s %s HTTP/1.0\r\n"
1738             "%s" "%s" "%s"
1739             "Content-length: %" APR_SIZE_T_FMT "\r\n"
1740             "Content-type: %s\r\n"
1741             "%s"
1742             "\r\n",
1743             method_str[method],
1744             (isproxy) ? fullurl : path,
1745             keepalive ? "Connection: Keep-Alive\r\n" : "",
1746             cookie, auth,
1747             postlen,
1748             (content_type != NULL) ? content_type : "text/plain", hdrs);
1749     }
1750     if (snprintf_res >= sizeof(_request)) {
1751         err("Request too long\n");
1752     }
1753
1754     if (verbosity >= 2)
1755         printf("INFO: %s header == \n---\n%s\n---\n",
1756                method_str[method], request);
1757
1758     reqlen = strlen(request);
1759
1760     /*
1761      * Combine headers and (optional) post file into one continuous buffer
1762      */
1763     if (send_body) {
1764         char *buff = xmalloc(postlen + reqlen + 1);
1765         strcpy(buff, request);
1766         memcpy(buff + reqlen, postdata, postlen);
1767         request = buff;
1768     }
1769
1770 #ifdef NOT_ASCII
1771     inbytes_left = outbytes_left = reqlen;
1772     status = apr_xlate_conv_buffer(to_ascii, request, &inbytes_left,
1773                    request, &outbytes_left);
1774     if (status || inbytes_left || outbytes_left) {
1775         fprintf(stderr, "only simple translation is supported (%d/%"
1776                         APR_SIZE_T_FMT "/%" APR_SIZE_T_FMT ")\n",
1777                         status, inbytes_left, outbytes_left);
1778         exit(1);
1779     }
1780 #endif              /* NOT_ASCII */
1781
1782     if (myhost) {
1783         /* This only needs to be done once */
1784         if ((rv = apr_sockaddr_info_get(&mysa, myhost, APR_UNSPEC, 0, 0, cntxt)) != APR_SUCCESS) {
1785             char buf[120];
1786             apr_snprintf(buf, sizeof(buf),
1787                          "apr_sockaddr_info_get() for %s", myhost);
1788             apr_err(buf, rv);
1789         }
1790     }
1791
1792     /* This too */
1793     if ((rv = apr_sockaddr_info_get(&destsa, connecthost,
1794                                     myhost ? mysa->family : APR_UNSPEC,
1795                                     connectport, 0, cntxt))
1796        != APR_SUCCESS) {
1797         char buf[120];
1798         apr_snprintf(buf, sizeof(buf),
1799                  "apr_sockaddr_info_get() for %s", connecthost);
1800         apr_err(buf, rv);
1801     }
1802
1803     /* ok - lets start */
1804     start = lasttime = apr_time_now();
1805     stoptime = tlimit ? (start + apr_time_from_sec(tlimit)) : AB_MAX;
1806
1807 #ifdef SIGINT
1808     /* Output the results if the user terminates the run early. */
1809     apr_signal(SIGINT, output_results);
1810 #endif
1811
1812     /* initialise first connection to determine destination socket address
1813      * which should be used for next connections. */
1814     con[0].socknum = 0;
1815     start_connect(&con[0]);
1816
1817     do {
1818         apr_int32_t n;
1819         const apr_pollfd_t *pollresults, *pollfd;
1820
1821         n = concurrency;
1822         do {
1823             status = apr_pollset_poll(readbits, aprtimeout, &n, &pollresults);
1824         } while (APR_STATUS_IS_EINTR(status));
1825         if (status != APR_SUCCESS)
1826             apr_err("apr_pollset_poll", status);
1827
1828         for (i = 0, pollfd = pollresults; i < n; i++, pollfd++) {
1829             struct connection *c;
1830
1831             c = pollfd->client_data;
1832
1833             /*
1834              * If the connection isn't connected how can we check it?
1835              */
1836             if (c->state == STATE_UNCONNECTED)
1837                 continue;
1838
1839             rtnev = pollfd->rtnevents;
1840
1841 #ifdef USE_SSL
1842             if (c->state == STATE_CONNECTED && c->ssl && SSL_in_init(c->ssl)) {
1843                 ssl_proceed_handshake(c);
1844                 continue;
1845             }
1846 #endif
1847
1848             /*
1849              * Notes: APR_POLLHUP is set after FIN is received on some
1850              * systems, so treat that like APR_POLLIN so that we try to read
1851              * again.
1852              *
1853              * Some systems return APR_POLLERR with APR_POLLHUP.  We need to
1854              * call read_connection() for APR_POLLHUP, so check for
1855              * APR_POLLHUP first so that a closed connection isn't treated
1856              * like an I/O error.  If it is, we never figure out that the
1857              * connection is done and we loop here endlessly calling
1858              * apr_poll().
1859              */
1860             if ((rtnev & APR_POLLIN) || (rtnev & APR_POLLPRI) || (rtnev & APR_POLLHUP))
1861                 read_connection(c);
1862             if ((rtnev & APR_POLLERR) || (rtnev & APR_POLLNVAL)) {
1863                 if (destsa->next && c->state == STATE_CONNECTING && good == 0) {
1864                     destsa = destsa->next;
1865                     start_connect(c);
1866                 }
1867                 else {
1868                     bad++;
1869                     err_except++;
1870                     /* avoid apr_poll/EINPROGRESS loop on HP-UX, let recv discover ECONNREFUSED */
1871                     if (c->state == STATE_CONNECTING) {
1872                         read_connection(c);
1873                     }
1874                     else {
1875                         start_connect(c);
1876                     }
1877                 }
1878                 continue;
1879             }
1880             if (rtnev & APR_POLLOUT) {
1881                 if (c->state == STATE_CONNECTING) {
1882                     rv = apr_socket_connect(c->aprsock, destsa);
1883                     if (rv != APR_SUCCESS) {
1884                         set_conn_state(c, STATE_UNCONNECTED);
1885                         apr_socket_close(c->aprsock);
1886                         err_conn++;
1887                         if (bad++ > 10) {
1888                             fprintf(stderr,
1889                                     "\nTest aborted after 10 failures\n\n");
1890                             apr_err("apr_socket_connect()", rv);
1891                         }
1892                         start_connect(c);
1893                         continue;
1894                     }
1895                     else {
1896                         set_conn_state(c, STATE_CONNECTED);
1897 #ifdef USE_SSL
1898                         if (c->ssl)
1899                             ssl_proceed_handshake(c);
1900                         else
1901 #endif
1902                         write_request(c);
1903                     }
1904                 }
1905                 else {
1906                     write_request(c);
1907                 }
1908             }
1909         }
1910     } while (lasttime < stoptime && done < requests);
1911
1912     if (heartbeatres)
1913         fprintf(stderr, "Finished %d requests\n", done);
1914     else
1915         printf("..done\n");
1916
1917     if (use_html)
1918         output_html_results();
1919     else
1920         output_results(0);
1921 }
1922
1923 /* ------------------------------------------------------- */
1924
1925 /* display copyright information */
1926 static void copyright(void)
1927 {
1928     if (!use_html) {
1929         printf("This is ApacheBench, Version %s\n", AP_AB_BASEREVISION " <$Revision$>");
1930         printf("Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/\n");
1931         printf("Licensed to The Apache Software Foundation, http://www.apache.org/\n");
1932         printf("\n");
1933     }
1934     else {
1935         printf("<p>\n");
1936         printf(" This is ApacheBench, Version %s <i>&lt;%s&gt;</i><br>\n", AP_AB_BASEREVISION, "$Revision$");
1937         printf(" Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/<br>\n");
1938         printf(" Licensed to The Apache Software Foundation, http://www.apache.org/<br>\n");
1939         printf("</p>\n<p>\n");
1940     }
1941 }
1942
1943 /* display usage information */
1944 static void usage(const char *progname)
1945 {
1946     fprintf(stderr, "Usage: %s [options] [http"
1947 #ifdef USE_SSL
1948         "[s]"
1949 #endif
1950         "://]hostname[:port]/path\n", progname);
1951 /* 80 column ruler:  ********************************************************************************
1952  */
1953     fprintf(stderr, "Options are:\n");
1954     fprintf(stderr, "    -n requests     Number of requests to perform\n");
1955     fprintf(stderr, "    -c concurrency  Number of multiple requests to make at a time\n");
1956     fprintf(stderr, "    -t timelimit    Seconds to max. to spend on benchmarking\n");
1957     fprintf(stderr, "                    This implies -n 50000\n");
1958     fprintf(stderr, "    -s timeout      Seconds to max. wait for each response\n");
1959     fprintf(stderr, "                    Default is 30 seconds\n");
1960     fprintf(stderr, "    -b windowsize   Size of TCP send/receive buffer, in bytes\n");
1961     fprintf(stderr, "    -B address      Address to bind to when making outgoing connections\n");
1962     fprintf(stderr, "    -p postfile     File containing data to POST. Remember also to set -T\n");
1963     fprintf(stderr, "    -u putfile      File containing data to PUT. Remember also to set -T\n");
1964     fprintf(stderr, "    -T content-type Content-type header to use for POST/PUT data, eg.\n");
1965     fprintf(stderr, "                    'application/x-www-form-urlencoded'\n");
1966     fprintf(stderr, "                    Default is 'text/plain'\n");
1967     fprintf(stderr, "    -v verbosity    How much troubleshooting info to print\n");
1968     fprintf(stderr, "    -w              Print out results in HTML tables\n");
1969     fprintf(stderr, "    -i              Use HEAD instead of GET\n");
1970     fprintf(stderr, "    -x attributes   String to insert as table attributes\n");
1971     fprintf(stderr, "    -y attributes   String to insert as tr attributes\n");
1972     fprintf(stderr, "    -z attributes   String to insert as td or th attributes\n");
1973     fprintf(stderr, "    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)\n");
1974     fprintf(stderr, "    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'\n");
1975     fprintf(stderr, "                    Inserted after all normal header lines. (repeatable)\n");
1976     fprintf(stderr, "    -A attribute    Add Basic WWW Authentication, the attributes\n");
1977     fprintf(stderr, "                    are a colon separated username and password.\n");
1978     fprintf(stderr, "    -P attribute    Add Basic Proxy Authentication, the attributes\n");
1979     fprintf(stderr, "                    are a colon separated username and password.\n");
1980     fprintf(stderr, "    -X proxy:port   Proxyserver and port number to use\n");
1981     fprintf(stderr, "    -V              Print version number and exit\n");
1982     fprintf(stderr, "    -k              Use HTTP KeepAlive feature\n");
1983     fprintf(stderr, "    -d              Do not show percentiles served table.\n");
1984     fprintf(stderr, "    -S              Do not show confidence estimators and warnings.\n");
1985     fprintf(stderr, "    -q              Do not show progress when doing more than 150 requests\n");
1986     fprintf(stderr, "    -l              Accept variable document length (use this for dynamic pages)\n");
1987     fprintf(stderr, "    -g filename     Output collected data to gnuplot format file.\n");
1988     fprintf(stderr, "    -e filename     Output CSV file with percentages served\n");
1989     fprintf(stderr, "    -r              Don't exit on socket receive errors.\n");
1990     fprintf(stderr, "    -m method       Method name\n");
1991     fprintf(stderr, "    -h              Display usage information (this message)\n");
1992 #ifdef USE_SSL
1993
1994 #ifndef OPENSSL_NO_SSL2
1995 #define SSL2_HELP_MSG "SSL2, "
1996 #else
1997 #define SSL2_HELP_MSG ""
1998 #endif
1999
2000 #ifndef OPENSSL_NO_SSL3
2001 #define SSL3_HELP_MSG "SSL3, "
2002 #else
2003 #define SSL3_HELP_MSG ""
2004 #endif
2005
2006 #ifdef HAVE_TLSV1_X
2007 #define TLS1_X_HELP_MSG ", TLS1.1, TLS1.2"
2008 #else
2009 #define TLS1_X_HELP_MSG ""
2010 #endif
2011
2012     fprintf(stderr, "    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)\n");
2013     fprintf(stderr, "    -f protocol     Specify SSL/TLS protocol\n");
2014     fprintf(stderr, "                    (" SSL2_HELP_MSG SSL3_HELP_MSG "TLS1" TLS1_X_HELP_MSG " or ALL)\n");
2015 #endif
2016     exit(EINVAL);
2017 }
2018
2019 /* ------------------------------------------------------- */
2020
2021 /* split URL into parts */
2022
2023 static int parse_url(const char *url)
2024 {
2025     char *cp;
2026     char *h;
2027     char *scope_id;
2028     apr_status_t rv;
2029
2030     /* Save a copy for the proxy */
2031     fullurl = apr_pstrdup(cntxt, url);
2032
2033     if (strlen(url) > 7 && strncmp(url, "http://", 7) == 0) {
2034         url += 7;
2035 #ifdef USE_SSL
2036         is_ssl = 0;
2037 #endif
2038     }
2039     else
2040 #ifdef USE_SSL
2041     if (strlen(url) > 8 && strncmp(url, "https://", 8) == 0) {
2042         url += 8;
2043         is_ssl = 1;
2044     }
2045 #else
2046     if (strlen(url) > 8 && strncmp(url, "https://", 8) == 0) {
2047         fprintf(stderr, "SSL not compiled in; no https support\n");
2048         exit(1);
2049     }
2050 #endif
2051
2052     if ((cp = strchr(url, '/')) == NULL)
2053         return 1;
2054     h = apr_pstrmemdup(cntxt, url, cp - url);
2055     rv = apr_parse_addr_port(&hostname, &scope_id, &port, h, cntxt);
2056     if (rv != APR_SUCCESS || !hostname || scope_id) {
2057         return 1;
2058     }
2059     path = apr_pstrdup(cntxt, cp);
2060     *cp = '\0';
2061     if (*url == '[') {      /* IPv6 numeric address string */
2062         host_field = apr_psprintf(cntxt, "[%s]", hostname);
2063     }
2064     else {
2065         host_field = hostname;
2066     }
2067
2068     if (port == 0) {        /* no port specified */
2069 #ifdef USE_SSL
2070         if (is_ssl)
2071             port = 443;
2072         else
2073 #endif
2074         port = 80;
2075     }
2076
2077     if ((
2078 #ifdef USE_SSL
2079          is_ssl && (port != 443)) || (!is_ssl &&
2080 #endif
2081          (port != 80)))
2082     {
2083         colonhost = apr_psprintf(cntxt,":%d",port);
2084     } else
2085         colonhost = "";
2086     return 0;
2087 }
2088
2089 /* ------------------------------------------------------- */
2090
2091 /* read data to POST/PUT from file, save contents and length */
2092
2093 static apr_status_t open_postfile(const char *pfile)
2094 {
2095     apr_file_t *postfd;
2096     apr_finfo_t finfo;
2097     apr_status_t rv;
2098     char errmsg[120];
2099
2100     rv = apr_file_open(&postfd, pfile, APR_READ, APR_OS_DEFAULT, cntxt);
2101     if (rv != APR_SUCCESS) {
2102         fprintf(stderr, "ab: Could not open POST data file (%s): %s\n", pfile,
2103                 apr_strerror(rv, errmsg, sizeof errmsg));
2104         return rv;
2105     }
2106
2107     rv = apr_file_info_get(&finfo, APR_FINFO_NORM, postfd);
2108     if (rv != APR_SUCCESS) {
2109         fprintf(stderr, "ab: Could not stat POST data file (%s): %s\n", pfile,
2110                 apr_strerror(rv, errmsg, sizeof errmsg));
2111         return rv;
2112     }
2113     postlen = (apr_size_t)finfo.size;
2114     postdata = xmalloc(postlen);
2115     rv = apr_file_read_full(postfd, postdata, postlen, NULL);
2116     if (rv != APR_SUCCESS) {
2117         fprintf(stderr, "ab: Could not read POST data file: %s\n",
2118                 apr_strerror(rv, errmsg, sizeof errmsg));
2119         return rv;
2120     }
2121     apr_file_close(postfd);
2122     return APR_SUCCESS;
2123 }
2124
2125 /* ------------------------------------------------------- */
2126
2127 /* sort out command-line args and call test */
2128 int main(int argc, const char * const argv[])
2129 {
2130     int l;
2131     char tmp[1024];
2132     apr_status_t status;
2133     apr_getopt_t *opt;
2134     const char *opt_arg;
2135     char c;
2136 #ifdef USE_SSL
2137     AB_SSL_METHOD_CONST SSL_METHOD *meth = SSLv23_client_method();
2138 #endif
2139
2140     /* table defaults  */
2141     tablestring = "";
2142     trstring = "";
2143     tdstring = "bgcolor=white";
2144     cookie = "";
2145     auth = "";
2146     proxyhost = "";
2147     hdrs = "";
2148
2149     apr_app_initialize(&argc, &argv, NULL);
2150     atexit(apr_terminate);
2151     apr_pool_create(&cntxt, NULL);
2152     apr_pool_abort_set(abort_on_oom, cntxt);
2153
2154 #ifdef NOT_ASCII
2155     status = apr_xlate_open(&to_ascii, "ISO-8859-1", APR_DEFAULT_CHARSET, cntxt);
2156     if (status) {
2157         fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", status);
2158         exit(1);
2159     }
2160     status = apr_xlate_open(&from_ascii, APR_DEFAULT_CHARSET, "ISO-8859-1", cntxt);
2161     if (status) {
2162         fprintf(stderr, "apr_xlate_open(from ASCII)->%d\n", status);
2163         exit(1);
2164     }
2165     status = apr_base64init_ebcdic(to_ascii, from_ascii);
2166     if (status) {
2167         fprintf(stderr, "apr_base64init_ebcdic()->%d\n", status);
2168         exit(1);
2169     }
2170 #endif
2171
2172     myhost = NULL; /* 0.0.0.0 or :: */
2173
2174     apr_getopt_init(&opt, cntxt, argc, argv);
2175     while ((status = apr_getopt(opt, "n:c:t:s:b:T:p:u:v:lrkVhwix:y:z:C:H:P:A:g:X:de:SqB:m:"
2176 #ifdef USE_SSL
2177             "Z:f:"
2178 #endif
2179             ,&c, &opt_arg)) == APR_SUCCESS) {
2180         switch (c) {
2181             case 'n':
2182                 requests = atoi(opt_arg);
2183                 if (requests <= 0) {
2184                     err("Invalid number of requests\n");
2185                 }
2186                 break;
2187             case 'k':
2188                 keepalive = 1;
2189                 break;
2190             case 'q':
2191                 heartbeatres = 0;
2192                 break;
2193             case 'c':
2194                 concurrency = atoi(opt_arg);
2195                 break;
2196             case 'b':
2197                 windowsize = atoi(opt_arg);
2198                 break;
2199             case 'i':
2200                 if (method != NO_METH)
2201                     err("Cannot mix HEAD with other methods\n");
2202                 method = HEAD;
2203                 break;
2204             case 'g':
2205                 gnuplot = xstrdup(opt_arg);
2206                 break;
2207             case 'd':
2208                 percentile = 0;
2209                 break;
2210             case 'e':
2211                 csvperc = xstrdup(opt_arg);
2212                 break;
2213             case 'S':
2214                 confidence = 0;
2215                 break;
2216             case 's':
2217                 aprtimeout = apr_time_from_sec(atoi(opt_arg)); /* timeout value */
2218                 break;
2219             case 'p':
2220                 if (method != NO_METH)
2221                     err("Cannot mix POST with other methods\n");
2222                 if (open_postfile(opt_arg) != APR_SUCCESS) {
2223                     exit(1);
2224                 }
2225                 method = POST;
2226                 send_body = 1;
2227                 break;
2228             case 'u':
2229                 if (method != NO_METH)
2230                     err("Cannot mix PUT with other methods\n");
2231                 if (open_postfile(opt_arg) != APR_SUCCESS) {
2232                     exit(1);
2233                 }
2234                 method = PUT;
2235                 send_body = 1;
2236                 break;
2237             case 'l':
2238                 nolength = 1;
2239                 break;
2240             case 'r':
2241                 recverrok = 1;
2242                 break;
2243             case 'v':
2244                 verbosity = atoi(opt_arg);
2245                 break;
2246             case 't':
2247                 tlimit = atoi(opt_arg);
2248                 requests = MAX_REQUESTS;    /* need to size data array on
2249                                              * something */
2250                 break;
2251             case 'T':
2252                 content_type = apr_pstrdup(cntxt, opt_arg);
2253                 break;
2254             case 'C':
2255                 cookie = apr_pstrcat(cntxt, "Cookie: ", opt_arg, "\r\n", NULL);
2256                 break;
2257             case 'A':
2258                 /*
2259                  * assume username passwd already to be in colon separated form.
2260                  * Ready to be uu-encoded.
2261                  */
2262                 while (apr_isspace(*opt_arg))
2263                     opt_arg++;
2264                 if (apr_base64_encode_len(strlen(opt_arg)) > sizeof(tmp)) {
2265                     err("Authentication credentials too long\n");
2266                 }
2267                 l = apr_base64_encode(tmp, opt_arg, strlen(opt_arg));
2268                 tmp[l] = '\0';
2269
2270                 auth = apr_pstrcat(cntxt, auth, "Authorization: Basic ", tmp,
2271                                        "\r\n", NULL);
2272                 break;
2273             case 'P':
2274                 /*
2275                  * assume username passwd already to be in colon separated form.
2276                  */
2277                 while (apr_isspace(*opt_arg))
2278                 opt_arg++;
2279                 if (apr_base64_encode_len(strlen(opt_arg)) > sizeof(tmp)) {
2280                     err("Proxy credentials too long\n");
2281                 }
2282                 l = apr_base64_encode(tmp, opt_arg, strlen(opt_arg));
2283                 tmp[l] = '\0';
2284
2285                 auth = apr_pstrcat(cntxt, auth, "Proxy-Authorization: Basic ",
2286                                        tmp, "\r\n", NULL);
2287                 break;
2288             case 'H':
2289                 hdrs = apr_pstrcat(cntxt, hdrs, opt_arg, "\r\n", NULL);
2290                 /*
2291                  * allow override of some of the common headers that ab adds
2292                  */
2293                 if (strncasecmp(opt_arg, "Host:", 5) == 0) {
2294                     opt_host = 1;
2295                 } else if (strncasecmp(opt_arg, "Accept:", 7) == 0) {
2296                     opt_accept = 1;
2297                 } else if (strncasecmp(opt_arg, "User-Agent:", 11) == 0) {
2298                     opt_useragent = 1;
2299                 }
2300                 break;
2301             case 'w':
2302                 use_html = 1;
2303                 break;
2304                 /*
2305                  * if any of the following three are used, turn on html output
2306                  * automatically
2307                  */
2308             case 'x':
2309                 use_html = 1;
2310                 tablestring = opt_arg;
2311                 break;
2312             case 'X':
2313                 {
2314                     char *p;
2315                     /*
2316                      * assume proxy-name[:port]
2317                      */
2318                     if ((p = strchr(opt_arg, ':'))) {
2319                         *p = '\0';
2320                         p++;
2321                         proxyport = atoi(p);
2322                     }
2323                     proxyhost = apr_pstrdup(cntxt, opt_arg);
2324                     isproxy = 1;
2325                 }
2326                 break;
2327             case 'y':
2328                 use_html = 1;
2329                 trstring = opt_arg;
2330                 break;
2331             case 'z':
2332                 use_html = 1;
2333                 tdstring = opt_arg;
2334                 break;
2335             case 'h':
2336                 usage(argv[0]);
2337                 break;
2338             case 'V':
2339                 copyright();
2340                 return 0;
2341             case 'B':
2342                 myhost = apr_pstrdup(cntxt, opt_arg);
2343                 break;
2344 #ifdef USE_SSL
2345             case 'Z':
2346                 ssl_cipher = strdup(opt_arg);
2347                 break;
2348             case 'm':
2349                 method = CUSTOM_METHOD;
2350                 method_str[CUSTOM_METHOD] = strdup(opt_arg);
2351                 break;
2352             case 'f':
2353                 if (strncasecmp(opt_arg, "ALL", 3) == 0) {
2354                     meth = SSLv23_client_method();
2355 #ifndef OPENSSL_NO_SSL2
2356                 } else if (strncasecmp(opt_arg, "SSL2", 4) == 0) {
2357                     meth = SSLv2_client_method();
2358 #endif
2359 #ifndef OPENSSL_NO_SSL3
2360                 } else if (strncasecmp(opt_arg, "SSL3", 4) == 0) {
2361                     meth = SSLv3_client_method();
2362 #endif
2363 #ifdef HAVE_TLSV1_X
2364                 } else if (strncasecmp(opt_arg, "TLS1.1", 6) == 0) {
2365                     meth = TLSv1_1_client_method();
2366                 } else if (strncasecmp(opt_arg, "TLS1.2", 6) == 0) {
2367                     meth = TLSv1_2_client_method();
2368 #endif
2369                 } else if (strncasecmp(opt_arg, "TLS1", 4) == 0) {
2370                     meth = TLSv1_client_method();
2371                 }
2372                 break;
2373 #endif
2374         }
2375     }
2376
2377     if (opt->ind != argc - 1) {
2378         fprintf(stderr, "%s: wrong number of arguments\n", argv[0]);
2379         usage(argv[0]);
2380     }
2381
2382     if (method == NO_METH) {
2383         method = GET;
2384     }
2385
2386     if (parse_url(apr_pstrdup(cntxt, opt->argv[opt->ind++]))) {
2387         fprintf(stderr, "%s: invalid URL\n", argv[0]);
2388         usage(argv[0]);
2389     }
2390
2391     if ((concurrency < 0) || (concurrency > MAX_CONCURRENCY)) {
2392         fprintf(stderr, "%s: Invalid Concurrency [Range 0..%d]\n",
2393                 argv[0], MAX_CONCURRENCY);
2394         usage(argv[0]);
2395     }
2396
2397     if (concurrency > requests) {
2398         fprintf(stderr, "%s: Cannot use concurrency level greater than "
2399                 "total number of requests\n", argv[0]);
2400         usage(argv[0]);
2401     }
2402
2403     if ((heartbeatres) && (requests > 150)) {
2404         heartbeatres = requests / 10;   /* Print line every 10% of requests */
2405         if (heartbeatres < 100)
2406             heartbeatres = 100; /* but never more often than once every 100
2407                                  * connections. */
2408     }
2409     else
2410         heartbeatres = 0;
2411
2412 #ifdef USE_SSL
2413 #ifdef RSAREF
2414     R_malloc_init();
2415 #else
2416     CRYPTO_malloc_init();
2417 #endif
2418     SSL_load_error_strings();
2419     SSL_library_init();
2420     bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
2421     bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
2422
2423     if (!(ssl_ctx = SSL_CTX_new(meth))) {
2424         BIO_printf(bio_err, "Could not initialize SSL Context.\n");
2425         ERR_print_errors(bio_err);
2426         exit(1);
2427     }
2428     SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL);
2429 #ifdef SSL_MODE_RELEASE_BUFFERS
2430     /* Keep memory usage as low as possible */
2431     SSL_CTX_set_mode (ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
2432 #endif
2433     if (ssl_cipher != NULL) {
2434         if (!SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher)) {
2435             fprintf(stderr, "error setting cipher list [%s]\n", ssl_cipher);
2436         ERR_print_errors_fp(stderr);
2437         exit(1);
2438     }
2439     }
2440     if (verbosity >= 3) {
2441         SSL_CTX_set_info_callback(ssl_ctx, ssl_state_cb);
2442     }
2443 #endif
2444 #ifdef SIGPIPE
2445     apr_signal(SIGPIPE, SIG_IGN);       /* Ignore writes to connections that
2446                                          * have been closed at the other end. */
2447 #endif
2448     copyright();
2449     test();
2450     apr_pool_destroy(cntxt);
2451
2452     return 0;
2453 }