2013-08-21 09:59:31 +00:00
|
|
|
package context
|
2013-08-21 05:24:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-04-05 16:08:03 +00:00
|
|
|
"errors"
|
2013-08-21 05:24:14 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2014-03-29 06:55:34 +00:00
|
|
|
"reflect"
|
2013-08-21 05:24:14 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/session"
|
2013-08-21 05:24:14 +00:00
|
|
|
)
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// BeegoInput operates the http request header ,data ,cookie and body.
|
|
|
|
// it also contains router params and current session.
|
2013-08-21 05:24:14 +00:00
|
|
|
type BeegoInput struct {
|
2014-03-29 06:55:34 +00:00
|
|
|
CruSession session.SessionStore
|
|
|
|
Params map[string]string
|
|
|
|
Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
|
|
|
|
Request *http.Request
|
|
|
|
RequestBody []byte
|
|
|
|
RunController reflect.Type
|
|
|
|
RunMethod string
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// NewInput return BeegoInput generated by http.Request.
|
2013-08-21 05:24:14 +00:00
|
|
|
func NewInput(req *http.Request) *BeegoInput {
|
|
|
|
return &BeegoInput{
|
2013-12-18 14:33:21 +00:00
|
|
|
Params: make(map[string]string),
|
|
|
|
Data: make(map[interface{}]interface{}),
|
|
|
|
Request: req,
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Protocol returns request protocol name, such as HTTP/1.1 .
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Protocol() string {
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.Proto
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Uri returns full request url with query string, fragment.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Uri() string {
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.RequestURI
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Url returns request url path (without query string, fragment).
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Url() string {
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.URL.String()
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Site returns base site url as scheme://domain type.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Site() string {
|
|
|
|
return input.Scheme() + "://" + input.Domain()
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Scheme returns request scheme as "http" or "https".
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Scheme() string {
|
2013-12-18 14:33:21 +00:00
|
|
|
if input.Request.URL.Scheme != "" {
|
|
|
|
return input.Request.URL.Scheme
|
|
|
|
} else if input.Request.TLS == nil {
|
2013-09-26 14:31:39 +00:00
|
|
|
return "http"
|
|
|
|
} else {
|
|
|
|
return "https"
|
|
|
|
}
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Domain returns host name.
|
|
|
|
// Alias of Host method.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Domain() string {
|
|
|
|
return input.Host()
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Host returns host name.
|
|
|
|
// if no host info in request, return localhost.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Host() string {
|
2013-12-18 14:33:21 +00:00
|
|
|
if input.Request.Host != "" {
|
|
|
|
hostParts := strings.Split(input.Request.Host, ":")
|
2013-08-21 05:24:14 +00:00
|
|
|
if len(hostParts) > 0 {
|
|
|
|
return hostParts[0]
|
|
|
|
}
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.Host
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Method returns http request method.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Method() string {
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.Method
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Is returns boolean of this request is on given method, such as Is("POST").
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Is(method string) bool {
|
|
|
|
return input.Method() == method
|
|
|
|
}
|
|
|
|
|
2014-04-05 16:08:03 +00:00
|
|
|
// Is this a GET method request?
|
|
|
|
func (input *BeegoInput) IsGet() bool {
|
|
|
|
return input.Is("GET")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a POST method request?
|
|
|
|
func (input *BeegoInput) IsPost() bool {
|
|
|
|
return input.Is("POST")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a Head method request?
|
|
|
|
func (input *BeegoInput) IsHead() bool {
|
|
|
|
return input.Is("HEAD")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a OPTIONS method request?
|
|
|
|
func (input *BeegoInput) IsOptions() bool {
|
|
|
|
return input.Is("OPTIONS")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a PUT method request?
|
|
|
|
func (input *BeegoInput) IsPut() bool {
|
|
|
|
return input.Is("PUT")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a DELETE method request?
|
|
|
|
func (input *BeegoInput) IsDelete() bool {
|
|
|
|
return input.Is("DELETE")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a PATCH method request?
|
|
|
|
func (input *BeegoInput) IsPatch() bool {
|
|
|
|
return input.Is("PATCH")
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// IsAjax returns boolean of this request is generated by ajax.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) IsAjax() bool {
|
2013-10-29 07:45:45 +00:00
|
|
|
return input.Header("X-Requested-With") == "XMLHttpRequest"
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// IsSecure returns boolean of this request is in https.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) IsSecure() bool {
|
|
|
|
return input.Scheme() == "https"
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// IsSecure returns boolean of this request is in webSocket.
|
2013-09-10 09:02:56 +00:00
|
|
|
func (input *BeegoInput) IsWebsocket() bool {
|
|
|
|
return input.Header("Upgrade") == "websocket"
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// IsSecure returns boolean of whether file uploads in this request or not..
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) IsUpload() bool {
|
2014-04-05 16:08:03 +00:00
|
|
|
return input.Header("Content-Type") == "multipart/form-data"
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// IP returns request client ip.
|
|
|
|
// if in proxy, return first proxy id.
|
|
|
|
// if error, return 127.0.0.1.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) IP() string {
|
|
|
|
ips := input.Proxy()
|
|
|
|
if len(ips) > 0 && ips[0] != "" {
|
|
|
|
return ips[0]
|
|
|
|
}
|
2013-12-18 14:33:21 +00:00
|
|
|
ip := strings.Split(input.Request.RemoteAddr, ":")
|
2013-08-21 05:24:14 +00:00
|
|
|
if len(ip) > 0 {
|
2013-12-25 12:13:38 +00:00
|
|
|
if ip[0] != "[" {
|
2013-12-20 10:56:02 +00:00
|
|
|
return ip[0]
|
|
|
|
}
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
return "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Proxy returns proxy client ips slice.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Proxy() []string {
|
2013-11-21 07:59:16 +00:00
|
|
|
if ips := input.Header("X-Forwarded-For"); ips != "" {
|
2013-08-21 05:24:14 +00:00
|
|
|
return strings.Split(ips, ",")
|
|
|
|
}
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Refer returns http referer header.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Refer() string {
|
2013-11-21 07:59:16 +00:00
|
|
|
return input.Header("Referer")
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// SubDomains returns sub domain string.
|
|
|
|
// if aa.bb.domain.com, returns aa.bb .
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) SubDomains() string {
|
|
|
|
parts := strings.Split(input.Host(), ".")
|
|
|
|
return strings.Join(parts[len(parts)-2:], ".")
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Port returns request client port.
|
|
|
|
// when error or empty, return 80.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Port() int {
|
2013-12-18 14:33:21 +00:00
|
|
|
parts := strings.Split(input.Request.Host, ":")
|
2013-08-21 05:24:14 +00:00
|
|
|
if len(parts) == 2 {
|
|
|
|
port, _ := strconv.Atoi(parts[1])
|
|
|
|
return port
|
|
|
|
}
|
|
|
|
return 80
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// UserAgent returns request client user agent string.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) UserAgent() string {
|
2013-11-21 07:59:16 +00:00
|
|
|
return input.Header("User-Agent")
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Param returns router param by a given key.
|
2013-11-26 03:05:49 +00:00
|
|
|
func (input *BeegoInput) Param(key string) string {
|
|
|
|
if v, ok := input.Params[key]; ok {
|
2013-08-21 05:24:14 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Query returns input data item string by a given string.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Query(key string) string {
|
2014-04-10 14:20:46 +00:00
|
|
|
if val := input.Param(key); val != "" {
|
|
|
|
return val
|
|
|
|
}
|
2014-04-05 16:08:03 +00:00
|
|
|
if input.Request.Form == nil {
|
|
|
|
input.Request.ParseForm()
|
|
|
|
}
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.Form.Get(key)
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Header returns request header item string by a given string.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Header(key string) string {
|
2013-12-18 14:33:21 +00:00
|
|
|
return input.Request.Header.Get(key)
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Cookie returns request cookie item string by a given key.
|
|
|
|
// if non-existed, return empty string.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Cookie(key string) string {
|
2013-12-18 14:33:21 +00:00
|
|
|
ck, err := input.Request.Cookie(key)
|
2013-08-21 05:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ck.Value
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Session returns current session item value by a given key.
|
2013-08-21 05:24:14 +00:00
|
|
|
func (input *BeegoInput) Session(key interface{}) interface{} {
|
|
|
|
return input.CruSession.Get(key)
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// Body returns the raw request body data as bytes.
|
2014-04-05 16:08:03 +00:00
|
|
|
func (input *BeegoInput) CopyBody() []byte {
|
2013-12-18 14:33:21 +00:00
|
|
|
requestbody, _ := ioutil.ReadAll(input.Request.Body)
|
|
|
|
input.Request.Body.Close()
|
2013-08-21 05:24:14 +00:00
|
|
|
bf := bytes.NewBuffer(requestbody)
|
2013-12-18 14:33:21 +00:00
|
|
|
input.Request.Body = ioutil.NopCloser(bf)
|
2013-09-09 15:17:51 +00:00
|
|
|
input.RequestBody = requestbody
|
2013-08-21 05:24:14 +00:00
|
|
|
return requestbody
|
|
|
|
}
|
2013-11-26 03:05:49 +00:00
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// GetData returns the stored data in this context.
|
2013-11-26 03:05:49 +00:00
|
|
|
func (input *BeegoInput) GetData(key interface{}) interface{} {
|
|
|
|
if v, ok := input.Data[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:13:38 +00:00
|
|
|
// SetData stores data with given key in this context.
|
|
|
|
// This data are only available in this context.
|
2013-11-26 03:05:49 +00:00
|
|
|
func (input *BeegoInput) SetData(key, val interface{}) {
|
|
|
|
input.Data[key] = val
|
|
|
|
}
|
2014-04-05 16:08:03 +00:00
|
|
|
|
2014-04-14 21:05:53 +00:00
|
|
|
// parseForm or parseMultiForm based on Content-type
|
2014-04-05 16:08:03 +00:00
|
|
|
func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
|
|
|
|
// Parse the body depending on the content type.
|
2014-04-14 21:05:53 +00:00
|
|
|
if strings.Contains(input.Header("Content-Type"), "multipart/form-data") {
|
2014-04-05 16:08:03 +00:00
|
|
|
if err := input.Request.ParseMultipartForm(maxMemory); err != nil {
|
|
|
|
return errors.New("Error parsing request body:" + err.Error())
|
|
|
|
}
|
2014-04-14 21:05:53 +00:00
|
|
|
} else if err := input.Request.ParseForm(); err != nil {
|
|
|
|
return errors.New("Error parsing request body:" + err.Error())
|
2014-04-05 16:08:03 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|