|
2022-06-20
| ||
| 09:48 | • Closed ticket [55bf73b52b]: http reuses connections after 101 Switching Protocol... artifact: 8173ee8554 user: jan.nijtmans | |
| 09:00 | • Ticket [55bf73b52b]: 3 changes artifact: 5ed8c7a298 user: adrianmedranocalvo | |
| 08:57 | • Ticket [55bf73b52b]: 3 changes artifact: 3f097a4c13 user: adrianmedranocalvo | |
|
2022-06-15
| ||
| 08:49 | • Ticket [55bf73b52b]: 3 changes artifact: ab89241ea3 user: kjnash | |
| 07:50 | • Ticket [55bf73b52b]: 3 changes artifact: eea9a6a839 user: jan.nijtmans | |
| 00:52 | • Ticket [55bf73b52b]: 3 changes artifact: 9e98323975 user: kjnash | |
|
2022-06-14
| ||
| 06:49 | • Ticket [55bf73b52b]: 3 changes artifact: 36add10a8f user: jan.nijtmans | |
| 06:44 | Fix Proposal for [55bf73b52b]: http reuses connections after 101 Switching Protocols. To be discusse... check-in: ed99670192 user: jan.nijtmans tags: bug-55bf73b52b | |
| 06:13 | • Ticket [55bf73b52b] http reuses connections after 101 Switching Protocols st... artifact: 0ec1563938 user: adrianmedranocalvo | |
|
2022-06-10
| ||
| 08:43 | • Open ticket [55bf73b52b]. artifact: e919bcb7c4 user: adrianmedranocalvo | |
|
2022-05-10
| ||
| 20:26 | • Ticket [55bf73b52b]: 6 changes artifact: be0305dd88 user: oehhar | |
| 17:01 | • Closed ticket [55bf73b52b]. artifact: 57551391f8 user: jan.nijtmans | |
| 16:09 | Fix [55bf73b52b]: http reuses connections after 101 Switching Protocols check-in: 1aa527993f user: jan.nijtmans tags: core-8-6-branch | |
| 15:29 | • Ticket [55bf73b52b] http reuses connections after 101 Switching Protocols st... artifact: ad6e2681c2 user: adrianmedranocalvo | |
| 14:17 | • Ticket [55bf73b52b]: 4 changes artifact: 3037694401 user: oehhar | |
| 10:38 | • Ticket [55bf73b52b]: 5 changes artifact: 488e3d568c user: jan.nijtmans | |
| 10:04 | • Ticket [55bf73b52b]: 3 changes artifact: 7c386ce4fb user: kjnash | |
|
2022-04-29
| ||
| 15:36 | • Ticket [55bf73b52b]: 4 changes artifact: 2a5a3a8275 user: adrianmedranocalvo | |
|
2022-03-11
| ||
| 11:14 | • Ticket [55bf73b52b]: 3 changes artifact: 4e1602633b user: adrianmedranocalvo | |
| 11:14 | • Ticket [55bf73b52b]: 3 changes artifact: 70cb4ec319 user: adrianmedranocalvo | |
| 11:06 | • New ticket [55bf73b52b]. artifact: d29a6fb8cf user: adrianmedranocalvo | |
http reuses connections after 101 Switching Protocols
The http package reuses connections even after receiving an "101 Switching Protocols" response.
The issue can be readily seen with the Tcllib websocket package, which uses the http package to establish the websocket connection.
```
% set ws [::websocket::open ws://ws.ifelse.io [lambda {args} { puts $args }]]
sock2d5b7b0
% set ws2 [::websocket::open ws://ws.ifelse.io [lambda {args} { puts $args }]]
sock2d5b7b0
```
The second call to ::websocket::open should open a new WebSocket connection, instead it reuses the former, which is no longer an HTTP connection. One can no longer open two WebSocket connections to the same server.
This change happened somewhere between 2.8.9 and 2.9.1.
The HTTP package should no longer reuse connections after receiving a "101 Switching Protocols" response.
ed99670192Fix Proposal for [55bf73b52b]: http reuses connections after 101 Switching Protocols. To be discussed check-in2022-06-14 06:44:561aa527993fFix [55bf73b52b]: http reuses connections after 101 Switching Protocols check-in2022-05-10 16:09:21
I've had quick look at the code. It's pretty involved. On a high level a fix would consist of: 1. Avoid pipelining further requests once a request includes the "Connection: upgrade" and "Upgrade" headers. 2. If the server answers to that request with a "101 Switching Protocols", the socket should no longer be considered HTTP.
All versions of http 2.9 before 2.9.7 have the bug. Also all versions of http 2.10 (these will be fixed if 2.9.7 is satisfactory). The branch bug-55bf73b52b-8.6 has a fix for Tcl 8.6 (as the new version 2.9.7 of http). The fix is almost as adrianmedranocalvo suggested: 1. Use a fresh connection if the request has "Connection: upgrade" and "Upgrade" headers. The value of -keepalive passed to http::geturl is ignored. The socket is not available for further HTTP requests. 2. If the server answers to that request with a "101 Switching Protocols" and response headers "Connection: upgrade" and "Upgrade", the socket is longer HTTP. The socket is not closed, and the -command callback passed to http::geturl is responsible for further processing. Also: 3. A test file has been added to the websocket Tcllib module. This tests simple WebSocket operations, including a regression test for this bug. Please add further tests of the websocket module to this file. 4. The bugfix should be suitable for protocol upgrades to TLS and HTTP/2, but this has not been tested. 5. The man page http(n) now has a brief section describing protocol upgrades.
Thanks, @kjnash for the implementation. I'm not so deep into the http protocol, but I can see the global concept.
Let's wait one day (for the GITHUB build), then it's GTG for me.
Dear Nash, dear Jan, dear Adriano,
thank you for the great action. It sounds complicated and apparently is solved, my appreciation.
Side note: IMHO, it is not required to increment the version of the http package to 2.9.7. I suppose, it would be sufficient to increment is only once per release, and TCL8.6.12 was released with http 2.9.5. So, 2.9.6 would be ok.
Thank you, Harald
Dear Nash, thank you very much for the excellent patch. I confirm: it works! I appreciate the effort and care you put into improving Tcl. Geehrter Dr. Oehlmann, though noisy, I prefer incrementing the patchLevel on each change, as then it's clear which changes one is running. Kind regards, Adrián.
Merged to 8.6 and up.
Thanks!
@harald. I see both sides: Most of the time, updating the version number is - indeed - not necessary every time. It should only be done if there is a good reason to do so. In this case, as the feature is used in tcllib, I can imagine applications want to "package require http 2.9.7", assuring that websockets work. Sounds like a good reason to me.
Regards, Jan Nijtmans
All ok ! Thank you all, you rock, Harald
I'm reopening the ticket, as there's an issue with the patch: header names are inspected case-sensitively, so it fails to work with servers using different casing as coded.
This can be seen in https://core.tcl-lang.org/tcl/artifact?name=26cc647554009c07&ln=270, where the Connection header is required to be in lower-case; the same holds for the Upgrade header a few lines below.
In the patch below, I solve this issue by extending the `state(connection)` variable to record a list of response connection options (e.g., {keep-alive upgrade}), and adding a new `state(upgrade)` variable holding the contents of the Upgrade response header. Checks are updated to adapt to this change; in particular, when testing whether a response is an 101 Switching Protocols response, we use those values instead of digging in the raw headers.
A similar change is done for request headers: the patch lifts the requirement that request headers are written in title-case (Connection & Upgrade). In order to do that, a new function is defined GetFieldValue, that gets the value of a request header field.
Additionally, the code has been improved to stop converting the -headers argument into a dictionary. This conversion is lossy: as per RFC 7230, a request or response may contain duplicated headers.
Please find the changes below:
diff --git i/src/tcl8.6.12/library/http/http.tcl w/src/tcl8.6.12/library/http/http.tcl
index 92d3a5a87..9e4cff11a 100644
--- i/src/tcl8.6.12/library/http/http.tcl
+++ w/src/tcl8.6.12/library/http/http.tcl
@@ -257,26 +257,11 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
}
# Is this an upgrade request/response?
- set upgradeResponse 0
- if { [info exists state(upgradeRequest)]
- && [info exists state(http)]
- && $state(upgradeRequest)
- && ([ncode $token] eq {101})
- } {
- # An upgrade must be requested by the client.
- # If 101 response, test server response headers for an upgrade.
- set connectionHd {}
- set upgradeHd {}
- if {[dict exists $state(meta) connection]} {
- set connectionHd [string tolower [dict get $state(meta) connection]]
- }
- if {[dict exists $state(meta) upgrade]} {
- set upgradeHd [string tolower [dict get $state(meta) upgrade]]
- }
- if {($connectionHd eq {upgrade}) && ($upgradeHd ne {})} {
- set upgradeResponse 1
- }
- }
+ set upgradeResponse \
+ [expr { [info exists state(upgradeRequest)] && $state(upgradeRequest)
+ && [info exists state(http)] && [ncode $token] eq {101}
+ && [info exists state(connection)] && "upgrade" in $state(connection)
+ && [info exists state(upgrade)] && "" ne $state(upgrade)}]
if { ($state(status) eq "timeout")
|| ($state(status) eq "error")
@@ -298,7 +283,7 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
catch {fileevent $state(sock) writable {}}
} elseif {
([info exists state(-keepalive)] && !$state(-keepalive))
- || ([info exists state(connection)] && ($state(connection) eq "close"))
+ || ([info exists state(connection)] && ("close" in $state(connection)))
} {
set closeQueue 1
set connId $state(socketinfo)
@@ -306,7 +291,7 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
CloseSocket $state(sock) $token
} elseif {
([info exists state(-keepalive)] && $state(-keepalive))
- && ([info exists state(connection)] && ($state(connection) ne "close"))
+ && ([info exists state(connection)] && ("close" in $state(connection)))
} {
KeepSocket $token
}
@@ -337,7 +322,7 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
# queued task if possible. Otherwise leave it idle and ready for its next
# use.
#
-# If $socketClosing(*), then ($state(connection) eq "close") and therefore
+# If $socketClosing(*), then ("close" in $state(connection)) and therefore
# this command will not be called by Finish.
#
# Arguments:
@@ -486,7 +471,7 @@ proc http::KeepSocket {token} {
(!$state(-pipeline))
&& [info exists socketWrQueue($connId)]
&& [llength $socketWrQueue($connId)]
- && ($state(connection) ne "close")
+ && ("close" ni $state(connection))
} {
# If not pipelined, (socketRdState eq Rready) tells us that we are
# ready for the next write - there is no need to check
@@ -772,7 +757,7 @@ proc http::geturl {url args} {
-strict boolean
-timeout integer
-validate boolean
- -headers dict
+ -headers list
}
set state(charset) $defaultCharset
set options {
@@ -786,8 +771,8 @@ proc http::geturl {url args} {
foreach {flag value} $args {
if {[regexp -- $pat $flag]} {
# Validate numbers
- if {($flag eq "-headers") ? [catch {dict size $value}] :
- ([info exists type($flag)] && ![string is $type($flag) -strict $value])
+ if { ([info exists type($flag)] && ![string is $type($flag) -strict $value])
+ || ($flag eq "-headers" && [llength $value] % 2 != 0)
} {
unset $token
return -code error \
@@ -985,12 +970,14 @@ proc http::geturl {url args} {
# c11a51c482]
set state(accept-types) $http(-accept)
- set state(upgradeRequest) [expr {
- [dict exists $state(-headers) Upgrade]
- && [dict exists $state(-headers) Connection]
- && ([dict get $state(-headers) Connection] eq {Upgrade})
- && ([dict get $state(-headers) Upgrade] ne {})
- }]
+ # Check whether this is an Upgrade request.
+ set connectionValues [SplitCommaSeparatedField \
+ [GetFieldValue $state(-headers) Connection]]
+ set connectionValues [string tolower $connectionValues]
+ set upgradeValues [SplitCommaSeparatedField \
+ [GetFieldValue $state(-headers) Upgrade]]
+ set state(upgradeRequest) [expr { "upgrade" in $connectionValues
+ && [llength $upgradeValue] >= 1}]
if {$isQuery || $isQueryChannel} {
# It's a POST.
@@ -1407,9 +1394,10 @@ proc http::Connected {token proto phost srvurl} {
if {[catch {
set state(method) $how
puts $sock "$how $srvurl HTTP/$state(-protocol)"
- if {[dict exists $state(-headers) Host]} {
+ set hostValue [GetFieldValue $state(-headers) Host]
+ if {$hostValue ne {}} {
# Allow Host spoofing. [Bug 928154]
- puts $sock "Host: [dict get $state(-headers) Host]"
+ puts $sock "Host: $hostValue"
} elseif {$port == $defport} {
# Don't add port in this case, to handle broken servers. [Bug
# #504508]
@@ -1439,7 +1427,7 @@ proc http::Connected {token proto phost srvurl} {
# Proxy-Connection header field in any requests"
set accept_encoding_seen 0
set content_type_seen 0
- dict for {key value} $state(-headers) {
+ foreach {key value} $state(-headers) {
set value [string map [list \n "" \r ""] $value]
set key [string map {" " -} [string trim $key]]
if {[string equal -nocase $key "host"]} {
@@ -2636,7 +2624,7 @@ proc http::Event {sock token} {
if { ([info exists state(connection)])
&& ([info exists socketMapping($state(socketinfo))])
- && ($state(connection) eq "keep-alive")
+ && ("keep-alive" in $state(connection))
&& ($state(-keepalive))
&& (!$state(reusing))
&& ($state(-pipeline))
@@ -2658,7 +2646,7 @@ proc http::Event {sock token} {
if { ([info exists state(connection)])
&& ([info exists socketMapping($state(socketinfo))])
- && ($state(connection) eq "close")
+ && ("close" in $state(connection))
&& ($state(-keepalive))
} {
# The server warns that it will close the socket after this
@@ -2729,7 +2717,7 @@ proc http::Event {sock token} {
# (totalsize == 0).
if { (!( [info exists state(connection)]
- && ($state(connection) eq "close")
+ && ("close" in $state(connection))
)
)
&& (![info exists state(transfer)])
@@ -2795,32 +2783,26 @@ proc http::Event {sock token} {
}
proxy-connection -
connection {
- set tmpHeader [string trim [string tolower $value]]
# RFC 7230 Section 6.1 states that a comma-separated
- # list is an acceptable value. According to
+ # list is an acceptable value.
+ foreach el [SplitCommaSeparatedField $value] {
+ lappend state(connection) [string tolower $el]
+ }
+
+ # According to
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
# any comma-separated list implies keep-alive, but I
# don't see this in the RFC so we'll play safe and
# scan any list for "close".
- if {$tmpHeader in {close keep-alive}} {
- # The common cases, continue.
- } elseif {[string first , $tmpHeader] < 0} {
- # Not a comma-separated list, not "close",
- # therefore "keep-alive".
- set tmpHeader keep-alive
- } else {
- set tmpResult keep-alive
- set tmpCsl [split $tmpHeader ,]
- # Optional whitespace either side of separator.
- foreach el $tmpCsl {
- if {[string trim $el] eq {close}} {
- set tmpResult close
- break
- }
- }
- set tmpHeader $tmpResult
+ # FIXME: support combining duplicate header field's values.
+ if { "close" ni $state(connection)
+ && "keep-alive" ni $state(connection)
+ } {
+ lappend state(connection) "keep-alive"
}
- set state(connection) $tmpHeader
+ }
+ upgrade {
+ set state(upgrade) [string trim $value]
}
}
lappend state(meta) $key [string trim $value]
@@ -3550,6 +3532,52 @@ proc http::ReceiveChunked {chan command} {
}
}
+# http::SplitCommaSeparatedFieldValue --
+# Return the individual values of a comma-separated field value.
+#
+# Arguments:
+# fieldValue Comma-separated header field value.
+#
+# Results:
+# List of values.
+proc http::SplitCommaSeparatedFieldValue {fieldValue} {
+ set r {}
+ foreach el [split $fieldValue ,] {
+ lappend r [string trim $el]
+ }
+ return $r
+}
+
+
+# http::GetFieldValue --
+# Return the value of a header field.
+#
+# Arguments:
+# headers Headers key-value list
+# fieldName Name of header field whose value to return.
+#
+# Results:
+# The value of the fieldName header field
+#
+# Field names are matched case-insensitively (RFC 7230 Section 3.2).
+#
+# If the field is present multiple times, it is assumed that the field is
+# defined as a comma-separated list and the values are combined (by separating
+# them with commas, see RFC 7230 Section 3.2.2) and returned at once.
+proc http::GetFieldValue {headers fieldName} {
+ set r {}
+ foreach {field value} $headers {
+ if {[string equal -nocase $fieldName $field]} {
+ if {$r eq {}} {
+ set r $value
+ } else {
+ append r ", $value"
+ }
+ }
+ }
+ return $r
+}
+
proc http::make-transformation-chunked {chan command} {
coroutine [namespace current]::dechunk$chan ::http::ReceiveChunked $chan $command
chan event $chan readable [namespace current]::dechunk$chan
The patch in previous comment contains a defect due to an incomplete renaming (SplitCommaSeparatedField -> SplitCommaSeparatedFieldValue). Please try the version below:
diff --git a/src/tcl8.6.12/library/http/http.tcl b/src/tcl8.6.12/library/http/http.tcl
index 92d3a5a87..ee4943700 100644
--- a/src/tcl8.6.12/library/http/http.tcl
+++ b/src/tcl8.6.12/library/http/http.tcl
@@ -257,26 +257,11 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
}
# Is this an upgrade request/response?
- set upgradeResponse 0
- if { [info exists state(upgradeRequest)]
- && [info exists state(http)]
- && $state(upgradeRequest)
- && ([ncode $token] eq {101})
- } {
- # An upgrade must be requested by the client.
- # If 101 response, test server response headers for an upgrade.
- set connectionHd {}
- set upgradeHd {}
- if {[dict exists $state(meta) connection]} {
- set connectionHd [string tolower [dict get $state(meta) connection]]
- }
- if {[dict exists $state(meta) upgrade]} {
- set upgradeHd [string tolower [dict get $state(meta) upgrade]]
- }
- if {($connectionHd eq {upgrade}) && ($upgradeHd ne {})} {
- set upgradeResponse 1
- }
- }
+ set upgradeResponse \
+ [expr { [info exists state(upgradeRequest)] && $state(upgradeRequest)
+ && [info exists state(http)] && [ncode $token] eq {101}
+ && [info exists state(connection)] && "upgrade" in $state(connection)
+ && [info exists state(upgrade)] && "" ne $state(upgrade)}]
if { ($state(status) eq "timeout")
|| ($state(status) eq "error")
@@ -298,7 +283,7 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
catch {fileevent $state(sock) writable {}}
} elseif {
([info exists state(-keepalive)] && !$state(-keepalive))
- || ([info exists state(connection)] && ($state(connection) eq "close"))
+ || ([info exists state(connection)] && ("close" in $state(connection)))
} {
set closeQueue 1
set connId $state(socketinfo)
@@ -306,7 +291,7 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
CloseSocket $state(sock) $token
} elseif {
([info exists state(-keepalive)] && $state(-keepalive))
- && ([info exists state(connection)] && ($state(connection) ne "close"))
+ && ([info exists state(connection)] && ("close" in $state(connection)))
} {
KeepSocket $token
}
@@ -337,7 +322,7 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
# queued task if possible. Otherwise leave it idle and ready for its next
# use.
#
-# If $socketClosing(*), then ($state(connection) eq "close") and therefore
+# If $socketClosing(*), then ("close" in $state(connection)) and therefore
# this command will not be called by Finish.
#
# Arguments:
@@ -486,7 +471,7 @@ proc http::KeepSocket {token} {
(!$state(-pipeline))
&& [info exists socketWrQueue($connId)]
&& [llength $socketWrQueue($connId)]
- && ($state(connection) ne "close")
+ && ("close" ni $state(connection))
} {
# If not pipelined, (socketRdState eq Rready) tells us that we are
# ready for the next write - there is no need to check
@@ -772,7 +757,7 @@ proc http::geturl {url args} {
-strict boolean
-timeout integer
-validate boolean
- -headers dict
+ -headers list
}
set state(charset) $defaultCharset
set options {
@@ -786,8 +771,8 @@ proc http::geturl {url args} {
foreach {flag value} $args {
if {[regexp -- $pat $flag]} {
# Validate numbers
- if {($flag eq "-headers") ? [catch {dict size $value}] :
- ([info exists type($flag)] && ![string is $type($flag) -strict $value])
+ if { ([info exists type($flag)] && ![string is $type($flag) -strict $value])
+ || ($flag eq "-headers" && [llength $value] % 2 != 0)
} {
unset $token
return -code error \
@@ -985,12 +970,14 @@ proc http::geturl {url args} {
# c11a51c482]
set state(accept-types) $http(-accept)
- set state(upgradeRequest) [expr {
- [dict exists $state(-headers) Upgrade]
- && [dict exists $state(-headers) Connection]
- && ([dict get $state(-headers) Connection] eq {Upgrade})
- && ([dict get $state(-headers) Upgrade] ne {})
- }]
+ # Check whether this is an Upgrade request.
+ set connectionValues [SplitCommaSeparatedFieldValue \
+ [GetFieldValue $state(-headers) Connection]]
+ set connectionValues [string tolower $connectionValues]
+ set upgradeValues [SplitCommaSeparatedFieldValue \
+ [GetFieldValue $state(-headers) Upgrade]]
+ set state(upgradeRequest) [expr { "upgrade" in $connectionValues
+ && [llength $upgradeValue] >= 1}]
if {$isQuery || $isQueryChannel} {
# It's a POST.
@@ -1407,9 +1394,10 @@ proc http::Connected {token proto phost srvurl} {
if {[catch {
set state(method) $how
puts $sock "$how $srvurl HTTP/$state(-protocol)"
- if {[dict exists $state(-headers) Host]} {
+ set hostValue [GetFieldValue $state(-headers) Host]
+ if {$hostValue ne {}} {
# Allow Host spoofing. [Bug 928154]
- puts $sock "Host: [dict get $state(-headers) Host]"
+ puts $sock "Host: $hostValue"
} elseif {$port == $defport} {
# Don't add port in this case, to handle broken servers. [Bug
# #504508]
@@ -1439,7 +1427,7 @@ proc http::Connected {token proto phost srvurl} {
# Proxy-Connection header field in any requests"
set accept_encoding_seen 0
set content_type_seen 0
- dict for {key value} $state(-headers) {
+ foreach {key value} $state(-headers) {
set value [string map [list \n "" \r ""] $value]
set key [string map {" " -} [string trim $key]]
if {[string equal -nocase $key "host"]} {
@@ -2636,7 +2624,7 @@ proc http::Event {sock token} {
if { ([info exists state(connection)])
&& ([info exists socketMapping($state(socketinfo))])
- && ($state(connection) eq "keep-alive")
+ && ("keep-alive" in $state(connection))
&& ($state(-keepalive))
&& (!$state(reusing))
&& ($state(-pipeline))
@@ -2658,7 +2646,7 @@ proc http::Event {sock token} {
if { ([info exists state(connection)])
&& ([info exists socketMapping($state(socketinfo))])
- && ($state(connection) eq "close")
+ && ("close" in $state(connection))
&& ($state(-keepalive))
} {
# The server warns that it will close the socket after this
@@ -2729,7 +2717,7 @@ proc http::Event {sock token} {
# (totalsize == 0).
if { (!( [info exists state(connection)]
- && ($state(connection) eq "close")
+ && ("close" in $state(connection))
)
)
&& (![info exists state(transfer)])
@@ -2795,32 +2783,26 @@ proc http::Event {sock token} {
}
proxy-connection -
connection {
- set tmpHeader [string trim [string tolower $value]]
# RFC 7230 Section 6.1 states that a comma-separated
- # list is an acceptable value. According to
+ # list is an acceptable value.
+ foreach el [SplitCommaSeparatedFieldValue $value] {
+ lappend state(connection) [string tolower $el]
+ }
+
+ # According to
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
# any comma-separated list implies keep-alive, but I
# don't see this in the RFC so we'll play safe and
# scan any list for "close".
- if {$tmpHeader in {close keep-alive}} {
- # The common cases, continue.
- } elseif {[string first , $tmpHeader] < 0} {
- # Not a comma-separated list, not "close",
- # therefore "keep-alive".
- set tmpHeader keep-alive
- } else {
- set tmpResult keep-alive
- set tmpCsl [split $tmpHeader ,]
- # Optional whitespace either side of separator.
- foreach el $tmpCsl {
- if {[string trim $el] eq {close}} {
- set tmpResult close
- break
- }
- }
- set tmpHeader $tmpResult
+ # FIXME: support combining duplicate header field's values.
+ if { "close" ni $state(connection)
+ && "keep-alive" ni $state(connection)
+ } {
+ lappend state(connection) "keep-alive"
}
- set state(connection) $tmpHeader
+ }
+ upgrade {
+ set state(upgrade) [string trim $value]
}
}
lappend state(meta) $key [string trim $value]
@@ -3550,6 +3532,52 @@ proc http::ReceiveChunked {chan command} {
}
}
+# http::SplitCommaSeparatedFieldValue --
+# Return the individual values of a comma-separated field value.
+#
+# Arguments:
+# fieldValue Comma-separated header field value.
+#
+# Results:
+# List of values.
+proc http::SplitCommaSeparatedFieldValue {fieldValue} {
+ set r {}
+ foreach el [split $fieldValue ,] {
+ lappend r [string trim $el]
+ }
+ return $r
+}
+
+
+# http::GetFieldValue --
+# Return the value of a header field.
+#
+# Arguments:
+# headers Headers key-value list
+# fieldName Name of header field whose value to return.
+#
+# Results:
+# The value of the fieldName header field
+#
+# Field names are matched case-insensitively (RFC 7230 Section 3.2).
+#
+# If the field is present multiple times, it is assumed that the field is
+# defined as a comma-separated list and the values are combined (by separating
+# them with commas, see RFC 7230 Section 3.2.2) and returned at once.
+proc http::GetFieldValue {headers fieldName} {
+ set r {}
+ foreach {field value} $headers {
+ if {[string equal -nocase $fieldName $field]} {
+ if {$r eq {}} {
+ set r $value
+ } else {
+ append r ", $value"
+ }
+ }
+ }
+ return $r
+}
+
proc http::make-transformation-chunked {chan command} {
coroutine [namespace current]::dechunk$chan ::http::ReceiveChunked $chan $command
chan event $chan readable [namespace current]::dechunk$chan
Thanks for the patch! Committed now to it's own branch for testing and discussion: https://core.tcl-lang.org/tcl/info/ed99670192598dd7
About the `state(connection)` and `state(upgrade)` variables: I agree on this change. On the change from dict -> list, I'm not so happy: I would prefer to keep it as a dict, but handle the joining when filling the dict from the headers. Anyway, I will have a further look.
Hi Adrian,
Many thanks for your bug report and patch. I have committed an update to the "bug-55bf73b52b" branch: [335145438e34383d].
I believe one "in" should be a "ni"; and one variable name is misspelled. Apart from that the only issue I spotted was the one that you had already labelled "FIXME", and I have attempted to fix it.
When testing, http-3.34 failed (test that -headers is a dict), so I have replaced the test with two more, 3.34 (test that -headers is a list) and 3.35 (test that -headers has an even number of elements). I have made a slight modification to the http.tcl code so it returns an appropriate error message in each case.
(Jan: I'm assuming that we are permitted to re-use old test numbers.)
I agree that state(-headers) must be a list, not a dict. We could attempt to munge the values into a dict, but these are request headers supplied by the caller of http::geturl, and I think it is best to keep them and send them to the server exactly as they were specified by the caller.
Best wishes,
Keith.
> I agree that state(-headers) must be a list, not a dict OK, you are the expert here ;-) Your corrections look good to me. Whenever you think it's ready, please merge to core-8-6-branch.
I'll wait a couple of days before merging, in case anyone else has comments.
Dear Nijtmans, dear Nash, thank you for your help and please excuse for the sloppy patch. Mr. Nash: I reviewed your changes and I think they are correct. Thank you for the careful and thorough review. We should respect the exact order and contents of the headers specified by the client, including duplicate ones. This can be crucial when dealing with non-conformant servers or intermediaries. Thank you everyone, kind regards, Adrián.
Please increase http package patchLevel when merging.
> Please increase http package patchLevel when merging.
patchLevel bumped now