File: testserver.pl

package info (click to toggle)
libwhisker2-perl 2.4-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 664 kB
  • ctags: 303
  • sloc: perl: 7,262; makefile: 52
file content (534 lines) | stat: -rw-r--r-- 11,531 bytes parent folder | download | duplicates (9)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/perl
#
# testserver.pl - copyright 2006 rain forest puppy
#
# This is a specialized web server emulator used to feed test
# cases to a web client
#

use IO::Socket;
use IO::Select;
$|++;

my $testcasedir = shift || '.';
chdir($testcasedir);

my $insocket = IO::Socket::INET->new(LocalPort => 8088, LocalHost => '127.0.0.1',
					Listen => 20, Proto  => 'tcp', Reuse  => 1);
die $! unless $insocket;

my $readers = IO::Select->new() or die $!;
$readers->add($insocket);

%clients = (); # headersend, content-length, data, # reqs on this socket
%crawler = (); # requests made to crawler handler

while(1){
	my @ready = $readers->can_read;
	foreach my $handle (@ready){
		my $fno = fileno($handle);
		if($fno == fileno($insocket)){
			my $newclient = $handle->accept();
			$clients{ fileno($newclient) } = [-1,-1,'',0];
			$newclient->autoflush(1);
			$readers->add($newclient);
			next;
		}

		my $data;
		my $res = sysread($handle, $data, 2048);
		if(!defined $res || $res==0){
			display_error("Client closed socket before request")
				if($clients{$fno}->[3] == 0);
			$readers->remove($fno);
			eval { $handle->close; };
			delete $clients{$fno};
			next;
		}

		$clients{$fno}->[2] .= $data;
	
		if(precheck_request($handle)){
			handle_request($handle);
		}
	}
}


# figure out if we have enough data to process the request
sub precheck_request {
	my $handle = shift;
	my $fno = fileno($handle);

	return 0 if(!defined $clients{$fno});

	# first, see if we have any data
	return 0 if(length($clients{$fno}->[2])==0);

	# find the end of the headers
	my $headersend = -1;
	if( $clients{$fno}->[0] == -1){
		if( $clients{$fno}->[2] =~ /(\r{0,1}\n\r{0,1}\n)/ ){
			my $endtoken = $1;
			$headersend = index( $clients{$fno}->[2], $endtoken );
			if($headersend == -1){ # weird...shutdown to prevent looping
				shutdown_request($handle);
				return 0;
			}
			$headersend += length($endtoken);
		} else {
			return 0;
		}
		$clients{$fno}->[0] = $headersend;
	}

	# do we have content data (dictated by content-length)
	my $contentlen = 0;
	if($clients{$fno}->[1] == -1){
		if( substr( $clients{$fno}->[2], 0, $headersend ) =~
				/\ncontent-length:[ \t]*(.+?)\r{0,1}\n/i){
			$contentlen = $1;
		} else {
			# no valid content-length, and we have all headers, so
			# we should be good to go
			$clients{$fno}->[1] = 0;
			return 1;
		}
		$clients{$fno}->[1] = $contentlen;
	}

	# do we have enough data to satisfy content length?
	if( (length( $clients{$fno}->[2] ) - $clients{$fno}->[0]) >= 
			$clients{$fno}->[1] ){
		return 1;
	}

	# we're not quite there yet
	return 0;
}


