1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-25 06:54:14 +00:00
Beego/context/input.go
2013-12-18 22:33:21 +08:00

182 lines
3.6 KiB
Go

package context
import (
"bytes"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/astaxie/beego/session"
)
type BeegoInput struct {
CruSession session.SessionStore
Params map[string]string
Data map[interface{}]interface{}
Request *http.Request
RequestBody []byte
}
func NewInput(req *http.Request) *BeegoInput {
return &BeegoInput{
Params: make(map[string]string),
Data: make(map[interface{}]interface{}),
Request: req,
}
}
func (input *BeegoInput) Protocol() string {
return input.Request.Proto
}
func (input *BeegoInput) Uri() string {
return input.Request.RequestURI
}
func (input *BeegoInput) Url() string {
return input.Request.URL.String()
}
func (input *BeegoInput) Site() string {
return input.Scheme() + "://" + input.Domain()
}
func (input *BeegoInput) Scheme() string {
if input.Request.URL.Scheme != "" {
return input.Request.URL.Scheme
} else if input.Request.TLS == nil {
return "http"
} else {
return "https"
}
}
func (input *BeegoInput) Domain() string {
return input.Host()
}
func (input *BeegoInput) Host() string {
if input.Request.Host != "" {
hostParts := strings.Split(input.Request.Host, ":")
if len(hostParts) > 0 {
return hostParts[0]
}
return input.Request.Host
}
return "localhost"
}
func (input *BeegoInput) Method() string {
return input.Request.Method
}
func (input *BeegoInput) Is(method string) bool {
return input.Method() == method
}
func (input *BeegoInput) IsAjax() bool {
return input.Header("X-Requested-With") == "XMLHttpRequest"
}
func (input *BeegoInput) IsSecure() bool {
return input.Scheme() == "https"
}
func (input *BeegoInput) IsWebsocket() bool {
return input.Header("Upgrade") == "websocket"
}
func (input *BeegoInput) IsUpload() bool {
return input.Request.MultipartForm != nil
}
func (input *BeegoInput) IP() string {
ips := input.Proxy()
if len(ips) > 0 && ips[0] != "" {
return ips[0]
}
ip := strings.Split(input.Request.RemoteAddr, ":")
if len(ip) > 0 {
return ip[0]
}
return "127.0.0.1"
}
func (input *BeegoInput) Proxy() []string {
if ips := input.Header("X-Forwarded-For"); ips != "" {
return strings.Split(ips, ",")
}
return []string{}
}
func (input *BeegoInput) Refer() string {
return input.Header("Referer")
}
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.Request.Host, ":")
if len(parts) == 2 {
port, _ := strconv.Atoi(parts[1])
return port
}
return 80
}
func (input *BeegoInput) UserAgent() string {
return input.Header("User-Agent")
}
func (input *BeegoInput) Param(key string) string {
if v, ok := input.Params[key]; ok {
return v
}
return ""
}
func (input *BeegoInput) Query(key string) string {
input.Request.ParseForm()
return input.Request.Form.Get(key)
}
func (input *BeegoInput) Header(key string) string {
return input.Request.Header.Get(key)
}
func (input *BeegoInput) Cookie(key string) string {
ck, err := input.Request.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.Request.Body)
input.Request.Body.Close()
bf := bytes.NewBuffer(requestbody)
input.Request.Body = ioutil.NopCloser(bf)
input.RequestBody = requestbody
return requestbody
}
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
}