1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 03:31:30 +00:00
Beego/context/output.go

364 lines
10 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-21 09:59:31 +00:00
package context
2013-08-21 05:24:14 +00:00
import (
"bytes"
"compress/flate"
"compress/gzip"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
2013-11-08 10:00:07 +00:00
"html/template"
2013-08-21 05:24:14 +00:00
"io"
"mime"
"net/http"
"path/filepath"
"strconv"
"strings"
2015-03-31 04:36:39 +00:00
"time"
2013-08-21 05:24:14 +00:00
)
2013-12-25 12:13:38 +00:00
// BeegoOutput does work for sending response header.
2013-08-21 05:24:14 +00:00
type BeegoOutput struct {
2013-09-09 16:00:11 +00:00
Context *Context
2013-08-21 05:24:14 +00:00
Status int
EnableGzip bool
}
2013-12-25 12:13:38 +00:00
// NewOutput returns new BeegoOutput.
// it contains nothing now.
2013-12-18 14:33:21 +00:00
func NewOutput() *BeegoOutput {
return &BeegoOutput{}
2013-08-21 05:24:14 +00:00
}
2013-12-25 12:13:38 +00:00
// Header sets response header item string via given key.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) Header(key, val string) {
2013-12-18 14:33:21 +00:00
output.Context.ResponseWriter.Header().Set(key, val)
2013-08-21 05:24:14 +00:00
}
2013-12-25 12:13:38 +00:00
// Body sets response body content.
// if EnableGzip, compress content string.
// it sends out response body directly.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) Body(content []byte) {
2015-09-10 07:31:09 +00:00
outputWriter := output.Context.ResponseWriter.(io.Writer)
2013-09-09 16:00:11 +00:00
if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" {
splitted := strings.SplitN(output.Context.Input.Header("Accept-Encoding"), ",", -1)
2013-08-21 05:24:14 +00:00
encodings := make([]string, len(splitted))
for i, val := range splitted {
encodings[i] = strings.TrimSpace(val)
}
for _, val := range encodings {
if val == "gzip" {
output.Header("Content-Encoding", "gzip")
2015-09-10 07:31:09 +00:00
outputWriter, _ = gzip.NewWriterLevel(output.Context.ResponseWriter, gzip.BestSpeed)
2013-08-21 05:24:14 +00:00
break
} else if val == "deflate" {
output.Header("Content-Encoding", "deflate")
2015-09-10 07:31:09 +00:00
outputWriter, _ = flate.NewWriter(output.Context.ResponseWriter, flate.BestSpeed)
2013-08-21 05:24:14 +00:00
break
}
}
} else {
output.Header("Content-Length", strconv.Itoa(len(content)))
}
// Write status code if it has been set manually
// Set it to 0 afterwards to prevent "multiple response.WriteHeader calls"
if output.Status != 0 {
output.Context.ResponseWriter.WriteHeader(output.Status)
output.Status = 0
}
2015-09-10 07:31:09 +00:00
outputWriter.Write(content)
switch outputWriter.(type) {
2013-08-21 05:24:14 +00:00
case *gzip.Writer:
2015-09-10 07:31:09 +00:00
outputWriter.(*gzip.Writer).Close()
2013-08-21 05:24:14 +00:00
case *flate.Writer:
2015-09-10 07:31:09 +00:00
outputWriter.(*flate.Writer).Close()
2013-08-21 05:24:14 +00:00
}
}
2013-12-25 12:13:38 +00:00
// Cookie sets cookie value via given key.
// others are ordered as cookie's max age time, path,domain, secure and httponly.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) {
var b bytes.Buffer
fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value))
2015-09-10 07:31:09 +00:00
//fix cookie not work in IE
if len(others) > 0 {
switch v := others[0].(type) {
case int:
if v > 0 {
fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v)
} else if v < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
case int64:
if v > 0 {
fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v)
} else if v < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
case int32:
if v > 0 {
fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v)
} else if v < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
}
}
2014-02-22 06:40:18 +00:00
// the settings below
// Path, Domain, Secure, HttpOnly
// can use nil skip set
// default "/"
2013-08-21 05:24:14 +00:00
if len(others) > 1 {
2014-02-22 03:12:57 +00:00
if v, ok := others[1].(string); ok && len(v) > 0 {
fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v))
}
2014-02-22 06:40:18 +00:00
} else {
fmt.Fprintf(&b, "; Path=%s", "/")
2013-08-21 05:24:14 +00:00
}
2014-02-22 06:40:18 +00:00
// default empty
2014-02-22 03:12:57 +00:00
if len(others) > 2 {
if v, ok := others[2].(string); ok && len(v) > 0 {
fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v))
}
2013-08-21 05:24:14 +00:00
}
2014-02-22 06:40:18 +00:00
// default empty
2014-02-22 03:12:57 +00:00
if len(others) > 3 {
var secure bool
switch v := others[3].(type) {
case bool:
secure = v
default:
2014-02-22 06:40:18 +00:00
if others[3] != nil {
secure = true
}
2014-02-22 03:12:57 +00:00
}
if secure {
fmt.Fprintf(&b, "; Secure")
}
2013-08-21 05:24:14 +00:00
}
2014-02-22 06:40:18 +00:00
// default false. for session cookie default true
httponly := false
2014-02-22 03:12:57 +00:00
if len(others) > 4 {
if v, ok := others[4].(bool); ok && v {
// HttpOnly = true
httponly = true
2014-02-22 03:12:57 +00:00
}
2013-08-21 05:24:14 +00:00
}
2014-02-22 03:12:57 +00:00
2014-02-22 06:40:18 +00:00
if httponly {
fmt.Fprintf(&b, "; HttpOnly")
}
2013-12-18 14:33:21 +00:00
output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String())
2013-08-21 05:24:14 +00:00
}
var cookieNameSanitizer = strings.NewReplacer("\n", "-", "\r", "-")
func sanitizeName(n string) string {
return cookieNameSanitizer.Replace(n)
}
var cookieValueSanitizer = strings.NewReplacer("\n", " ", "\r", " ", ";", " ")
func sanitizeValue(v string) string {
return cookieValueSanitizer.Replace(v)
}
2015-09-10 07:31:09 +00:00
// JSON writes json to response body.
2013-12-25 12:13:38 +00:00
// if coding is true, it converts utf-8 to \u0000 type.
2015-09-10 07:31:09 +00:00
func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) error {
output.Header("Content-Type", "application/json; charset=utf-8")
2013-09-09 16:00:11 +00:00
var content []byte
var err error
if hasIndent {
content, err = json.MarshalIndent(data, "", " ")
} else {
content, err = json.Marshal(data)
}
2013-08-21 05:24:14 +00:00
if err != nil {
2013-12-18 14:33:21 +00:00
http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
2013-08-21 05:24:14 +00:00
return err
}
2013-09-09 16:00:11 +00:00
if coding {
2015-09-10 07:31:09 +00:00
content = []byte(stringsToJSON(string(content)))
2013-09-09 16:00:11 +00:00
}
2013-08-21 05:24:14 +00:00
output.Body(content)
return nil
}
2015-09-10 07:31:09 +00:00
// JSONP writes jsonp to response body.
func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error {
output.Header("Content-Type", "application/javascript; charset=utf-8")
2013-09-09 16:00:11 +00:00
var content []byte
var err error
if hasIndent {
content, err = json.MarshalIndent(data, "", " ")
} else {
content, err = json.Marshal(data)
}
2013-08-21 05:24:14 +00:00
if err != nil {
2013-12-18 14:33:21 +00:00
http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
2013-08-21 05:24:14 +00:00
return err
}
2013-09-09 16:00:11 +00:00
callback := output.Context.Input.Query("callback")
2013-08-21 05:24:14 +00:00
if callback == "" {
return errors.New(`"callback" parameter required`)
}
2015-09-10 07:31:09 +00:00
callbackContent := bytes.NewBufferString(" " + template.JSEscapeString(callback))
callbackContent.WriteString("(")
callbackContent.Write(content)
callbackContent.WriteString(");\r\n")
output.Body(callbackContent.Bytes())
2013-08-21 05:24:14 +00:00
return nil
}
2015-09-10 07:31:09 +00:00
// XML writes xml string to response body.
func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
output.Header("Content-Type", "application/xml; charset=utf-8")
2013-09-09 16:00:11 +00:00
var content []byte
var err error
if hasIndent {
content, err = xml.MarshalIndent(data, "", " ")
} else {
content, err = xml.Marshal(data)
}
2013-08-21 05:24:14 +00:00
if err != nil {
2013-12-18 14:33:21 +00:00
http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
2013-08-21 05:24:14 +00:00
return err
}
output.Body(content)
return nil
}
2013-12-25 12:13:38 +00:00
// Download forces response for download file.
// it prepares the download response header automatically.
2014-05-16 18:56:50 +00:00
func (output *BeegoOutput) Download(file string, filename ...string) {
2013-08-21 05:24:14 +00:00
output.Header("Content-Description", "File Transfer")
output.Header("Content-Type", "application/octet-stream")
2014-05-16 18:56:50 +00:00
if len(filename) > 0 && filename[0] != "" {
output.Header("Content-Disposition", "attachment; filename="+filename[0])
} else {
output.Header("Content-Disposition", "attachment; filename="+filepath.Base(file))
}
2013-08-21 05:24:14 +00:00
output.Header("Content-Transfer-Encoding", "binary")
output.Header("Expires", "0")
output.Header("Cache-Control", "must-revalidate")
output.Header("Pragma", "public")
2013-12-18 14:33:21 +00:00
http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
2013-08-21 05:24:14 +00:00
}
2013-12-25 12:13:38 +00:00
// ContentType sets the content type from ext string.
// MIME type is given in mime package.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) ContentType(ext string) {
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
ctype := mime.TypeByExtension(ext)
if ctype != "" {
output.Header("Content-Type", ctype)
}
}
2013-12-25 12:13:38 +00:00
// SetStatus sets response status code.
// It writes response header directly.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) SetStatus(status int) {
output.Status = status
}
2013-12-25 12:13:38 +00:00
// IsCachable returns boolean of this request is cached.
// HTTP 304 means cached.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsCachable(status int) bool {
return output.Status >= 200 && output.Status < 300 || output.Status == 304
}
2013-12-25 12:13:38 +00:00
// IsEmpty returns boolean of this request is empty.
// HTTP 201204 and 304 means empty.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsEmpty(status int) bool {
return output.Status == 201 || output.Status == 204 || output.Status == 304
}
2013-12-25 12:13:38 +00:00
// IsOk returns boolean of this request runs well.
// HTTP 200 means ok.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsOk(status int) bool {
return output.Status == 200
}
2013-12-25 12:13:38 +00:00
// IsSuccessful returns boolean of this request runs successfully.
// HTTP 2xx means ok.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsSuccessful(status int) bool {
return output.Status >= 200 && output.Status < 300
}
2013-12-25 12:13:38 +00:00
// IsRedirect returns boolean of this request is redirection header.
// HTTP 301,302,307 means redirection.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsRedirect(status int) bool {
return output.Status == 301 || output.Status == 302 || output.Status == 303 || output.Status == 307
}
2013-12-25 12:13:38 +00:00
// IsForbidden returns boolean of this request is forbidden.
// HTTP 403 means forbidden.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsForbidden(status int) bool {
return output.Status == 403
}
2013-12-25 12:13:38 +00:00
// IsNotFound returns boolean of this request is not found.
// HTTP 404 means forbidden.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsNotFound(status int) bool {
return output.Status == 404
}
2015-09-10 07:31:09 +00:00
// IsClientError returns boolean of this request client sends error data.
2013-12-25 12:13:38 +00:00
// HTTP 4xx means forbidden.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsClientError(status int) bool {
return output.Status >= 400 && output.Status < 500
}
2013-12-25 12:13:38 +00:00
// IsServerError returns boolean of this server handler errors.
// HTTP 5xx means server internal error.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) IsServerError(status int) bool {
return output.Status >= 500 && output.Status < 600
}
2013-09-09 16:00:11 +00:00
2015-09-10 07:31:09 +00:00
func stringsToJSON(str string) string {
2013-09-09 16:00:11 +00:00
rs := []rune(str)
jsons := ""
for _, r := range rs {
rint := int(r)
if rint < 128 {
jsons += string(r)
} else {
jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
}
}
return jsons
}
2015-09-10 07:31:09 +00:00
// Session sets session item value with given key.
func (output *BeegoOutput) Session(name interface{}, value interface{}) {
output.Context.Input.CruSession.Set(name, value)
}