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

263 lines
6.7 KiB
Go
Raw Normal View History

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"
)
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
res http.ResponseWriter
}
func NewOutput(res http.ResponseWriter) *BeegoOutput {
return &BeegoOutput{
res: res,
}
}
func (output *BeegoOutput) Header(key, val string) {
output.res.Header().Set(key, val)
}
func (output *BeegoOutput) Body(content []byte) {
output_writer := output.res.(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")
output_writer, _ = gzip.NewWriterLevel(output.res, gzip.BestSpeed)
break
} else if val == "deflate" {
output.Header("Content-Encoding", "deflate")
output_writer, _ = flate.NewWriter(output.res, flate.BestSpeed)
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()
}
}
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 {
switch others[0].(type) {
case int:
if others[0].(int) > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", others[0].(int))
} else if others[0].(int) < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
case int64:
if others[0].(int64) > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", others[0].(int64))
} else if others[0].(int64) < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
case int32:
if others[0].(int32) > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", others[0].(int32))
} else if others[0].(int32) < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
}
}
if len(others) > 1 {
fmt.Fprintf(&b, "; Path=%s", sanitizeValue(others[1].(string)))
}
if len(others) > 2 {
fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(others[2].(string)))
}
if len(others) > 3 {
fmt.Fprintf(&b, "; Secure")
}
if len(others) > 4 {
fmt.Fprintf(&b, "; HttpOnly")
}
output.res.Header().Add("Set-Cookie", b.String())
}
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-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-09-09 16:00:11 +00:00
http.Error(output.res, 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-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-09-09 16:00:11 +00:00
http.Error(output.res, 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-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-09-09 16:00:11 +00:00
http.Error(output.res, err.Error(), http.StatusInternalServerError)
2013-08-21 05:24:14 +00:00
return err
}
output.Body(content)
return nil
}
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-09-09 16:00:11 +00:00
http.ServeFile(output.res, output.Context.Request, file)
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)
}
}
func (output *BeegoOutput) SetStatus(status int) {
output.res.WriteHeader(status)
output.Status = status
}
func (output *BeegoOutput) IsCachable(status int) bool {
return output.Status >= 200 && output.Status < 300 || output.Status == 304
}
func (output *BeegoOutput) IsEmpty(status int) bool {
return output.Status == 201 || output.Status == 204 || output.Status == 304
}
func (output *BeegoOutput) IsOk(status int) bool {
return output.Status == 200
}
func (output *BeegoOutput) IsSuccessful(status int) bool {
return output.Status >= 200 && output.Status < 300
}
func (output *BeegoOutput) IsRedirect(status int) bool {
return output.Status == 301 || output.Status == 302 || output.Status == 303 || output.Status == 307
}
func (output *BeegoOutput) IsForbidden(status int) bool {
return output.Status == 403
}
func (output *BeegoOutput) IsNotFound(status int) bool {
return output.Status == 404
}
func (output *BeegoOutput) IsClientError(status int) bool {
return output.Status >= 400 && output.Status < 500
}
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
}
func (output *BeegoOutput) Session(name interface{}, value interface{}) {
output.Context.Input.CruSession.Set(name, value)
}