1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 21:41:29 +00:00

1、增加cookiejar支持

2、增加Setting结构,便于统一设置请求参数
3、增加服务端测试php脚本
This commit is contained in:
CurveSoft 2014-06-03 21:20:10 +08:00
parent 03080b3ef2
commit ebb3b91df9
3 changed files with 174 additions and 40 deletions

View File

@ -17,21 +17,44 @@ import (
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/http/cookiejar"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"time" "time"
"sync"
) )
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. // Get returns *BeegoHttpRequest with GET method.
func Get(url string) *BeegoHttpRequest { func Get(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
req.Method = "GET" req.Method = "GET"
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent) return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting }
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil, nil, nil}
} }
// Post returns *BeegoHttpRequest with POST method. // Post returns *BeegoHttpRequest with POST method.
@ -39,8 +62,7 @@ func Post(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
req.Method = "POST" req.Method = "POST"
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent) return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting }
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil, nil, nil}
} }
// Put returns *BeegoHttpRequest with PUT method. // Put returns *BeegoHttpRequest with PUT method.
@ -48,8 +70,7 @@ func Put(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
req.Method = "PUT" req.Method = "PUT"
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent) return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting }
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil, nil, nil}
} }
// Delete returns *BeegoHttpRequest DELETE GET method. // Delete returns *BeegoHttpRequest DELETE GET method.
@ -57,8 +78,7 @@ func Delete(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
req.Method = "DELETE" req.Method = "DELETE"
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent) return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting }
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil, nil, nil}
} }
// Head returns *BeegoHttpRequest with HEAD method. // Head returns *BeegoHttpRequest with HEAD method.
@ -66,8 +86,19 @@ func Head(url string) *BeegoHttpRequest {
var req http.Request var req http.Request
req.Method = "HEAD" req.Method = "HEAD"
req.Header = http.Header{} req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent) return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting }
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil, nil, nil} }
// 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. // BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
@ -76,30 +107,43 @@ type BeegoHttpRequest struct {
req *http.Request req *http.Request
params map[string]string params map[string]string
files map[string]string files map[string]string
showdebug bool setting BeegoHttpSettings
connectTimeout time.Duration }
readWriteTimeout time.Duration
tlsClientConfig *tls.Config // Change request settings
proxy func(*http.Request) (*url.URL, error) func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest {
transport http.RoundTripper 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. // Debug sets show debug or not when executing request.
func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest {
b.showdebug = isdebug b.setting.ShowDebug = isdebug
return b return b
} }
// SetTimeout sets connect time out and read-write time out for BeegoRequest. // SetTimeout sets connect time out and read-write time out for BeegoRequest.
func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHttpRequest { func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHttpRequest {
b.connectTimeout = connectTimeout b.setting.ConnectTimeout = connectTimeout
b.readWriteTimeout = readWriteTimeout b.setting.ReadWriteTimeout = readWriteTimeout
return b return b
} }
// SetTLSClientConfig sets tls connection configurations if visiting https url. // SetTLSClientConfig sets tls connection configurations if visiting https url.
func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest { func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest {
b.tlsClientConfig = config b.setting.TlsClientConfig = config
return b return b
} }
@ -134,7 +178,7 @@ func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest {
// Set transport to // Set transport to
func (b *BeegoHttpRequest) SetTransport(transport http.RoundTripper) *BeegoHttpRequest { func (b *BeegoHttpRequest) SetTransport(transport http.RoundTripper) *BeegoHttpRequest {
b.transport = transport b.setting.Transport = transport
return b return b
} }
@ -146,7 +190,7 @@ func (b *BeegoHttpRequest) SetTransport(transport http.RoundTripper) *BeegoHttpR
// return u, nil // return u, nil
// } // }
func (b *BeegoHttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHttpRequest { func (b *BeegoHttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHttpRequest {
b.proxy = proxy b.setting.Proxy = proxy
return b return b
} }
@ -194,9 +238,9 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if b.req.Method == "GET" && len(paramBody) > 0 { if b.req.Method == "GET" && len(paramBody) > 0 {
if strings.Index(b.url, "?") != -1 { if strings.Index(b.url, "?") != -1 {
b.url += "&" + paramBody b.url += "&"+paramBody
} else { } else {
b.url = b.url + "?" + paramBody b.url = b.url+"?"+paramBody
} }
} else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 { } else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 {
if len(b.files) > 0 { if len(b.files) > 0 {
@ -234,7 +278,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
url, err := url.Parse(b.url) url, err := url.Parse(b.url)
if url.Scheme == "" { if url.Scheme == "" {
b.url = "http://" + b.url b.url = "http://"+b.url
url, err = url.Parse(b.url) url, err = url.Parse(b.url)
} }
if err != nil { if err != nil {
@ -242,7 +286,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
} }
b.req.URL = url b.req.URL = url
if b.showdebug { if b.setting.ShowDebug {
dump, err := httputil.DumpRequest(b.req, true) dump, err := httputil.DumpRequest(b.req, true)
if err != nil { if err != nil {
println(err.Error()) println(err.Error())
@ -250,32 +294,47 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
println(string(dump)) println(string(dump))
} }
trans := b.transport trans := b.setting.Transport
if trans == nil { if trans == nil {
// create default transport // create default transport
trans = &http.Transport{ trans = &http.Transport{
TLSClientConfig: b.tlsClientConfig, TLSClientConfig: b.setting.TlsClientConfig,
Proxy: b.proxy, Proxy: b.setting.Proxy,
Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout), Dial: TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout),
} }
} else { } else {
// if b.transport is *http.Transport then set the settings. // if b.transport is *http.Transport then set the settings.
if t, ok := trans.(*http.Transport); ok { if t, ok := trans.(*http.Transport); ok {
if t.TLSClientConfig == nil { if t.TLSClientConfig == nil {
t.TLSClientConfig = b.tlsClientConfig t.TLSClientConfig = b.setting.TlsClientConfig
} }
if t.Proxy == nil { if t.Proxy == nil {
t.Proxy = b.proxy t.Proxy = b.setting.Proxy
} }
if t.Dial == nil { if t.Dial == nil {
t.Dial = TimeoutDialer(b.connectTimeout, b.readWriteTimeout) t.Dial = TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout)
} }
} }
} }
var jar http.CookieJar
if b.setting.EnableCookie {
if defaultCookieJar == nil {
createDefaultCookie()
}
jar = defaultCookieJar
}else {
jar = nil
}
client := &http.Client{ client := &http.Client{
Transport: trans, Transport: trans,
Jar:jar,
}
if b.setting.UserAgent != "" {
b.req.Header.Set("User-Agent", b.setting.UserAgent)
} }
resp, err := client.Do(b.req) resp, err := client.Do(b.req)

View File

@ -1,4 +1,4 @@
// Beego (http://beego.me/) // Beego (http://localhost/httplib_test.php)
// @description beego is an open-source, high-performance web framework for the Go programming language. // @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 // @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE // @license http://github.com/astaxie/beego/blob/master/LICENSE
@ -7,13 +7,13 @@
package httplib package httplib
import ( import (
"testing"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing"
) )
func TestGetUrl(t *testing.T) { func TestGetUrl(t *testing.T) {
resp, err := Get("http://beego.me/").Debug(true).Response() resp, err := Get("http://localhost/httplib_test.php").Debug(true).Response()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -29,7 +29,7 @@ func TestGetUrl(t *testing.T) {
t.Fatal("data is no") t.Fatal("data is no")
} }
str, err := Get("http://beego.me/").String() str, err := Get("http://localhost/httplib_test.php").String()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -39,13 +39,62 @@ func TestGetUrl(t *testing.T) {
} }
func TestPost(t *testing.T) { func TestPost(t *testing.T) {
b := Post("http://beego.me/").Debug(true) b := Post("http://localhost/httplib_test.php").Debug(true)
b.Param("username", "astaxie") b.Param("username", "astaxie")
b.Param("password", "hello") b.Param("password", "hello")
b.PostFile("uploadfile", "httplib.go") b.PostFile("uploadfile", "httplib_test.php")
str, err := b.String() str, err := b.String()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Println(str) fmt.Println(str)
} }
func TestSimpleGetString(t *testing.T) {
fmt.Println("TestSimpleGetString==========================================")
html, err := Get("http://localhost/httplib_test.php").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://localhost/httplib_test.php").SetEnableCookie(true).String()
if err != nil {
t.Fatal(err)
}
fmt.Println(html)
html, err = Get("http://localhost/httplib_test.php").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://localhost/httplib_test.php").String()
if err != nil {
t.Fatal(err)
}
fmt.Println(html)
html, err = Get("http://localhost/httplib_test.php").String()
if err != nil {
t.Fatal(err)
}
fmt.Println(html)
fmt.Println("TestDefaultSetting==========================================")
}

26
httplib/httplib_test.php Normal file
View File

@ -0,0 +1,26 @@
<?php
session_id() or session_start();
if (!isset($_SESSION['first_time'])) {
$_SESSION['first_time'] = time();
}
$data = array();
$HTTP = array();
foreach ($_SERVER as $head => $value) {
if (strpos($head, "HTTP_") === 0) {
$HTTP[$head] = $value;
}
}
$data['HTTP'] = $HTTP;
$data['GET'] = $_GET;
$data['POST'] = $_POST;
$data['REQUEST'] = $_REQUEST;
$data['SESSION'] = $_SESSION;
$data['COOKIE'] = $_COOKIE;
$data['FILES'] = $_FILES;
$data['SERVER'] = $_SERVER;
$data['ENV'] = $_ENV;
//echo json_encode($data);
//echo "<pre>";
print_r($data);
//echo "</pre>";