This file is indexed.

/usr/share/perl5/CGI/Test.pm is in libcgi-test-perl 1.111-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
#################################################################
#  Copyright (c) 2001, Raphael Manfredi
#  Copyright (c) 2011-2016, Alex Tokarev
#
#  You may redistribute only under the terms of the Artistic License,
#  as specified in the README file that comes with the distribution.
#

package CGI::Test;

use strict;
use warnings;

use Carp;
use HTTP::Status;
use URI;
use File::Temp qw(mkstemp);
use File::Spec;
use File::Basename;
use Cwd qw(abs_path);

use vars qw($VERSION);

$VERSION = '1.111';

use constant WINDOWS => eval { $^O =~ /Win32|cygwin/ };

#############################################################################
#
# ->new
#
# Creation routine
#
# Arguments:
#    base_url		URL to cgi-bin, e.g. http://foo:18/cgi-bin
#    cgi_dir		physical location of base_url
#    tmp_dir		(optional) temporary directory to use
#    cgi_env		(optional) default CGI environment
#    doc_dir		(optional) physical location of docs, for path translation
#
#############################################################################
sub new
{
    my $this = bless {}, shift;
    my %params = @_;

    my $ubase = $params{-base_url};
    my $dir   = $params{-cgi_dir};
    my $doc   = $params{-doc_dir} || ".";
    my $tmp   = $params{-tmp_dir} || $ENV{TMPDIR} || $ENV{TEMP} || "/tmp";
    my $env   = $params{-cgi_env};

    my $uri = URI->new($ubase);
    croak "-base_url $ubase is not within the http scheme"
        unless ( $uri->scheme eq 'http' || $uri->scheme eq 'https' );

    my ($server, $path) = $this->split_uri($uri);
    $this->{host_port} = $server;
    $this->{scheme}    = $uri->scheme;
    $this->{host}      = $uri->host;
    $this->{port}      = $uri->port;
    $this->{base_path} = $path;
    $this->{cgi_dir}   = $dir;
    $this->{tmp_dir}   = $tmp;
    $env = {} unless defined $env;
    $this->{cgi_env} = $env;
    $this->{doc_dir} = $doc;

    #
    # The following default settings will apply unless alternatives given
    # by user via the -cgi_env parameter.
    #

    my %dflt = (AUTH_TYPE           => "Basic",
                GATEWAY_INTERFACE   => "CGI/1.1",
                HTTP_ACCEPT         => "*/*",
                HTTP_CONNECTION     => "Close",
                HTTP_USER_AGENT     => "CGI::Test",
                HTTP_ACCEPT_CHARSET => "iso-8859-1",
                REMOTE_HOST         => "localhost",
                REMOTE_ADDR         => "127.0.0.1",
                SERVER_NAME         => $uri->host,
                SERVER_PORT         => $uri->port,
                SERVER_PROTOCOL     => "HTTP/1.1",
                SERVER_SOFTWARE     => "CGI::Test",
                );

    while (my ($key, $value) = each %dflt)
    {
        $env->{$key} = $value unless exists $env->{$key};
    }

    #
    # Object types to create depending on returned content-type.
    # If not listed here, "Other" is assummed.
    #

    $this->{_obj_type} = {'text/plain' => 'Text',
                          'text/html'  => 'HTML',
                          };

    return $this;
}

######################################################################
#
######################################################################
sub make
{
    my $class = shift;
    return $class->new(@_);
}

#
# Attribute access
#

######################################################################
sub host_port
{
    my $this = shift;
    return $this->{host_port};
}

######################################################################
sub base_uri
{
    my $this = shift;

    my $scheme = $this->{scheme};
    my $host   = $this->{host};
    my $port   = $this->{port};
    my $base   = $this->{base_path};

    return $scheme . '://' . $host . ':' . $port . $base;
}

######################################################################
sub host
{
    my $this = shift;
    return $this->{host};
}

######################################################################
sub port
{
    my $this = shift;
    return $this->{port};
}

