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
3e4c0159
Commit
3e4c0159
authored
Jun 04, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #631 from curvesoft/master
cookiejar support
parents
03080b3e
d689be30
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
148 additions
and
40 deletions
+148
-40
httplib.go
httplib/httplib.go
+94
-35
httplib_test.go
httplib/httplib_test.go
+54
-5
No files found.
httplib/httplib.go
View file @
3e4c0159
...
...
@@ -16,22 +16,45 @@ import (
"mime/multipart"
"net"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"os"
"strings"
"sync"
"time"
)
var
defaultUserAgent
=
"beegoServer"
var
defaultSetting
=
BeegoHttpSettings
{
false
,
"beegoServer"
,
60
*
time
.
Second
,
60
*
time
.
Second
,
nil
,
nil
,
nil
,
false
}
var
defaultCookieJar
http
.
CookieJar
var
settingMutex
sync
.
Mutex
// createDefaultCookieJar creates a global cookiejar to store cookies.
func
createDefaultCookie
()
{
settingMutex
.
Lock
()
defer
settingMutex
.
Unlock
()
defaultCookieJar
,
_
=
cookiejar
.
New
(
nil
)
}
// Overwrite default settings
func
SetDefaultSetting
(
setting
BeegoHttpSettings
)
{
settingMutex
.
Lock
()
defer
settingMutex
.
Unlock
()
defaultSetting
=
setting
if
defaultSetting
.
ConnectTimeout
==
0
{
defaultSetting
.
ConnectTimeout
=
60
*
time
.
Second
}
if
defaultSetting
.
ReadWriteTimeout
==
0
{
defaultSetting
.
ReadWriteTimeout
=
60
*
time
.
Second
}
}
// Get returns *BeegoHttpRequest with GET method.
func
Get
(
url
string
)
*
BeegoHttpRequest
{
var
req
http
.
Request
req
.
Method
=
"GET"
req
.
Header
=
http
.
Header
{}
req
.
Header
.
Set
(
"User-Agent"
,
defaultUserAgent
)
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
false
,
60
*
time
.
Second
,
60
*
time
.
Second
,
nil
,
nil
,
nil
}
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
defaultSetting
}
}
// Post returns *BeegoHttpRequest with POST method.
...
...
@@ -39,8 +62,7 @@ func Post(url string) *BeegoHttpRequest {
var
req
http
.
Request
req
.
Method
=
"POST"
req
.
Header
=
http
.
Header
{}
req
.
Header
.
Set
(
"User-Agent"
,
defaultUserAgent
)
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
false
,
60
*
time
.
Second
,
60
*
time
.
Second
,
nil
,
nil
,
nil
}
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
defaultSetting
}
}
// Put returns *BeegoHttpRequest with PUT method.
...
...
@@ -48,8 +70,7 @@ func Put(url string) *BeegoHttpRequest {
var
req
http
.
Request
req
.
Method
=
"PUT"
req
.
Header
=
http
.
Header
{}
req
.
Header
.
Set
(
"User-Agent"
,
defaultUserAgent
)
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
false
,
60
*
time
.
Second
,
60
*
time
.
Second
,
nil
,
nil
,
nil
}
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
defaultSetting
}
}
// Delete returns *BeegoHttpRequest DELETE GET method.
...
...
@@ -57,8 +78,7 @@ func Delete(url string) *BeegoHttpRequest {
var
req
http
.
Request
req
.
Method
=
"DELETE"
req
.
Header
=
http
.
Header
{}
req
.
Header
.
Set
(
"User-Agent"
,
defaultUserAgent
)
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
false
,
60
*
time
.
Second
,
60
*
time
.
Second
,
nil
,
nil
,
nil
}
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
defaultSetting
}
}
// Head returns *BeegoHttpRequest with HEAD method.
...
...
@@ -66,40 +86,64 @@ func Head(url string) *BeegoHttpRequest {
var
req
http
.
Request
req
.
Method
=
"HEAD"
req
.
Header
=
http
.
Header
{}
req
.
Header
.
Set
(
"User-Agent"
,
defaultUserAgent
)
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
false
,
60
*
time
.
Second
,
60
*
time
.
Second
,
nil
,
nil
,
nil
}
return
&
BeegoHttpRequest
{
url
,
&
req
,
map
[
string
]
string
{},
map
[
string
]
string
{},
defaultSetting
}
}
// BeegoHttpSettings
type
BeegoHttpSettings
struct
{
ShowDebug
bool
UserAgent
string
ConnectTimeout
time
.
Duration
ReadWriteTimeout
time
.
Duration
TlsClientConfig
*
tls
.
Config
Proxy
func
(
*
http
.
Request
)
(
*
url
.
URL
,
error
)
Transport
http
.
RoundTripper
EnableCookie
bool
}
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
type
BeegoHttpRequest
struct
{
url
string
req
*
http
.
Request
params
map
[
string
]
string
files
map
[
string
]
string
showdebug
bool
connectTimeout
time
.
Duration
readWriteTimeout
time
.
Duration
tlsClientConfig
*
tls
.
Config
proxy
func
(
*
http
.
Request
)
(
*
url
.
URL
,
error
)
transport
http
.
RoundTripper
url
string
req
*
http
.
Request
params
map
[
string
]
string
files
map
[
string
]
string
setting
BeegoHttpSettings
}
// Change request settings
func
(
b
*
BeegoHttpRequest
)
Setting
(
setting
BeegoHttpSettings
)
*
BeegoHttpRequest
{
b
.
setting
=
setting
return
b
}
// SetEnableCookie sets enable/disable cookiejar
func
(
b
*
BeegoHttpRequest
)
SetEnableCookie
(
enable
bool
)
*
BeegoHttpRequest
{
b
.
setting
.
EnableCookie
=
enable
return
b
}
// SetUserAgent sets User-Agent header field
func
(
b
*
BeegoHttpRequest
)
SetAgent
(
useragent
string
)
*
BeegoHttpRequest
{
b
.
setting
.
UserAgent
=
useragent
return
b
}
// Debug sets show debug or not when executing request.
func
(
b
*
BeegoHttpRequest
)
Debug
(
isdebug
bool
)
*
BeegoHttpRequest
{
b
.
s
howd
ebug
=
isdebug
b
.
s
etting
.
ShowD
ebug
=
isdebug
return
b
}
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
func
(
b
*
BeegoHttpRequest
)
SetTimeout
(
connectTimeout
,
readWriteTimeout
time
.
Duration
)
*
BeegoHttpRequest
{
b
.
c
onnectTimeout
=
connectTimeout
b
.
r
eadWriteTimeout
=
readWriteTimeout
b
.
setting
.
C
onnectTimeout
=
connectTimeout
b
.
setting
.
R
eadWriteTimeout
=
readWriteTimeout
return
b
}
// SetTLSClientConfig sets tls connection configurations if visiting https url.
func
(
b
*
BeegoHttpRequest
)
SetTLSClientConfig
(
config
*
tls
.
Config
)
*
BeegoHttpRequest
{
b
.
t
lsClientConfig
=
config
b
.
setting
.
T
lsClientConfig
=
config
return
b
}
...
...
@@ -134,7 +178,7 @@ func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest {
// Set transport to
func
(
b
*
BeegoHttpRequest
)
SetTransport
(
transport
http
.
RoundTripper
)
*
BeegoHttpRequest
{
b
.
t
ransport
=
transport
b
.
setting
.
T
ransport
=
transport
return
b
}
...
...
@@ -146,7 +190,7 @@ func (b *BeegoHttpRequest) SetTransport(transport http.RoundTripper) *BeegoHttpR
// return u, nil
// }
func
(
b
*
BeegoHttpRequest
)
SetProxy
(
proxy
func
(
*
http
.
Request
)
(
*
url
.
URL
,
error
))
*
BeegoHttpRequest
{
b
.
p
roxy
=
proxy
b
.
setting
.
P
roxy
=
proxy
return
b
}
...
...
@@ -242,7 +286,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
}
b
.
req
.
URL
=
url
if
b
.
s
howd
ebug
{
if
b
.
s
etting
.
ShowD
ebug
{
dump
,
err
:=
httputil
.
DumpRequest
(
b
.
req
,
true
)
if
err
!=
nil
{
println
(
err
.
Error
())
...
...
@@ -250,32 +294,47 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
println
(
string
(
dump
))
}
trans
:=
b
.
t
ransport
trans
:=
b
.
setting
.
T
ransport
if
trans
==
nil
{
// create default transport
trans
=
&
http
.
Transport
{
TLSClientConfig
:
b
.
t
lsClientConfig
,
Proxy
:
b
.
p
roxy
,
Dial
:
TimeoutDialer
(
b
.
connectTimeout
,
b
.
r
eadWriteTimeout
),
TLSClientConfig
:
b
.
setting
.
T
lsClientConfig
,
Proxy
:
b
.
setting
.
P
roxy
,
Dial
:
TimeoutDialer
(
b
.
setting
.
ConnectTimeout
,
b
.
setting
.
R
eadWriteTimeout
),
}
}
else
{
// if b.transport is *http.Transport then set the settings.
if
t
,
ok
:=
trans
.
(
*
http
.
Transport
);
ok
{
if
t
.
TLSClientConfig
==
nil
{
t
.
TLSClientConfig
=
b
.
t
lsClientConfig
t
.
TLSClientConfig
=
b
.
setting
.
T
lsClientConfig
}
if
t
.
Proxy
==
nil
{
t
.
Proxy
=
b
.
p
roxy
t
.
Proxy
=
b
.
setting
.
P
roxy
}
if
t
.
Dial
==
nil
{
t
.
Dial
=
TimeoutDialer
(
b
.
connectTimeout
,
b
.
r
eadWriteTimeout
)
t
.
Dial
=
TimeoutDialer
(
b
.
setting
.
ConnectTimeout
,
b
.
setting
.
R
eadWriteTimeout
)
}
}
}
var
jar
http
.
CookieJar
if
b
.
setting
.
EnableCookie
{
if
defaultCookieJar
==
nil
{
createDefaultCookie
()
}
jar
=
defaultCookieJar
}
else
{
jar
=
nil
}
client
:=
&
http
.
Client
{
Transport
:
trans
,
Jar
:
jar
,
}
if
b
.
setting
.
UserAgent
!=
""
{
b
.
req
.
Header
.
Set
(
"User-Agent"
,
b
.
setting
.
UserAgent
)
}
resp
,
err
:=
client
.
Do
(
b
.
req
)
...
...
httplib/httplib_test.go
View file @
3e4c0159
// Beego (http://beego.me
/
)
// Beego (http://beego.me)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
...
...
@@ -13,7 +13,7 @@ import (
)
func
TestGetUrl
(
t
*
testing
.
T
)
{
resp
,
err
:=
Get
(
"http://beego.me
/
"
)
.
Debug
(
true
)
.
Response
()
resp
,
err
:=
Get
(
"http://beego.me"
)
.
Debug
(
true
)
.
Response
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -29,7 +29,7 @@ func TestGetUrl(t *testing.T) {
t
.
Fatal
(
"data is no"
)
}
str
,
err
:=
Get
(
"http://beego.me
/
"
)
.
String
()
str
,
err
:=
Get
(
"http://beego.me"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -39,13 +39,62 @@ func TestGetUrl(t *testing.T) {
}
func
TestPost
(
t
*
testing
.
T
)
{
b
:=
Post
(
"http://beego.me
/
"
)
.
Debug
(
true
)
b
:=
Post
(
"http://beego.me"
)
.
Debug
(
true
)
b
.
Param
(
"username"
,
"astaxie"
)
b
.
Param
(
"password"
,
"hello"
)
b
.
PostFile
(
"uploadfile"
,
"httplib.go"
)
b
.
PostFile
(
"uploadfile"
,
"httplib
_test
.go"
)
str
,
err
:=
b
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
str
)
}
func
TestSimpleGetString
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestSimpleGetString=========================================="
)
html
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
SetAgent
(
"beegoooooo"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestSimpleGetString=========================================="
)
}
func
TestSimpleGetStringWithDefaultCookie
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestSimpleGetStringWithDefaultCookie=========================================="
)
html
,
err
:=
Get
(
"http://httpbin.org/cookies/set?k1=v1"
)
.
SetEnableCookie
(
true
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
html
,
err
=
Get
(
"http://httpbin.org/cookies"
)
.
SetEnableCookie
(
true
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestSimpleGetStringWithDefaultCookie=========================================="
)
}
func
TestDefaultSetting
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestDefaultSetting=========================================="
)
var
def
BeegoHttpSettings
def
.
EnableCookie
=
true
//def.ShowDebug = true
def
.
UserAgent
=
"UserAgent"
//def.ConnectTimeout = 60*time.Second
//def.ReadWriteTimeout = 60*time.Second
def
.
Transport
=
nil
//http.DefaultTransport
SetDefaultSetting
(
def
)
html
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
html
,
err
=
Get
(
"http://httpbin.org/headers"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestDefaultSetting=========================================="
)
}
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