File: cgi.pl

package info (click to toggle)
lighttpd 1.4.82-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,800 kB
  • sloc: ansic: 103,536; perl: 2,720; sh: 783; yacc: 726; makefile: 715
file content (98 lines) | stat: -rwxr-xr-x 2,894 bytes parent folder | download
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
#!/usr/bin/env perl

# env
if ($ENV{"QUERY_STRING"} =~ /^env=(\w+)/) {
    my $v = defined($ENV{$1}) ? $ENV{$1} : "[$1 not found]";
    print "Status: 200\r\n\r\n$v";
    exit 0;
}

# redirection
if ($ENV{"QUERY_STRING"} eq "internal-redir") {
    # (not actually 404 error, but use separate script from cgi.pl for testing)
    print "Location: /404.pl/internal-redir\r\n\r\n";
    exit 0;
}

# redirection
if ($ENV{"QUERY_STRING"} eq "external-redir") {
    print "Location: http://www.example.org:2048/\r\n\r\n";
    exit 0;
}

# 404
if ($ENV{"QUERY_STRING"} eq "send404") {
    print "Status: 404\n\nsend404\n";
    exit 0;
}

# X-Sendfile
if ($ENV{"QUERY_STRING"} eq "xsendfile") {
    # add path prefix if cygwin tests running for win32native executable
    # (strip volume so path starts with '/'; works only on same volume)
    my $prefix = $ENV{CYGROOT} || "";
    $prefix =~ s/^[a-z]://i;

    # urlencode path for CGI header
    # (including urlencode ',' if in path, for X-Sendfile2 w/ FastCGI (not CGI))
    # (This implementation is not minimal encoding;
    #  encode everything that is not alphanumeric, '.' '_', '-', '/')
    require Cwd;
    my $path = $prefix . Cwd::getcwd() . "/index.txt";
    # (alternative: run cygpath command, if available, on cygwin or msys2)
    $path = substr($path, length($prefix)+2)
      if (($^O eq "msys" && uc($ENV{MSYSTEM} || "") ne "MSYS")
          || ($^O eq "cygwin" && exists $ENV{MSYSTEM}));
    $path =~ s#([^\w./-])#"%".unpack("H2",$1)#eg;

    print "Status: 200\r\n";
    print "X-Sendfile: $path\r\n\r\n";
    exit 0;
}

# NPH
if ($ENV{"QUERY_STRING"} =~ /^nph=(\w+)/) {
    print "Status: $1 FooBar\r\n\r\n";
    exit 0;
}

# crlfcrash
if ($ENV{"QUERY_STRING"} eq "crlfcrash") {
    print "Location: http://www.example.org/\r\n\n\n";
    exit 0;
}

# POST length
if ($ENV{"QUERY_STRING"} eq "post-len") {
    $cl = $ENV{CONTENT_LENGTH} || 0;
    my $len = 0;
    if ($ENV{"REQUEST_METHOD"} eq "POST") {
        while (<>) { # expect test data to end in newline
            $len += length($_);
            last if $len >= $cl;
        }
    }
    print "Status: 200\r\n\r\n$len";
    exit 0;
}

# trailer
# note: gets merged into headers unless lighttpd configured to stream response
# note: client browsers (e.g. Firefox) might support only Server-Timing
#       (which Firefox Inspector displays in response Timings, not Headers)
#   https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Trailer
#   https://caniuse.com/?search=trailer
if ($ENV{"QUERY_STRING"} eq "trailer") {
    $|=1;
    print "Status: 200\r\nTransfer-Encoding: chunked\r\n\r\n";
    # (chose not to add 'sleep' here)
    print "0\r\nTest-Trailer: testing\r\n\r\n";
    #print "0\r\nServer-Timing: metric;desc=\"test-trailer\";dur=9.87\r\n\r\n";
    exit 0;
}

# default
print "Content-Type: text/plain\r\n\r\n";
print $ENV{"QUERY_STRING"};

0;