######################################################################
sub base_path
{
    my $this = shift;
    return $this->{base_path};
}

######################################################################
sub cgi_dir
{
    my $this = shift;
    return $this->{cgi_dir};
}

######################################################################
sub doc_dir
{
    my $this = shift;
    return $this->{doc_dir};
}

######################################################################
sub tmp_dir
{
    my $this = shift;
    return $this->{tmp_dir};
}

######################################################################
sub cgi_env
{
    my $this = shift;
    return $this->{cgi_env};
}

######################################################################
sub _obj_type
{
    my $this = shift;
    return $this->{_obj_type};
}

######################################################################
sub http_headers {
    my ($self) = @_;

    return $self->{http_headers};
}

######################################################################
#
# ->_dpath
#
# Returns direct path to final component of argument,
# i.e. the original path with . and .. items removed.
#
# Will probably only work on Unix (possibly Win32 if paths given with "/").
#
######################################################################
sub _dpath
{
    my $this  = shift;
    my ($dir) = @_;
    my $root  = ($dir =~ s|^/||) ? "/" : "";
    my @cur;
    foreach my $item (split(m|/|, $dir))
    {
        next if $item eq '.';
        if ($item eq '..')
        {
            pop(@cur);
        }
        else
        {
            push(@cur, $item);
        }
    }
    my $path = $root . join('/', @cur);
    $path =~ tr|/||s;
    return $path;
}

######################################################################
#
# ->split_uri
#
# Split down URI into (server, path, query) components.
#
######################################################################
sub split_uri
{
    my $this = shift;
    my ($uri) = @_;
    return ($uri->host_port, $this->_dpath($uri->path), $uri->query);
}

######################################################################
#
# ->GET
#
# Perform an HTTP GET request on a CGI URI by running the script directly.
# Returns a CGI::Test::Page object representing the returned page, or the
# error.
#
# Optional $user provides the name of the "authenticated" user running
# this script.
#
######################################################################
sub GET
{
    my $this = shift;
    my ($uri, $user) = @_;

    return $this->_cgi_request($uri, $user, undef);
}

######################################################################
#
# ->POST
#
# Perform an HTTP POST request on a CGI URI by running the script directly.
# Returns a CGI::Test::Page object representing the returned page, or the
# error.
#
# Data to send to the script are held in $input, a CGI::Test::Input object.
#
# Optional $user provides the name of the "authenticated" user running
# this script.
#
######################################################################
sub POST
{
    my $this = shift;
    my ($uri, $input, $user) = @_;

    return $this->_cgi_request($uri, $user, $input);
}

