Please Help

RudeCGI™ Open Source C++ CGI Library

Version 5.1.0

DETAIL:  FIELD | CONSTR | METHOD CGI

Class rude::CGI


This is the public interface to the entire RudeCGI component.

The interface is separated from its implementation using the bridge pattern.


1. Optional Methods to call first


These methods configure the CGI object before it parses any incoming data.
As such, they must be called before instantiating the CGI object, which triggers the parsing.
Calling these methods after the parsing occurs has no effect.




2. Obtaining the CGI object


After all optional instance methods are called, it is safe to access the single instance of the CGI object.
Since the CGI object uses the Singleton design pattern, it is globally accessable from everywhere in your program.
However, since only one instance ever exists - don't delete it, unless you are absolutely sure your program is done with it.
  • CGI mycgi;

3. Using the CGI object (exploratory methods)


After obtaining the CGI object, the following methods allow you to find out what data was sent..





4. Using the CGI object (data methods)


The rest of the methods deal with accessing a particular piece of data, and they all exist in three forms.
The first form, method(int index), accesses a data-pair at a specified index within the collection, regardless of the fieldname.
The second form, method(const char *fieldname), accesses the first data-pair identified by fieldname,
in the likely event that only one data-pair with the given fieldname was submitted.
For those cases where multiple values were submitted with the same identifying fieldname, the third form,
method(const char *fieldname, int position), allows access to each data-pair associated with the name.


 

5. Cleaning up

Once your program is done using the CGI object, you should free the memory by calling the finish() method.

 



Constructor Summary
  CGI()
  virtual ~CGI()
          
 
Method Summary
 static void addPathMapName( const char* pathname )
          Appends a pathname to the set of path names.
 const char* contenttype( const char* fieldname, int position )
          Returns the MIME content-type of the nth data member identified by fieldname.
 const char* contenttype( const char* fieldname )
          Returns the MIME content-type of the data identified by fieldname.
 const char* contenttype( int index )
          Returns the MIME content-type of the data identified at index within the collection.
 const char* datasource( int index )
          Returns the source (form, path, cookie) of the data found at index within the collection.
 const char* datasource( const char* fieldname, int position )
          Returns the source (form, path, cookie) of the nth data member identified by fieldname.
 const char* datasource( const char* fieldname )
          Returns the source (form, path, cookie) of the data identified by fieldname.
 bool exists( const char* fieldname )
          Check if a certain field exists.
 const char* fieldnameAt( int position )
          Returns the name of the field at the specified index within the data collection.
 const char* filename( int index )
          Returns a safe filename for the uploaded file found at index within the entire collection.
 const char* filename( const char* fieldname )
          Returns a safe filename for the uploaded file identified by fieldname.
 const char* filename( const char* fieldname, int position )
          Returns a safe filename for the nth uploaded file identified by fieldname.
 const char* filepath( const char* fieldname )
          Returns the original filepath of the uploaded data.
 const char* filepath( int index )
          Returns the original filepath of the uploaded data.
 const char* filepath( const char* fieldname, int position )
          Returns the original filepath of the uploaded data.
 static void finish( )
          Frees all memory and shuts down the module.
 static rude::CGI* instance()
          Returns the Singleton instance of the CGI object.
 bool isFile( const char* fieldname, int position )
          Determines if the nth data-pair identified by fieldname is an uploaded file.
 bool isFile( const char* fieldname )
          Determines if the first data-pair identified by fieldname is an uploaded file.
 bool isFile( int index )
          Determines if the data-pair identified at index within the collection is an uploaded file.
 int length( int index )
          Returns the length of the data identified at index within the entire collection.
 int length( const char* fieldname )
          Returns the length of the data identified by fieldname.
 int length( const char* fieldname, int position )
          Returns the length of the nth data member identified by fieldname.
 static void maxPostLength( long bytes )
          Sets the maximum number of bytes that is acceptable for POST'ed data.
 int numValues( const char* fieldname )
          Returns the number of values identified by fieldname that were sent.
 int numValues()
          Returns the total number of key=value pairs that were found.
 static void parseCookies( bool shouldParse )
          Enable / Disable cookie parsing (default enabled).
 static void parsePath( bool shouldParse )
          Enable / disable parsing of PATH_INFO (default enabled).
 static void parsePathMap( bool shouldParse )
          Setting this to true enables the parsing of PATH_INFO using a pathmap.
 void setCaseSensitive( bool isCaseSenstive )
          Turns case-sensitive name matching on/off (default on).
 static void setPathDelimiter( char delimiter )
          Sets the delimiter used between the name and value of data stored in PATH_INFO (default is '=').
 static void setPathMapVoid( const char* emptyname )
          Use this method to set a value in the path that is to be interpreted as empty data (default is "default").
 const char* value( const char* fieldname )
          Returns the value of the data-pair identified by fieldname.
 const char* value( const char* fieldname, int position )
          Returns the nth value of the data-pair identified by fieldname, where n is determined by position.
 const char* value( int index )
          Returns the value of the data-pair that exists at the specified index.
 static const char* version()
          Returns the version of the CGI library
 

