Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gzip, UTF-8, CORS e.t.c. #1

Merged
merged 9 commits into from Oct 7, 2011
12 changes: 9 additions & 3 deletions example.tcl 100644 → 100755
@@ -1,4 +1,8 @@
source wibble.tcl
#!/usr/bin/tclsh8.6
# change the above line to point to the tclsh8.6 executable

lappend auto_path .
package require wibble

# =============================== example code ================================

Expand All @@ -17,9 +21,11 @@ if {$argv0 eq [info script]} {
::wibble::handle / dirlist root $root
::wibble::handle / notfound

# Start a server and enter the event loop.
# Start a server and enter the event loop if not already there.
catch {
::wibble::listen 8080
vwait forever
if {!$tcl_interactive} {
vwait forever
}
}
}
3 changes: 3 additions & 0 deletions pkgIndex.tcl
@@ -0,0 +1,3 @@
# package index for wibble
if {![package vsatisfies [package provide Tcl] 8.6]} {error "Tcl 8.6 is required"}
package ifneeded wibble 0.1 [list source [file join $dir wibble.tcl]]
25 changes: 23 additions & 2 deletions wibble.tcl 100644 → 100755
Expand Up @@ -643,7 +643,7 @@ proc ::wibble::getrequest {port chan peerhost peerport} {
# Receive and parse the first line. Process "." and ".." path components.
regexp {^\s*(\S*)\s+(\S*)\s+(.*?)\s*$} [getline] _ method uri protocol
regexp {^([^?]*)(\?.*)?$} $uri _ path query
set path [regsub -all {(?:/|^)\.(?=/|$)} [dehex $path] /]
set path [regsub -all {(?:/|^)\.(?=/|$)} [encoding convertfrom utf-8 [dehex $path]] /]
while {[regexp -indices {(?:/[^/]*/+|^[^/]*/+|^)\.\.(?=/|$)} $path range]} {
set path [string replace $path {*}$range ""]
}
Expand Down Expand Up @@ -827,6 +827,23 @@ proc ::wibble::process {port socket peerhost peerport} {
if {[dict get $request method] ne "HEAD"} {
set file [open [dict get $response contentfile]]
}
} elseif {[dict exists $response header content-encoding] &&
[dict get $response header content-encoding] eq "gzip"
} {
set gzip [binary format "H*iH*" "1f8b0800" [clock seconds] "0003"]
set content [dict get $response content]
append gzip [zlib deflate $content]
append gzip [binary format i [zlib crc32 $content]]
append gzip [binary format i [string length $content]]
set response [dict merge $response {
header {
Vary Accept-Encoding
Content-Encoding gzip
Accept-Ranges bytes
}
}]
dict set response content $gzip
set size [string length [dict get $response content]]
} elseif {[dict exists $response contentchan]} {
if {[dict exists $response contentsize]} {
set size [dict get $response contentsize]
Expand Down Expand Up @@ -869,7 +886,11 @@ proc ::wibble::process {port socket peerhost peerport} {
Content-Length Content-Location Content-MD5 Content-Range
Content-Type Date ETag Expires Last-Modified Location Pragma
Proxy-Authenticate Retry-After Server Set-Cookie Trailer
Transfer-Encoding Upgrade Vary Via Warning WWW-Authenticate
Transfer-Encoding Upgrade Vary Via Warning WWW-Authenticate
Access-Control-Allow-Origin Origin Access-Control-Allow-Credentials
Access-Control-Expose-Headers Access-Control-Max-Age
Access-Control-Allow-Methods Access-Control-Allow-Headers
Access-Control-Request-Method Access-Control-Request-Headers
} $key]
if {$normalizedkey ne ""} {
set key $normalizedkey
Expand Down