# process the request (assumes enough data based on precheck_request)
sub handle_request {
	my $handle = shift;
	my $fno = fileno($handle);

	if(!defined $clients{$fno}){
		display_error("Integrity error; client doesn't exist in state table");
		shutdown_request($handle);
		return;
	}
	
	$clients{$fno}->[3]++;

	my ($requestline, $url);

	if($clients{$fno}->[2]=~m/^([a-z0-9]+[ \t]+([^ \t\r\n]+)(.*?)\n)/i ){
		$requestline = $1;
		$url = $2;
	} else {
		display_error("Bad HTTP request format", $clients{$fno}->[2]);
		shutdown_request($handle);
		return;
	}

	if( $url=~m#^/AUTH/(.+?)/# ){
		my $answer = '';
		my $a = $1;
		$a =~ tr/+/ /;
		if($clients{$fno}->[2] !~ /Authorization: $a/){
			$answer = <<EOT;
HTTP/1.0 401 Authentication Required
WWW-Authenticate: Basic
Connection: close
Content-Length: 0

EOT
		}
		else
		{
			$answer = <<EOT;
HTTP/1.0 200 OK
Connection: close

You successfully authenticated
EOT
		}

		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}


	if( $url=~m#^/TESTCASE/(.+?)/# ){
		run_testcase($handle, $1);
		return;
	}
	
	if( $url=~m#^/CUSTOM/(.+)$# ){
		run_custom($handle, $1);
		return;
	}

	if( $url=~m#^/CRAWLERRESET/# ){
		%crawler = ();
		my $answer = <<EOT;
HTTP/1.0 200 OK
Status: crawler reset
Connection: close

Crawler was reset.
EOT
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}

	if( $url=~m#^/CRAWLERSTART/# ){
		$crawler{ $url }++;
		my $answer = <<EOT;
HTTP/1.0 200 OK
Status: crawler start
Connection: close

EOT
		my $answer2 = <<EOT;
<a href="/CRAWLER/A/">A</a>
<a href="/CRAWLER/B/">B</a>
<a href="/CRAWLER/C/">C</a>
<a href="/CRAWLER/D/">D</a>
<a href="/CRAWLER/E/">E</a>
EOT
		$answer .= $answer2 if($requestline!~/^HEAD /);
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}

	if( $url=~m#^/CRAWLER/([0-9]+)/# ){
		$crawler{ $url }++;
		my $x = $1 +1;
		my $answer = <<EOT;
HTTP/1.0 200 OK
Status: crawler continuation
Connection: close

EOT
		my $answer2 = <<EOT;
<a href="/CRAWLER/$x/">$x</a>
EOT
		$answer .= $answer2 if($requestline!~/^HEAD /);
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}
	
	if( $url=~m#^/CRAWLER/([A-Z]+)/# ){
		$crawler{ $url }++;
		my $x = $1;
		my $answer = <<EOT;
HTTP/1.0 200 OK
Status: crawler continuation
Connection: close

EOT
		my $answer2 = '';
		if(length($x) >= 4){
			$answer2 .= 'No more links';
		} else {
			$x .= $x;
			$x2 = substr($x,0,1);
			$answer2 .= "<a href=\"/CRAWLER/$x/\">$x</a>\n";
			$answer2 .= "<a href=\"/CRAWLER/$x2/\">$x2</a>\n";
			$answer2 .= "<a href=\"/CRAWLER/$x/\">$x</a>\n";
		}
		$answer .= $answer2 if($requestline !~ /^HEAD /);
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}

	if( $url=~m#^/CRAWLERRESULT/# ){
		my $answer = <<EOT;
HTTP/1.0 200 OK
Status: crawler result
Connection: close

EOT
		foreach(sort keys %crawler){
			$answer .= $_ . ': ' . $crawler{$_} . "\n";
		}
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}

	if( $url=~m#^/SHUTDOWN/# ){
		exit;
	}
	
	# if we get here, it's not a testcase or custom test; so error out	
	display_error("Non-testcase request", $clients{$fno}->[2]);
	my $answer = <<EOT;
HTTP/1.0 403 Forbidden
Status: non-testcase request
Connection: close

You requested a forbidden resource.
EOT
	
	syswrite($handle, $answer, length($answer));
	shutdown_request($handle);
}

sub run_testcase {
	my $handle = shift;
	my $case = shift;

	$case=~tr/-_a-z0-9A-Z//cd;
	
	if(!-e $case.'.case'){
		my $answer = <<EOT;
HTTP/1.0 404 Test case not found
Status: missing test case
Connection: close

The specified test case was not found.
EOT
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}

	if(!open(IN,"<$case".'.case')){
		my $answer = <<EOT;
HTTP/1.0 500 Problem opening test case
Status: test case error
Connection: close

There was a problem opening the test case.
EOT
		syswrite($handle, $answer, length($answer));
		shutdown_request($handle);
		return;
	}

	binmode(IN); # Stupid Windows
	my $target = new_default_attribs();

	my $headers = '';
	my $data = '';

	# first line might be custom attributes
	my $attribs = <IN>;
	if($attribs !~ m#^HTTP/#){
		parse_custom_attribs($attribs, $target);
	} else {
		$headers .= $attribs;
	}

	my $inheaders=1;
	while(<IN>){
		if($inheaders) {
			$headers .= $_;
		} else {
			$data .= $_;
		}
		if($inheaders && ($_ eq "\x0a" || $_ eq "\x0d\x0a")){
			$inheaders--;
		}
	}
	close(IN);

	_run_ex($handle, $target, $headers, $data);
}

