1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 22:50:55 +00:00
Beego/context/input.go

184 lines
3.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"
"io/ioutil"
"net/http"
"strconv"
"strings"
2013-12-03 13:37:39 +00:00
"github.com/astaxie/beego/session"
2013-08-21 05:24:14 +00:00
)
type BeegoInput struct {
CruSession session.SessionStore
2013-11-26 03:05:49 +00:00
Params map[string]string
Data map[interface{}]interface{}
2013-12-18 14:33:21 +00:00
Request *http.Request
2013-08-21 05:24:14 +00:00
RequestBody []byte
}
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
}
}
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
}
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
}
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
}
func (input *BeegoInput) Site() string {
return input.Scheme() + "://" + input.Domain()
}
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
}
func (input *BeegoInput) Domain() string {
return input.Host()
}
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"
}
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
}
func (input *BeegoInput) Is(method string) bool {
return input.Method() == method
}
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
}
func (input *BeegoInput) IsSecure() bool {
return input.Scheme() == "https"
}
2013-09-10 09:02:56 +00:00
func (input *BeegoInput) IsWebsocket() bool {
return input.Header("Upgrade") == "websocket"
}
2013-08-21 05:24:14 +00:00
func (input *BeegoInput) IsUpload() bool {
2013-12-18 14:33:21 +00:00
return input.Request.MultipartForm != nil
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 {
if ip[0] != "["{
return ip[0]
}
2013-08-21 05:24:14 +00:00
}
return "127.0.0.1"
}
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{}
}
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
}
func (input *BeegoInput) SubDomains() string {
parts := strings.Split(input.Host(), ".")
return strings.Join(parts[len(parts)-2:], ".")
}
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
}
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-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 ""
}
func (input *BeegoInput) Query(key string) string {
2013-12-18 14:33:21 +00:00
input.Request.ParseForm()
return input.Request.Form.Get(key)
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
}
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
}
func (input *BeegoInput) Session(key interface{}) interface{} {
return input.CruSession.Get(key)
}
func (input *BeegoInput) Body() []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
func (input *BeegoInput) GetData(key interface{}) interface{} {
if v, ok := input.Data[key]; ok {
return v
}
return nil
}
func (input *BeegoInput) SetData(key, val interface{}) {
input.Data[key] = val
}