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
}
proc httpheader {{type "text/html"} {cache 0} {extra ""}} {{done 0}} {
proc httpheader {{type "text/html"} {cache 0} {extra {}}} {{done 0}} {
if {$done} return
if {!$cache} {
puts -nonewline "Content-Type: $type; charset=\"UTF-8\"; no-cache\r\n"
puts -nonewline "Expires: -1\r\n"
puts -nonewline "Connection: close\r\n"
puts -nonewline "Pragma: no-cache\r\n"
puts -nonewline "Cache-Control: no-cache\r\n"
set hdr [dict create \
"Content-Type" "$type; charset=\"UTF-8\"; no-cache" \
"Expires" "-1" \
"Connection" "close" \
"Pragma" "no-cache" \
"Cache-Control" "no-cache"]
} 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" }
puts -nonewline "\r\n"