sub _run_ex {
	my $handle = shift;
	my $target = shift;
	my $headers = shift;
	my $data = shift;

	if($target->{stallinitial}>0){
		sleep($target->{stallinitial});
	}	

	if($target->{'100continue'} > 0){
		my $msg = <<EOT;
HTTP/1.0 100 Continue
Content-length: 0
Status: still working...

EOT
		for(1..$target->{'100continue'}){
			if(!syswrite($handle, $msg, length($msg))){
				display_error("Problem sending 100 Continue messages");
				shutdown_request($handle);
				return;
			}
			sleep(1);
		}
	}
	
	if(!syswrite($handle, $headers, length($headers))){
		display_error("Problem sending HTTP response headers");
		shutdown_request($handle);
		return;
	}

	if($target->{stallmidstream}>0){
		sleep($target->{stallmidstream});
	}

	if(!syswrite($handle, $data, length($data))){
		display_error("Problem sending response data");
		shutdown_request($handle);
		return;
	}

	if($target->{stallend}>0){
		sleep($target->{stallend});
	}

	if($target->{keepalive}==1){
		if($target->{forceclose}==1){
			shutdown_request($handle);
		} else {
			reset_request($handle);
		}
	} else {
		if($target->{forceopen}==1){
			reset_request($handle);
		} else {
			shutdown_request($handle);
		}
	}
}

sub run_custom {
	my $handle = shift;
	my $attribs = shift;

	my $target = new_default_attribs();
	parse_custom_attribs($attribs, $target);

	my $headers = "HTTP/" . $target->{httpversion} .
		" " . $target->{httpresponse} . " Response" .
		$target->{eol};
	
	if($target->{keepalive}==1){
		$headers .= "Connection: keep-alive" . $target->{eol};
	} else {
		$headers .= "Connection: close" . $target->{eol};
	}

	if($target->{chunked}==1){
		$headers .= "Transfer-encoding: chunked" . $target->{eol};
	} else {
		$headers .= "Content-length: " . $target->{contentlength} .
			$target->{eol};
	}

	$headers .= $target->{eol};

	my $data = $target->{content};

	if($target->{contentusepost}==1 || $target->{contentreflect}==1){
		$data = '';
		my $fno = fileno($handle);
		if($target->{contentreflect}==1){
			$data .= substr($clients{$fno}->[2], 0,
				$clients{$fno}->[0]);
		}
		$data .= substr( $clients{$fno}->[2],
				$clients{$fno}->[0], $clients{$fno}->[1] );
	}
	
	if($target->{chunked}==1){
		$data = sprintf("%02x",length($data)) . $target->{eol}
			. $data . $target->{eol} . '0' .
			$target->{eol};
	}	

	_run_ex($handle, $target, $headers, $data);

}

sub parse_custom_attribs {
	my $attribstr = shift;
	my $attribhr = shift;

	my @attr = split(/\//, $attribstr);
	foreach (@attr){
		next if($_ eq '');
		my ($k,$v) = split(/=/, $_, 2);
		if($v=~tr/%//){
			$v =~ s/%([a-f0-9][a-f0-9])/pack("C",hex($1))/eig;
		}
		if(defined $attribhr->{$k}){
			$attribhr->{$k} = $v; 
		}
	}
}

sub new_default_attribs {
	my %attribs = (

		httpversion => '1.0',	# http version to use
		httpresponse => 200,	# http response code
		eol => "\x0d\x0a",	# EOL
		keepalive => 0,		# use keepalives?
		chunked => 0,		# use chunked output?
		content => 'abcde',	# content to return
		contentusepost => 0,	# get content from posted data
		contentreflect => 0,	# get content from full request
		contentlength => 5,	# value of content-length header
		forceclose => 0,	# close the connection no matter what
		forceopen => 0,		# keep the connection open no matter what
		'100continue' => 0,	# number of 100 Continue responses to inject
		stallinitial => 0,	# wait specified number of seconds before responding
		stallmidstream => 0,	# wait specified number of seconds during response
		stallend => 0		# wait specified number of seconds after response 
					# (but before connection close)
	
	);
	
	return \%attribs;
}

sub reset_request {
	my $handle = shift;
	my $fno = fileno($handle);
	
	substr($clients{$fno}->[2], 0, $clients{$fno}->[0], '');
	$clients{$fno}->[0] = -1;
	if($clients{$fno}->[1] > 0){
		substr($clients{$fno}->[2], 0, $clients{$fno}->[1], '');
	}
	$clients{$fno}->[1] = -1;
	
	# we're supposed to be lenient about trailing CRLFs...
	if(length($clients{$fno}->[2])>0){
		$clients{$fno}->[2] =~ s/^[\r\n]+//;
	}
}

sub shutdown_request {
	my $handle = shift;
	my $fno = fileno($handle);
	
#	sleep(1);
	eval { $handle->close; };
	$readers->remove($fno);
	delete $clients{$fno};
}

sub display_error {
	print STDERR '-' x 76, "\n";
	print STDERR "Error: ", shift, "\n";
	if( defined $_[0] ){
		print STDERR shift, "\n";
	}
}