support gzip #37
Showing
2 changed files
with
42 additions
and
3 deletions
| ... | @@ -37,6 +37,7 @@ var ( | ... | @@ -37,6 +37,7 @@ var ( |
| 37 | SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo | 37 | SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo |
| 38 | UseFcgi bool | 38 | UseFcgi bool |
| 39 | MaxMemory int64 | 39 | MaxMemory int64 |
| 40 | EnableGzip bool // enable gzip | ||
| 40 | 41 | ||
| 41 | GlobalSessions *session.Manager //GlobalSessions | 42 | GlobalSessions *session.Manager //GlobalSessions |
| 42 | ) | 43 | ) |
| ... | @@ -66,6 +67,7 @@ func init() { | ... | @@ -66,6 +67,7 @@ func init() { |
| 66 | SessionSavePath = "" | 67 | SessionSavePath = "" |
| 67 | UseFcgi = false | 68 | UseFcgi = false |
| 68 | MaxMemory = 1 << 26 //64MB | 69 | MaxMemory = 1 << 26 //64MB |
| 70 | EnableGzip = false | ||
| 69 | } else { | 71 | } else { |
| 70 | HttpAddr = AppConfig.String("httpaddr") | 72 | HttpAddr = AppConfig.String("httpaddr") |
| 71 | if v, err := AppConfig.Int("httpport"); err != nil { | 73 | if v, err := AppConfig.Int("httpport"); err != nil { |
| ... | @@ -135,6 +137,11 @@ func init() { | ... | @@ -135,6 +137,11 @@ func init() { |
| 135 | } else { | 137 | } else { |
| 136 | UseFcgi = ar | 138 | UseFcgi = ar |
| 137 | } | 139 | } |
| 140 | if ar, err := AppConfig.Bool("enablegzip"); err != nil { | ||
| 141 | EnableGzip = false | ||
| 142 | } else { | ||
| 143 | EnableGzip = ar | ||
| 144 | } | ||
| 138 | } | 145 | } |
| 139 | StaticDir["/static"] = "static" | 146 | StaticDir["/static"] = "static" |
| 140 | 147 | ... | ... |
| ... | @@ -2,6 +2,8 @@ package beego | ... | @@ -2,6 +2,8 @@ package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "compress/gzip" | ||
| 6 | "compress/zlib" | ||
| 5 | "encoding/json" | 7 | "encoding/json" |
| 6 | "encoding/xml" | 8 | "encoding/xml" |
| 7 | "github.com/astaxie/beego/session" | 9 | "github.com/astaxie/beego/session" |
| ... | @@ -91,9 +93,39 @@ func (c *Controller) Render() error { | ... | @@ -91,9 +93,39 @@ func (c *Controller) Render() error { |
| 91 | if err != nil { | 93 | if err != nil { |
| 92 | return err | 94 | return err |
| 93 | } else { | 95 | } else { |
| 94 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true) | 96 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 95 | c.Ctx.ContentType("text/html") | 97 | output_writer := c.Ctx.ResponseWriter.(io.Writer) |
| 96 | c.Ctx.ResponseWriter.Write(rb) | 98 | if EnableGzip == true && c.Ctx.Request.Header.Get("Accept-Encoding") != "" { |
| 99 | splitted := strings.SplitN(c.Ctx.Request.Header.Get("Accept-Encoding"), ",", -1) | ||
| 100 | encodings := make([]string, len(splitted)) | ||
| 101 | |||
| 102 | for i, val := range splitted { | ||
| 103 | encodings[i] = strings.TrimSpace(val) | ||
| 104 | } | ||
| 105 | for _, val := range encodings { | ||
| 106 | if val == "gzip" { | ||
| 107 | c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip") | ||
| 108 | output_writer, _ = gzip.NewWriterLevel(c.Ctx.ResponseWriter, gzip.BestSpeed) | ||
| 109 | |||
| 110 | break | ||
| 111 | } else if val == "deflate" { | ||
| 112 | c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "deflate") | ||
| 113 | output_writer, _ = zlib.NewWriterLevel(c.Ctx.ResponseWriter, zlib.BestSpeed) | ||
| 114 | break | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } else { | ||
| 118 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true) | ||
| 119 | } | ||
| 120 | output_writer.Write(rb) | ||
| 121 | switch output_writer.(type) { | ||
| 122 | case *gzip.Writer: | ||
| 123 | output_writer.(*gzip.Writer).Close() | ||
| 124 | case *zlib.Writer: | ||
| 125 | output_writer.(*zlib.Writer).Close() | ||
| 126 | case io.WriteCloser: | ||
| 127 | output_writer.(io.WriteCloser).Close() | ||
| 128 | } | ||
| 97 | return nil | 129 | return nil |
| 98 | } | 130 | } |
| 99 | return nil | 131 | return nil | ... | ... |
-
Please register or sign in to post a comment