Use dicts for `httpheader`

This commit is contained in:
df 2021-02-16 00:28:24 +00:00
parent 8016482d29
commit 772d91a9f8
1 changed files with 15 additions and 7 deletions

View File

@ -33,16 +33,24 @@ if {![exists -proc require]} {
exit exit
} }
proc httpheader {{type "text/html"} {cache 0} {extra ""}} {{done 0}} { proc httpheader {{type "text/html"} {cache 0} {extra {}}} {{done 0}} {
if {$done} return if {$done} return
if {!$cache} { if {!$cache} {
puts -nonewline "Content-Type: $type; charset=\"UTF-8\"; no-cache\r\n" set hdr [dict create \
puts -nonewline "Expires: -1\r\n" "Content-Type" "$type; charset=\"UTF-8\"; no-cache" \
puts -nonewline "Connection: close\r\n" "Expires" "-1" \
puts -nonewline "Pragma: no-cache\r\n" "Connection" "close" \
puts -nonewline "Cache-Control: no-cache\r\n" "Pragma" "no-cache" \
"Cache-Control" "no-cache"]
} else { } else {
puts -nonewline "Content-Type: $type; charset=\"UTF-8\"\r\n" set hdr [dict create "Content-Type" "$type; charset=\"UTF-8\""]
}
if {![catch {dict size $extra}]} {
set hdr [dict merge $hdr $extra]
set extra ""
}
dict for {k v} $hdr {
puts -nonewline "$k: $v\r\n"
} }
if {$extra ne ""} { puts -nonewline "$extra" } if {$extra ne ""} { puts -nonewline "$extra" }
puts -nonewline "\r\n" puts -nonewline "\r\n"