#!/local/bin/perl # a perl script to return an image or text or existing file on demand #first get user input data %user_input = get_form_data(); # depends on the query string, the output is different if ($query_data{'format'} eq 'text') { # return a piece of plain text print "Content-type: text/plain", "\n\n"; print "Yellow Crane Tower is an imposing pagoda close to the Yangzi River. ", "\n"; print "Situated at the top of Sheshan (Snake Hill), in Wuchang, the tower ", "\n"; print "was originally built at a place called Yellow Crane Rock projecting ", "\n"; print "over the water, hence the name. Over the centuries the tower was ", "\n"; print "destroyed by fire many times, but its popularity with Wuhan residents ", "\n"; print "ensured that it was always rebuilt. The current tower was completed ", "\n"; print "in 1985 and its design was copied from a Qing dynasty (1644-1911) ", "\n"; print "picture. The tower has 5 stories and rises to 51 meters (168ft). ", "\n"; print "Covered with yellow glazed tiles and supported with 72 huge pillars, ", "\n"; print "it has 60 upturned eaves layer upon layer. It is an authentic ", "\n"; print "reproduction of both the exterior and interior design, with the ", "\n"; print "exception of the addition of air-conditioning and an elevator.", "\n"; } elsif ($query_data{'format'} eq 'image') { # return an image $image = 'cranetower.jpg'; # open the image file in read mode if (open (IMAGE, "<" . $image)) { # the size of the image in bytes $len = (stat ($image))[7]; print "Content-type: image/jpeg", "\n"; print "Content-length: $len", "\n\n"; } print ; } elsif ($query_data{'format'} eq 'both') { # Server Redirection: redirect the server to an existing document # the server will return it as if it is the response from the CGI program print 'Location: cranetower.html', "\n\n" } else { print "Content-type: text/plain", "\n\n"; print "Sorry! This section is under construction", "\n"; } sub get_form_data { # get the type of method that was used by the # user to issue this request $request_method = $ENV{'REQUEST_METHOD'}; # if the GET method was used if ($request_method eq "GET") { # get the query request from the environment variable $query = $ENV{'QUERY_STRING'}; # if the POST method was used } elsif ($request_method eq "POST") { # get the query request from the standard input # first we get the size of the query request (# of chars) $query_size = $ENV{'CONTENT_LENGTH'}; # then we read from STDIN that many number of chars read (STDIN, $query, $query_size); # otherwise, we got an invalid request } else { print "Content-type: text/html", "\n"; print "Status: 500 Server Error", "\n\n"; print "Server Error", "\n"; print "

Server Error

", "\n"; print "
Unsuported request method
", "\n"; exit; } # now we have the query string, let's constrcut a hash table from it # where keys are the keys in the query string, values are the corresponding values %query_data = (); # first we separat each key value pairs @key_values = split (/&/, $query); # for each key value pair ... foreach $key_value (@key_values) { ($key, $value) = split (/=/, $key_value); # replace + with a space $value =~ tr/+/ /; # the regular expression matches any hexadecimal value # and store it in the variable $1 # the pack and hex operator convert the value in $1 to an ASCII equivalent # e means the second argument as an expression $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; # insert the key value pair into the query hash # we need to consider multiple values (select element with "multiple" turned on) # if the key value pair already exist in the hash if (defined($query_data{$key})) { # we append the new value to the old ones $query_data{$key} = join (", ", $query_data{$key}, $value); } else { # otherwise, just add a new entry in the hash $query_data{$key} = $value; } } return qurey_data; }