-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.pl
137 lines (111 loc) · 3.07 KB
/
env.pl
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
#!/usr/bin/perl
# Display the contents and environment of an HTTP CGI request
# Denis Howe 2001-09-25
# ############################################################################ #
use Cwd;
$| = 1;
print "Content-type: text/html
Set-Cookie: Showenv=Foo; Path=/
<html>
<head><title>Current environment</title></head>
<body>
<h1>Current environment</h1>
";
%misc = (
"Current directory" => shownull(getcwd()),
"Running as user" => shownull(getlogin() || getpwuid($<)),
"ARGC" => $#ARGV+1,
'Script path ($0)' => $0,
"Script modified" => scalar(gmtime([stat $0]->[9])) . " GMT",
"Calls" => $ENV{calls}++, # Perl for ISAPI persistence test
"Time" => scalar(gmtime()) . " GMT",
);
showhash("Script execution environment", %misc);
if (@ARGV)
{
foreach (0..$#ARGV) {$ARGV{'$' . $_+1,} = $ARGV[$_]};
showhash("Command line arguments", %ARGV);
}
else
{
print "<h2>No command line arguments</h2>\n\n";
}
showhash("Environment variables", %ENV);
$method = $ENV{REQUEST_METHOD} || "GET";
if ($method eq "POST")
{
sysread(STDIN, $input, $ENV{CONTENT_LENGTH});
print "<h2>Form POST data (", length($input),
" bytes)</h2>\n\n", URL2html($input), "\n<p>\n";
if ($ENV{CONTENT_TYPE} =~ m|multipart/form-data|i)
{
$ENV{CONTENT_TYPE} =~ /boundary=/i;
foreach (split($', $input))
{
s|--$||; # Each segment ends with --
print "<h2>Content (", length($_), " bytes)</h2>\n\n",
'<table border="1" cellspacing="0"><tr><td><pre>', $_,
"</pre></td></tr></table>\n";
}
$input = "";
}
}
else
{
$input = $ENV{QUERY_STRING};
print "<h2>No Form data</h2>\n\n" unless ($input);
}
if ($input)
{
foreach (split /&/, $input) {/=/ ? ($form{$`} = $') : ($form{$_} = "")};
showhash("Form Fields", %form);
}
$method = $form{method} if ($form{method});
$post = $method eq "POST";
print '<form action="', $ENV{SCRIPT_NAME}, '" method="', $method, '">
Next time this form will return its results using
GET:<input name="method" type="radio" value="GET" ',
$post || "checked", '>
POST:<input name=method type="radio" value="POST" ',
$post && "checked", '>
<P>
Text: <input name="text" type="text" value="', $form{text}, '"><br>
Input file: <input name="file" type="file"><br>
<input type="submit" value="Submit">
<form>
</body>
</html>
';
exit 0;
# ############################################################################ #
# Turn a hash into an HTML TABLE
sub showhash
{
my ($title, %hash) = @_;
print "<h2>", $title, '</h2>
<table border="1" cellspacing="0" cellpadding="3">
<tr><td><b>Key</b></td><td><b>Value</b></td></tr>
';
foreach (sort keys %hash)
{
print "<tr><td>$_</td>";
local $_ = $hash{$_};
$_ = URL2html($_);
# If no spaces, give the browser a place to break long lines
/ / || s|.{80}|$&<font color="red">-</font> |g;
print "<td>$_</td></tr>\n";
}
print "</table>\n\n";
}
sub shownull
{
$_[0] eq "" ? " " : $_[0];
}
sub URL2html
{
local ($_) = shift;
s/%([0-9a-f][0-9a-f])/pack("H", $1)/ieg;
s/&/&/g; s/</</g; s/\$/$/g;
$_;
}
# ############################################################################ #