Difference between revisions of "Ns startcontent"
Jump to navigation
Jump to search
m |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
'''NAME''' | '''NAME''' | ||
− | : ns_startcontent - | + | : ns_startcontent - Configures connection for encoding non-binary content to be returned with ns_write |
'''SYNOPSIS''' | '''SYNOPSIS''' | ||
− | : '''ns_startcontent''' ''?-type | + | : '''ns_startcontent''' ''?-type content-type_and_charset'' | -charset charset?'' |
'''DESCRIPTION''' | '''DESCRIPTION''' | ||
− | : This command is used to set the | + | : This command is used to set the appropriate encoding prior to returning the content part of the response via [[ns_write]]. |
+ | |||
+ | : If you write the headers to the client with ns_write instead of letting AOLserver do it (via ns_return or ns_respond), then AOLserver does not parse the content-type. You must explicitly tell it what charset to use immediately after you write the headers, by calling ns_startcontent in one of these forms: | ||
+ | |||
+ | * '''ns_startcontent''' | ||
+ | : Tells AOLserver that you have written the headers and do not wish the content to be translated. | ||
+ | * '''ns_startcontent''' -charset ''charset'' | ||
+ | : Tells AOLserver that you have written the headers and wish the following content to be translated to the specified charset. | ||
+ | * '''ns_startcontent''' -type ''content-type_and_charset'' | ||
+ | : Tells AOLserver that you have written the headers and wish the following content to be translated to the charset specified by content-type, which should be the same value you sent to the client in the Content-Type header. If content-type starts with text/ and does not contain a charset parameter, AOLserver 4.0 uses the server default as configured in ns/parameters OutputCharset. AOLserver 4.5 will actually throw an error if charset is not specified, so you may wish to redefine ns_startcontent in Tcl as listed in the notes below. | ||
+ | |||
'''EXAMPLES''' | '''EXAMPLES''' | ||
+ | : From [http://dqd.com/~mayoff/encoding-doc.html Character Encoding in AOLserver]: | ||
+ | # Assume japanesetext.html_sj is stored in Shift-JIS encoding. | ||
+ | set fd [open japanesetext.html_sj r] | ||
+ | fconfigure $fd -encoding shiftjis | ||
+ | set html [read $fd [file size japanesetext.html_sj]] | ||
+ | close $fd | ||
+ | set charset [ns_choosecharset -preference {utf-8 shift-jis euc-jp iso-2022-jp}] | ||
+ | set type "text/html; charset=$charset" | ||
+ | ns_write "HTTP/1.0 200 OK | ||
+ | Content-Type: $type | ||
+ | \n" | ||
+ | ns_startcontent -type $type | ||
+ | ns_write $html | ||
+ | |||
+ | '''NOTES''' | ||
+ | : In AOLserver 4.5, ns_startcontent's behavior has changed, so it may be convenient to reimplement ns_startcontent to be backward compatible as follows: | ||
− | + | if {[ns_info version] >= 4.5} { | |
+ | catch {rename ns_startcontent {}} | ||
+ | proc ns_startcontent {args} { | ||
+ | # | ||
+ | # Re-implement ns_startcontent in Tcl in AOLserver 4.5 | ||
+ | # because the -type option no longer falls back to | ||
+ | # server's default encoding like it did in 4.0. | ||
+ | # Luckily, in 4.5, ns_adp_mimetype now just | ||
+ | # calls Ns_ConnSetType() which still does | ||
+ | # and works outside of adps | ||
+ | # | ||
+ | if {[llength $args]} { | ||
+ | switch [string range [lindex $args 0] 1 end] { | ||
+ | charset { | ||
+ | ns_conn encoding [ns_encodingforcharset [lindex $args 1]] | ||
+ | } | ||
+ | type { | ||
+ | ns_adp_mimetype [lindex $args 1] | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | # NaviServer removed write_encoded | ||
+ | catch {ns_conn write_encoded 1} | ||
+ | return "" | ||
+ | } | ||
+ | } | ||
'''SEE ALSO''' | '''SEE ALSO''' | ||
− | : [[ns_write]], [[ns_config]] | + | : [[ns_write]], [[ns_choosecharset]], [[ns_config]] |
Latest revision as of 04:45, 11 October 2009
<manpage>ns_startcontent</manpage>
NAME
- ns_startcontent - Configures connection for encoding non-binary content to be returned with ns_write
SYNOPSIS
- ns_startcontent ?-type content-type_and_charset | -charset charset?
DESCRIPTION
- This command is used to set the appropriate encoding prior to returning the content part of the response via ns_write.
- If you write the headers to the client with ns_write instead of letting AOLserver do it (via ns_return or ns_respond), then AOLserver does not parse the content-type. You must explicitly tell it what charset to use immediately after you write the headers, by calling ns_startcontent in one of these forms:
- ns_startcontent
- Tells AOLserver that you have written the headers and do not wish the content to be translated.
- ns_startcontent -charset charset
- Tells AOLserver that you have written the headers and wish the following content to be translated to the specified charset.
- ns_startcontent -type content-type_and_charset
- Tells AOLserver that you have written the headers and wish the following content to be translated to the charset specified by content-type, which should be the same value you sent to the client in the Content-Type header. If content-type starts with text/ and does not contain a charset parameter, AOLserver 4.0 uses the server default as configured in ns/parameters OutputCharset. AOLserver 4.5 will actually throw an error if charset is not specified, so you may wish to redefine ns_startcontent in Tcl as listed in the notes below.
EXAMPLES
# Assume japanesetext.html_sj is stored in Shift-JIS encoding. set fd [open japanesetext.html_sj r] fconfigure $fd -encoding shiftjis set html [read $fd [file size japanesetext.html_sj]] close $fd set charset [ns_choosecharset -preference {utf-8 shift-jis euc-jp iso-2022-jp}] set type "text/html; charset=$charset" ns_write "HTTP/1.0 200 OK Content-Type: $type \n" ns_startcontent -type $type ns_write $html
NOTES
- In AOLserver 4.5, ns_startcontent's behavior has changed, so it may be convenient to reimplement ns_startcontent to be backward compatible as follows:
if {[ns_info version] >= 4.5} { catch {rename ns_startcontent {}} proc ns_startcontent {args} { # # Re-implement ns_startcontent in Tcl in AOLserver 4.5 # because the -type option no longer falls back to # server's default encoding like it did in 4.0. # Luckily, in 4.5, ns_adp_mimetype now just # calls Ns_ConnSetType() which still does # and works outside of adps # if {[llength $args]} { switch [string range [lindex $args 0] 1 end] { charset { ns_conn encoding [ns_encodingforcharset [lindex $args 1]] } type { ns_adp_mimetype [lindex $args 1] } } } # NaviServer removed write_encoded catch {ns_conn write_encoded 1} return "" } }
SEE ALSO