fix #121
Showing
1 changed file
with
40 additions
and
40 deletions
| ... | @@ -2,8 +2,8 @@ package beego | ... | @@ -2,8 +2,8 @@ package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "compress/flate" | ||
| 5 | "compress/gzip" | 6 | "compress/gzip" |
| 6 | "compress/zlib" | ||
| 7 | "crypto/hmac" | 7 | "crypto/hmac" |
| 8 | "crypto/sha1" | 8 | "crypto/sha1" |
| 9 | "encoding/base64" | 9 | "encoding/base64" |
| ... | @@ -109,39 +109,7 @@ func (c *Controller) Render() error { | ... | @@ -109,39 +109,7 @@ func (c *Controller) Render() error { |
| 109 | return err | 109 | return err |
| 110 | } else { | 110 | } else { |
| 111 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8") | 111 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 112 | output_writer := c.Ctx.ResponseWriter.(io.Writer) | 112 | c.writeToWriter(rb) |
| 113 | if EnableGzip == true && c.Ctx.Request.Header.Get("Accept-Encoding") != "" { | ||
| 114 | splitted := strings.SplitN(c.Ctx.Request.Header.Get("Accept-Encoding"), ",", -1) | ||
| 115 | encodings := make([]string, len(splitted)) | ||
| 116 | |||
| 117 | for i, val := range splitted { | ||
| 118 | encodings[i] = strings.TrimSpace(val) | ||
| 119 | } | ||
| 120 | for _, val := range encodings { | ||
| 121 | if val == "gzip" { | ||
| 122 | c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip") | ||
| 123 | output_writer, _ = gzip.NewWriterLevel(c.Ctx.ResponseWriter, gzip.BestSpeed) | ||
| 124 | |||
| 125 | break | ||
| 126 | } else if val == "deflate" { | ||
| 127 | c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "deflate") | ||
| 128 | output_writer, _ = zlib.NewWriterLevel(c.Ctx.ResponseWriter, zlib.BestSpeed) | ||
| 129 | break | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } else { | ||
| 133 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true) | ||
| 134 | } | ||
| 135 | output_writer.Write(rb) | ||
| 136 | switch output_writer.(type) { | ||
| 137 | case *gzip.Writer: | ||
| 138 | output_writer.(*gzip.Writer).Close() | ||
| 139 | case *zlib.Writer: | ||
| 140 | output_writer.(*zlib.Writer).Close() | ||
| 141 | case io.WriteCloser: | ||
| 142 | output_writer.(io.WriteCloser).Close() | ||
| 143 | } | ||
| 144 | return nil | ||
| 145 | } | 113 | } |
| 146 | return nil | 114 | return nil |
| 147 | } | 115 | } |
| ... | @@ -203,6 +171,41 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -203,6 +171,41 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 203 | return []byte{}, nil | 171 | return []byte{}, nil |
| 204 | } | 172 | } |
| 205 | 173 | ||
| 174 | func (c *Controller) writeToWriter(rb []byte) { | ||
| 175 | output_writer := c.Ctx.ResponseWriter.(io.Writer) | ||
| 176 | if EnableGzip == true && c.Ctx.Request.Header.Get("Accept-Encoding") != "" { | ||
| 177 | splitted := strings.SplitN(c.Ctx.Request.Header.Get("Accept-Encoding"), ",", -1) | ||
| 178 | encodings := make([]string, len(splitted)) | ||
| 179 | |||
| 180 | for i, val := range splitted { | ||
| 181 | encodings[i] = strings.TrimSpace(val) | ||
| 182 | } | ||
| 183 | for _, val := range encodings { | ||
| 184 | if val == "gzip" { | ||
| 185 | c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip") | ||
| 186 | output_writer, _ = gzip.NewWriterLevel(c.Ctx.ResponseWriter, gzip.BestSpeed) | ||
| 187 | |||
| 188 | break | ||
| 189 | } else if val == "deflate" { | ||
| 190 | c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "deflate") | ||
| 191 | output_writer, _ = flate.NewWriter(c.Ctx.ResponseWriter, flate.BestSpeed) | ||
| 192 | break | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } else { | ||
| 196 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true) | ||
| 197 | } | ||
| 198 | output_writer.Write(rb) | ||
| 199 | switch output_writer.(type) { | ||
| 200 | case *gzip.Writer: | ||
| 201 | output_writer.(*gzip.Writer).Close() | ||
| 202 | case *flate.Writer: | ||
| 203 | output_writer.(*flate.Writer).Close() | ||
| 204 | case io.WriteCloser: | ||
| 205 | output_writer.(io.WriteCloser).Close() | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 206 | func (c *Controller) Redirect(url string, code int) { | 209 | func (c *Controller) Redirect(url string, code int) { |
| 207 | c.Ctx.Redirect(code, url) | 210 | c.Ctx.Redirect(code, url) |
| 208 | } | 211 | } |
| ... | @@ -217,9 +220,8 @@ func (c *Controller) ServeJson() { | ... | @@ -217,9 +220,8 @@ func (c *Controller) ServeJson() { |
| 217 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) | 220 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) |
| 218 | return | 221 | return |
| 219 | } | 222 | } |
| 220 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true) | ||
| 221 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8") | 223 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8") |
| 222 | c.Ctx.ResponseWriter.Write(content) | 224 | c.writeToWriter(content) |
| 223 | } | 225 | } |
| 224 | 226 | ||
| 225 | func (c *Controller) ServeJsonp() { | 227 | func (c *Controller) ServeJsonp() { |
| ... | @@ -237,9 +239,8 @@ func (c *Controller) ServeJsonp() { | ... | @@ -237,9 +239,8 @@ func (c *Controller) ServeJsonp() { |
| 237 | callback_content.WriteString("(") | 239 | callback_content.WriteString("(") |
| 238 | callback_content.Write(content) | 240 | callback_content.Write(content) |
| 239 | callback_content.WriteString(");\r\n") | 241 | callback_content.WriteString(");\r\n") |
| 240 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(callback_content.Len()), true) | ||
| 241 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8") | 242 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8") |
| 242 | c.Ctx.ResponseWriter.Write(callback_content.Bytes()) | 243 | c.writeToWriter(callback_content.Bytes()) |
| 243 | } | 244 | } |
| 244 | 245 | ||
| 245 | func (c *Controller) ServeXml() { | 246 | func (c *Controller) ServeXml() { |
| ... | @@ -248,9 +249,8 @@ func (c *Controller) ServeXml() { | ... | @@ -248,9 +249,8 @@ func (c *Controller) ServeXml() { |
| 248 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) | 249 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) |
| 249 | return | 250 | return |
| 250 | } | 251 | } |
| 251 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true) | ||
| 252 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/xml;charset=UTF-8") | 252 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/xml;charset=UTF-8") |
| 253 | c.Ctx.ResponseWriter.Write(content) | 253 | c.writeToWriter(content) |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | func (c *Controller) Input() url.Values { | 256 | func (c *Controller) Input() url.Values { | ... | ... |
-
Please register or sign in to post a comment