Constructor Detail

CGI

protected CGI( rude::cgiparser::Req_AbstractImplementation* implementation );
This method cannot be called - use CGI::instance() instead.

~CGI

public virtual ~CGI();


Method Detail

addPathMapName

public static void addPathMapName( const char* pathname );
Appends a pathname to the set of path names. This allows mapping from PATH_INFO directly into data--pairs.


For example, instead of


http://mydomain.com/mypage?color=red&age=16&name=mark


You can use PATH_INFO, and map the fieldnames color, age, and name directly to the path,


CGI::addPathMapName("color");

CGI::addPathMapName("age");

CGI::addPathMapName("name");


And use the following url instead:


http://mydomain.com/mypage/red/16/mark


contenttype

public const char* contenttype( const char* fieldname, int position );
Returns the MIME content-type of the nth data member identified by fieldname.
See Also:
contenttype(int)

contenttype

public const char* contenttype( const char* fieldname );
Returns the MIME content-type of the data identified by fieldname.
See Also:
contenttype(int)

contenttype

public const char* contenttype( int index );
Returns the MIME content-type of the data identified at index within the collection.
If the data is not an uploaded file, then the content-type defaults to application/x-formdata

datasource

public const char* datasource( int index );
Returns the source (form, path, cookie) of the data found at index within the collection.
Currently, the sources can either be "form", "cookie", or "path".
If the source is "form", then the data pair was obtained from POST'ed data or the Query String.
If the source is "cookie", the the data pair was extraced from the submitted cookies.
If the source is "path", then the data was extracted from PATH_INFO.


Please note: "form" data includes file uploads (multipart/form-data) as well as XML, which are both sent as POST'ed data.

datasource

public const char* datasource( const char* fieldname, int position );
Returns the source (form, path, cookie) of the nth data member identified by fieldname.
See Also:
datasource(int)

datasource

public const char* datasource( const char* fieldname );
Returns the source (form, path, cookie) of the data identified by fieldname.
See Also:
datasource(int)

exists

public bool exists( const char* fieldname );
Check if a certain field exists.
Returns true if at least one data item matching the fieldname was sent (regardless of whether the value is empty or not), false otherwise.

fieldnameAt

public const char* fieldnameAt( int position );
Returns the name of the field at the specified index within the data collection.
If the fieldname is empty, the method returns "".
If the index is out of bounds, the method returns NULL.



filename

public const char* filename( int index );
Returns a safe filename for the uploaded file found at index within the entire collection. It determines the name either by parsing the filepath, or parsing the submitted filename.
Spaces are converted to underscores, and illegal file characters (based on windows systems) are removed.
The filename is also truncated to an acceptable length.

filename

public const char* filename( const char* fieldname );
Returns a safe filename for the uploaded file identified by fieldname.
See Also:
filename(int)

filename

public const char* filename( const char* fieldname, int position );
Returns a safe filename for the nth uploaded file identified by fieldname.
See Also:
filename(int)

filepath

public const char* filepath( const char* fieldname );
Returns the original filepath of the uploaded data.
Returns:
The original filepath if it exists. If the data is not an uploaded file, then this method returns NULL.

filepath

public const char* filepath( int index );
Returns the original filepath of the uploaded data.
Returns:
The original filepath if it exists. If the data is not an uploaded file, then this method returns NULL.

filepath

public const char* filepath( const char* fieldname, int position );
Returns the original filepath of the uploaded data.
Returns:
The original filepath if it exists. If the data is not an uploaded file, then this method returns NULL.

instance

public static CGI* instance();
Returns the Singleton instance of the CGI object.
The first call to this method triggers the parsing of all incoming data.
Subsequent calls to this method simply return the initialized CGI instance.


Example:



CGI *parser = CGI::instance();


isFile

public bool isFile( const char* fieldname, int position );
Determines if the nth data-pair identified by fieldname is an uploaded file.


Returns:
true if the data pair is an uploaded file, false otherwise


isFile

public bool isFile( const char* fieldname );
Determines if the first data-pair identified by fieldname is an uploaded file.


Returns:
true if the data pair is an uploaded file, false otherwise


isFile

public bool isFile( int index );
Determines if the data-pair identified at index within the collection is an uploaded file.


Returns:
true if the data pair is an uploaded file, false otherwise


length

public int length( int index );
Returns the length of the data identified at index within the entire collection.
Most form data submitted is string data, this method is used primarily for uploaded files which may contain NULL bytes.

length

public int length( const char* fieldname );
Returns the length of the data identified by fieldname.
See Also:
length(int)

length

public int length( const char* fieldname, int position );
Returns the length of the nth data member identified by fieldname.
See Also:
length(int)

maxPostLength

public static void maxPostLength( long bytes );
Sets the maximum number of bytes that is acceptable for POST'ed data. The default value, 0, allows an unlimited length.
If a non-zero positive number is used, then parsing of POST'ed data will not occur if the length is equal to or greater than the maximum allowable length.
If you don't expect large amounts of data to be sent to the application, setting a maximum POST thresh-hold will prevent D.O.S. attacks involving large
data transmissions. To eliminate POSTed data altogether, use the value 1. Note that parsing of the query-string, PATH_INFO, and cookies will still occur
regardless of whether or not POST'ed data is ignored.


