9995168f by Francois

Update router.go

1 parent 02eacb8e
Showing 1 changed file with 43 additions and 0 deletions
...@@ -473,7 +473,39 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -473,7 +473,39 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
473 middleware.Exception("403", rw, r, "403 Forbidden") 473 middleware.Exception("403", rw, r, "403 Forbidden")
474 goto Admin 474 goto Admin
475 } 475 }
476
477 //This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request
478 isStaticFileToCompress := false
479 if StaticExtensionsToGzip != nil && len(StaticExtensionsToGzip) > 0 {
480 for _, statExtension := range StaticExtensionsToGzip {
481 if strings.HasSuffix(strings.ToLower(file), strings.ToLower(statExtension)) {
482 isStaticFileToCompress = true
483 break
484 }
485 }
486 }
487
488 if isStaticFileToCompress {
489 if EnableGzip {
490 w.contentEncoding = GetAcceptEncodingZip(r)
491 }
492
493 memzipfile, err := OpenMemZipFile(file, w.contentEncoding)
494 if err != nil {
495 return
496 }
497
498 w.InitHeadContent(finfo.Size())
499
500 if strings.HasSuffix(file, ".mustache") {
501 w.Header().Set("Content-Type", "text/html; charset=utf-8") //FIXME: hardcode
502 }
503
504 http.ServeContent(w, r, file, finfo.ModTime(), memzipfile)
505 } else {
476 http.ServeFile(w, r, file) 506 http.ServeFile(w, r, file)
507 }
508
477 w.started = true 509 w.started = true
478 goto Admin 510 goto Admin
479 } 511 }
...@@ -901,6 +933,7 @@ type responseWriter struct { ...@@ -901,6 +933,7 @@ type responseWriter struct {
901 writer http.ResponseWriter 933 writer http.ResponseWriter
902 started bool 934 started bool
903 status int 935 status int
936 contentEncoding string
904 } 937 }
905 938
906 // Header returns the header map that will be sent by WriteHeader. 939 // Header returns the header map that will be sent by WriteHeader.
...@@ -908,6 +941,16 @@ func (w *responseWriter) Header() http.Header { ...@@ -908,6 +941,16 @@ func (w *responseWriter) Header() http.Header {
908 return w.writer.Header() 941 return w.writer.Header()
909 } 942 }
910 943
944 func (w *responseWriter) InitHeadContent(contentlength int64) {
945 if w.contentEncoding == "gzip" {
946 w.Header().Set("Content-Encoding", "gzip")
947 } else if w.contentEncoding == "deflate" {
948 w.Header().Set("Content-Encoding", "deflate")
949 } else {
950 w.Header().Set("Content-Length", strconv.FormatInt(contentlength, 10))
951 }
952 }
953
911 // Write writes the data to the connection as part of an HTTP reply, 954 // Write writes the data to the connection as part of an HTTP reply,
912 // and sets `started` to true 955 // and sets `started` to true
913 func (w *responseWriter) Write(p []byte) (int, error) { 956 func (w *responseWriter) Write(p []byte) (int, error) {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!