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

350 lines
9.5 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"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
2013-11-08 10:00:07 +00:00
"html/template"
2016-03-13 03:16:19 +00:00
"io"
2013-08-21 05:24:14 +00:00
"mime"
"net/http"
2016-04-27 14:05:31 +00:00
"net/url"
2016-03-23 13:27:28 +00:00
"os"
2013-08-21 05:24:14 +00:00
"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
}
2015-12-10 13:59:54 +00:00
// Reset init BeegoOutput
func (output *BeegoOutput) Reset(ctx *Context) {
output.Context = ctx
output.Status = 0
}
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.
2016-02-12 03:36:25 +00:00
func (output *BeegoOutput) Body(content []byte) error {
2015-11-10 02:55:47 +00:00
var encoding string
2015-11-12 02:08:57 +00:00
var buf = &bytes.Buffer{}
2015-11-10 02:55:47 +00:00
if output.EnableGzip {
2015-12-10 13:59:54 +00:00
encoding = ParseEncoding(output.Context.Request)
2015-11-10 02:55:47 +00:00
}
2015-11-12 02:08:57 +00:00
if b, n, _ := WriteBody(encoding, buf, content); b {
2015-11-10 02:55:47 +00:00
output.Header("Content-Encoding", n)
2016-12-31 10:44:38 +00:00
output.Header("Content-Length", strconv.Itoa(buf.Len()))
} 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
2016-03-13 03:16:19 +00:00
} else {
output.Context.ResponseWriter.Started = true
}
2016-03-13 03:16:19 +00:00
io.Copy(output.Context.ResponseWriter, buf)
return nil
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 {
2015-11-10 03:47:10 +00:00
var maxAge int64
2015-09-10 07:31:09 +00:00
switch v := others[0].(type) {
2015-11-09 03:03:57 +00:00
case int:
2015-11-10 03:47:10 +00:00
maxAge = int64(v)
2015-11-09 03:03:57 +00:00
case int32:
2015-11-10 03:47:10 +00:00
maxAge = int64(v)
case int64:
maxAge = v
}
switch {
case maxAge > 0:
2015-11-10 03:47:10 +00:00
fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(maxAge)*time.Second).UTC().Format(time.RFC1123), maxAge)
case maxAge < 0:
2015-11-10 03:47:10 +00:00
fmt.Fprintf(&b, "; Max-Age=0")
2015-09-10 07:31:09 +00:00
}
}
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
2014-02-22 03:12:57 +00:00
if len(others) > 4 {
if v, ok := others[4].(bool); ok && v {
2016-08-06 13:13:58 +00:00
fmt.Fprintf(&b, "; HttpOnly")
2014-02-22 03:12:57 +00:00
}
2013-08-21 05:24:14 +00:00
}
2014-02-22 03:12:57 +00:00
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
}
2016-02-12 03:36:25 +00:00
return output.Body(content)
2013-08-21 05:24:14 +00:00
}
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`)
}
callback = template.JSEscapeString(callback)
callbackContent := bytes.NewBufferString(" if(window." + callback + ")" + callback)
2015-09-10 07:31:09 +00:00
callbackContent.WriteString("(")
callbackContent.Write(content)
callbackContent.WriteString(");\r\n")
2016-02-12 03:36:25 +00:00
return output.Body(callbackContent.Bytes())
2013-08-21 05:24:14 +00:00
}
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
}
2016-02-12 03:36:25 +00:00
return output.Body(content)
2013-08-21 05:24:14 +00:00
}
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) {
2016-03-23 13:27:28 +00:00
// check get file error, file not found or other error.
if _, err := os.Stat(file); err != nil {
http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
return
}
2016-04-27 14:05:31 +00:00
var fName string
2014-05-16 18:56:50 +00:00
if len(filename) > 0 && filename[0] != "" {
2016-04-27 14:05:31 +00:00
fName = filename[0]
2014-05-16 18:56:50 +00:00
} else {
2016-04-27 14:05:31 +00:00
fName = filepath.Base(file)
2014-05-16 18:56:50 +00:00
}
2016-04-27 14:05:31 +00:00
output.Header("Content-Disposition", "attachment; filename="+url.QueryEscape(fName))
output.Header("Content-Description", "File Transfer")
output.Header("Content-Type", "application/octet-stream")
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.
func (output *BeegoOutput) IsCachable() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsEmpty() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsOk() bool {
2013-08-21 05:24:14 +00:00
return output.Status == 200
}
2013-12-25 12:13:38 +00:00
// IsSuccessful returns boolean of this request runs successfully.
// HTTP 2xx means ok.
func (output *BeegoOutput) IsSuccessful() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsRedirect() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsForbidden() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsNotFound() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsClientError() bool {
2013-08-21 05:24:14 +00:00
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.
func (output *BeegoOutput) IsServerError() bool {
2013-08-21 05:24:14 +00:00
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)
}