Mdc::CGI

Mdc::CGI


Package Name

 Mdc::CGI  v1.092b - 8:21 PM 2004-11-10


Synopsis

Routines to read CGI input, cookie and client type.

 use Mdc::CGI;
 my $CGI = new Mdc::CGI;
 die "Error: $CGI->{error}" if $CGI->{error};
 my $name = $CGI->{query}{name};
 $CGI->Print( "Hello $name!" );


Description


Methods

new

Create a new cgi object. Only one object is allowed to be created at a time. You may undef the first object before creating another, but why would you want to to that? Parameters may be supplied during object creation. These parameters are described below.

max_content_length
Defines the maximum content length for posted data. If the posted content length is larger than this value, the input data will not be parsed and an error code will be returned. The default value is 4096.

max_key_length
Defines the maximum character length in bytes allowed for the keys. If the _parse_query method encounters a key which has a length that exceeds this value, the key and value pair will be dropped and an error code retured. The default key length is 32 bytes.

max_value_length
Defines the maximum character length in bytes allowed for input values. If the _parse_query method encounters a value which has a length that exceeds this value, the key and value pair will be dropped and an error code retured. The default value length is 32767 bytes.

max_cookie_length
Defines the maximum character length in bytes allowed for the client cookie header. If the _parse_cookie method encounters a cookie string that is larger than this value, the cookie will not be parsed and will return an error code.

trap_html
A flag that if set to ``1'' will cause the _parse_query method to return an error code if any HTML is encounterd in the input data. The default value is ``1''.

EXPIRES
Sets the expiration date of the client cookie in days, hours or minutes from the time the SetCookie method was called. Seconds are not accepted as there is no garantee the the system time on the client machine will be accurate to within a few seconds of your server's system time. The cookie_expires parameter can contain a positive or negative integer followed by an h, m or D. The default is blank.
 my $CGI = new Mdc::CGI( EXPIRES => '10D' );    # cookie will expire in 10 days

If the cookie_expires parameter is not defined or is blank, the cookie header will not be given an expiration time which has the effect of being a ``session cookie'', meaning it will expire only after the client browser has been closed.

CONTEXT
 my $CGI = new Mdc::CGI( CONTEXT => 'path' );

The CONTEXT paramter adjusts the PATH and DOMAIN values of the cookie header. Using this paramter allows you to expose the client cookie to other scripts on your site or even to other sites within your domain. The default is 'script' which will only allow the script which issued the cookie to read it again.

 Valid context values:

 script         Only current script will be able to retrieve cookies from client

 path           Expose client cookie to other scripts in same path

 site           Expose client cookie to other scripts and paths in same web site

 domain         Expose client cookie to other scripts within the current domain
PATH
Override the default cookie PATH parameter.

DOMAIN
Override the default cookie DOMAIN parameter.

HTTPONLY
By default, the HTTPONLY attribute will be added to the cookie header to prevent JavaScript on most browsers from accessing the cookie. Resetting this attribute to blank or zero will allow JavaScript to access the client cookie.

SECURE
If set to 1, the SECURE attribute will be added to the cookie header. This has the result of causing the client browser to only return the cookie if in secure mode. Default value is 0.


Parsing Client Data

At the time of object creation, several parsing routines will gather all of the available client and server data and place it into separate hashes. If an error occures during any of the parsing routines, an error code will be assigned to the object's 'error' attribute.

These routines are described below:

query
The 'query' attribute of the CGI object contains a hash reference to the parsed query data or the posted data. Several checks are performed on the query data before it is assigned to the query hash. An error code will be assigned to the object's 'error' attribute if any of the following checks fail.
 - non-ascii characters
 - html and meta characters
 - content length
 - key size
 - value size
 - multiple keys
 - invalid keys

Examples:

 print $CGI->{query}{key};              # directly access a query value
                                        # - or -
 my $q = $CGI->{query};                 # get the hash reference and...
 print $q->{key};                       # make your code look a little cleaner

agent
script
cookie
url
referer

_parse_cookie

Parse client cookie. Data will be assigned to the hash reference that is provided as argument.

 my $err = $CGI->_parse_cookie(\%cookie);

_parse_url

Parse the current SCRIPT_URI. If SCRIPT_URI is not supplied by your web server, it will be constructed. Results are placed in the supplied hash reference.

 my $err = $CGI->_parse_url( \%url );

SetCookie

Set the outgoing client cookie.

 $CLIENT->SetCookie( key => value, ... );
key
A key that will be assigned to the client cookie. Currently assigned key/value pairs will be retained.

value
A value that will be associated with the key.

CONTEXT
See b<new> above.

ClearCookie

Clear the client cookie. Supply a list of keys to be cleared.

 $CGI->ClearCookie( LIST );

Print

Print a scalar or list to the client.

 $CGI->Print( "Hello ", "World!" );

If imported at the ``use'' statement, this method may be called directly as a stand-alone subroutine.

 Print "Hello ", "World!";


Examples


Error Codes

 1201   Invalid character(s) encountered in input data
 1202   Invalid character(s) encountered in input key
 1203   Input key exceeds maximum length
 1204   Input value exceeds maximum length
 1205   Invalid character(s) encountered in cookie data
 1206   Cookie data exceeds maximum length
 1207   Invalid method
 1208   Content exceeds maximum length
 1209   Possible HTML or meta characters found in input data
 1210   Possible HTML or meta characters found in referer string
 1211   Input key not defined
 1212   Input value not defined
 1213   Invalid character(s) in HTTP_USER_AGENT
 1214   Invalid character(s) in REMOTE_ADDR
 1215   Hex character(s) encountered in input data
 1216   Only one CGI object may be defined at a time


Copyright

 Mdc::CGI Copyright (c) 2003-04 Mark K Mueller. All rights reserved.
 http://www.markmueller.com/

MkM 7:10 PM 2004-07-18