cgi_util.cc File Reference

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include "cgi_util.h"
#include "util.h"
#include "debug.h"

Include dependency graph for cgi_util.cc:

Go to the source code of this file.

Defines

#define CRLF   "\r\n"
#define FILE_DELIMITER   '/'

Functions

bool do_version (const string &script_ver, const string &dataset_ver)
 Send a version number.
void ErrMsgT (const string &Msgt)
 Logs an error message.
string find_ancillary_file (const string &pathname, const string &ext, const string &dir, const string &file)
 Find a file with ancillary data.
string find_group_ancillary_file (const string &name, const string &ext)
bool found_override (string name, string &doc)
string get_user_supplied_docs (string name, string cgi)
 Look for the user supplied CGI- and dataset-specific HTML* documents.
time_t last_modified_time (const string &name)
string name_path (const string &path)
 Returns the filename portion of a pathname.
bool remove_mime_header (FILE *in)
 Read and discard the MIME header of the stream in.
string rfc822_date (const time_t t)
void set_mime_binary (FILE *out, ObjectType type, const string &ver, EncodingType enc, const time_t last_modified)
void set_mime_error (FILE *out, int code, const string &reason, const string &version)
void set_mime_html (FILE *out, ObjectType type, const string &ver, EncodingType enc, const time_t last_modified)
void set_mime_not_modified (FILE *out)
 Send a `Not Modified' response.
void set_mime_text (FILE *out, ObjectType type, const string &ver, EncodingType enc, const time_t last_modified)


Define Documentation

#define CRLF   "\r\n"

Definition at line 85 of file cgi_util.cc.

Referenced by do_version(), remove_mime_header(), set_mime_binary(), set_mime_error(), set_mime_html(), set_mime_not_modified(), and set_mime_text().

#define FILE_DELIMITER   '/'

Definition at line 81 of file cgi_util.cc.

Referenced by name_path().


Function Documentation

bool do_version ( const string &  script_ver,
const string &  dataset_ver 
)

This sends a formatted block of text to the client, containing version information about various aspects of the server. The arguments allow you to enclose version information about the filter program and the dataset in the message. Either argument (or both) may be omitted, in which case no script or dataset version information will be printed.

Parameters:
script_ver The version of the filter script executing this function.
dataset_ver The version of the dataset.
Returns:
TRUE for success. Always returns true.

Definition at line 106 of file cgi_util.cc.

References CRLF, DAP_PROTOCOL_VERSION, and DVR.

Referenced by DODSFilter::send_version_info().

void ErrMsgT ( const string &  Msgt  ) 

Prints an error message in the httpd system log file, along with a time stamp and the client host name (or address).

Use this instead of the functions in liberrmsg.a in the programs run by the CGIs to report errors so those errors show up in HTTPD's log files.

Returns:
void

Definition at line 307 of file cgi_util.cc.

Referenced by DODSFilter::print_usage().

string find_ancillary_file ( const string &  pathname,
const string &  ext,
const string &  dir,
const string &  file 
)

This function accepts a dataset path name, and searches for a matching ancillary data file name with a very specific set of search rules, given here:

    directory           filename          extension
    same                same            `.'given
    given               same            `.'given
    same                given           `.'given
    given               given           `.'given
    

Where ``same'' refers to the input dataset pathname, and ``given'' refers to the function arguments.

For example, If you call this function with a dataset name of /a/data, an extension of das, a directory of b, and a filename of ralph, the function will look (in order) for the following files:

    /a/data.das
    /b/data.das
    /a/ralph.das
    /b/ralph.das
    

The function will return a string containing the name of the first file in the list that exists, if any.

Note:
This code now checks for pathname.ext 3/17/99 jhrg
Parameters:
pathname The input pathname of a dataset.
ext The input extension the desired file is to have.
dir The input directory in which the desired file may be found.
file The input filename the desired file may have.
Returns:
A string containing the pathname of the file found by searching with the given components. If no file was found, the null string is returned.

Definition at line 173 of file cgi_util.cc.

References DBG.

Referenced by DODSFilter::get_das_last_modified_time(), DODSFilter::get_data_last_modified_time(), DODSFilter::get_dds_last_modified_time(), DODSFilter::read_ancillary_das(), and DODSFilter::read_ancillary_dds().

string find_group_ancillary_file ( const string &  name,
const string &  ext 
)

Assume that name refers to a file that is one of a group of files which share a common `base' name and differ only by some prefix or suffix digits (e.g. 00base, 01base, ... or base00, ... have the base name base). This function looks for a file base.ext.

