add error return for (Un)GzipData

This commit is contained in:
Tamás Gulácsi 2013-01-04 23:33:07 +01:00
parent ce7394f3bf
commit 8243710351
3 changed files with 24 additions and 34 deletions

View File

@ -42,7 +42,7 @@ var (
store *storage.Store store *storage.Store
) )
var fileNameEscaper = strings.NewReplacer("\\","\\\\","\"","\\\"") var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
func statusHandler(w http.ResponseWriter, r *http.Request) { func statusHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{}) m := make(map[string]interface{})
@ -156,7 +156,9 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
} else { } else {
n.Data = storage.UnGzipData(n.Data) if n.Data, err = storage.UnGzipData(n.Data); err != nil {
debug("lookup error:", err, r.URL.Path)
}
} }
} }
} }

View File

@ -10,54 +10,40 @@ import (
/* /*
* Default more not to gzip since gzip can be done on client side. * Default more not to gzip since gzip can be done on client side.
*/ */
func IsGzippable(ext, mtype string) bool { func IsGzippable(ext, mtype string) bool {
if strings.HasPrefix(mtype, "text/"){ if strings.HasPrefix(mtype, "text/") {
return true
}
if ext == ".zip" {
return false
}
if ext == ".rar" {
return false
}
if ext == ".gz" {
return false
}
if ext == ".pdf" {
return true return true
} }
if ext == ".css" { switch ext {
case ".zip", ".rar", ".gz", ".bz2", ".xz":
return false
case ".pdf", ".txt", ".html", ".css", ".js", ".json":
return true return true
} }
if ext == ".js" {
return true
}
if ext == ".json" {
return true
}
if strings.HasPrefix(mtype, "application/") { if strings.HasPrefix(mtype, "application/") {
if strings.HasSuffix(mtype, "xml") { if strings.HasSuffix(mtype, "xml") ||
return true strings.HasSuffix(mtype, "script") {
}
if strings.HasSuffix(mtype, "script") {
return true return true
} }
} }
return false return false
} }
func GzipData(input []byte) []byte {
func GzipData(input []byte) ([]byte, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression) w, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
if _, err := w.Write(input); err != nil { if _, err := w.Write(input); err != nil {
println("error compressing data:", err) println("error compressing data:", err)
return nil, err
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
println("error closing compressed data:", err) println("error closing compressed data:", err)
return nil, err
} }
return buf.Bytes() return buf.Bytes(), nil
} }
func UnGzipData(input []byte) []byte { func UnGzipData(input []byte) ([]byte, error) {
buf := bytes.NewBuffer(input) buf := bytes.NewBuffer(input)
r, _ := gzip.NewReader(buf) r, _ := gzip.NewReader(buf)
defer r.Close() defer r.Close()
@ -65,5 +51,5 @@ func UnGzipData(input []byte) []byte {
if err != nil { if err != nil {
println("error uncompressing data:", err) println("error uncompressing data:", err)
} }
return output return output, err
} }

View File

@ -12,8 +12,8 @@ import (
) )
const ( const (
NeedleHeaderSize = 16 //should never change this NeedleHeaderSize = 16 //should never change this
NeedlePaddingSize = 8 NeedlePaddingSize = 8
NeedleChecksumSize = 4 NeedleChecksumSize = 4
) )
@ -64,7 +64,9 @@ func NewNeedle(r *http.Request) (n *Needle, fname string, e error) {
mtype = contentType mtype = contentType
} }
if IsGzippable(ext, mtype) { if IsGzippable(ext, mtype) {
data = GzipData(data) if data, e = GzipData(data); e != nil {
return
}
n.SetGzipped() n.SetGzipped()
} }
if ext == ".gz" { if ext == ".gz" {