-
Notifications
You must be signed in to change notification settings - Fork 112
/
rand-http-reqs
executable file
·423 lines (368 loc) · 9.31 KB
/
rand-http-reqs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env perl
use strict;
use warnings;
use IO::Socket::INET ();
use String::Random ();
use Time::HiRes qw( sleep );
use List::Util qw( shuffle );
use Getopt::Long qw( GetOptions :config no_ignore_case );
use FindBin ();
use File::Spec ();
my $port = 80;
my $host = 'localhost';
my $njobs = 1;
my ($verbose, $help, @custom_headers, $seed, $url_prefix);
my $timeout = 5;
my $num = -1;
GetOptions("concurrency|c=i" => \$njobs,
"header|H=s@" => \@custom_headers,
"help|h" => \$help,
"host=s" => \$host,
"num=i" => \$num,
"port=i" => \$port,
"seed=i" => \$seed,
"timeout=i" => \$timeout,
"prefix=s" => \$url_prefix,
"verbose" => \$verbose) or usage(1);
if ($help) {
usage(0);
}
my ($custom_conn_header, $custom_host_header, $custom_ua_header);
for my $h (@custom_headers) {
if ($h =~ /^\s*connection\s*:/i) {
$custom_conn_header = 1;
}
if ($h =~ /^\s*host\s*:/i) {
$custom_host_header = 1;
}
if ($h =~ /^\s*user-agent\s*:/i) {
$custom_ua_header = 1;
}
}
use Math::Random::Secure;
if (!defined $seed) {
$seed = int(Math::Random::Secure::rand(2**31));
}
#$seed = 0;
#my $seed = 1271164558;
warn "seed: $seed\n";
srand($seed);
my $strgen = String::Random->new;
my $req;
my @ua;
sub load_ua {
my $infile = File::Spec->catfile($FindBin::Bin, "ua.txt");
open my $in, $infile or
die "cannot open $infile for reading: $!\n";
while (<$in>) {
if (/\S/) {
chomp;
push @ua, $_;
}
}
close $in;
}
sub do_connect {
#print "connecting...";
my ($sock, $i);
for ($i = 0; $i < 2000000; $i++) {
$sock = IO::Socket::INET->new(
PeerHost => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => $timeout,
Blocking => 0,
);
if (!$sock) {
warn "failed to connect to $host:$port: $!\n";
sleep 1;
next;
}
return $sock;
}
die "failed to connect. aborted.\n";
}
sub gen_ua {
my $n = int rand 100;
#if ($n == 0) {
#warn "empty ua";
#return '';
#}
#if ($n == 2) {
#warn "no ua";
#return undef;
#}
my $i = int rand scalar @ua;
return $ua[$i];
}
sub gen_header {
my @h;
my $n = int rand 50;
for (my $i = 0; $i < $n; $i++) {
my $len = int(rand 20) + 1;
my $key = $strgen->randregex("[A-Za-z0-9_]{$len}");
$len = int(rand 50) + 1;
my $value = $strgen->randregex('[a-zA-Z0-9 !~^@#/.;:+=*?<>]{' . $len . '}');
push @h, "$key: $value";
}
if (!$custom_host_header) {
push @h, "Host: $host";
}
if (!$custom_ua_header) {
my $ua = gen_ua();
if (defined $ua) {
push @h, "User-Agent: $ua";
}
}
if (!$custom_conn_header) {
push @h, "Connection: Keep-Alive";
}
push @h, @custom_headers;
return join "\r\n", shuffle @h;
}
sub gen_uri_comp {
my $value = '';
my $len = int(rand 20) + 1;
for (my $i = 0; $i < $len; $i++) {
if (int(rand 3) == 0) {
my $c = rand(255) + 1;
$c = sprintf("%.02x", $c);
if (int(rand 2) == 0) {
$value .= '%' . uc($c);
} else {
$value .= '%' . lc($c);
}
} else {
no warnings;
# ._~:/?#[]@!$&'()*+,;=
$value .= $strgen->randregex("[-0-9a-zA-Z._~:/@!,;=+*]");
}
}
$value;
}
sub gen_path {
my $n = int rand 20;
my @comp;
if ($url_prefix) {
push @comp, $url_prefix;
}
for (my $i = 0; $i < $n; $i++) {
my $value = gen_uri_comp();
#warn "uri comp: $value";
push @comp, $value;
}
my $url = join "/", @comp;
if (!$url_prefix) {
return "/" . $url;
}
#warn "$url\n";
return $url;
}
sub gen_args {
my $n = int rand 30;
my @args;
for (my $i = 0; $i < $n; $i++) {
my $key = gen_uri_comp();
my $value = gen_uri_comp();
push @args, "$key=$value";
}
return join "&", @args;
}
sub gen_url {
my $url = gen_path() . "?" . gen_args();
my $max_size = 10*1024;
if (length $url > $max_size) {
$url = substr $url, 0, $max_size;
}
return $url;
}
sub gen_req {
my $url = gen_url();
my $header = gen_header();
my $total = 5;
my $n = int rand $total;
if ($n == 0) {
#return "HEAD $url HTTP/1.1\r\n$header\r\n\r\n";
}
if ($n == 2) {
my $body = gen_args();
my $len = length $body;
return "POST $url HTTP/1.1\r\n$header\r\nContent-Length: $len\r\n\r\n$body";
}
return "GET $url HTTP/1.1\r\n$header\r\n\r\n";
}
sub send_request {
my $sock = shift;
$req = gen_req();
my $rest = length $req;
my $delay = 0.001;
my $waited = 0;
while (1) {
my $bytes = $sock->send($req);
if (!$bytes) {
if ($! =~ /Resource temporarily unavailable/i) {
sleep $delay;
$waited += $delay;
if ($waited >= $timeout) {
warn "Timed out: request: [", $req, "]\n";
return undef;
}
next;
}
warn "failed to send request: $!\n";
return undef;
}
$rest -= $bytes;
if ($rest == 0) {
#warn "request sent";
return 1;
}
sleep $delay;
$waited += $delay;
if ($waited >= $timeout) {
warn "Timed out: request: [", $req, "]\n";
return undef;
}
}
}
sub check_status_code {
my $resp = shift;
my $status;
if ($resp =~ m{^HTTP/1\.\d+\s+(\d+)}) {
$status = $1;
}
if ($status != 200) {
if ($verbose) {
warn "Found status $status: request: [$req] response.\n";
} else {
warn "Found status $status.\n";
}
}
}
sub read_response {
my $sock = shift;
my $resp = '';
my $waited = 0;
my $delay = 0.001;
while (1) {
my $bytes = sysread($sock, my $buf, 1024);
if (!defined $bytes) {
if ($! =~ /Resource temporarily unavailable/i) {
sleep $delay;
$waited += $delay;
if ($waited >= $timeout) {
warn "Timed out: response: [", $resp, "]\n",
"request: [", $req, "]\n";
return undef;
}
next;
}
warn "[", scalar(localtime()), "] Failed to recv: $! ($resp)\n";
return undef;
}
if ($bytes == 0) {
#warn "Connection closed.\n";
return undef;
}
$resp .= $buf;
#warn "received: ", $resp;
if ($resp =~ /^Transfer-Encoding:\s+chunked\r\n/ims) {
if ($resp =~ /^0\r\n\r\n$/sm) {
check_status_code($resp);
return 1;
}
} elsif ($resp =~ /^Content-Length:\s+(\d+)\r\n/ims) {
my $len = $1;
if ($resp =~ /\r\n\r\n(.*)/s) {
my $body = $1;
if (length($body) >= $len) {
check_status_code($resp);
return 1;
}
}
} else {
if ($resp =~ /\r\n\r\n\z/sm) {
check_status_code($resp);
return 1;
}
}
}
}
sub process {
my $job_id = shift;
my $sock = do_connect();
my $i = 0;
while ($num < 0 || $i < $num) {
if (!send_request($sock)) {
close $sock;
$sock = do_connect();
next;
}
if (!read_response($sock)) {
close $sock;
$sock = do_connect();
next;
}
++$i;
print "\r[$job_id] request $i";
}
}
my @child_pids;
sub cleanup {
#warn "CLENAUP!!!";
for my $pid (@child_pids) {
warn "killing child $pid...\n";
kill(9, $pid);
waitpid($pid, 0);
}
};
load_ua();
$| = 1;
#die gen_req();
my $in_child;
for (my $i = 0; $i < $njobs; $i++) {
#warn "forking job $i\n";
my $pid = fork();
if (!defined $pid) {
die "failed to fork: $!\n";
}
if ($pid == 0) {
$in_child = 1;
process($i);
exit();
} else {
push @child_pids, $pid;
}
}
for my $pid (@child_pids) {
waitpid($pid, 0);
}
sub END {
cleanup() unless $in_child;
}
sub usage {
my $fatal = shift;
my $msg = <<_EOC_;
Usage: $0 [options]
Options:
--concurrency <num>
-c <num> Specifies the concurrency level. Default to 1.
-n <num> Specifies the number of requests generated per worker
--header <header>
-H <header> Specifiy extra request headers in the form
"Name: Value". Multiple such options are supported.
--help
-h Print this usage.
--host <host> Specifies the host name for the target server.
Default to localhost.
--port <port>
-p <port> Specifies the target port number. Default to 80.
--verbose
-v More verbose information.
_EOC_
if ($fatal) {
die $msg;
}
print $msg;
exit(0);
}