######################################################################
#
# ->_cgi_request
#
# Common routine to handle GET and POST.
#
######################################################################
sub _cgi_request
{
    my $this = shift;
    my ($uri, $user, $input) = @_;    # $input defined for POST

    my $u = URI->new($uri);
    croak "URI $uri is not within the http scheme"
        unless $u->scheme eq 'http';

    require CGI::Test::Page::Error;
    my $error = "CGI::Test::Page::Error";

    my ($userver, $upath, $uquery) = $this->split_uri($u);
    my $server    = $this->host_port;
    my $base_path = $this->base_path . "/";

    croak "URI $uri is not located on server $server"
        unless $userver eq $server;

    croak "URI $uri is not located under the $base_path directory"
        unless substr($upath, 0, length $base_path) eq $base_path;

    substr($upath, 0, length $base_path) = '';

    #
    # We have script + path_info in the $upath variable.  To determine where
    # the path_info starts, we have to walk through the components and
    # compare, at each step, the current walk-through path with one on the
    # filesystem under cgi_dir.
    #

    my $cgi_dir = $this->cgi_dir;
    my @components = split(m|/|, $upath);
    my @script;

    while (@components)
    {
        my $item = shift @components;
        if (-e File::Spec->catfile($cgi_dir, @script, $item))
        {
            push(@script, $item);
        }
        else
        {
            unshift @components, $item;
            last;
        }
    }

    my $script      = File::Spec->catfile($cgi_dir, @script);        # Real
    my $script_name = $base_path . join("/",        @script);        # Virtual
    my $path        = "/" . join("/",               @components);    # Virtual

    return $error->new(RC_NOT_FOUND,    $this) unless -f $script;
    return $error->new(RC_UNAUTHORIZED, $this) unless -x $script;

    #
    # Prepare input for POST requests.
    #

    my @post = ();
    local $SIG{PIPE} = 'IGNORE';
    local (*PREAD, *PWRITE);
    
    my ($in_fh, $out_fh, $in_fname, $out_fname);
    
    if (defined $input) {
        # In Windows, we use temp files instead of pipes to avoid
        # stream duplication errors
        if ( WINDOWS ) {
            ($in_fh, $in_fname) =
                mkstemp(File::Spec->catfile($this->tmp_dir, "cgi_in.XXXXXX"));
            
            binmode $in_fh;
            
            syswrite $in_fh, $input->data, $input->length;
            close $in_fh;
            
            @post = (
                -in_fname => $in_fname,
                -input    => $input,
            );
        }
        else {
            if ( not pipe(PREAD, PWRITE) ) {
                warn "can't open pipe: $!";
                return $error->new(RC_INTERNAL_SERVER_ERROR, $this);
            }

            @post = (
                -in    => \*PREAD,
                -input => $input,
            );
        }
    }

    #
    # Prepare temporary file for storing output, which we'll parse once
    # the script is done.
    #

    ($out_fh, $out_fname) =
      mkstemp(File::Spec->catfile($this->tmp_dir, "cgi_out.XXXXXX"));
    
    close $out_fh if WINDOWS;

    select((select(STDOUT), $| = 1)[ 0 ]);
    print STDOUT "";    # Flush STDOUT before forking

    #
    # Fork...
    #

    my $pid = fork;
    die "can't fork: $!" unless defined $pid;

    #
    # Child will run the CGI program with no input if it's a GET and
    # output stored to $fh.  When issuing a POST, data will be provided
    # by the parent through a pipe in Unixy systems, or through a temp file
    # in Windows.
    #

    if ($pid == 0) {
        close PWRITE if defined $input && !WINDOWS; # Writing side of the pipe
        
        $this->_run_cgi(
            -script_file => $script,         # Real path
            -script_name => $script_name,    # Virtual path, given in URI
            -user        => $user,
            -out         => $out_fh,
            -out_fname   => $out_fname,
            -uri         => $u,
            -path_info   => $path,
            @post,                           # Additional params for POST
        );
        
        confess "not reachable!";
    }

    #
    # Parent process
    #

    close $out_fh unless WINDOWS;
    
    if (defined $input && !WINDOWS)
    {                                        # Send POST input data
        close PREAD;
        syswrite PWRITE, $input->data, $input->length;
        close PWRITE or warn "failure while closing pipe: $!";
    }

    my $child = waitpid $pid, 0;

    if ($pid != $child)
    {
        warn "waitpid returned with pid=$child, but expected pid=$pid";
        kill 'TERM', $pid or warn "can't SIGTERM pid $pid: $!";
        unlink $in_fname  or warn "can't unlink $in_fname: $!";
        unlink $out_fname or warn "can't unlink $out_fname: $!";
        return $error->new(RC_NO_CONTENT, $this);
    }

    #
    # Get header within generated response, and determine Content-Type.
    #

    my $header = $this->_parse_header($out_fname);
    unless (scalar keys %$header)
    {
        warn "script $script_name generated no valid headers";
        unlink $in_fname  or warn "can't unlink $in_fname: $!";
        unlink $out_fname or warn "can't unlink $out_fname: $!";
        return $error->new(RC_INTERNAL_SERVER_ERROR, $this);
    }
    
    #
    # Return error page if we got 5xx status
    #
    
    if ( my ($status) = ($header->{Status} || '') =~ /^(5\d\d)/ ) {
        return $error->new($status, $this);
    }

    #
    # Store headers for later retrieval
    #

    $this->{http_headers} = $header;

    #
    # Create proper page object, which will parse the results file as needed.
    #

    my $type      = $header->{'Content-Type'};
    my $base_type = lc($type);
    $base_type =~ s/;.*//;    # Strip type parameters
    my $objtype = $this->_obj_type->{$base_type} || "Other";
    $objtype = "CGI::Test::Page::$objtype";

    eval "require $objtype";
    die "can't load module $objtype: $@" if chop $@;

    my $page = $objtype->new(
                        -server       => $this,
                        -file         => $out_fname,
                        -content_type => $type,    # raw type, with parameters
                        -user         => $user,
                        -uri          => $u,
                        );
    
    if ($in_fname) {
        unlink $in_fname  or warn "can't unlink $in_fname: $!";
    }
    
    unlink $out_fname or warn "can't unlink $out_fname: $!";

    return $page;
}

