Ns_ConnSetType(3) Routines to manage the HTTP response type

Other Alias

Ns_ConnGetType

SYNOPSIS

#include "ns.h"



char *
Ns_ConnGetType(conn)


void
Ns_ConnSetType(conn, type)

ARGUMENTS

Ns_Conn    conn    (in)
Pointer to open connection.
char    *type    (in)
Character string with response mimetype.


DESCRIPTION

These routines both manage the eventual content-type header which is generated by a later call to Ns_ConnFlush or Ns_ConnFlushDirect as well as manage the appropriate character encoding for text output types.

char *Ns_ConnGetType(conn)
Return the current HTTP mime type (e.g., "text/html; charset=iso-8859-1") or NULL if no type has yet been set.

void Ns_ConnSetType(conn, type)
Sets the mimetype of the response to the given type. A later call to Ns_ConnFlush will include a header of the form content-type: type when generating the response.

CHARSETS AND ENCODINGS

For text types, a call to Ns_ConnSetType can also include an optional "charset=" attribute. If no charset is specified, the server will append a default charset if specified as the outputcharset server configuration variable.

With a given or automatically appended charset for text types, the server will then set the output encoding to the cooresponding Tcl_Encoding, for example, mapping the charset "iso-8859-1" to the Tcl_Encoding equivalent "iso8859-1". All text later sent via Ns_ConnFlush will be first encoded using the determined Tcl_Encoding (calls to Ns_ConnFlushDirect will bypass this encoding step). See the man pages on Ns_GetCharsetEncoding for details on how these mappings are configured.

The charset modification feature was added in later versions of AOLserver to support legacy code which may have been sprinkled with direct calls to set text types without specifying the charset, e.g., calls such as:

   ns_return 200 text/html "<body>hello</body>"

EXAMPLES

The following example demonstrates sending Japanese character data. In this case, assume "utf8string" contains a series of UTF-8 bytes with various Japanese characters. The call to Ns_ConnSetType will setup the appropriate "shiftjis" output Tcl_Encoding to match the given "shift_jis" charset:

   Ns_ConnSetStatus(conn, 200);
   Ns_ConnSetType(conn, "text/html; charset=shift_jis");
   Ns_ConnFlushDirect(conn, utf8string, -1, 0);

The following demonstrates the behavior of the default server charset encoding. Assume the following is set in the config file:

   ns_section ns/server/serverName
   ns_param outputcharset iso-8859-1

In this case, a call to Ns_ConnSetType(conn, "text/html") without a specific charset would be modified to include "charset=iso-8859-1". Based on this modification, the output encoding would be set to the "iso8859-1" Tcl_Encoding.

KEYWORDS

connectionn, response, status, encoding, charset