Tcl 对HTTP请求重用TCP套接字

Tcl 对HTTP请求重用TCP套接字,tcl,Tcl,我试图让Tcl http包重用到服务器的初始TCP连接,以遵循重定向响应。从手册页面我得到的印象是,指定选项-keepalive 1应该可以实现这一点。然而,在我的测试中,我观察到使用了两个独立的TCP连接。这是我的代码: package require http # Redirect test page set url http://jigsaw.w3.org/HTTP/300/301.html # Make the HTTP library report what it is doing

我试图让Tcl http包重用到服务器的初始TCP连接,以遵循重定向响应。从手册页面我得到的印象是,指定选项
-keepalive 1
应该可以实现这一点。然而,在我的测试中,我观察到使用了两个独立的TCP连接。这是我的代码:

package require http

# Redirect test page
set url http://jigsaw.w3.org/HTTP/300/301.html

# Make the HTTP library report what it is doing
proc http::Log {args} {
    puts [join $args]
}

set tok [http::geturl $url -keepalive 1]
if {[http::ncode $tok] in {301 302}} {
    # Determine the new URL
    set meta [http::meta $tok]
    set key [lsearch -exact -nocase -inline [dict keys $meta] location]
    set loc [dict get $meta $key]
    http::cleanup $tok
    puts "Redirecting to: $loc"
    set tok [http::geturl $loc -keepalive 1]
}
puts [http::code $tok]
http::cleanup $tok
这将生成以下输出,清楚地显示每个请求都打开和关闭了套接字:

^A1 URL http://jigsaw.w3.org/HTTP/300/301.html - token ::http::1
Using sock560e29012cc0 for jigsaw.w3.org:80 - token ::http::1 keepalive
^B1 begin sending request - token ::http::1
^C1 end sending request - token ::http::1
^D1 begin receiving response - token ::http::1
^E1 end of response headers - token ::http::1
^F1 end of response body (unchunked) - token ::http::1
Closing connection jigsaw.w3.org:80 (sock sock560e29012cc0)
Redirecting to: http://jigsaw.w3.org/HTTP/300/Overview.html
^A2 URL http://jigsaw.w3.org/HTTP/300/Overview.html - token ::http::2
Using sock560e28f6e820 for jigsaw.w3.org:80 - token ::http::2 keepalive
^B2 begin sending request - token ::http::2
^C2 end sending request - token ::http::2
^D2 begin receiving response - token ::http::2
^E2 end of response headers - token ::http::2
^F2 end of response body (unchunked) - token ::http::2
Closing connection jigsaw.w3.org:80 (sock sock560e28f6e820)
HTTP/1.1 200 OK

使用同一TCP连接发送多个请求还需要做什么?

。我可以复制您记录的行为(在core-8-6-branch中使用http.tcl)。闻起来像虫子。在内部,当第二个
http::geturl
启动时,内部状态(connection)字段保持为“close”,也没有可用的套接字映射。值得一试。@mrcalvin在较旧的Tcl版本上测试表明,这个问题是由http 2.9引入的。我打开了一个错误报告。奇怪。我可以复制您记录的行为(在core-8-6-branch中使用http.tcl)。闻起来像虫子。在内部,当第二个
http::geturl
启动时,内部状态(connection)字段保持为“close”,也没有可用的套接字映射。值得一试。@mrcalvin在较旧的Tcl版本上测试表明,这个问题是由http 2.9引入的。我打开了一个bug报告。