aboutsummaryrefslogtreecommitdiff
path: root/doc/examples/cgi-bin/simple1.cgi
diff options
context:
space:
mode:
Diffstat (limited to 'doc/examples/cgi-bin/simple1.cgi')
-rw-r--r--doc/examples/cgi-bin/simple1.cgi125
1 files changed, 125 insertions, 0 deletions
diff --git a/doc/examples/cgi-bin/simple1.cgi b/doc/examples/cgi-bin/simple1.cgi
new file mode 100644
index 00000000..15a20458
--- /dev/null
+++ b/doc/examples/cgi-bin/simple1.cgi
@@ -0,0 +1,125 @@
1#!/usr/bin/perl
2# Perl CGI document template... by Luis Figueiredo (stdio@netc.pt)
3#
4#
5#
6
7
8#use strict;
9my %POST;
10my %QUERY_STRING;
11my %COOKIE;
12my $stdindata;
13
14while(<STDIN>) {
15 $stdindata.=$_;
16};
17if($ENV{QUERY_STRING}) {
18 my (@varsdata)=split(/&/,$ENV{QUERY_STRING});
19 foreach (@varsdata) {
20 my ($name,$value)=split(/=/,$_);
21 $value =~ s/\+/ /g;
22 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
23 $QUERY_STRING{$name}=$value;
24 };
25
26};
27if($ENV{REQUEST_METHOD} =~ m/POST/ && $ENV{CONTENT_TYPE} =~ m/application\/x-www-form-urlencoded/i) {
28 my (@varsdata)=split(/&/,$stdindata);
29 foreach (@varsdata) {
30 my ($name,$value)=split(/=/,$_);
31 $value =~ s/\+/ /g;
32 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
33 $POST{$name}=$value;
34 };
35
36};
37if($ENV{HTTP_COOKIE} || $ENV{COOKIE}) {
38 my $cookie=$ENV{HTTP_COOKIE} || $ENV{COOKIE};
39 my(@cookiedata)=split(/; /,$cookie);
40 foreach(@cookiedata) {
41 $_ =~ /(.*?)=(.*)/;
42 $COOKIE{$1}=$2;
43 };
44};
45
46sub mydate {
47 my $format=shift;
48 my($time) = @_;
49 my(%mult) = ('s'=>1,
50 'm'=>60,
51 'h'=>60*60,
52 'd'=>60*60*24,
53 'M'=>60*60*24*30,
54 'y'=>60*60*24*365);
55 my($offset);
56 if (!$time || (lc($time) eq 'now')) {
57 $offset = 0;
58 } elsif ($time=~/^\d+/) {
59 $offset=($time-time);
60 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
61 $offset = ($mult{$2} || 1)*$1;
62 } else {
63 $offset=($time-time);
64 }
65 my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
66 my(@LMON)=qw/January February March April May June July August September October November December/;
67 my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
68 my(@LWDAY) = qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturdat/;
69 my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time+$offset);
70 $year+=1900;
71 $format =~ s/\%Y/$year/eg;
72 $format =~ s/\%a/$WDAY[$wday]/eg;
73 $format =~ s/\%A/$LWDAY[$wday]/eg;
74 $format =~ s/\%m/sprintf("%02d",$mon+1)/eg;
75 $format =~ s/\%d/sprintf("%02d",$mday)/eg;
76 $format =~ s/\%H/sprintf("%02d",$hour)/eg;
77 $format =~ s/\%M/sprintf("%02d",$min)/eg;
78 $format =~ s/\%S/sprintf("%02d",$sec)/eg;
79 $format =~ s/\%b/$MON[$mon]/eg;
80 $format =~ s/\%B/$LMON[$mon]/eg;
81 $format =~ s/\%Z/GMT/g;
82 return $format;
83}
84
85sub cookie {
86 my $name=shift;
87 my $value=shift;
88 my $expire=shift;
89 if($value eq "") {
90 print "Set-Cookie: $name; path=$ENV{SCRIPT_NAME}; ";
91 } else {
92 print "Set-Cookie: $name=$value; path=$ENV{SCRIPT_NAME}; ";
93 };
94 if($expire) {
95 print "expires=".mydate("%a, %d-%b-%Y %H:%M:%S %Z",$expire);
96 };
97 #print " secure";
98 print "\r\n"; # end cookie
99};
100
101
102#COOKIES
103
104
105#print "Date: ".mydate("%a, %d %b %Y %H:%M:%S %Z","+5m")."\n";
106cookie("teste1","maria","+5m");
107cookie("login","manel","+5m");
108print "Content-type: text/html\r\n\r\n";
109
110
111
112print "<HTML>\n";
113print "<BODY bgcolor='EFEFEF'>\n";
114print "Simple cgi demo<BR>\n";
115print "<form action='$ENV{SCRIPT_NAME}?teste=new' method='POST'>\n";
116print "post value: <input type='text' name='login'>\n";
117print "<input type=submit name='go' value='send'>\n";
118print "</form><BR>\n";
119
120print "results:<table border=1><TR><TD>cookie 'maria'</TD><TD> $COOKIE{teste1}</TD></TR>\n";
121print "<TR><TD>post 'login':</TD><TD> $POST{login}</TD></TR>\n";
122print "<TR><TD>querystring 'teste':</TD><TD> $QUERY_STRING{teste}</TD></TR>\n";
123print "</TABLE>\n";
124
125print "</BODY></HTML>\n";