CGIScriptsandPerl

CGIScriptsandPerl - Lecture Some CGI Scripts and More on...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 1 Lecture Some CGI Scripts and More on Perl
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 2 CGI Scripts in Perl • In this section we offer many, relatively small, cgi scripts. Each has a specific purpose and can be used as a tool box to create more ambitious server- side programs • go to http://www-scf.usc.edu/~csci571/Special/perlexamples.html to see these examples actually working
Background image of page 2
Perl Copyright © Ellis Horowitz 1999-2010 3 Encoded Data When data is sent certain characters must be encoded, e.g. ", /, blank Each character has a hexadecimal equivalent, as shown previously The browser transforms special characters into their hexadecimal equivalents and the cgi script must transform back from hexadecimal to the character. Example: here is a form with a single text box; encoding.pl does the transformation <HTML><HEAD><TITLE>Encoding</TITLE></HEAD> <BODY><H1>Please enter some text</H1> <FORM ACTION=/cgi-bin/encoding.pl METHOD=POST> Enter text here: <INPUT NAME=input><BR> <INPUT TYPE=submit> <INPUT TYPE=reset></FORM> </BODY></HTML>
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 4 Decoding Perl Script #!/usr/usc/bin/perl $size_of_form_info =$ENV{‘CONTENT_LENGTH’}; read(STDIN, $form_info, $size_of_form_info); $form_info =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; #s is substitute, \dA-Fa-f looks for hex number and stores it in $1 #pack and hex convert the value in $1 to ASCII, e evaluates second part #of the substitute command as an expression, g replaces all occurrences ($field_name, $data) = split (/=/, $form_info); print "Content-type: text/plain", "\n\n"; print "Your input is: $data, right?", "\n"; exit(0);
Background image of page 4
Perl Copyright © Ellis Horowitz 1999-2010 5 Using Extra Path Info to Print File Contents Consider the URL: http://mydomain/cgi-bin/display.pl/myfile.txt where myfile.txt is stored in the document root. This program displays the contents of myfile.txt #!/usr/usc/bin/perl #displays a text file located in document root hierarchy $plaintext_file = $ENV{‘PATH_TRANSLATED’}; print "Content-type: text/plain", "\n\n"; if ($plaintext_file =~ /\.\./) {#prevent . . from being used print "Invalid characters in filename", "\n"; } else { if (open (FILE, "<", $plaintext_file)) { while (<FILE>) { print; } close(FILE); } else { print "Sorry, file cannot be read", "\n"; } } exit(0); open() defines a filehandle FILE and < denotes input <FILE> is a filehandle that reads a line
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 6 Program to Return a GIF Image Invoke this program as: <a href=cgi-bin/displaygif.pl>Click Here</a> #!/usr/usc/bin/perl $gif_image = join("/", $ENV{‘DOCUMENT_ROOT’}, "icons/tiger.gif"); if (open (IMAGE, "<", $gif_image)) { $no_bytes = (stat ($gif_image))[7]; #stat returns a 13 element array whose # 8th element is the file size print "Content-type: image/gif", "\n"; print "Content-length: $no_bytes", "\n\n"; print <IMAGE>; } else { print "Content-type: text/plain", "\n\n"; print "Sorry, cannot open the file $gif_image", "\n"; } exit(0); IMAGE is a filehandle which opens for reading
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2011 for the course CSCI 571 at USC.

Page1 / 58

CGIScriptsandPerl - Lecture Some CGI Scripts and More on...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online