######################################################################
#
# ->_run_cgi
#
# Run the specified script within a CGI environment.
#
# The -user is the name of the authenticated user running this script.
#
# The -in and -out parameters are file handles where STDIN and STDOUT
# need to be connected to.  If $in is undefined, STDIN is connected
# to /dev/null.
#
# Returns nothing.
#
######################################################################
sub _run_cgi
{
    my $this = shift;

    my %params = @_;

    my $script    = $params{-script_file};
    my $name      = $params{-script_name};
    my $user      = $params{-user};
    my $in        = $params{-in};
    my $in_fname  = $params{-in_fname};
    my $out       = $params{-out};
    my $out_fname = $params{-out_fname};
    my $u         = $params{-uri};
    my $path      = $params{-path_info};
    my $input     = $params{-input};

    #
    # Connect file descriptors.
    #

    if ( !WINDOWS ) {
        if (defined $in)
        {
            open(STDIN, '<&=' . fileno($in)) || die "can't redirect STDIN: $!";
        }
        else
        {
            my $devnull = File::Spec->devnull;
            open(STDIN, $devnull) || die "can't open $devnull: $!";
        }
        open(STDOUT, '>&=' . fileno($out)) || die "can't redirect STDOUT: $!";
    }

    #
    # Setup default CGI environment.
    #

    while (my ($key, $value) = each %{$this->cgi_env})
    {
        $ENV{$key} = $value;
    }

    #
    # Where there is a script input, setup CONTENT_* variables.
    # If there's no input, delete CONTENT_* variables.
    #

    if (defined $input)
    {
        $ENV{CONTENT_TYPE}   = $input->mime_type;
        $ENV{CONTENT_LENGTH} = $input->length;
    }
    else
    {
        delete $ENV{CONTENT_TYPE};
        delete $ENV{CONTENT_LENGTH};
    }

    #
    # Supersede whatever they may have set for the following variables,
    # which are very request-specific:
    #

    $ENV{REQUEST_METHOD}  = defined $input ? "POST" : "GET";
    $ENV{PATH_INFO}       = $path;
    $ENV{SCRIPT_NAME}     = $name;
    $ENV{SCRIPT_FILENAME} = $script;
    $ENV{HTTP_HOST}       = $u->host_port;

    if (length $path)
    {
        $ENV{PATH_TRANSLATED} = $this->doc_dir . $path;
    }
    else
    {
        delete $ENV{PATH_TRANSLATED};
    }

    if (defined $user)
    {
        $ENV{REMOTE_USER} = $user;
    }
    else
    {
        delete $ENV{REMOTE_USER};
        delete $ENV{AUTH_TYPE};
    }

    if (defined $u->query)
    {
        $ENV{QUERY_STRING} = $u->query;
    }
    else
    {
        delete $ENV{QUERY_STRING};
    }
    
    #
    # This is a way of letting Perl test scripts to run under
    # the same Perl version that CGI::Test is running with
    #

    $ENV{PERL} = $^X;

    #
    # Make sure the script sees the same @INC as we do currently.
    # This is very important when running a regression test suite, to
    # make sure any CGI script using the module we're testing will see
    # the files from the build directory.
    #
    # Since we're about to chdir() to the cgi-bin directory, we must anchor
    # any relative path to the current working directory.
    #
    my $path_sep = WINDOWS ? ';' : ':';

    $ENV{PERL5LIB} = join($path_sep, map {-e $_ ? abs_path($_) : $_} @INC);

    # Also make sure that temp directory is available for the script,
    # else older CGI.pm may choke and default to some not-quite-sane
    # values that do not work in Windows

    $ENV{TMPDIR} = $this->tmp_dir;

    #
    # Now run the script, changing the current directory to the location
    # of the script, as a web server would.
    #

    my $directory = dirname($script);
    my $basename  = basename($script);

    chdir $directory or die "can't cd to $directory: $!";

    if ( WINDOWS ) {
        my $cmd_line = $input ? "$basename < ${in_fname} > ${out_fname}"
                     :          "$basename < NUL >${out_fname}"
                     ;

        exec $cmd_line;
    }
    else {
        exec "./$basename";
    }

    die "could not exec $script: $!";
}

