22671c52 by shuo li

Fix subdomain, add test, space and comment fix

1 parent ab99d5f1
...@@ -27,7 +27,7 @@ import ( ...@@ -27,7 +27,7 @@ import (
27 "github.com/astaxie/beego/session" 27 "github.com/astaxie/beego/session"
28 ) 28 )
29 29
30 // BeegoInput operates the http request header ,data ,cookie and body. 30 // BeegoInput operates the http request header, data, cookie and body.
31 // it also contains router params and current session. 31 // it also contains router params and current session.
32 type BeegoInput struct { 32 type BeegoInput struct {
33 CruSession session.SessionStore 33 CruSession session.SessionStore
...@@ -153,12 +153,12 @@ func (input *BeegoInput) IsSecure() bool { ...@@ -153,12 +153,12 @@ func (input *BeegoInput) IsSecure() bool {
153 return input.Scheme() == "https" 153 return input.Scheme() == "https"
154 } 154 }
155 155
156 // IsSecure returns boolean of this request is in webSocket. 156 // IsWebsocket returns boolean of this request is in webSocket.
157 func (input *BeegoInput) IsWebsocket() bool { 157 func (input *BeegoInput) IsWebsocket() bool {
158 return input.Header("Upgrade") == "websocket" 158 return input.Header("Upgrade") == "websocket"
159 } 159 }
160 160
161 // IsSecure returns boolean of whether file uploads in this request or not.. 161 // IsUpload returns boolean of whether file uploads in this request or not..
162 func (input *BeegoInput) IsUpload() bool { 162 func (input *BeegoInput) IsUpload() bool {
163 return strings.Contains(input.Header("Content-Type"), "multipart/form-data") 163 return strings.Contains(input.Header("Content-Type"), "multipart/form-data")
164 } 164 }
...@@ -189,16 +189,24 @@ func (input *BeegoInput) Proxy() []string { ...@@ -189,16 +189,24 @@ func (input *BeegoInput) Proxy() []string {
189 return []string{} 189 return []string{}
190 } 190 }
191 191
192 // Referer returns http referer header.
193 func (input *BeegoInput) Referer() string {
194 return input.Header("Referer")
195 }
196
192 // Refer returns http referer header. 197 // Refer returns http referer header.
193 func (input *BeegoInput) Refer() string { 198 func (input *BeegoInput) Refer() string {
194 return input.Header("Referer") 199 return input.Referer()
195 } 200 }
196 201
197 // SubDomains returns sub domain string. 202 // SubDomains returns sub domain string.
198 // if aa.bb.domain.com, returns aa.bb . 203 // if aa.bb.domain.com, returns aa.bb .
199 func (input *BeegoInput) SubDomains() string { 204 func (input *BeegoInput) SubDomains() string {
200 parts := strings.Split(input.Host(), ".") 205 parts := strings.Split(input.Host(), ".")
206 if len(parts) >= 3 {
201 return strings.Join(parts[:len(parts)-2], ".") 207 return strings.Join(parts[:len(parts)-2], ".")
208 }
209 return ""
202 } 210 }
203 211
204 // Port returns request client port. 212 // Port returns request client port.
...@@ -237,6 +245,7 @@ func (input *BeegoInput) Query(key string) string { ...@@ -237,6 +245,7 @@ func (input *BeegoInput) Query(key string) string {
237 } 245 }
238 246
239 // Header returns request header item string by a given string. 247 // Header returns request header item string by a given string.
248 // if non-existed, return empty string.
240 func (input *BeegoInput) Header(key string) string { 249 func (input *BeegoInput) Header(key string) string {
241 return input.Request.Header.Get(key) 250 return input.Request.Header.Get(key)
242 } 251 }
...@@ -252,11 +261,12 @@ func (input *BeegoInput) Cookie(key string) string { ...@@ -252,11 +261,12 @@ func (input *BeegoInput) Cookie(key string) string {
252 } 261 }
253 262
254 // Session returns current session item value by a given key. 263 // Session returns current session item value by a given key.
264 // if non-existed, return empty string.
255 func (input *BeegoInput) Session(key interface{}) interface{} { 265 func (input *BeegoInput) Session(key interface{}) interface{} {
256 return input.CruSession.Get(key) 266 return input.CruSession.Get(key)
257 } 267 }
258 268
259 // Body returns the raw request body data as bytes. 269 // CopyBody returns the raw request body data as bytes.
260 func (input *BeegoInput) CopyBody() []byte { 270 func (input *BeegoInput) CopyBody() []byte {
261 requestbody, _ := ioutil.ReadAll(input.Request.Body) 271 requestbody, _ := ioutil.ReadAll(input.Request.Body)
262 input.Request.Body.Close() 272 input.Request.Body.Close()
......
...@@ -70,3 +70,45 @@ func TestParse(t *testing.T) { ...@@ -70,3 +70,45 @@ func TestParse(t *testing.T) {
70 } 70 }
71 fmt.Println(user) 71 fmt.Println(user)
72 } 72 }
73
74 func TestSubDomain(t *testing.T) {
75 r, _ := http.NewRequest("GET", "http://www.example.com/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil)
76 beegoInput := NewInput(r)
77
78 subdomain := beegoInput.SubDomains()
79 if subdomain != "www" {
80 t.Fatal("Subdomain parse error, got" + subdomain)
81 }
82
83 r, _ = http.NewRequest("GET", "http://localhost/", nil)
84 beegoInput.Request = r
85 if beegoInput.SubDomains() != "" {
86 t.Fatal("Subdomain parse error, should be empty, got " + beegoInput.SubDomains())
87 }
88
89 r, _ = http.NewRequest("GET", "http://aa.bb.example.com/", nil)
90 beegoInput.Request = r
91 if beegoInput.SubDomains() != "aa.bb" {
92 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
93 }
94
95 /* TODO Fix this
96 r, _ = http.NewRequest("GET", "http://127.0.0.1/", nil)
97 beegoInput.Request = r
98 if beegoInput.SubDomains() != "" {
99 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
100 }
101 */
102
103 r, _ = http.NewRequest("GET", "http://example.com/", nil)
104 beegoInput.Request = r
105 if beegoInput.SubDomains() != "" {
106 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
107 }
108
109 r, _ = http.NewRequest("GET", "http://aa.bb.cc.dd.example.com/", nil)
110 beegoInput.Request = r
111 if beegoInput.SubDomains() != "aa.bb.cc.dd" {
112 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
113 }
114 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!