Parameters:
name The name (full or relative) to one member of a group of files.
ext The extension of the group's ancillary file. Note that ext should include a period (.) if that needs to separate the base name from the extension.
Returns:
The pathname to the group's ancillary file if found, otherwise the empty string ("").

Definition at line 240 of file cgi_util.cc.

References DBG.

Referenced by get_user_supplied_docs().

bool found_override ( string  name,
string &  doc 
)

Look for the override file by taking the dataset name and appending `.ovr' to it. If such a file exists, then read it in and store the contents in doc. Note that the file contents are not checked to see if they are valid HTML (which they must be).

Returns:
True if the `override file' is present, false otherwise. in the later case doc's contents are undefined.

Definition at line 664 of file cgi_util.cc.

string get_user_supplied_docs ( string  name,
string  cgi 
)

Look in the CGI directory (given by cgi) for a per-cgi HTML* file. Also look for a dataset-specific HTML* document. Catenate the documents and return them in a single String variable.

Similarly, to locate the dataset-specific HTML* file it catenates `.html' to name, where name is the name of the dataset. If the filename part of name is of the form [A-Za-z]+[0-9]*.* then this function also looks for a file whose name is [A-Za-z]+.html For example, if name is .../data/fnoc1.nc this function first looks for .../data/fnoc1.nc.html. However, if that does not exist it will look for .../data/fnoc.html. This allows one `per-dataset' file to be used for a collection of files with the same root name.

Note:
An HTML* file contains HTML without the html, head or body tags (my own notation).
Returns:
A String which contains these two documents catenated. Documents that don't exist are treated as `empty'.

Definition at line 726 of file cgi_util.cc.

References find_group_ancillary_file().

Here is the call graph for this function:

time_t last_modified_time ( const string &  name  ) 

Get the last modified time. Assume name is a file and find its last modified time. If name is not a file, then return now as the last modified time.

Parameters:
name The name of a file.
Returns:
The last modified time or the current time.

Definition at line 439 of file cgi_util.cc.

Referenced by DODSFilter::get_das_last_modified_time(), DODSFilter::get_data_last_modified_time(), DODSFilter::get_dataset_last_modified_time(), and DODSFilter::get_dds_last_modified_time().

string name_path ( const string &  path  ) 

Given a pathname, this function returns just the file name component of the path. That is, given /a/b/c/ralph.nc.das, it returns ralph.nc.

Parameters:
path A C-style simple string containing a pathname to be parsed.
Returns:
A C-style simple string containing the filename component of the given pathname.

Definition at line 350 of file cgi_util.cc.

References FILE_DELIMITER.

bool remove_mime_header ( FILE *  in  ) 

