spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / perl / cookbook / chap4 current pageTo page 2To page 3To page 4To page 5To page 6
[next]

mod_perl Developer's Cookbook

Developer News
SaaS Tool Offers Custom Database Development
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Chapter 4: Communicating with the Apache Server

Introduction

In addition to the Apache request record, there are also two other important data structures of which you should be aware—the server record and the connection record. The Apache server record contains information about the server itself, such as the values set by the ErrorLog and ServerAlias directives. The Apache connection record contains information about the current connection, such as the IP address of the initiating client and the authentication method for the request. As with the request record, the server and connection records are both defined in src/include/httpd.h in the Apache sources.

Access to each of these records is granted through two separate classes, appropriately named Apache::Server and Apache::Connection. Through these two classes, the Apache::Log class, and a few methods provided by the base Apache class, accessing and sometimes modifying properties logically connected to the server and its operation are possible. Although many of the following methods are not used frequently, they are all good to understand and suitable for scribbling on your mental chalkboard for later use.

4.1. Accessing the Apache::Server Object

You want to access various properties of the Apache server, such as the ServerName or TimeOut directives.

Technique

Create an Apache::Server object and use its methods.

package Cookbook::ViewServer;

use Apache::Constants qw(OK);
use Apache::Log;

use strict;

sub handler {

 # Get the Apache request object...
 my $r = shift;

 # ... and the Apache::Server object for this request.
 my $s = $r->server;

 $r->send_http_header('text/plain');

 # Iterate through all the configured servers.
 for (my $s = Apache->server; $s; $s = $s->next) {
  print "User directive:    ", $s->uid,       "\n";
  print "Group directive:    ", $s->gid,       "\n";
  print "Port directive:    ", $s->port,      "\n";
  print "TimeOut directive:   ", $s->timeout,     "\n";
  print "ErrorLog directive:  ", $s->error_fname,   "\n";
  print "LogLevel directive:  ", $s->loglevel,    "\n";
  print "ServerName directive: ", $s->server_hostname, "\n";
  print "ServerAdmin directive: ", $s->server_admin,  "\n";

  print "ServerAlias directives:\n" if $s->is_virtual;
  print "\t$_\n" foreach @{$s->names};
  print "-" x 30,                    "\n";
 }

 return OK;
}
1;

Comments

The previous chapter discussed the Apache request record and the various pieces of per-request data it holds. In addition to those fields already discussed, the request record holds a pointer to information about the Apache server responsible for servicing the current request. The Apache::Server class provides the interface to many of the base server configuration directives from the Apache server record. An Apache::Server object can be created in two ways, either by digging it out from the current request:

my $s = $r->server;

or by retrieving it directly through the Apache class:

my $s = Apache->server;

The difference between the two methods is that $r->server() returns the Apache::Server object associated with the current request. Apache->server(), on the other hand, returns the main Apache::Server object in Apache's internal list of configured servers. The convention is to place the Apache::Server object in the $s variable, which is how it will be presented throughout this book.

In most cases, you will want to retrieve the Apache::Server object directly from the request object, because that is the server directly involved in the current request. However, if you are interested in inspecting or altering any of the properties of the Apache server outside of a request, such as during module initialization, in <Perl> sections, or in startup.pl script, you can use Apache->server().

A partial list of Apache::Server methods is given in Table 4.1.

Table 4.1 Some Apache::Server Methods

Method

Example Value

Details

error_fname()

logs/error_log

Returns the value of the filename specified by the ErrorLog directive.

gid()

99

The numeric value of the group specified by the Group directive.

is_virtual()

TRUE

Returns true if the server is a virtual server.

loglevel()

3

Provides access to the numeric value Apache uses to represent the LogLevel setting. Requires use()ing the Apache::Log class.

names()

array reference

Returns an array reference containing the names of any configured ServerAlias directives.

next()

Apache::Server object

Returns the next server in Apache's internal list of configured servers.

port()

80

Returns the value given by the Port directive.

server_admin()

authors@modperlcookbook.org

Returns the value of the ServerAdmin directive.

server_hostname()

helm.example.com

Returns the value of the ServerName directive.

timeout()

300

Provides access to the TimeOut directive.

uid()

99

Returns the numeric value of the user specified by the User directive.


The very important thing to remember is that even though the Apache::Server object is available at request time through the Apache request object, the scope of its attributes outlasts the current request. In fact, any changes you make to values in the server record are maintained for the life of the child, and will affect any requests to that same child process. Thus, it pays to be careful in your manipulation of server attributes and to know that changes you make will not be localized to the current request.

Although the Apache::Server class offers a direct hook into the raw server configuration, the base Apache class has a few methods that dig out some of the same information but are more applicable to most programming needs. For instance, the get_server_name() and get_server_port() methods from the Apache class are both sensitive to the UseCanonicalName directive, making them preferable over direct access to the corresponding fields in the server record at request time.


home / programming / perl / cookbook / chap4 current pageTo page 2To page 3To page 4To page 5To page 6
[next]

Copyright © Pearson Education and


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: March 18, 2002
Revised: March 18, 2002


URL: http://webreference.com/programming/perl/cookbook/chap4/