1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 20:10:49 +00:00
Beego/context/output.go

342 lines
9.3 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
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"
)
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) {
2013-12-18 14:33:21 +00:00
output_writer := 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")
2013-12-18 14:33:21 +00:00
output_writer, _ = 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")
2013-12-18 14:33:21 +00:00
output_writer, _ = 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)))
}
output_writer.Write(content)
switch output_writer.(type) {
case *gzip.Writer:
output_writer.(*gzip.Writer).Close()
case *flate.Writer:
output_writer.(*flate.Writer).Close()
}
}
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))
if len(others) > 0 {
2014-02-22 03:12:57 +00:00
switch v := others[0].(type) {
2013-08-21 05:24:14 +00:00
case int:
2014-02-22 03:12:57 +00:00
if v > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", v)
} else if v < 0 {
2013-08-21 05:24:14 +00:00
fmt.Fprintf(&b, "; Max-Age=0")
}
case int64:
2014-02-22 03:12:57 +00:00
if v > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", v)
} else if v < 0 {
2013-08-21 05:24:14 +00:00
fmt.Fprintf(&b, "; Max-Age=0")
}
case int32:
2014-02-22 03:12:57 +00:00
if v > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", v)
} else if v < 0 {
2013-08-21 05:24:14 +00:00
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)
}
2013-12-25 12:13:38 +00:00
// Json writes json to response body.
// if coding is true, it converts utf-8 to \u0000 type.
2013-09-09 16:00:11 +00:00
func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) error {
2013-08-21 05:24:14 +00:00
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 {
content = []byte(stringsToJson(string(content)))
}
2013-08-21 05:24:14 +00:00
output.Body(content)
return nil
}
2013-12-25 12:13:38 +00:00
// Jsonp writes jsonp to response body.
2013-09-09 16:00:11 +00:00
func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error {
2013-08-21 05:24:14 +00:00
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`)
}
callback_content := bytes.NewBufferString(" " + template.JSEscapeString(callback))
2013-08-21 05:24:14 +00:00
callback_content.WriteString("(")
callback_content.Write(content)
callback_content.WriteString(");\r\n")
output.Body(callback_content.Bytes())
return nil
}
2013-12-25 12:13:38 +00:00
// Xml writes xml string to response body.
2013-09-09 16:02:22 +00:00
func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error {
2013-08-21 05:24:14 +00:00
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.
2013-08-21 05:24:14 +00:00
func (output *BeegoOutput) Download(file string) {
output.Header("Content-Description", "File Transfer")
output.Header("Content-Type", "application/octet-stream")
output.Header("Content-Disposition", "attachment; filename="+filepath.Base(file))
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) {
2013-12-18 14:33:21 +00:00
output.Context.ResponseWriter.WriteHeader(status)
2013-08-21 05:24:14 +00:00
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
}
2013-12-25 12:13:38 +00:00
// IsClient returns boolean of this request client sends error data.
// 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
func stringsToJson(str string) string {
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
}
2013-12-25 12:13:38 +00:00
// Sessions sets session item value with given key.
func (output *BeegoOutput) Session(name interface{}, value interface{}) {
output.Context.Input.CruSession.Set(name, value)
}