######################################################################
#
# ->_parse_header
#
# Look for a set of leading HTTP headers in the file, and insert them
# into a hash table (we don't expect duplicates).
#
# Returns ref to hash containing the headers.
#
######################################################################
sub _parse_header
{
    my $this = shift;
    my ($file) = @_;
    my %header;
    local *FILE;
    open(FILE, $file) || warn "can't open $file: $!";
    local $_;
    my $field;

    while (<FILE>)
    {
        last if /^\015?\012$/ || /^\015\012$/;
        s/\015?\012$//;
        if (s/^\s+/ /)
        {
            last if $field eq '';    # Cannot be a header
            $header{$field} .= $_ if $field ne '';
        }
        elsif (($field, my $value) = /^([\w-]+)\s*:\s*(.*)/)
        {
            $field =~ s/(\w+)/\u\L$1/g;    # Normalize spelling
            if (exists $header{$field})
            {
                warn "duplicate $field header in $file";
                $header{$field} .= " ";
            }
            $header{$field} .= $value;
        }
        else
        {
            warn "mangled header in $file";
            %header = ();                  # Discard what we read sofar
            last;
        }
    }
    close FILE;
    return \%header;
}

1;

=head1 NAME

CGI::Test - CGI regression test framework

=head1 SYNOPSIS

 # In some t/script.t regression test, for instance
 use CGI::Test;
 use Test::More tests => 7;

 my $ct = CGI::Test->new(
    -base_url   => "http://some.server:1234/cgi-bin",
    -cgi_dir    => "/path/to/cgi-bin",
 );

 my $page = $ct->GET("http://some.server:1234/cgi-bin/script?arg=1");
 like $page->content_type, qr|text/html\b|, "Content type";

 my $form = $page->forms->[0];
 is $form->action, "/cgi-bin/some_target", "Form action URI";

 my $menu = $form->menu_by_name("months");
 ok $menu->is_selected("January"), "January selected";
 ok !$menu->is_selected("March"),  "March not selected";
 ok $menu->multiple,               "Menu is multi-choice";

 my $send = $form->submit_by_name("send_form");
 ok defined $send, "Send form defined";

 #
 # Now interact with the CGI
 #

 $menu->select("March");        # "click" on the March label
 my $answer = $send->press;     # "click" on the send button
 
 # and make sure we don't get an HTTP error
 ok $answer->is_ok, "Answer response";

=head1 DESCRIPTION

The C<CGI::Test> module provides a CGI regression test framework which
allows you to run your CGI programs offline, i.e. outside a web server,
and interact with them programmatically, without the need to type data
and click from a web browser.

If you're using the C<CGI> module, you may be familiar with its offline
testing mode.  However, this mode is appropriate for simple things, and
there is no support for conducting a full session with a stateful script.
C<CGI::Test> fills this gap by providing the necessary infrastructure to
run CGI scripts, then parse the output to construct objects that can be
queried, and on which you can interact to "play" with the script's control
widgets, finally submitting data back.  And so on...

