As a test, can you try using regsub instead of string map?
http://www.tcl.tk/man/tcl8.4/TclCmd/regsub.htm
Also, you can simplify the collection size logic a bit and make sure you avoid collecting more than 1Mb of payload by modifying your HTTP_RESPONSE code:
when HTTP_RESPONSE {
if {[HTTP::header value Content-Type] contains "text"} {
if { [HTTP::header exists "Content-Length"] and [HTTP::header "Content-Length"] < 1048575 } {
set content_length [HTTP::header "Content-Length"]
} else {
set content_length 1048576
}
if { $content_length > 0 } {
HTTP::collect $content_length
}
}
}
Aaron