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 namereturns the value of the argument named
name. 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_valuereturns the value of the argument as a string. If the argument does not exist, the
default: defaults to
method argument_exists :
string -> bool
falseif the named parameter is missing and
method multiple_argument :
string -> cgi_argument list
#multiple_argument namereturns all the values of the argument named
method arguments :
method environment :
method request_method :
[ `DELETE | `GET | `HEAD | `POST | `PUT of cgi_argument ]
method finalize :
unit -> unit
#finalizefor 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
#finalizeat the end of the request cycle (even when its terminated by an uncaught exception when
#config.default_exn_handleris true) so you do not have to worry much about calling it yourself.
method url :
?with_query_string:query_string_spec -> unit -> string
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
#out_channelis commited for the first time or
#env#send_output_header()is 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
method output :
method out_channel :
#pos_outreturns 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 fregisters the function
fto be executed when
#finalizeis called (which is done automatically when the request finishes). The functions are executed in the reverse order in which they were registered.