[ Table Of Contents | Keyword Index ]

ns_return(n) 4.99.2 "NaviServer Built-in Commands"

NAME

ns_return - Construct and send a response

TABLE OF CONTENTS

    TABLE OF CONTENTS
    SYNOPSIS
    DESCRIPTION
    COMMANDS
    EXAMPLES
    SEE ALSO
    KEYWORDS

SYNOPSIS

ns_return ?-binary? status mimetype body
ns_respond ?-status status? ?-type mimetype? ?-length length? ?-headers setid? ?-string body? ?-binary body? ?-file filepath? ?-fileid channel?
ns_returnfile status mimetype filepath
ns_returnfp status mimetype channel length
ns_headers status ?mimetype? ?length?
ns_write data ?data ...?
ns_connsendfp channel length

DESCRIPTION

These commands are used to construct a response and send it to the requesting client.

ns_return , ns_respond, ns_returnfile and ns_returnfp return complete responses with body content, while ns_headers, nw_write and ns_connsendfp are used to build up a response in stages.

Unless otherwise noted, these commands return true if the command failed and false otherwise. All of them can raise an error if their arguments are incorrect.

COMMANDS

ns_return ?-binary? status mimetype body
Return a complete response to the client consisting of the given body and a set of automatically constructed headers. status is an integer HTTP status code. mimetype is the mime-type of the body.

If body is a binary object or the -binary option is given then the Tcl body object will be converted to a byte-array and no encoding conversion will take place. Otherwise, the encoding in effect for the current connection will be used to encode the body

After the command completes the connection is returned to the driver thread to be closed or monitored for keep-alive and the calling connection thread continues to execure code.

ns_respond ?-status status? ?-type mimetype? ?-length length? ?-headers setid? ?-string body? ?-binary body? ?-file filepath? ?-fileid channel?
Return a complete response to the client using exactly one of -string, -binary, -file or -fileid as the body.

After the command completes the connection is returned to the driver thread to be closed or monitored for keep-alive and the calling connection thread continues to execure code.

-status status
The HTTP status code. Default 200.

-type mimetype
The mime-type of the response body. Default */*.

-headers setid
A set of headers which will replace any pending headers for the response.

-string body
The Tcl string to send as the response body. The body may be encoded into an apprpriate character set if required by the client and server settings.

-binary body
The byte-array representation of the given Tcl object will be used as the reponse body. No character set conversion will be done.

-file filepath
The file identified by the given pathname will be sent as the response body. No character set conversion will be done.

-fileid channel
length bytes of the given channel will be used as the response body. No character set conversion will be done.

-length length
Specifies the number of bytes to send from the channel.
ns_returnfile status mimetype filepath
Return the given file as the response body. Headers are automatically constructed using status, mimetype and any pending headers.

After the command completes the connection is returned to the driver thread to be closed or monitored for keep-alive and the calling connection thread continues to execure code.

ns_returnfp status mimetype channel length
Return length bytes from channel as the response body. The current offset of the channel is the beginning of the body, and the offset will be left at length bytes.

After the command completes the connection is returned to the driver thread to be closed or monitored for keep-alive and the calling connection thread continues to execure code.

ns_headers status ?mimetype? ?length?
Construct a set of response headers using the given status, mimetype, length, and any pending headers set by e.g. ns_setcookie or manually via ns_conn outputheaders.

The headers are queued but not sent. No further headers may be set. The headers will be written to the client when data is written, e.g. by ns_write or ns_connsendfp. If no data is written, the headers will be flushed to the client when the connection is closed.

ns_headers returns 0 (zero).

ns_write data ?data ...?
Write data directly to the client. No headers are sent.

Binary data will be written directly to the connection. Text data may be encoded according to the client and server settings.

ns_write will send data more efficiently if you pass it multiple arguments rather than concatenating them yourself.

ns_write returns true if all the data was written successfully and false otherwise.

After the command completes the connection remains open and available in the calling connection thread.

ns_connsendfp channel length
Send length bytes from channel directly to the client. No headers are sent, no character encoding is done.

An error is raised if length bytes could not be sent or if the channel is invalid.

After the command completes the connection remains open and available in the calling connection thread.

EXAMPLES

A traditional greeting:

 
ns_register_proc GET /hello {
    ns_return 200 text/plain "Hello World!"
}

SEE ALSO

ns_adp_puts , ns_guesstype , ns_register_fastpath, ns_returnerror

KEYWORDS

charset , response , return , status