我的 Tcl 源文件是 utf-8 格式。Tclhttpd 不能正确发送国家字符,所以我稍微修改了一下。但是,我也发送二进制文件,如 jpg 图像,有时二进制块存在于我的其他 utf-8 HTML 中。我很难计算正确的 Content-length 以完全匹配浏览器接收的内容(否则一些尾随字符会破坏下一个请求标头或浏览器每个请求等待 30 秒,直到超时)。
换句话说,我可以知道puts $socket
写入套接字的字节数吗?
我发现了一个特定的 11 字节序列,它搞砸了计数:
proc dump3 string {
binary scan $string c* c
binary scan $string H* hex
return [sdump $string]\n$c\n$hex
};#dump3
proc Httpd_ReturnData {sock type content {code 200} {close 0}} {
global Httpd
upvar #0 Httpd$sock data
#...skip non-pertinent code...
set content \x4f\x4e\xc2\x00\x03\xff\xff\x80\x00\x3c\x2f
#content=ONÂÿÿ�</
#79 78 -62 0 3 -1 -1 -128 0 60 47
#4f4ec20003ffff80003c2f
puts content=[dump3 $content]
puts utf8=[dump3 [encoding convertto utf-8 $content]]
if {[catch {
puts "string length=[string length $content] type=$type"
puts "stringblength=[string bytelength $content]"
set len [string length $content]
if [string match -nocase *utf-8* $type] {
fconfigure $sock -encoding utf-8
set len [string bytelength $content]
}
puts "len=$len fcon=[fconfigure $sock]"
HttpdRespondHeader $sock $type $close $len $code
HttpdSetCookie $sock
puts $sock ""
if {$data(proto) != "HEAD"} {
##fconfigure $sock -translation binary -blocking $Httpd(sockblock)
##native: -translation {auto crlf}
fconfigure $sock -translation lf -blocking $Httpd(sockblock)
puts -nonewline $sock $content
}
Httpd_SockClose $sock $close
} err]} {
HttpdCloseFinal $sock $err
}
}
控制台上的输出是:
内容=ONÂÿÿ�</ 79 78 -62 0 3 -1 -1 -128 0 60 47 4f4ec20003ffff80003c2f utf8=ONÃ�ÿÿÂ�</ 79 78 -61 -126 0 3 -61 -65 -61 -65 -62 -128 0 60 47 4f4ec3820003c3bfc3bfc280003c2f 字符串长度=11 类型=文本/html;字符集=utf-8 字符串长度=17 len=17 fcon=-blocking 0 -buffering full -buffersize 16384 -encoding utf-8 -eofchar {{} {}} -translation {auto crlf} -peername {128.0.0.71 128.0.0.71 55305} -sockname {128.0.0.8第 8016 代} HttpdRespondHeader 17
结果Content-Length: 17太多了,浏览器一直在等待。如果我只能事先知道,我的字符串会产生多少字节puts
,剩下的就很容易了。有办法吗?