Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
22671c52
Commit
22671c52
authored
Dec 17, 2014
by
shuo li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix subdomain, add test, space and comment fix
parent
ab99d5f1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
6 deletions
+58
-6
input.go
context/input.go
+16
-6
input_test.go
context/input_test.go
+42
-0
No files found.
context/input.go
View file @
22671c52
...
@@ -27,7 +27,7 @@ import (
...
@@ -27,7 +27,7 @@ import (
"github.com/astaxie/beego/session"
"github.com/astaxie/beego/session"
)
)
// BeegoInput operates the http request header
,data ,
cookie and body.
// BeegoInput operates the http request header
, data,
cookie and body.
// it also contains router params and current session.
// it also contains router params and current session.
type
BeegoInput
struct
{
type
BeegoInput
struct
{
CruSession
session
.
SessionStore
CruSession
session
.
SessionStore
...
@@ -153,12 +153,12 @@ func (input *BeegoInput) IsSecure() bool {
...
@@ -153,12 +153,12 @@ func (input *BeegoInput) IsSecure() bool {
return
input
.
Scheme
()
==
"https"
return
input
.
Scheme
()
==
"https"
}
}
// Is
Secure
returns boolean of this request is in webSocket.
// Is
Websocket
returns boolean of this request is in webSocket.
func
(
input
*
BeegoInput
)
IsWebsocket
()
bool
{
func
(
input
*
BeegoInput
)
IsWebsocket
()
bool
{
return
input
.
Header
(
"Upgrade"
)
==
"websocket"
return
input
.
Header
(
"Upgrade"
)
==
"websocket"
}
}
// Is
Secure
returns boolean of whether file uploads in this request or not..
// Is
Upload
returns boolean of whether file uploads in this request or not..
func
(
input
*
BeegoInput
)
IsUpload
()
bool
{
func
(
input
*
BeegoInput
)
IsUpload
()
bool
{
return
strings
.
Contains
(
input
.
Header
(
"Content-Type"
),
"multipart/form-data"
)
return
strings
.
Contains
(
input
.
Header
(
"Content-Type"
),
"multipart/form-data"
)
}
}
...
@@ -189,16 +189,24 @@ func (input *BeegoInput) Proxy() []string {
...
@@ -189,16 +189,24 @@ func (input *BeegoInput) Proxy() []string {
return
[]
string
{}
return
[]
string
{}
}
}
// Referer returns http referer header.
func
(
input
*
BeegoInput
)
Referer
()
string
{
return
input
.
Header
(
"Referer"
)
}
// Refer returns http referer header.
// Refer returns http referer header.
func
(
input
*
BeegoInput
)
Refer
()
string
{
func
(
input
*
BeegoInput
)
Refer
()
string
{
return
input
.
Header
(
"Referer"
)
return
input
.
Referer
(
)
}
}
// SubDomains returns sub domain string.
// SubDomains returns sub domain string.
// if aa.bb.domain.com, returns aa.bb .
// if aa.bb.domain.com, returns aa.bb .
func
(
input
*
BeegoInput
)
SubDomains
()
string
{
func
(
input
*
BeegoInput
)
SubDomains
()
string
{
parts
:=
strings
.
Split
(
input
.
Host
(),
"."
)
parts
:=
strings
.
Split
(
input
.
Host
(),
"."
)
return
strings
.
Join
(
parts
[
:
len
(
parts
)
-
2
],
"."
)
if
len
(
parts
)
>=
3
{
return
strings
.
Join
(
parts
[
:
len
(
parts
)
-
2
],
"."
)
}
return
""
}
}
// Port returns request client port.
// 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 {
}
}
// Header returns request header item string by a given string.
// Header returns request header item string by a given string.
// if non-existed, return empty string.
func
(
input
*
BeegoInput
)
Header
(
key
string
)
string
{
func
(
input
*
BeegoInput
)
Header
(
key
string
)
string
{
return
input
.
Request
.
Header
.
Get
(
key
)
return
input
.
Request
.
Header
.
Get
(
key
)
}
}
...
@@ -252,11 +261,12 @@ func (input *BeegoInput) Cookie(key string) string {
...
@@ -252,11 +261,12 @@ func (input *BeegoInput) Cookie(key string) string {
}
}
// Session returns current session item value by a given key.
// Session returns current session item value by a given key.
// if non-existed, return empty string.
func
(
input
*
BeegoInput
)
Session
(
key
interface
{})
interface
{}
{
func
(
input
*
BeegoInput
)
Session
(
key
interface
{})
interface
{}
{
return
input
.
CruSession
.
Get
(
key
)
return
input
.
CruSession
.
Get
(
key
)
}
}
// Body returns the raw request body data as bytes.
//
Copy
Body returns the raw request body data as bytes.
func
(
input
*
BeegoInput
)
CopyBody
()
[]
byte
{
func
(
input
*
BeegoInput
)
CopyBody
()
[]
byte
{
requestbody
,
_
:=
ioutil
.
ReadAll
(
input
.
Request
.
Body
)
requestbody
,
_
:=
ioutil
.
ReadAll
(
input
.
Request
.
Body
)
input
.
Request
.
Body
.
Close
()
input
.
Request
.
Body
.
Close
()
...
...
context/input_test.go
View file @
22671c52
...
@@ -70,3 +70,45 @@ func TestParse(t *testing.T) {
...
@@ -70,3 +70,45 @@ func TestParse(t *testing.T) {
}
}
fmt
.
Println
(
user
)
fmt
.
Println
(
user
)
}
}
func
TestSubDomain
(
t
*
testing
.
T
)
{
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
)
beegoInput
:=
NewInput
(
r
)
subdomain
:=
beegoInput
.
SubDomains
()
if
subdomain
!=
"www"
{
t
.
Fatal
(
"Subdomain parse error, got"
+
subdomain
)
}
r
,
_
=
http
.
NewRequest
(
"GET"
,
"http://localhost/"
,
nil
)
beegoInput
.
Request
=
r
if
beegoInput
.
SubDomains
()
!=
""
{
t
.
Fatal
(
"Subdomain parse error, should be empty, got "
+
beegoInput
.
SubDomains
())
}
r
,
_
=
http
.
NewRequest
(
"GET"
,
"http://aa.bb.example.com/"
,
nil
)
beegoInput
.
Request
=
r
if
beegoInput
.
SubDomains
()
!=
"aa.bb"
{
t
.
Fatal
(
"Subdomain parse error, got "
+
beegoInput
.
SubDomains
())
}
/* TODO Fix this
r, _ = http.NewRequest("GET", "http://127.0.0.1/", nil)
beegoInput.Request = r
if beegoInput.SubDomains() != "" {
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
}
*/
r
,
_
=
http
.
NewRequest
(
"GET"
,
"http://example.com/"
,
nil
)
beegoInput
.
Request
=
r
if
beegoInput
.
SubDomains
()
!=
""
{
t
.
Fatal
(
"Subdomain parse error, got "
+
beegoInput
.
SubDomains
())
}
r
,
_
=
http
.
NewRequest
(
"GET"
,
"http://aa.bb.cc.dd.example.com/"
,
nil
)
beegoInput
.
Request
=
r
if
beegoInput
.
SubDomains
()
!=
"aa.bb.cc.dd"
{
t
.
Fatal
(
"Subdomain parse error, got "
+
beegoInput
.
SubDomains
())
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment