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.
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// Package context provide the context utils
|
2014-08-18 08:41:43 +00:00
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// import "github.com/astaxie/beego/context"
|
|
|
|
//
|
|
|
|
// ctx := context.Context{Request:req,ResponseWriter:rw}
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// more docs http://beego.me/docs/module/context.md
|
2013-08-21 09:59:31 +00:00
|
|
|
package context
|
2013-08-21 05:24:14 +00:00
|
|
|
|
|
|
|
import (
|
2015-12-21 14:51:18 +00:00
|
|
|
"bufio"
|
2016-02-12 03:27:59 +00:00
|
|
|
"bytes"
|
2014-02-22 03:58:53 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
2015-12-21 14:51:18 +00:00
|
|
|
"errors"
|
2014-02-22 03:58:53 +00:00
|
|
|
"fmt"
|
2016-02-12 03:27:59 +00:00
|
|
|
"io"
|
2015-12-21 14:51:18 +00:00
|
|
|
"net"
|
2013-11-26 03:05:49 +00:00
|
|
|
"net/http"
|
2014-02-22 03:58:53 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
2014-07-24 15:12:21 +00:00
|
|
|
"github.com/astaxie/beego/utils"
|
2013-08-21 05:24:14 +00:00
|
|
|
)
|
|
|
|
|
2015-12-10 13:59:54 +00:00
|
|
|
// NewContext return the Context with Input and Output
|
|
|
|
func NewContext() *Context {
|
|
|
|
return &Context{
|
|
|
|
Input: NewInput(),
|
|
|
|
Output: NewOutput(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// Context Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter.
|
2013-12-25 12:13:38 +00:00
|
|
|
// 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
|
2015-12-10 16:20:17 +00:00
|
|
|
ResponseWriter *Response
|
2015-09-10 07:31:09 +00:00
|
|
|
_xsrfToken string
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 13:59:54 +00:00
|
|
|
// Reset init Context, BeegoInput and BeegoOutput
|
|
|
|
func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx.Request = r
|
2016-02-24 03:47:55 +00:00
|
|
|
if ctx.ResponseWriter == nil {
|
|
|
|
ctx.ResponseWriter = &Response{}
|
|
|
|
}
|
|
|
|
ctx.ResponseWriter.reset(rw)
|
2015-12-10 13:59:54 +00:00
|
|
|
ctx.Input.Reset(ctx)
|
|
|
|
ctx.Output.Reset(ctx)
|
|
|
|
}
|
|
|
|
|
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)
|
2014-08-19 01:48:21 +00:00
|
|
|
ctx.ResponseWriter.WriteHeader(status)
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Abort stops this request.
|
2015-02-26 15:34:43 +00:00
|
|
|
// if beego.ErrorMaps exists, panic body.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (ctx *Context) Abort(status int, body string) {
|
2015-02-26 16:12:10 +00:00
|
|
|
panic(body)
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// WriteString Write string to response body.
|
2013-12-25 12:13:38 +00:00
|
|
|
// it sends response body.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (ctx *Context) WriteString(content string) {
|
2014-08-14 13:37:52 +00:00
|
|
|
ctx.ResponseWriter.Write([]byte(content))
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// GetCookie Get cookie from request by a given key.
|
2013-12-25 12:13:38 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// SetCookie Set cookie for response.
|
2013-12-25 12:13:38 +00:00
|
|
|
// 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...)
|
|
|
|
}
|
2014-02-22 03:58:53 +00:00
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// GetSecureCookie Get secure cookie from request by a given key.
|
2014-02-22 03:58:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// SetSecureCookie Set Secure cookie for response.
|
2014-02-22 03:58:53 +00:00
|
|
|
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...)
|
|
|
|
}
|
2014-07-24 15:12:21 +00:00
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// XSRFToken creates a xsrf token string and returns.
|
|
|
|
func (ctx *Context) XSRFToken(key string, expire int64) string {
|
|
|
|
if ctx._xsrfToken == "" {
|
2014-07-24 15:12:21 +00:00
|
|
|
token, ok := ctx.GetSecureCookie(key, "_xsrf")
|
|
|
|
if !ok {
|
|
|
|
token = string(utils.RandomCreateBytes(32))
|
|
|
|
ctx.SetSecureCookie(key, "_xsrf", token, expire)
|
|
|
|
}
|
2015-09-10 07:31:09 +00:00
|
|
|
ctx._xsrfToken = token
|
2014-07-24 15:12:21 +00:00
|
|
|
}
|
2015-09-10 07:31:09 +00:00
|
|
|
return ctx._xsrfToken
|
2014-07-24 15:12:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 07:31:09 +00:00
|
|
|
// CheckXSRFCookie checks xsrf token in this request is valid or not.
|
2014-07-24 15:12:21 +00:00
|
|
|
// the token can provided in request header "X-Xsrftoken" and "X-CsrfToken"
|
|
|
|
// or in form field value named as "_xsrf".
|
2015-09-10 07:31:09 +00:00
|
|
|
func (ctx *Context) CheckXSRFCookie() bool {
|
2014-07-24 15:12:21 +00:00
|
|
|
token := ctx.Input.Query("_xsrf")
|
|
|
|
if token == "" {
|
|
|
|
token = ctx.Request.Header.Get("X-Xsrftoken")
|
|
|
|
}
|
|
|
|
if token == "" {
|
|
|
|
token = ctx.Request.Header.Get("X-Csrftoken")
|
|
|
|
}
|
|
|
|
if token == "" {
|
|
|
|
ctx.Abort(403, "'_xsrf' argument missing from POST")
|
2015-02-23 03:15:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-09-10 07:31:09 +00:00
|
|
|
if ctx._xsrfToken != token {
|
2014-07-24 15:12:21 +00:00
|
|
|
ctx.Abort(403, "XSRF cookie does not match POST argument")
|
2015-02-23 03:15:55 +00:00
|
|
|
return false
|
2014-07-24 15:12:21 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2015-12-10 16:20:17 +00:00
|
|
|
|
|
|
|
//Response is a wrapper for the http.ResponseWriter
|
|
|
|
//started set to true if response was written to then don't execute other handler
|
|
|
|
type Response struct {
|
|
|
|
http.ResponseWriter
|
2016-03-09 07:56:18 +00:00
|
|
|
Started bool
|
|
|
|
Status int
|
2016-02-24 03:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) reset(rw http.ResponseWriter) {
|
|
|
|
r.ResponseWriter = rw
|
|
|
|
r.Status = 0
|
|
|
|
r.Started = false
|
2015-12-10 16:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the data to the connection as part of an HTTP reply,
|
|
|
|
// and sets `started` to true.
|
|
|
|
// started means the response has sent out.
|
2016-03-10 13:47:50 +00:00
|
|
|
func (r *Response) Write(p []byte) (int, error) {
|
|
|
|
r.Started = true
|
|
|
|
return r.ResponseWriter.Write(p)
|
2015-12-10 16:20:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 13:47:50 +00:00
|
|
|
// Copy writes the data to the connection as part of an HTTP reply,
|
2016-02-12 03:27:59 +00:00
|
|
|
// and sets `started` to true.
|
|
|
|
// started means the response has sent out.
|
2016-03-10 13:47:50 +00:00
|
|
|
func (r *Response) Copy(buf *bytes.Buffer) (int64, error) {
|
|
|
|
r.Started = true
|
|
|
|
return io.Copy(r.ResponseWriter, buf)
|
2016-02-12 03:27:59 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:20:17 +00:00
|
|
|
// WriteHeader sends an HTTP response header with status code,
|
|
|
|
// and sets `started` to true.
|
2016-03-10 13:47:50 +00:00
|
|
|
func (r *Response) WriteHeader(code int) {
|
|
|
|
if r.Status > 0 {
|
2016-03-09 07:56:18 +00:00
|
|
|
//prevent multiple response.WriteHeader calls
|
2016-02-24 02:31:44 +00:00
|
|
|
return
|
|
|
|
}
|
2016-03-10 13:47:50 +00:00
|
|
|
r.Status = code
|
|
|
|
r.Started = true
|
|
|
|
r.ResponseWriter.WriteHeader(code)
|
2015-12-10 16:20:17 +00:00
|
|
|
}
|
2015-12-21 14:51:18 +00:00
|
|
|
|
|
|
|
// Hijack hijacker for http
|
2016-03-10 13:47:50 +00:00
|
|
|
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
hj, ok := r.ResponseWriter.(http.Hijacker)
|
2015-12-21 14:51:18 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.New("webserver doesn't support hijacking")
|
|
|
|
}
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush http.Flusher
|
2016-03-10 13:47:50 +00:00
|
|
|
func (r *Response) Flush() {
|
|
|
|
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
2015-12-21 14:51:18 +00:00
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify http.CloseNotifier
|
2016-03-10 13:47:50 +00:00
|
|
|
func (r *Response) CloseNotify() <-chan bool {
|
|
|
|
if cn, ok := r.ResponseWriter.(http.CloseNotifier); ok {
|
2015-12-21 14:51:18 +00:00
|
|
|
return cn.CloseNotify()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|