Read the input stream in and discard the MIME header. The MIME header is separated from the body of the document by a single blank line. If no MIME header is found, then the input stream is `emptied' and will contain nothing.

Returns:
True if a MIME header is found, false otherwise.

Definition at line 690 of file cgi_util.cc.

References CRLF.

string rfc822_date ( const time_t  t  ) 

Given a constant pointer to a time_t, return a RFC 822/1123 style date.

This function returns the RFC 822 date with the exception that the RFC 1123 modification for four-digit years is implemented.

Returns:
The RFC 822/1123 style date in a C++ string.
Parameters:
t A const time_t pointer.

Definition at line 416 of file cgi_util.cc.

Referenced by set_mime_binary(), set_mime_error(), set_mime_html(), set_mime_not_modified(), and set_mime_text().

void set_mime_binary ( FILE *  out,
ObjectType  type,
const string &  ver,
EncodingType  enc,
const time_t  last_modified 
)

Write an HTTP 1.0 response header for our binary response document (i.e., the DataDDS object).

Parameters:
out Write the MIME header to this FILE pointer.
type The type of this this response. Defaults to application/octet-stream.
ver The version string; denotes the libdap implementation version.
enc How is this response encoded? Can be plain or deflate or the x_... versions of those. Default is x_plain.
last_modified The time to use for the Last-Modified header value. Default is zero which means use the current time.

Definition at line 580 of file cgi_util.cc.

References CRLF, DAP_PROTOCOL_VERSION, DVR, rfc822_date(), and x_plain.

Referenced by DODSFilter::send_blob(), and DODSFilter::send_data().

Here is the call graph for this function:

void set_mime_error ( FILE *  out,
int  code,
const string &  reason,
const string &  version 
)

Generate an HTTP 1.0 response header for an Error object.

Parameters:
out Write the MIME header to this FILE pointer.
code HTTP 1.0 response code. Should be 400, ... 500, ...
reason Reason string of the HTTP 1.0 response header.
version The version string; denotes the DAP spec and implementation version.

Definition at line 619 of file cgi_util.cc.

References CRLF, DAP_PROTOCOL_VERSION, DVR, and rfc822_date().

Here is the call graph for this function:

void set_mime_html ( FILE *  out,
ObjectType  type,
const string &  ver,
EncodingType  enc,
const time_t  last_modified 
)

Generate an HTTP 1.0 response header for a html document.

Parameters:
out Write the MIME header to this FILE pointer.
type The type of this this response.
ver The version string; denotes the libdap implementation version.
enc How is this response encoded? Can be plain or deflate or the x_... versions of those. Default is x_plain.
last_modified The time to use for the Last-Modified header value. Default is zero which means use the current time.

Definition at line 531 of file cgi_util.cc.

References CRLF, DAP_PROTOCOL_VERSION, dods_error, DVR, rfc822_date(), and x_plain.

Here is the call graph for this function:

void set_mime_not_modified ( FILE *  out  ) 

Use this function to create a response signaling that the target of a conditional get has not been modified relative to the condition given in the request. This will have to be a date until the servers support ETags.

Parameters:
out Write the response to this FILE pointer.

Definition at line 647 of file cgi_util.cc.

References CRLF, and rfc822_date().

Referenced by DODSFilter::send_blob(), DODSFilter::send_das(), DODSFilter::send_data(), DODSFilter::send_dds(), and DODSFilter::send_ddx().

Here is the call graph for this function:

void set_mime_text ( FILE *  out,
ObjectType  type,
const string &  ver,
EncodingType  enc,
const time_t  last_modified 
)

Generate an HTTP 1.0 response header for a text document. This is used when returning a serialized DAS or DDS object.

Parameters:
out Write the MIME header to this FILE pointer.
type The type of this this response. Defaults to application/octet-stream.
ver The version string; denotes the libdap implementation version.
enc How is this response encoded? Can be plain or deflate or the x_... versions of those. Default is x_plain.
last_modified The time to use for the Last-Modified header value. Default is zero which means use the current time.

Definition at line 480 of file cgi_util.cc.

References CRLF, dap4_ddx, DAP_PROTOCOL_VERSION, dods_error, DVR, rfc822_date(), and x_plain.

Referenced by DODSFilter::send_das(), DODSFilter::send_dds(), and DODSFilter::send_ddx().

Here is the call graph for this function:


Generated on Wed Jun 27 12:56:46 2007 for libdap++ by  doxygen 1.4.7