class type cgi =
Object symbolizing a CGI-like request/response cycle.
This is the minimal set of services a connector must provide. Additional methods may be defined for specific connectors.
method argument :
string -> cgi_argument
#argument name returns the value of the argument named
If the argument appears several times, only one of its
instances is used.
Not_foundif no such argument exists.
method argument_value :
?default:string -> string -> string
#argument_value returns the value of the argument as a
string. If the argument does not exist, the
default: defaults to
method argument_exists :
string -> bool
false if the named parameter is
method multiple_argument :
string -> cgi_argument list
#multiple_argument name returns all the values of the
method arguments :
The complete list of arguments.
method environment :
The environment object. This object is the "outer layer" of the activation object that connects it with real I/O channels.
method request_method :
[ `DELETE | `GET | `HEAD | `POST | `PUT of cgi_argument ]
The HTTP method used to make the request.
method finalize :
unit -> unit
This method calls
#finalize for every CGI argument
(including the possible one of PUT) to ensure that all files
are deleted. It also executes all functions registered with
#at_exit. It does not close the in/out channels, however.
This method is not registered in the garbage collector, and it
is a bad idea to do so. However, all connectors offered in
Netcgi automatically call
#finalize at the end of the
request cycle (even when its terminated by an uncaught exception
#config.default_exn_handler is true) so you do not have
to worry much about calling it yourself.
method url :
?with_query_string:query_string_spec -> unit -> string
Returns the URL of the current CGI-like script. (Note that it may differ from the actual URL that requested the script if, for example, rewriting rules were specified in the web server configuration.)
protocol: The URL scheme. By default, the URL scheme is used that is described in the environment
with_authority: Whether to include authority part (e.g. http or https) of the URL, and if yes, from which source. Default:
with_script_name: Whether to include the part of the URL path identifying the CGI script, and if yes, from which source. Default:
with_path_info: Whether to include the rest of the URL path exceeding the script name, and if yes, from which source. Default:
with_query_string: Whether to include a query string, and if yes, which one. Only arguments with
`Memorywill be added. Default:
`None, i.e. no query string.
method set_header :
?set_cookie:Nethttp.cookie list ->
?set_cookies:Cookie.t list ->
?style_type:string -> ?fields:(string * string list) list -> unit -> unit
Sets the header (removing any previous one). When the output
channel supports transactions, it is possible to set the
header (possibly several times) until the
commited for the first time or
called. When there is no support for transactions, the header
must be set before the first byte of output is written.
#set_header is called a second time, it will overwrite
all the header fields.
status: Sets the HTTP status of the reply according to RFC 2616. Defaults to "no status", but the server normally complements an
`Okstatus in this case.
content_type: Sets the content type. Defaults to
content_length: Sets the content length (in bytes). Default: No such field.
set_cookie: Deprecated, use
set_cookies: Sets a number of cookies. Default:
. Remember that the browser may not support more than 20 cookies per web server. You can query the cookies using
env#cookie. If you set cookies, you want to think about an appropriate
cachesetting. You may also want to add a P3P header (Platform for Privacy Preferences) -- otherwise your cookies may be discarded by some browsers.
cache: Sets the cache behavior for replies to GET requests. The default is
`Unspecified. It is strongly recommended to specify the caching behaviour!!! You are on the safe side with
`No_cache, forcing every page to be regenerated. If your data do not change frequently,
`Max_age ntells the caches to store the data at most
filename: Sets the filename associated with the page. This filename is taken for the "save as..." dialog. Default:
"", i.e. no filename. Note: It is bad practice if the filename contains problematic characters (backslash, double quote, space), or the names of directories. It is recommended that you set
content_typeto "application/octet-stream" for this feture to work with most browsers and, if possible, to set
content_lengthbecause that usually improves the download dialog.)
script_type: Sets the language of the script tag (for HTML replies). It is recommended to use this field if there are
ONXXXattributes containing scripts before the first
<SCRIPT>element, because you cannot specify the script language for the
style_type: Sets the language of the style tag (for HTML replies). It is recommended to use this field if there are
STYLEattributes containing scripts before the first
<STYLE>element, because you cannot specify the style language for the
style_typemust be a media type, e.g. "text/css". Default: no language is specified.
fields: Sets additional fields of the header. Default:
method set_redirection_header :
?set_cookies:Cookie.t list ->
?fields:(string * string list) list -> string -> unit
Sets the header such that a redirection to the specified URL is performed. If the URL begins with "http:" the redirection directive is passed back to the client, and the client will repeat the request for the new location (with a GET method). If the URL begins with "/", the server performs the redirection, and it is invisible for the client.
method output :
method out_channel :
The output channel to which the generated content is intended
to be written. The header is not stored in this channel, so
#pos_out returns the size of the DATA in bytes (useful to
set Content-Length). Note that HEAD requests must not send
back a message body so, in this case, all data sent to this
channel is discarded. This allows your scripts to work
unmodified for GET, POST and HEAD requests.
The output channel may have transactional semantics, and
because of this, it is an
Implementations are free to support transactions or not.
After all data have been written, the method
must be called, even if there is no support for
cgi # out_channel # output_string "Hello world!\n"; cgi # out_channel # commit_work()
Example for an error handler and a transaction buffer: If an error happens, it is possible to roll the channel back, and to write the error message.
try cgi # set_header ... (); cgi # out_channel # output_string "Hello World!"; ... cgi # out_channel # commit_work(); with err -> cgi # out_channel # rollback_work(); cgi # set_header ... (); cgi # out_channel # output_string "Software error!"; ... cgi # out_channel # commit_work();
method at_exit :
(unit -> unit) -> unit
#at_exit f registers the function
f to be executed when
#finalize is called (which is done automatically when the
request finishes). The functions are executed in the reverse
order in which they were registered.