33 #include <sys/socket.h>
36 #include <netinet/in.h>
39 #include <arpa/inet.h>
54 #define PROXY_PORT "3128"
55 #define PROXY_ADDR "127.0.0.1"
57 #define READ_BUF_SZ 4096
58 #define min(x,y) ((x)<(y)? (x) : (y))
70 static const char *
const crlf =
"\r\n";
74 #define REPLY_HDR_SZ 8192
96 struct tm *gmt = gmtime(t);
98 (void) strftime(buf, 127,
"%A, %d-%b-%y %H:%M:%S GMT", gmt);
106 const char *end =
NULL;
108 p1 = strstr(mime,
"\n\r\n");
109 p2 = strstr(mime,
"\n\n");
112 end = p1 < p2 ? p1 : p2;
116 end += (end == p1 ? 3 : 2);
124 fprintf(stderr,
"\rWaiting for open connections to finish...\n");
125 signal(sig, SIG_DFL);
136 memset(&hints,
'\0',
sizeof(
struct addrinfo));
159 char msg[4096], buf[4096];
164 char *method, *
url, *file, *
size, *checksum;
168 method = strtok(tmp,
" ");
169 url = strtok(
NULL,
" ");
170 file = strtok(
NULL,
" ");
172 checksum = strtok(
NULL,
" ");
177 if (file && strcmp(file,
"-") == 0)
181 if (checksum && strcmp(checksum,
"-") == 0)
184 snprintf(buf,
sizeof(buf)-1,
"%s %s HTTP/1.0\r\n", method, url);
186 strcat(msg,
"Accept: */*\r\n");
187 strcat(msg,
"Proxy-Connection: Keep-Alive\r\n");
188 if (
opt_ims && (lrand48() & 0x03) == 0) {
189 w = time(
NULL) - (lrand48() & 0x3FFFF);
190 snprintf(buf,
sizeof(buf)-1,
"If-Modified-Since: %s\r\n",
mkrfc850(&w));
194 if ((file_fd = open(file, O_RDONLY)) < 0) {
198 if (fstat(file_fd, &st)) {
203 snprintf(buf,
sizeof(buf)-1,
"Content-length: %d\r\n", st.st_size);
208 if (write(fd, msg, len) < 0) {
210 perror(
"request write");
215 while ((len = read(file_fd, buf,
sizeof buf)) > 0) {
216 if (write(fd, buf, len) < 0) {
218 perror(
"body write");
230 r = calloc(1,
sizeof(
struct _r));
249 for (t = buf; t < end; t += strcspn(t,
crlf), t += strspn(t,
crlf)) {
250 if (strncasecmp(t, hdr, strlen(hdr)) == 0) {
264 static char result[8192];
265 for (t = buf; t < end; t += strcspn(t,
crlf), t += strspn(t,
crlf)) {
266 if (strncasecmp(t, hdr, strlen(hdr)) == 0) {
271 strncat(result, t, strcspn(t,
crlf));
282 fprintf(stderr,
"ERROR! Short reply, expected %d bytes got %d\n",
286 fprintf(stderr,
"WARNING: %s Object size mismatch, expected %d got %d\n",
289 fprintf(stderr,
"WARNING: %s Checksum error. Expected %d got %d\n",
308 write(
trace_fd,
"\n[CLOSED]\n", 10);
312 memcpy(buf, inbuf, len);
314 fprintf(stderr,
"WARNING: %s, server closed socket after %d+%d bytes\n", r->
url, r->
hdr_offset, r->
bytes_read);
336 memmove(buf, buf + hlen, len);
345 memmove(buf + blen, buf, len);
355 fprintf(stderr,
"WARNING: %s got reply %s\n", r->
url,
url);
361 fprintf(stderr,
"ERROR!!!\n");
367 int bytes_left, bytes_used;
371 bytes_used = len < bytes_left ? len : bytes_left;
373 bytes_left = len + 1;
377 for (i = 0; i < bytes_used; i++)
378 r->
sum += (
int) buf[i] & 0xFF;
382 if (bytes_left == bytes_used) {
391 memmove(buf, buf + bytes_used, len);
406 perror(
"read reply");
415 static int pconn_fd = -1;
416 static char buf[8192];
418 struct timeval
now,
last, start;
426 gettimeofday(&start,
NULL);
436 fprintf(stderr,
"TERMINATED: Connection closed\n");
448 while ((r = nextr) !=
NULL) {
478 if (fgets(buf, 8191, stdin) ==
NULL) {
479 fprintf(stderr,
"Done Reading URLS\n");
484 if ((t = strchr(buf,
'\n')))
498 FD_SET(pconn_fd, &R);
499 x = select(pconn_fd + 1, &R,
NULL,
NULL, &to);
506 fprintf(stderr,
"TIMEOUT %s; %d, %d\n",
Requests->
url,
510 if (FD_ISSET(pconn_fd, &R)) {
516 if (
now.tv_sec >
last.tv_sec) {
520 dt = (
int) (
now.tv_sec - start.tv_sec);
524 printf(
"T+ %6d: %9d req (%+4d), %4d pend, %3d/sec avg, %dmb, %dkb/sec avg\n",
540 fprintf(stderr,
"usage: %s: -p port -h host -n max -t tracefile -i -c -l lifetime\n",
progname);
549 setbuf(stdout,
NULL);
550 setbuf(stderr,
NULL);
552 while ((c =
getopt(argc, argv,
"p:h:n:t:icl:r")) != -1) {
584 signal(SIGPIPE, SIG_IGN);