Add gzip encoding responses.

BUG=skia:
R=mtklein@google.com

Author: jcgregorio@google.com

Review URL: https://codereview.chromium.org/298433002

git-svn-id: http://skia.googlecode.com/svn/trunk@14786 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/webtry/setup/continue_install b/experimental/webtry/setup/continue_install
index b816eb0..7891be4 100755
--- a/experimental/webtry/setup/continue_install
+++ b/experimental/webtry/setup/continue_install
@@ -56,6 +56,5 @@
 
 cd experimental/webtry
 
-go get github.com/mattn/go-sqlite3
-go get github.com/go-sql-driver/mysql
+go get -d
 go build webtry.go
diff --git a/experimental/webtry/webtry.go b/experimental/webtry/webtry.go
index 0c5186e..2c3d7c9 100644
--- a/experimental/webtry/webtry.go
+++ b/experimental/webtry/webtry.go
@@ -8,8 +8,6 @@
 	"encoding/json"
 	"flag"
 	"fmt"
-	_ "github.com/go-sql-driver/mysql"
-	_ "github.com/mattn/go-sqlite3"
 	htemplate "html/template"
 	"io/ioutil"
 	"log"
@@ -24,6 +22,12 @@
 	"time"
 )
 
+import (
+	"github.com/fiorix/go-web/autogzip"
+	_ "github.com/go-sql-driver/mysql"
+	_ "github.com/mattn/go-sqlite3"
+)
+
 const (
 	RESULT_COMPILE = `../../experimental/webtry/safec++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -DSK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_CAN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I../../tools/flags -I../../include/config -I../../include/core -I../../include/pathops -I../../include/pipe -I../../include/effects -I../../include/ports -I../../src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno-exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unused-parameter -m64 -fno-rtti -Wnon-virtual-dtor -c ../../../cache/%s.cpp -o ../../../cache/%s.o`
 	LINK           = `../../experimental/webtry/safec++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start-group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/gyp/libflags.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc.a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a libskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontconfig -ldl -lfreetype`
@@ -336,6 +340,7 @@
 // reportError formats an HTTP error response and also logs the detailed error message.
 func reportError(w http.ResponseWriter, r *http.Request, err error, message string) {
 	log.Printf("Error: %s\n%s", message, err.Error())
+	w.Header().Set("Content-Type", "text/plain")
 	http.Error(w, message, 500)
 }
 
@@ -351,6 +356,7 @@
 		http.Error(w, "Failed to serialize a response", 500)
 		return
 	}
+	w.Header().Set("Content-Type", "text/plain")
 	w.Write(resp)
 }
 
@@ -381,6 +387,7 @@
 		return
 	}
 	filename := match[1]
+	w.Header().Set("Content-Type", "image/png")
 	http.ServeFile(w, r, fmt.Sprintf("../../../inout/%s", filename))
 }
 
@@ -414,6 +421,7 @@
 		}
 		recent = append(recent, Try{Hash: hash, CreateTS: create_ts.Format("2006-02-01")})
 	}
+	w.Header().Set("Content-Type", "text/html")
 	if err := recentTemplate.Execute(w, Recent{Tries: recent, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
 		log.Printf("ERROR: Failed to expand template: %q\n", err)
 	}
@@ -484,6 +492,7 @@
 			hash = tries[len(tries)-1].Hash
 			code, _ = getCode(hash)
 		}
+		w.Header().Set("Content-Type", "text/html")
 		if err := workspaceTemplate.Execute(w, Workspace{Tries: tries, Code: code, Name: name, Hash: hash, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
 			log.Printf("ERROR: Failed to expand template: %q\n", err)
 		}
@@ -537,6 +546,7 @@
 		return
 	}
 	// Expand the template.
+	w.Header().Set("Content-Type", "text/html")
 	if err := iframeTemplate.Execute(w, userCode{Code: code, Hash: hash}); err != nil {
 		log.Printf("ERROR: Failed to expand template: %q\n", err)
 	}
@@ -604,6 +614,7 @@
 			}
 		}
 		// Expand the template.
+		w.Header().Set("Content-Type", "text/html")
 		if err := indexTemplate.Execute(w, userCode{Code: code, Hash: hash, Titlebar: Titlebar{GitHash: gitHash, GitInfo: gitInfo}}); err != nil {
 			log.Printf("ERROR: Failed to expand template: %q\n", err)
 		}
@@ -683,23 +694,24 @@
 			reportTryError(w, r, err, "Failed to serialize a response.", hash)
 			return
 		}
+		w.Header().Set("Content-Type", "application/json")
 		w.Write(resp)
 	}
 }
 
 func main() {
 	flag.Parse()
-	http.HandleFunc("/i/", imageHandler)
-	http.HandleFunc("/w/", workspaceHandler)
-	http.HandleFunc("/recent/", recentHandler)
-	http.HandleFunc("/iframe/", iframeHandler)
-	http.HandleFunc("/json/", tryInfoHandler)
+	http.HandleFunc("/i/", autogzip.HandleFunc(imageHandler))
+	http.HandleFunc("/w/", autogzip.HandleFunc(workspaceHandler))
+	http.HandleFunc("/recent/", autogzip.HandleFunc(recentHandler))
+	http.HandleFunc("/iframe/", autogzip.HandleFunc(iframeHandler))
+	http.HandleFunc("/json/", autogzip.HandleFunc(tryInfoHandler))
 
 	// Resources are served directly
 	// TODO add support for caching/etags/gzip
-	http.Handle("/res/", http.FileServer(http.Dir("./")))
+	http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./"))))
 
 	// TODO Break out /c/ as it's own handler.
-	http.HandleFunc("/", mainHandler)
+	http.HandleFunc("/", autogzip.HandleFunc(mainHandler))
 	log.Fatal(http.ListenAndServe(*port, nil))
 }