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

113 lines
3.1 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 (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
2013-11-26 03:05:49 +00:00
"net/http"
"strconv"
"strings"
"time"
2013-12-03 13:37:39 +00:00
"github.com/astaxie/beego/middleware"
2013-08-21 05:24:14 +00:00
)
2013-12-25 12:13:38 +00:00
// Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter.
// BeegoInput and BeegoOutput provides some api to operate request and response more easily.
2013-08-21 05:24:14 +00:00
type Context struct {
Input *BeegoInput
Output *BeegoOutput
Request *http.Request
ResponseWriter http.ResponseWriter
}
2013-12-25 12:13:38 +00:00
// Redirect does redirection to localurl with http header status code.
// It sends http response header directly.
2013-08-21 05:24:14 +00:00
func (ctx *Context) Redirect(status int, localurl string) {
ctx.Output.Header("Location", localurl)
ctx.Output.SetStatus(status)
}
2013-12-25 12:13:38 +00:00
// Abort stops this request.
// if middleware.ErrorMaps exists, panic body.
// if middleware.HTTPExceptionMaps exists, panic HTTPException struct with status and body string.
2013-09-09 16:00:11 +00:00
func (ctx *Context) Abort(status int, body string) {
ctx.Output.SetStatus(status)
// first panic from ErrorMaps, is is user defined error functions.
if _, ok := middleware.ErrorMaps[body]; ok {
panic(body)
}
// second panic from HTTPExceptionMaps, it is system defined functions.
2013-11-26 03:05:49 +00:00
if e, ok := middleware.HTTPExceptionMaps[status]; ok {
if len(body) >= 1 {
e.Description = body
}
panic(e)
}
// last panic user string
panic(body)
2013-09-09 16:00:11 +00:00
}
2013-12-25 12:13:38 +00:00
// Write string to response body.
// it sends response body.
2013-08-21 05:24:14 +00:00
func (ctx *Context) WriteString(content string) {
ctx.Output.Body([]byte(content))
}
2013-12-25 12:13:38 +00:00
// Get cookie from request by a given key.
// It's alias of BeegoInput.Cookie.
2013-08-21 05:24:14 +00:00
func (ctx *Context) GetCookie(key string) string {
return ctx.Input.Cookie(key)
}
2013-12-25 12:13:38 +00:00
// Set cookie for response.
// It's alias of BeegoOutput.Cookie.
2013-08-21 05:24:14 +00:00
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
ctx.Output.Cookie(name, value, others...)
}
// Get secure cookie from request by a given key.
func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) {
val := ctx.Input.Cookie(key)
if val == "" {
return "", false
}
parts := strings.SplitN(val, "|", 3)
if len(parts) != 3 {
return "", false
}
vs := parts[0]
timestamp := parts[1]
sig := parts[2]
h := hmac.New(sha1.New, []byte(Secret))
fmt.Fprintf(h, "%s%s", vs, timestamp)
if fmt.Sprintf("%02x", h.Sum(nil)) != sig {
return "", false
}
res, _ := base64.URLEncoding.DecodeString(vs)
return string(res), true
}
// Set Secure cookie for response.
func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{}) {
vs := base64.URLEncoding.EncodeToString([]byte(value))
timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
h := hmac.New(sha1.New, []byte(Secret))
fmt.Fprintf(h, "%s%s", vs, timestamp)
sig := fmt.Sprintf("%02x", h.Sum(nil))
cookie := strings.Join([]string{vs, timestamp, sig}, "|")
ctx.Output.Cookie(name, cookie, others...)
}