Note that the CGI scripts you can test with C<CGI::Test> need not be
implemented in Perl at all.  As far as this framework is concerned, CGI
scripts are executables that are run on a CGI-like environment and which
produce an output.

To use the C<CGI::Test> framework, you need to configure a C<CGI::Test>
object to act like a web server, by providing the URL base where
CGI scripts lie on this pseudo-server, and which physical directory
corresponds to that URL base.

From then on, you may issue GET and POST requests giving an URL, and
the pseudo-server returns a C<CGI::Test::Page> object representing the
outcome of the request.  This page may be an error, plain text, some
binary data, or an HTML page (see L<CGI::Test::Page> for details).

The latter (an HTML page) can contain one or more CGI forms (identified
by C<E<lt>FORME<gt>> tags), which are described by instances of
C<CGI::Test::Form> objects (see L<CGI::Test::Form> for details).

Forms can be queried to see whether they contain a particular type
of widget (menu, text area, button, etc...), of a particular name
(that's the CGI parameter name).  Once found, one may interact with
a widget as the user would from a browser.  Widgets are described by
polymorphic objects which conform to the C<CGI::Test::Form::Widget> type.
The specific interaction that is offered depends on the dynamic type of
the object (see L<CGI::Test::Form::Widget> for details).

An interaction with a form ends by a submission of the form data to the
server, and getting a reply back.  This is done by pressing a submit button,
and the press() routine returns a new page.  Naturally, no server is
contacted at all within the C<CGI::Test> framework, and the CGI script is
ran through a proper call to one of the GET/POST method on the
C<CGI::Test> object.

=head1 INTERFACE

=head2 Creation Interface

The creation routine C<new()> takes the following mandatory parameters:

=over 4

=item C<-base_url> => I<URL of the cgi-bin directory>

Defines the URL domain which is handled by C<CGI::Test>.
This is the URL of the C<cgi-bin> directory.

Note that there is no need to have something actually running on the
specified host or port, and the server name can be any host name,
whether it exists or not.  For instance, if you say:

    -base_url => "http://foo.example.com:70/cgi-bin"

you simply declare that the C<CGI::Test> object will know how to handle
a GET request for, say:

    http://foo.example.com:70/cgi-bin/script

and it will do so I<internally>, without contacting C<foo.example.com>
on port 70...

=item C<-cgi_dir> => I<path to the cgi-bin directory>

Defines the physical path corresponding to the C<cgi-bin> directory defined
by the C<-base_url> parameter.

For instance, given the settings:

    -base_url => "http://foo.example.com:70/cgi-bin",
    -cgi_dir  => "/home/ram/cgi/test"

then requesting

    http://foo.example.com:70/cgi-bin/script

will actually run

    /home/ram/cgi/test/script

Those things are really easier to understand via examples than via
formal descriptions, aren't they?

=back

The following optional arguments may also be provided:

=over 4

=item C<-cgi_env> => I<HASH ref>

Defines additional environment variables that must be set, or changes
hardwirted defaults.  Some variables like C<CONTENT_TYPE> really depend
on the request and will be dynamically computed by C<CGI::Test>.

For instance:

    -cgi_env => {
        HTTP_USER_AGENT     => "Mozilla/4.76",
        AUTH_TYPE           => "Digest",
    }

See L<CGI ENVIRONMENT VARIABLES> for more details on which environment
variables are defined, and which may be superseded.

=item C<-doc_dir> => I<path to document tree>

This defines the root directory of the HTTP server, for path translation.
It defaults to C</var/www>.

B<NOTE>: C<CGI::Test> only serves CGI scripts for now, so this setting
is not terribly useful, unless you care about C<PATH_TRANSLATED>.

=item C<-tmp_dir> => I<path to temporary directory>

The temporary directory to use for internal files created while processing
requests.  Defaults to the value of the environment variable C<TMPDIR>,
or C</tmp> if it is not set.

=back

=head2 Object Interface

The following methods, listed in alphabetical order, are available:

=over 4

=item C<GET> I<url_string> [, I<auth_user>]

Issues an HTTP GET request of the specified URL, given as the string
I<url_string>.  It must be in the http scheme, and must lie within the
configured CGI space (i.e. under the base URL given at creation time
via C<-base_url>).

Optionally, you may specify the name of an authenticated user as the
I<auth_user> string. C<CGI::Test> will simply setup the CGI environment
variable C<REMOTE_USER> accordingly.  Since we're in a testing framework,
you can pretend to be anyone you like.  See L<CGI ENVIRONMENT VARIABLES>
for more information on environment variables, and in particular
C<AUTH_TYPE>.

C<GET> returns a C<CGI::Test::Page> polymorphic object, i.e. an object whose
dynamic type is an heir of C<CGI::Test::Page>.  See L<CGI::Test::Page> for
more information on this class hierarchy.

=item C<POST> I<url_string>, I<input_data> [, I<auth_user>]

Issues an HTTP POST request of the specified URL.  See C<GET> above for
a discussion on I<url_string> and I<auth_user>, which applies to C<POST>
as well.

The I<input_data> parameter must be a C<CGI::Test::Input> object.
It specifies the CGI parameters to be sent to the script.  Users normally
don't issue POST requests manually: they are the result of submits on
forms, which are obtained via an initial GET.  Nonetheless, you can
create your own input easily and issue a "faked" POST request, to see
how your script might react to inconsistent (and probably malicious)
input for instance.  See L<CGI::Test::Input> to learn how to construct
suitable input.

C<POST> returns a C<CGI::Test::Page> polymorphic object, like C<GET> does.

=item C<base_path>

The base path in the URL space of the base URL configured at creation time.
It's the URL with the scheme, host and port information removed.

=item C<cgi_dir>

The configured CGI root directory where scripts to be run are held.

=item C<doc_dir>

The configured document root directory.

=item C<host_port>

The host and port of the base URL you configured at creation time.

=item C<split_uri> I<URI>

Splits an URI object into server (host and port), path and query components.
The path is simplified using UNIX semantics, i.e. C</./> is ignored and
stripped, and C</../> is resolved by forgetting the path component that
immediately precedes it (no attempt is made to make sure the translated path
was indeed pointing to an existing directory: simplification happens in the
path space).

Returns the list (host, path, query).

=item C<tmp_dir>

The temporary directory that is being used.

=item C<http_headers>

Returns hashref with parsed HTTP headers received from CGI script.

=back

=head1 CGI ENVIRONMENT VARIABLES

The CGI protocol defines a set of environment variables which are to be set
by the web server before invoking the script.  The environment created by
C<CGI::Test> conforms to the CGI/1.1 specifications.

Here is a list of all the known variables.  Some of those are marked
I<read-only>.  It means you may choose to set them via the C<-cgi_env>
switch of the C<new()> routine, but your settings will have no effect and
C<CGI::Test> will always compute a suitable value.

Variables are listed in alphabetical order:

=over 4

=item C<AUTH_TYPE>

The authentication scheme used to authenticate the user given by C<REMOTE_USER>.
This variable is not present in the environment if there was no user specified
in the GET/POST requests.

By default, it is set to "Basic" when present.

=item C<CONTENT_LENGTH>

Read-only variable, giving the length of data to be read on STDIN by POST
requests (as told by C<REQUEST_METHOD>).  If is not present for GET requests.

=item C<CONTENT_TYPE>

Read-only variable, giving the MIME type of data to be read on STDIN by POST
requests (as told by C<REQUEST_METHOD>).  If is not present for GET requests.

=item C<GATEWAY_INTERFACE>

The Common Gateway Interface (CGI) version specification.
Defaults to "CGI/1.1".

=item C<HTTP_ACCEPT>

The set of Content-Type that are said to be accepted by the client issuing
the HTTP request.  Since there is no browser making any request here, the
default is set to "*/*".

It is up to your script to honour the value of this variable if it wishes to
be nice with the client.

=item C<HTTP_ACCEPT_CHARSET>

The charset that is said to be accepted by the client issuing the HTTP
request.  Since there is no browser making any request here, the
default is set to "iso-8859-1".

=item C<HTTP_CONNECTION>

Whether the connection should be kept alive by the server or closed after
this request.  Defaults to "Close", but since there's no connection and
no real client...

=item C<HTTP_HOST>

This is the host processing the HTTP request.
It is a read-only variable, set to the hostname and port parts of the
requested URL.

=item C<HTTP_USER_AGENT>

The user agent tag string.  This can be used by scripts to emit code that
can be understood by the client, and is also further abused to derive the
OS type where the user agent runs.

In order to be as neutral as possible, it is set to "CGI::Test" by default.

=item C<PATH_INFO>

Read-only variable set to the extra path information part of the requested URL.
Always present, even if empty.

=item C<PATH_TRANSLATED>

This read-only variable is only present when there is a non-empty C<PATH_INFO>
variable.  It is simply set to the value of C<PATH_INFO> with the document
rootdir path prepended to it (the value of the C<-doc_dir> creation argument).

=item C<QUERY_STRING>

This very important read-only variable is the query string present in the
requested URL.  Note that it may very well be set even for a POST request.

=item C<REMOTE_ADDR>

The IP address of the client making the requst.  Can be used to implement
an access policy from within the script.  Here, given that there's no real
client, the default is set to "127.0.0.1", which is the IP of the local
loopback interface.

=item C<REMOTE_HOST>

The DNS-translated hostname of the IP address held in C<REMOTE_ADDR>.
Here, for testing purposes, it is not computed after C<REMOTE_ADDR> but can
be freely set.  Defaults to "localhost".

=item C<REMOTE_USER>

This read-only variable is only present when making an authenticated GET or
POST request.  Its value is the name of the user we are supposed to have
successfully authenticated, using the scheme held in C<AUTH_TYPE>.

=item C<REQUEST_METHOD>

Read-only variable, whose value is either C<GET> or C<POST>.

=item C<SCRIPT_FILENAME>

Read-only variable set to the filesystem path of the CGI script being run.

=item C<SCRIPT_NAME>

Read-only variable set to the virtual  path of the CGI script being run,
i.e. the path given in the requested URL.

=item C<SERVER_NAME>

The host name running the server, which defaults to the host name present
in the base URL, provided at creation time as the C<-base_url> argument.

=item C<SERVER_PORT>

The port where the server listens, which defaults to the port present
in the base URL, provided at creation time as the C<-base_url> argument.
If no port was explicitely given, 80 is assumed.

=item C<SERVER_PROTOCOL>

The protocol which must be followed when replying to the client request.
Set to "HTTP/1.1" by default.

=item C<SERVER_SOFTWARE>

The name of the server software.  Defaults to "CGI::Test".

=back

=head1 BUGS

There are some, most probably.  Please notify me about them.

The following limitations (in decreasing amount of importance)
are known and may be lifted one day -- patches welcome:

=over 4

=item *

There is no support for cookies.  A CGI installing cookies and expecting
them to be resent on further invocations to friendly scripts is bound
to disappointment.

=item *

There is no support for plain document retrieval: only CGI scripts can
be fetched by an HTTP request for now.

=back

=head1 PUBLIC REPOSITORY

CGI::Test now has a publicly accessible Git server provided by Github.com:
L<http://github.com/nohuhu/CGI-Test>

=head1 REPORTING BUGS

Please use Github issue tracker to open bug reports and maintenance
requests.

=head1 AUTHORS

The original author is Raphael Manfredi.

Steven Hilton was long time maintainer of this module.

Current maintainer is Alex Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify
it under the terms of the Artistic License, a copy of which can be
found with Perl 5.6.0.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Artistic License for more details.

=head1 SEE ALSO

CGI(3), CGI::Test::Page(3), CGI::Test::Form(3), CGI::Test::Input(3),
CGI::Test::Form::Widget(3), HTTP::Status(3), URI(3).

=cut