1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 06:40:54 +00:00
Beego/context/input.go

181 lines
3.6 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"
"github.com/astaxie/beego/session"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type BeegoInput struct {
CruSession session.SessionStore
2013-11-26 03:05:49 +00:00
Params map[string]string
Data map[interface{}]interface{}
2013-08-21 05:24:14 +00:00
req *http.Request
RequestBody []byte
}
func NewInput(req *http.Request) *BeegoInput {
return &BeegoInput{
2013-11-26 03:05:49 +00:00
Params: make(map[string]string),
Data: make(map[interface{}]interface{}),
req: req,
2013-08-21 05:24:14 +00:00
}
}
func (input *BeegoInput) Protocol() string {
return input.req.Proto
}
func (input *BeegoInput) Uri() string {
return input.req.RequestURI
}
func (input *BeegoInput) Url() string {
return input.req.URL.String()
}
func (input *BeegoInput) Site() string {
return input.Scheme() + "://" + input.Domain()
}
func (input *BeegoInput) Scheme() string {
2013-09-26 14:31:39 +00:00
if input.req.URL.Scheme != "" {
return input.req.URL.Scheme
} else if input.req.TLS == nil {
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 {
if input.req.Host != "" {
hostParts := strings.Split(input.req.Host, ":")
if len(hostParts) > 0 {
return hostParts[0]
}
return input.req.Host
}
return "localhost"
}
func (input *BeegoInput) Method() string {
return input.req.Method
}
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 {
return input.req.MultipartForm != nil
}
func (input *BeegoInput) IP() string {
ips := input.Proxy()
if len(ips) > 0 && ips[0] != "" {
return ips[0]
}
ip := strings.Split(input.req.RemoteAddr, ":")
if len(ip) > 0 {
return ip[0]
}
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 {
parts := strings.Split(input.req.Host, ":")
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-11-26 08:47:50 +00:00
input.req.ParseForm()
2013-08-21 05:24:14 +00:00
return input.req.Form.Get(key)
}
func (input *BeegoInput) Header(key string) string {
return input.req.Header.Get(key)
}
func (input *BeegoInput) Cookie(key string) string {
ck, err := input.req.Cookie(key)
if err != nil {
return ""
}
return ck.Value
}
func (input *BeegoInput) Session(key interface{}) interface{} {
return input.CruSession.Get(key)
}
func (input *BeegoInput) Body() []byte {
requestbody, _ := ioutil.ReadAll(input.req.Body)
input.req.Body.Close()
bf := bytes.NewBuffer(requestbody)
input.req.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
}