• Aucun résultat trouvé

mod_actions

Dans le document Apache Server2 Apache Server2 (Page 149-153)

mod_ext_filter Filtering output with external programs.

mod_actions

The mod_actionsmodule is compiled by default. mod_actionsenables you to run a CGI script based on MIME-typeor on the HTTP request method. It offers these directives.

Action

The Actiondirective enables you to associate an action for a specific MIME type.

The action is usually a CGI script that processes the file being requested. This allows you to execute a CGI script for a given MIME type.

Syntax: Action MIME_type cgi_script

Context:Server config, virtual host, directory, per-directory access control file (.htaccess)

Override:FileInfo

For example, the following directive makes Apache run the specified script when-ever an HTML file is requested:

Action text/html /cgi-bin/somescript.pl

The script receives the URL and file path of the requested document via the stan-dard CGI PATH_INFOand PATH_TRANSLATEDenvironment variables. This can be useful in developing filter scripts. This section discusses one such filter script.

When a text file (.txt) is requested via the Web, it appears on the Web browser in a less-than-desirable format, because line breaks are not translated by most Web browsers in any manner. Usually, most text files appear as a large paragraph. By using the Actiondirective, you can develop a better solution.

For a more interesting example, let’s say that you now want to develop a solution that not only displays the text files better on the Web browser, but also inserts a

copyright message at the end of each text file. To accomplish this, you need to do two things. First, add the following directive in httpd.conffile:

Action plain/text /cgi-bin/textfilter.pl

Then, develop the Perl script textfilterwhich will display the text file the way you wish. Listing 5-1 shows one such script. You can find this script on the accom-panying CD-ROM in the Scripts/Chapter 5directory.

Listing 5-1: textfilter.pl

#!/usr/bin/perl

#

# Script: textfilter.pl

#

# Purpose: This filter script converts plain text files

# into an HTML document but keeps the text layout

# as is.

#

# Copyright (c) 2001 by Mohammed J. Kabir

#

# License: GPL

#

# The copyright message file is always stored in

# the server’s document root

# directory and is called copyright.html.

#

my $copyright_file = $ENV{DOCUMENT_ROOT} . “/copyright.html”;

# Get the requested document’s path

my $path_translated = $ENV{PATH_TRANSLATED};

# Other variables needed for storing data my $line;

my @text;

my @html;

# Store the path info and the file name of requested doc in an array

@filename = split(/\//,$path_translated);

# Because HTML tags are used to display the text file,

# lets print the text/html content header.

print “Content-type: text/html\n\n”;

# Read the document requested and store the data

# in @text array variable

Continued

Listing 5-1 (continued)

@text = &readFile($path_translated);

# Now print the following HTML document tags.

# These tags will be sent before the actual document content

#

# Now print each line stored in the @text array

# (that is, the content of the document requested)

#

foreach $line (@text) { print $line; }

# Now read the copyright file and store the content

# in the @html array variable

#

@html = &readFile($copyright_file);

# Print each line stored in the @html array (that is,

# the content of the copyright message file)

#

foreach $line (@html){ print $line; }

# Exit the filter exit 0;

sub readFile {

#

# Subroutine: readFile

# Purpose: Reads a file if it exists or else prints

# an error message and exits script

#

# Get the name of the passed file name and store

# it in variable $file my $file = shift;

# Local buffer variable my @buffer;

# If the file exists, open it and read all the

# lines into the @buffer array variable if(-e $file) {

open(FP,$file) || die “Can not open $file.”;

while(<FP>){

push(@buffer,$_);

}

close(FP);

} else {

push(@buffer,”$file is missing.”);

}

# Return the content of the buffer.

return (@buffer);

}

The preceding script reads the requested text file and prints out the content inside a few HTML tags that enable the content to be displayed as is. This is accomplished by using the HTML tag <PRE>. After the content is printed, the copyright message file content is inserted at the end of the output. This enables a copyright message to be printed with each requested text file. Figure 5-1 shows an example output where a text file is being displayed on the Web browser.

Figure 5-1: Output of textfilter.pl

As you can see, the requested filename appears as the title. The document is block quoted, and a custom copyright message is printed. The copyright message file is stored in the document’s root directory. The file used in this example is:

</PRE>

<BLOCKQUOTE>

<CENTER>

<HR>

Copyright (c) 2001 Mohammed J. Kabir (kabir@mobidac.com})

</CENTER>

</BODY>

</HTML>

Script

The Scriptdirective is like the Actiondirective, but instead of associating an action with a MIME-type, it associates the action with an HTTP request such as GET, POST, PUT, or DELETE. The CGI script receives the URL and file path of the requested document using the standard CGI PATH_INFOand PATH_TRANSLATED environment variables.

Syntax:Script method cgi-script Context:Server config, virtual host, directory

This directive defines default action. In other words, if you have defined the following:

Script POST /cgi-bin/deafult_post.pl

in an Apache configuration file (such as srm.conf), then whenever a request is made via the HTTP POSTmethod, it will be processed as usual, unless the default action specified by the directive needs to be used. For example, the following HTML form does not specify a CGI script as its action:

<FORM METHOD=”POST”>

Enter Name: <INPUT TYPE=TEXT NAME=”name” SIZE=25>

<INPUT TYPE=SUBMIT VALUE=”Click Here”>

</FORM>

If a name is submitted by a user via this form, there is no specified CGI script to process the information, in which case, the default POSTaction /cgi-bin/

default_post.plscript is run. However, if the <FORM . . .>tag is changed to:

<FORM ACTION=”/cgi-bin/form_processor.pl” METHOD=”POST”>

then whenever the form is submitted, the /cgi-bin/form_processor.plscript is called as usual. What you do in the default action script is up to you. In an Internet service provider setup, I recommend making the default script print meaningful messages, so that the HTML form developer user can get a clue about what he or she is doing incorrectly.

In case of the GETrequest, the default action is used only if the request accompa-nies query data. For example, www.yoursite.com/somefile.htmlis processed as usual, but if a request such as http://www.yoursite.com/somefile.

html?some=data is received, the default action for GETwill be run.

Dans le document Apache Server2 Apache Server2 (Page 149-153)