2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// Package httplib is used as http.Client
|
2014-08-18 08:41:43 +00:00
|
|
|
// Usage:
|
|
|
|
//
|
2014-08-22 22:48:40 +00:00
|
|
|
// import "github.com/astaxie/beego/httplib"
|
2014-08-18 08:41:43 +00:00
|
|
|
//
|
2014-08-18 13:01:49 +00:00
|
|
|
// b := httplib.Post("http://beego.me/")
|
2014-08-18 08:41:43 +00:00
|
|
|
// b.Param("username","astaxie")
|
|
|
|
// b.Param("password","123456")
|
|
|
|
// b.PostFile("uploadfile1", "httplib.pdf")
|
|
|
|
// b.PostFile("uploadfile2", "httplib.txt")
|
|
|
|
// str, err := b.String()
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
// }
|
|
|
|
// fmt.Println(str)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// more docs http://beego.me/docs/module/httplib.md
|
2013-08-06 08:13:45 +00:00
|
|
|
package httplib
|
2013-08-03 14:20:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-04-08 13:45:00 +00:00
|
|
|
"compress/gzip"
|
2013-12-10 14:01:50 +00:00
|
|
|
"crypto/tls"
|
2013-08-03 14:20:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-10-30 03:16:09 +00:00
|
|
|
"log"
|
2014-05-08 08:58:08 +00:00
|
|
|
"mime/multipart"
|
2013-08-03 14:20:09 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2014-06-03 13:20:10 +00:00
|
|
|
"net/http/cookiejar"
|
2014-06-04 13:04:50 +00:00
|
|
|
"net/http/httputil"
|
2013-08-03 14:20:09 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2015-06-13 03:15:13 +00:00
|
|
|
"sync"
|
2014-06-04 13:04:50 +00:00
|
|
|
"time"
|
2013-08-03 14:20:09 +00:00
|
|
|
)
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
var defaultSetting = BeegoHTTPSettings{
|
2015-06-13 03:15:13 +00:00
|
|
|
UserAgent: "beegoServer",
|
|
|
|
ConnectTimeout: 60 * time.Second,
|
|
|
|
ReadWriteTimeout: 60 * time.Second,
|
|
|
|
Gzip: true,
|
|
|
|
DumpBody: true,
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:20:10 +00:00
|
|
|
var defaultCookieJar http.CookieJar
|
2015-06-13 03:15:13 +00:00
|
|
|
var settingMutex sync.Mutex
|
2014-06-03 13:20:10 +00:00
|
|
|
|
2014-08-22 23:07:12 +00:00
|
|
|
// createDefaultCookie creates a global cookiejar to store cookies.
|
2014-06-03 13:20:10 +00:00
|
|
|
func createDefaultCookie() {
|
2015-06-13 03:15:13 +00:00
|
|
|
settingMutex.Lock()
|
|
|
|
defer settingMutex.Unlock()
|
2014-06-03 13:20:10 +00:00
|
|
|
defaultCookieJar, _ = cookiejar.New(nil)
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// SetDefaultSetting Overwrite default settings
|
|
|
|
func SetDefaultSetting(setting BeegoHTTPSettings) {
|
2015-06-13 03:15:13 +00:00
|
|
|
settingMutex.Lock()
|
|
|
|
defer settingMutex.Unlock()
|
2014-06-03 13:20:10 +00:00
|
|
|
defaultSetting = setting
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// NewBeegoRequest return *BeegoHttpRequest with specific method
|
|
|
|
func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
|
2014-08-22 23:02:47 +00:00
|
|
|
var resp http.Response
|
2015-06-13 03:15:13 +00:00
|
|
|
u, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
2016-01-04 14:41:25 +00:00
|
|
|
log.Println("Httplib:", err)
|
2015-06-13 03:15:13 +00:00
|
|
|
}
|
2014-08-22 08:43:42 +00:00
|
|
|
req := http.Request{
|
2015-06-13 03:15:13 +00:00
|
|
|
URL: u,
|
2014-08-22 23:02:47 +00:00
|
|
|
Method: method,
|
|
|
|
Header: make(http.Header),
|
2014-08-22 08:43:42 +00:00
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
}
|
2015-09-11 14:28:28 +00:00
|
|
|
return &BeegoHTTPRequest{
|
2015-06-13 03:15:13 +00:00
|
|
|
url: rawurl,
|
2015-04-25 18:04:34 +00:00
|
|
|
req: &req,
|
2015-08-23 16:16:56 +00:00
|
|
|
params: map[string][]string{},
|
2015-04-25 18:04:34 +00:00
|
|
|
files: map[string]string{},
|
|
|
|
setting: defaultSetting,
|
|
|
|
resp: &resp,
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:43:42 +00:00
|
|
|
// Get returns *BeegoHttpRequest with GET method.
|
2015-09-11 14:28:28 +00:00
|
|
|
func Get(url string) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
return NewBeegoRequest(url, "GET")
|
2014-08-22 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Post returns *BeegoHttpRequest with POST method.
|
2015-09-11 14:28:28 +00:00
|
|
|
func Post(url string) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
return NewBeegoRequest(url, "POST")
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Put returns *BeegoHttpRequest with PUT method.
|
2015-09-11 14:28:28 +00:00
|
|
|
func Put(url string) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
return NewBeegoRequest(url, "PUT")
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 22:47:42 +00:00
|
|
|
// Delete returns *BeegoHttpRequest DELETE method.
|
2015-09-11 14:28:28 +00:00
|
|
|
func Delete(url string) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
return NewBeegoRequest(url, "DELETE")
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Head returns *BeegoHttpRequest with HEAD method.
|
2015-09-11 14:28:28 +00:00
|
|
|
func Head(url string) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
return NewBeegoRequest(url, "HEAD")
|
2014-06-03 13:20:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// BeegoHTTPSettings is the http.Client setting
|
|
|
|
type BeegoHTTPSettings struct {
|
2014-06-03 13:20:10 +00:00
|
|
|
ShowDebug bool
|
|
|
|
UserAgent string
|
|
|
|
ConnectTimeout time.Duration
|
|
|
|
ReadWriteTimeout time.Duration
|
2015-09-11 14:28:28 +00:00
|
|
|
TLSClientConfig *tls.Config
|
2014-06-03 13:20:10 +00:00
|
|
|
Proxy func(*http.Request) (*url.URL, error)
|
|
|
|
Transport http.RoundTripper
|
2016-09-28 10:04:51 +00:00
|
|
|
CheckRedirect func(req *http.Request, via []*http.Request) error
|
2014-06-03 13:20:10 +00:00
|
|
|
EnableCookie bool
|
2015-04-08 13:45:00 +00:00
|
|
|
Gzip bool
|
2015-06-13 03:15:13 +00:00
|
|
|
DumpBody bool
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// BeegoHTTPRequest provides more useful methods for requesting one url than http.Request.
|
|
|
|
type BeegoHTTPRequest struct {
|
2014-06-04 13:04:50 +00:00
|
|
|
url string
|
|
|
|
req *http.Request
|
2015-08-23 16:16:56 +00:00
|
|
|
params map[string][]string
|
2014-06-04 13:04:50 +00:00
|
|
|
files map[string]string
|
2015-09-11 14:28:28 +00:00
|
|
|
setting BeegoHTTPSettings
|
2014-08-18 13:01:49 +00:00
|
|
|
resp *http.Response
|
|
|
|
body []byte
|
2015-04-08 16:11:25 +00:00
|
|
|
dump []byte
|
2014-06-03 13:20:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// GetRequest return the request object
|
|
|
|
func (b *BeegoHTTPRequest) GetRequest() *http.Request {
|
2015-06-13 03:15:13 +00:00
|
|
|
return b.req
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// Setting Change request settings
|
|
|
|
func (b *BeegoHTTPRequest) Setting(setting BeegoHTTPSettings) *BeegoHTTPRequest {
|
2014-06-03 13:20:10 +00:00
|
|
|
b.setting = setting
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-08-20 15:36:58 +00:00
|
|
|
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetBasicAuth(username, password string) *BeegoHTTPRequest {
|
2014-08-20 15:36:58 +00:00
|
|
|
b.req.SetBasicAuth(username, password)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-06-03 13:20:10 +00:00
|
|
|
// SetEnableCookie sets enable/disable cookiejar
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetEnableCookie(enable bool) *BeegoHTTPRequest {
|
2014-06-03 13:20:10 +00:00
|
|
|
b.setting.EnableCookie = enable
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUserAgent sets User-Agent header field
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetUserAgent(useragent string) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
b.setting.UserAgent = useragent
|
2014-06-03 13:20:10 +00:00
|
|
|
return b
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Debug sets show debug or not when executing request.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
b.setting.ShowDebug = isdebug
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// DumpBody setting whether need to Dump the Body.
|
|
|
|
func (b *BeegoHTTPRequest) DumpBody(isdump bool) *BeegoHTTPRequest {
|
2015-06-13 03:15:13 +00:00
|
|
|
b.setting.DumpBody = isdump
|
2013-08-03 14:20:09 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// DumpRequest return the DumpRequest
|
|
|
|
func (b *BeegoHTTPRequest) DumpRequest() []byte {
|
2015-04-08 16:11:25 +00:00
|
|
|
return b.dump
|
2015-04-08 14:58:37 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHTTPRequest {
|
2014-06-03 13:20:10 +00:00
|
|
|
b.setting.ConnectTimeout = connectTimeout
|
|
|
|
b.setting.ReadWriteTimeout = readWriteTimeout
|
2013-08-03 14:20:09 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// SetTLSClientConfig sets tls connection configurations if visiting https url.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetTLSClientConfig(config *tls.Config) *BeegoHTTPRequest {
|
|
|
|
b.setting.TLSClientConfig = config
|
2013-12-10 14:01:50 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Header add header item string in request.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) Header(key, value string) *BeegoHTTPRequest {
|
2013-08-03 14:20:09 +00:00
|
|
|
b.req.Header.Set(key, value)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// SetHost set the request host
|
|
|
|
func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
|
2015-04-08 12:12:10 +00:00
|
|
|
b.req.Host = host
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// SetProtocolVersion Set the protocol version for incoming requests.
|
2014-05-09 07:48:50 +00:00
|
|
|
// Client requests always use HTTP/1.1.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
|
2014-05-09 07:48:50 +00:00
|
|
|
if len(vers) == 0 {
|
|
|
|
vers = "HTTP/1.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
major, minor, ok := http.ParseHTTPVersion(vers)
|
|
|
|
if ok {
|
|
|
|
b.req.Proto = vers
|
|
|
|
b.req.ProtoMajor = major
|
|
|
|
b.req.ProtoMinor = minor
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// SetCookie add cookie into request.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetCookie(cookie *http.Cookie) *BeegoHTTPRequest {
|
2013-12-19 08:29:46 +00:00
|
|
|
b.req.Header.Add("Cookie", cookie.String())
|
2013-12-12 07:23:17 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// SetTransport set the setting transport
|
|
|
|
func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPRequest {
|
2014-06-03 13:20:10 +00:00
|
|
|
b.setting.Transport = transport
|
2014-02-20 05:53:13 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// SetProxy set the http proxy
|
2014-02-20 05:53:13 +00:00
|
|
|
// example:
|
|
|
|
//
|
|
|
|
// func(req *http.Request) (*url.URL, error) {
|
|
|
|
// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
|
|
|
|
// return u, nil
|
|
|
|
// }
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHTTPRequest {
|
2014-06-03 13:20:10 +00:00
|
|
|
b.setting.Proxy = proxy
|
2014-02-20 05:53:13 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-09-28 10:04:51 +00:00
|
|
|
// SetCheckRedirect specifies the policy for handling redirects.
|
|
|
|
//
|
|
|
|
// If CheckRedirect is nil, the Client uses its default policy,
|
|
|
|
// which is to stop after 10 consecutive requests.
|
|
|
|
func (b *BeegoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *BeegoHTTPRequest {
|
|
|
|
b.setting.CheckRedirect = redirect
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Param adds query param in to request.
|
|
|
|
// params build query string as ?key1=value1&key2=value2...
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
2015-08-23 16:16:56 +00:00
|
|
|
if param, ok := b.params[key]; ok {
|
|
|
|
b.params[key] = append(param, value)
|
|
|
|
} else {
|
|
|
|
b.params[key] = []string{value}
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// PostFile add a post file to the request
|
|
|
|
func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest {
|
2014-05-08 08:58:08 +00:00
|
|
|
b.files[formname] = filename
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Body adds request raw body.
|
|
|
|
// it supports string and []byte.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
|
2013-08-03 14:20:09 +00:00
|
|
|
switch t := data.(type) {
|
2015-04-26 07:24:04 +00:00
|
|
|
case string:
|
2013-08-03 14:20:09 +00:00
|
|
|
bf := bytes.NewBufferString(t)
|
|
|
|
b.req.Body = ioutil.NopCloser(bf)
|
|
|
|
b.req.ContentLength = int64(len(t))
|
2015-04-26 07:24:04 +00:00
|
|
|
case []byte:
|
2013-08-03 14:20:09 +00:00
|
|
|
bf := bytes.NewBuffer(t)
|
|
|
|
b.req.Body = ioutil.NopCloser(bf)
|
|
|
|
b.req.ContentLength = int64(len(t))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// JSONBody adds request raw body encoding by JSON.
|
|
|
|
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
2015-03-06 06:12:24 +00:00
|
|
|
if b.req.Body == nil && obj != nil {
|
2016-03-08 09:04:14 +00:00
|
|
|
byts, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
2015-03-06 06:12:24 +00:00
|
|
|
return b, err
|
|
|
|
}
|
2016-03-08 09:04:14 +00:00
|
|
|
b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
|
|
|
|
b.req.ContentLength = int64(len(byts))
|
2015-03-06 06:12:24 +00:00
|
|
|
b.req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
2015-02-23 03:30:59 +00:00
|
|
|
// build GET url with query string
|
2015-06-13 03:15:13 +00:00
|
|
|
if b.req.Method == "GET" && len(paramBody) > 0 {
|
|
|
|
if strings.Index(b.url, "?") != -1 {
|
2015-04-26 07:42:10 +00:00
|
|
|
b.url += "&" + paramBody
|
2015-06-13 03:15:13 +00:00
|
|
|
} else {
|
|
|
|
b.url = b.url + "?" + paramBody
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
2015-02-23 03:30:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-13 03:15:13 +00:00
|
|
|
// build POST/PUT/PATCH url and body
|
|
|
|
if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH") && b.req.Body == nil {
|
2015-02-23 03:30:59 +00:00
|
|
|
// with files
|
2014-05-08 08:58:08 +00:00
|
|
|
if len(b.files) > 0 {
|
2014-10-30 03:16:09 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
bodyWriter := multipart.NewWriter(pw)
|
|
|
|
go func() {
|
|
|
|
for formname, filename := range b.files {
|
|
|
|
fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
|
|
|
|
if err != nil {
|
2016-01-04 14:41:25 +00:00
|
|
|
log.Println("Httplib:", err)
|
2014-10-30 03:16:09 +00:00
|
|
|
}
|
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2016-01-04 14:41:25 +00:00
|
|
|
log.Println("Httplib:", err)
|
2014-10-30 03:16:09 +00:00
|
|
|
}
|
|
|
|
//iocopy
|
|
|
|
_, err = io.Copy(fileWriter, fh)
|
|
|
|
fh.Close()
|
|
|
|
if err != nil {
|
2016-01-04 14:41:25 +00:00
|
|
|
log.Println("Httplib:", err)
|
2014-10-30 03:16:09 +00:00
|
|
|
}
|
2014-05-08 08:58:08 +00:00
|
|
|
}
|
2014-10-30 03:16:09 +00:00
|
|
|
for k, v := range b.params {
|
2015-08-23 16:16:56 +00:00
|
|
|
for _, vv := range v {
|
|
|
|
bodyWriter.WriteField(k, vv)
|
|
|
|
}
|
2014-05-08 08:58:08 +00:00
|
|
|
}
|
2014-10-30 03:16:09 +00:00
|
|
|
bodyWriter.Close()
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
b.Header("Content-Type", bodyWriter.FormDataContentType())
|
|
|
|
b.req.Body = ioutil.NopCloser(pr)
|
2015-02-23 03:30:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// with params
|
|
|
|
if len(paramBody) > 0 {
|
2014-05-08 08:58:08 +00:00
|
|
|
b.Header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
b.Body(paramBody)
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
2015-02-23 03:30:59 +00:00
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) getResponse() (*http.Response, error) {
|
2015-02-23 03:30:59 +00:00
|
|
|
if b.resp.StatusCode != 0 {
|
|
|
|
return b.resp, nil
|
|
|
|
}
|
2015-09-11 14:28:28 +00:00
|
|
|
resp, err := b.DoRequest()
|
2015-06-13 03:15:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.resp = resp
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// DoRequest will do the client.Do
|
|
|
|
func (b *BeegoHTTPRequest) DoRequest() (*http.Response, error) {
|
2015-02-23 03:30:59 +00:00
|
|
|
var paramBody string
|
|
|
|
if len(b.params) > 0 {
|
2015-06-13 03:15:13 +00:00
|
|
|
var buf bytes.Buffer
|
2015-02-23 03:30:59 +00:00
|
|
|
for k, v := range b.params {
|
2015-08-23 16:16:56 +00:00
|
|
|
for _, vv := range v {
|
|
|
|
buf.WriteString(url.QueryEscape(k))
|
|
|
|
buf.WriteByte('=')
|
|
|
|
buf.WriteString(url.QueryEscape(vv))
|
|
|
|
buf.WriteByte('&')
|
|
|
|
}
|
2015-02-23 03:30:59 +00:00
|
|
|
}
|
2015-06-13 03:15:13 +00:00
|
|
|
paramBody = buf.String()
|
|
|
|
paramBody = paramBody[0 : len(paramBody)-1]
|
2015-02-23 03:30:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
b.buildURL(paramBody)
|
2013-08-03 14:20:09 +00:00
|
|
|
url, err := url.Parse(b.url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-28 05:52:24 +00:00
|
|
|
|
2013-08-03 14:20:09 +00:00
|
|
|
b.req.URL = url
|
|
|
|
|
2014-06-03 13:20:10 +00:00
|
|
|
trans := b.setting.Transport
|
2014-02-20 05:53:13 +00:00
|
|
|
|
|
|
|
if trans == nil {
|
|
|
|
// create default transport
|
|
|
|
trans = &http.Transport{
|
2016-08-17 14:49:30 +00:00
|
|
|
TLSClientConfig: b.setting.TLSClientConfig,
|
|
|
|
Proxy: b.setting.Proxy,
|
|
|
|
Dial: TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout),
|
|
|
|
MaxIdleConnsPerHost: -1,
|
2014-02-20 05:53:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if b.transport is *http.Transport then set the settings.
|
|
|
|
if t, ok := trans.(*http.Transport); ok {
|
|
|
|
if t.TLSClientConfig == nil {
|
2015-09-11 14:28:28 +00:00
|
|
|
t.TLSClientConfig = b.setting.TLSClientConfig
|
2014-02-20 05:53:13 +00:00
|
|
|
}
|
|
|
|
if t.Proxy == nil {
|
2014-06-03 13:20:10 +00:00
|
|
|
t.Proxy = b.setting.Proxy
|
2014-02-20 05:53:13 +00:00
|
|
|
}
|
|
|
|
if t.Dial == nil {
|
2014-06-03 13:20:10 +00:00
|
|
|
t.Dial = TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout)
|
2014-02-20 05:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
2014-02-20 05:53:13 +00:00
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
var jar http.CookieJar
|
2014-06-03 13:20:10 +00:00
|
|
|
if b.setting.EnableCookie {
|
|
|
|
if defaultCookieJar == nil {
|
|
|
|
createDefaultCookie()
|
|
|
|
}
|
|
|
|
jar = defaultCookieJar
|
|
|
|
}
|
|
|
|
|
2014-02-20 05:53:13 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: trans,
|
2014-06-04 13:04:50 +00:00
|
|
|
Jar: jar,
|
2014-06-03 13:20:10 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 15:21:41 +00:00
|
|
|
if b.setting.UserAgent != "" && b.req.Header.Get("User-Agent") == "" {
|
2014-06-03 13:20:10 +00:00
|
|
|
b.req.Header.Set("User-Agent", b.setting.UserAgent)
|
2014-02-20 05:53:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 10:04:51 +00:00
|
|
|
if b.setting.CheckRedirect != nil {
|
|
|
|
client.CheckRedirect = b.setting.CheckRedirect
|
|
|
|
}
|
|
|
|
|
2014-08-22 09:12:46 +00:00
|
|
|
if b.setting.ShowDebug {
|
2015-06-13 03:15:13 +00:00
|
|
|
dump, err := httputil.DumpRequest(b.req, b.setting.DumpBody)
|
2014-08-22 09:12:46 +00:00
|
|
|
if err != nil {
|
2015-04-26 07:42:10 +00:00
|
|
|
log.Println(err.Error())
|
2014-08-22 09:12:46 +00:00
|
|
|
}
|
2015-04-08 16:11:25 +00:00
|
|
|
b.dump = dump
|
2014-08-22 09:12:46 +00:00
|
|
|
}
|
2015-06-13 03:15:13 +00:00
|
|
|
return client.Do(b.req)
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// String returns the body string in response.
|
|
|
|
// it calls Response inner.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) String() (string, error) {
|
2013-08-03 14:20:09 +00:00
|
|
|
data, err := b.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Bytes returns the body []byte in response.
|
|
|
|
// it calls Response inner.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
|
2014-08-18 13:01:49 +00:00
|
|
|
if b.body != nil {
|
|
|
|
return b.body, nil
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
resp, err := b.getResponse()
|
2015-06-13 03:15:13 +00:00
|
|
|
if err != nil {
|
2013-08-03 14:20:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-13 03:15:13 +00:00
|
|
|
if resp.Body == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2013-08-03 14:20:09 +00:00
|
|
|
defer resp.Body.Close()
|
2015-04-08 13:45:00 +00:00
|
|
|
if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
reader, err := gzip.NewReader(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.body, err = ioutil.ReadAll(reader)
|
|
|
|
} else {
|
|
|
|
b.body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
2015-04-25 18:04:34 +00:00
|
|
|
return b.body, err
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// ToFile saves the body data in response to one file.
|
|
|
|
// it calls Response inner.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) ToFile(filename string) error {
|
2015-04-25 18:04:34 +00:00
|
|
|
f, err := os.Create(filename)
|
2013-08-03 14:20:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-25 18:04:34 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2015-06-13 03:15:13 +00:00
|
|
|
resp, err := b.getResponse()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Body == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2014-08-18 13:29:45 +00:00
|
|
|
_, err = io.Copy(f, resp.Body)
|
2014-08-17 13:13:29 +00:00
|
|
|
return err
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// ToJSON returns the map that marshals from the body bytes as json in response .
|
2013-12-27 09:11:39 +00:00
|
|
|
// it calls Response inner.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {
|
2013-08-03 14:20:09 +00:00
|
|
|
data, err := b.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-23 03:30:59 +00:00
|
|
|
return json.Unmarshal(data, v)
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:28:28 +00:00
|
|
|
// ToXML returns the map that marshals from the body bytes as xml in response .
|
2013-12-27 09:11:39 +00:00
|
|
|
// it calls Response inner.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
|
2013-08-03 14:20:09 +00:00
|
|
|
data, err := b.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-23 03:30:59 +00:00
|
|
|
return xml.Unmarshal(data, v)
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// Response executes request client gets response mannually.
|
2015-09-11 14:28:28 +00:00
|
|
|
func (b *BeegoHTTPRequest) Response() (*http.Response, error) {
|
2013-08-03 14:20:09 +00:00
|
|
|
return b.getResponse()
|
|
|
|
}
|
|
|
|
|
2013-12-27 09:11:39 +00:00
|
|
|
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
|
2013-08-03 14:20:09 +00:00
|
|
|
func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
|
|
|
|
return func(netw, addr string) (net.Conn, error) {
|
|
|
|
conn, err := net.DialTimeout(netw, addr, cTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-25 18:04:34 +00:00
|
|
|
err = conn.SetDeadline(time.Now().Add(rwTimeout))
|
|
|
|
return conn, err
|
2013-08-03 14:20:09 +00:00
|
|
|
}
|
|
|
|
}
|