Example:



CGI::maxPostLength( 1048576 ); // 1 MB File Upload Limit


Parameters:
bytes - A value from 0 to the maximum positive signed long value.

numValues

public int numValues( const char* fieldname );
Returns the number of values identified by fieldname that were sent. For example, if the following query string were sent:


?color=blue&color=red&color=yellow


Then numValues("color") would return 3. Note that the value associated with a fieldname can be empty ("").
As such, the following query string would yield the same result as the one above:


?color=&color=&color=yellow


This method can also be used to determine if a field was sent at all (if the result is 0).
However, the method exists() is also available for this purpose.



numValues

public int numValues();
Returns the total number of key=value pairs that were found.
The number includes values obtained from PATH_INFO and HTTP_COOKIE in addition to the GET and POSTed data.

parseCookies

public static void parseCookies( bool shouldParse );
Enable / Disable cookie parsing (default enabled).
Setting this to false disables the parsing of cookie data.


Example:



CGI::parseCookies( false );



parsePath

public static void parsePath( bool shouldParse );
Enable / disable parsing of PATH_INFO (default enabled).
Setting this to false disables the parsing of PATH_INFO using a character delimiter.
If the delimiting character is not present, the path info is ignored.
If the delimiting character is not present and parsePathMap() is enabled,
then the path info for the section in question is Mapped to the next pathmap index.


Example:



CGI::parsePath( false );



parsePathMap

public static void parsePathMap( bool shouldParse );
Setting this to true enables the parsing of PATH_INFO using a pathmap. By default, this is set to false.

If set to true, parsing by path map occurs after parsing by delimiter.
Paths are mapped only after failing to be parsed by delimiter - either by not haveing the delimiter present in the path section,
or having parsePath() set to false.


Example:



CGI::parsePathMap( true );



setCaseSensitive

public void setCaseSensitive( bool isCaseSenstive );
Turns case-sensitive name matching on/off (default on).
By default, matching the names of incoming data is case-sensitive. Setting this to false makes the matching
case-insensitive.


Example:



CGI *parser = CGI::instance();
parser->setCaseSensitive(false);
const char *emailAddress1 = parser->value("EmAiL"); // will match "email"
parser->setCaseSensitive(true);
const char *emailAddress2 = parser->value("EmAiL"); // will NOT match "email"


setPathDelimiter

public static void setPathDelimiter( char delimiter );
Sets the delimiter used between the name and value of data stored in PATH_INFO (default is '=').


So, for example, if PATH_INFO was:




/color=light%20blue/age=16/sessionid=429842


Then, the fields and values extracted from PATH_INFO would be:







NameValue
color light blue
age16
sessionid429842


If you expected the data to be stored in the following manner instead:



/color:light%20blue/age:16/sessionid:429842


Then you would set the delimiter to ':'.


If the delimiter is set to NULL, then path data is not extracted by delimiters, only the PathMap if it exists.



setPathMapVoid

public static void setPathMapVoid( const char* emptyname );
Use this method to set a value in the path that is to be interpreted as empty data (default is "default").


So, for example, if you set up the path map as follows:



CGI::addPathMapName("color");
CGI::addPathMapName("age");
CGI::addPathMapName("name");


Then, given the following url:



http://mydomain.com/program.exe/red/default/mark


Then the following data will be extracted:






NameValue
colorred
namemark


Note: age is ignored, since the empty value "default" was provided )



value

public const char* value( const char* fieldname );
Returns the value of the data-pair identified by fieldname.
If more than one data-pair exists with the given name, then this returns the value of the first data-pair,
in essence, it is the same as calling value(fieldname, 0) (see below) .
If no value exists for the fieldname, or the fieldname is not a valid identifier, the method returns "".
This method will never return NULL.

value

public const char* value( const char* fieldname, int position );
Returns the nth value of the data-pair identified by fieldname, where n is determined by position.
If no value exists for the fieldname at the given position, the method returns "".

If the fieldname is not a valid identifier, or the position is out of bounds, then the method returns NULL.



Example: print out selected categories (from a form with checkboxes named "cat")



cout << "Selected Categories :\n";
int x=0;
const char *category;
while(category = parser->value("cat", x)) // will fail after last position
{
cout << category << "\n";
x++;
}



value

public const char* value( int index );
Returns the value of the data-pair that exists at the specified index.
If no value exists for the data-pair at that index, the method returns the empty string ("").
If the index is out of bounds, the method returns NULL.



Example: Print out all name-value pairs submitted


int total=parser->numValues();

for(int x=0; x< total; x++)

{

cout << parser->fieldnameAt(x) << "=" << parser->value(x) << "\n";

}




version

public static const char* version();
Returns the version of the CGI library

DETAIL:  FIELD | CONSTR | METHOD CGI
Generated on October 05, 2003 at 13:29