bench-forgejo/modules/httplib/httplib.go

453 lines
11 KiB
Go
Raw Normal View History

2014-05-06 21:20:31 +05:30
// Copyright 2013 The Beego Authors. All rights reserved.
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package httplib
import (
"bytes"
"crypto/tls"
"encoding/json"
"encoding/xml"
"io"
"io/ioutil"
2014-11-19 01:43:08 +05:30
"log"
2014-08-23 18:43:55 +05:30
"mime/multipart"
2014-05-06 21:20:31 +05:30
"net"
"net/http"
2014-08-23 18:43:55 +05:30
"net/http/cookiejar"
2014-05-06 21:20:31 +05:30
"net/http/httputil"
"net/url"
"os"
"strings"
2014-08-23 18:43:55 +05:30
"sync"
2014-05-06 21:20:31 +05:30
"time"
)
2015-08-27 20:36:14 +05:30
var defaultSetting = Settings{false, "GogsServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false}
2014-08-23 18:43:55 +05:30
var defaultCookieJar http.CookieJar
var settingMutex sync.Mutex
// createDefaultCookie creates a global cookiejar to store cookies.
func createDefaultCookie() {
settingMutex.Lock()
defer settingMutex.Unlock()
defaultCookieJar, _ = cookiejar.New(nil)
}
2016-11-25 12:02:09 +05:30
// SetDefaultSetting overwrites default settings
2015-08-27 20:36:14 +05:30
func SetDefaultSetting(setting Settings) {
2014-08-23 18:43:55 +05:30
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
}
}
2016-11-25 12:02:09 +05:30
// newRequest returns *Request with specific method
2015-10-26 18:46:24 +05:30
func newRequest(url, method string) *Request {
2014-08-23 18:43:55 +05:30
var resp http.Response
req := http.Request{
Method: method,
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
2015-08-27 20:36:14 +05:30
return &Request{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
2014-08-23 18:43:55 +05:30
}
2014-05-06 21:20:31 +05:30
2015-08-27 20:36:14 +05:30
// Get returns *Request with GET method.
func Get(url string) *Request {
2015-10-26 18:46:24 +05:30
return newRequest(url, "GET")
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
// Post returns *Request with POST method.
func Post(url string) *Request {
2015-10-26 18:46:24 +05:30
return newRequest(url, "POST")
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
// Put returns *Request with PUT method.
func Put(url string) *Request {
2015-10-26 18:46:24 +05:30
return newRequest(url, "PUT")
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
// Delete returns *Request DELETE method.
func Delete(url string) *Request {
2015-10-26 18:46:24 +05:30
return newRequest(url, "DELETE")
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
// Head returns *Request with HEAD method.
func Head(url string) *Request {
2015-10-26 18:46:24 +05:30
return newRequest(url, "HEAD")
2014-08-23 18:43:55 +05:30
}
2016-11-25 12:02:09 +05:30
// Settings is the default settings for http client
2015-08-27 20:36:14 +05:30
type Settings struct {
2014-08-23 18:43:55 +05:30
ShowDebug bool
UserAgent string
ConnectTimeout time.Duration
ReadWriteTimeout time.Duration
2016-11-25 12:02:09 +05:30
TLSClientConfig *tls.Config
2014-08-23 18:43:55 +05:30
Proxy func(*http.Request) (*url.URL, error)
Transport http.RoundTripper
EnableCookie bool
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// Request provides more useful methods for requesting one url than http.Request.
2015-08-27 20:36:14 +05:30
type Request struct {
2014-08-23 18:43:55 +05:30
url string
req *http.Request
params map[string]string
files map[string]string
2015-08-27 20:36:14 +05:30
setting Settings
2014-08-23 18:43:55 +05:30
resp *http.Response
body []byte
}
2016-11-25 12:02:09 +05:30
// Setting changes request settings
2015-08-27 20:36:14 +05:30
func (r *Request) Setting(setting Settings) *Request {
r.setting = setting
return r
2014-08-23 18:43:55 +05:30
}
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
2015-08-27 20:36:14 +05:30
func (r *Request) SetBasicAuth(username, password string) *Request {
r.req.SetBasicAuth(username, password)
return r
2014-08-23 18:43:55 +05:30
}
// SetEnableCookie sets enable/disable cookiejar
2015-08-27 20:36:14 +05:30
func (r *Request) SetEnableCookie(enable bool) *Request {
r.setting.EnableCookie = enable
return r
2014-08-23 18:43:55 +05:30
}
// SetUserAgent sets User-Agent header field
2015-08-27 20:36:14 +05:30
func (r *Request) SetUserAgent(useragent string) *Request {
r.setting.UserAgent = useragent
return r
2014-05-06 21:20:31 +05:30
}
// Debug sets show debug or not when executing request.
2015-08-27 20:36:14 +05:30
func (r *Request) Debug(isdebug bool) *Request {
r.setting.ShowDebug = isdebug
return r
2014-05-06 21:20:31 +05:30
}
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
2015-08-27 20:36:14 +05:30
func (r *Request) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *Request {
r.setting.ConnectTimeout = connectTimeout
r.setting.ReadWriteTimeout = readWriteTimeout
return r
2014-05-06 21:20:31 +05:30
}
// SetTLSClientConfig sets tls connection configurations if visiting https url.
2015-08-27 20:36:14 +05:30
func (r *Request) SetTLSClientConfig(config *tls.Config) *Request {
2016-11-25 12:02:09 +05:30
r.setting.TLSClientConfig = config
2015-08-27 20:36:14 +05:30
return r
2014-05-06 21:20:31 +05:30
}
// Header add header item string in request.
2015-08-27 20:36:14 +05:30
func (r *Request) Header(key, value string) *Request {
r.req.Header.Set(key, value)
return r
}
2016-11-25 12:02:09 +05:30
// Headers returns headers in request.
2015-08-27 20:36:14 +05:30
func (r *Request) Headers() http.Header {
return r.req.Header
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// SetProtocolVersion sets the protocol version for incoming requests.
2014-08-23 18:43:55 +05:30
// Client requests always use HTTP/1.1.
2015-08-27 20:36:14 +05:30
func (r *Request) SetProtocolVersion(vers string) *Request {
2014-08-23 18:43:55 +05:30
if len(vers) == 0 {
vers = "HTTP/1.1"
}
major, minor, ok := http.ParseHTTPVersion(vers)
if ok {
2015-08-27 20:36:14 +05:30
r.req.Proto = vers
r.req.ProtoMajor = major
r.req.ProtoMinor = minor
2014-08-23 18:43:55 +05:30
}
2015-08-27 20:36:14 +05:30
return r
2014-08-23 18:43:55 +05:30
}
2014-05-06 21:20:31 +05:30
// SetCookie add cookie into request.
2015-08-27 20:36:14 +05:30
func (r *Request) SetCookie(cookie *http.Cookie) *Request {
r.req.Header.Add("Cookie", cookie.String())
return r
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// SetTransport sets transport to
2015-08-27 20:36:14 +05:30
func (r *Request) SetTransport(transport http.RoundTripper) *Request {
r.setting.Transport = transport
return r
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// SetProxy sets http proxy
2014-05-06 21:20:31 +05:30
// example:
//
// func(req *http.Request) (*url.URL, error) {
// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
// return u, nil
// }
2015-08-27 20:36:14 +05:30
func (r *Request) SetProxy(proxy func(*http.Request) (*url.URL, error)) *Request {
r.setting.Proxy = proxy
return r
2014-05-06 21:20:31 +05:30
}
// Param adds query param in to request.
// params build query string as ?key1=value1&key2=value2...
2015-08-27 20:36:14 +05:30
func (r *Request) Param(key, value string) *Request {
r.params[key] = value
return r
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// PostFile uploads file via http
2015-08-27 20:36:14 +05:30
func (r *Request) PostFile(formname, filename string) *Request {
r.files[formname] = filename
return r
2014-08-23 18:43:55 +05:30
}
2014-05-06 21:20:31 +05:30
// Body adds request raw body.
// it supports string and []byte.
2015-08-27 20:36:14 +05:30
func (r *Request) Body(data interface{}) *Request {
2014-05-06 21:20:31 +05:30
switch t := data.(type) {
case string:
bf := bytes.NewBufferString(t)
2015-08-27 20:36:14 +05:30
r.req.Body = ioutil.NopCloser(bf)
r.req.ContentLength = int64(len(t))
2014-05-06 21:20:31 +05:30
case []byte:
bf := bytes.NewBuffer(t)
2015-08-27 20:36:14 +05:30
r.req.Body = ioutil.NopCloser(bf)
r.req.ContentLength = int64(len(t))
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
return r
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
func (r *Request) getResponse() (*http.Response, error) {
if r.resp.StatusCode != 0 {
return r.resp, nil
2014-08-23 18:43:55 +05:30
}
2014-05-06 21:20:31 +05:30
var paramBody string
2015-08-27 20:36:14 +05:30
if len(r.params) > 0 {
2014-05-06 21:20:31 +05:30
var buf bytes.Buffer
2015-08-27 20:36:14 +05:30
for k, v := range r.params {
2014-05-06 21:20:31 +05:30
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
buf.WriteByte('&')
}
paramBody = buf.String()
paramBody = paramBody[0 : len(paramBody)-1]
}
2015-08-27 20:36:14 +05:30
if r.req.Method == "GET" && len(paramBody) > 0 {
if strings.Index(r.url, "?") != -1 {
r.url += "&" + paramBody
2014-05-06 21:20:31 +05:30
} else {
2015-08-27 20:36:14 +05:30
r.url = r.url + "?" + paramBody
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
} else if r.req.Method == "POST" && r.req.Body == nil {
if len(r.files) > 0 {
2014-11-19 01:43:08 +05:30
pr, pw := io.Pipe()
bodyWriter := multipart.NewWriter(pw)
go func() {
2015-08-27 20:36:14 +05:30
for formname, filename := range r.files {
2014-11-19 01:43:08 +05:30
fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
if err != nil {
log.Fatal(err)
}
fh, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
//iocopy
_, err = io.Copy(fileWriter, fh)
fh.Close()
if err != nil {
log.Fatal(err)
}
2014-08-23 18:43:55 +05:30
}
2015-08-27 20:36:14 +05:30
for k, v := range r.params {
2014-11-19 01:43:08 +05:30
bodyWriter.WriteField(k, v)
2014-08-23 18:43:55 +05:30
}
2014-11-19 01:43:08 +05:30
bodyWriter.Close()
pw.Close()
}()
2015-08-27 20:36:14 +05:30
r.Header("Content-Type", bodyWriter.FormDataContentType())
r.req.Body = ioutil.NopCloser(pr)
2014-11-19 01:43:08 +05:30
} else if len(paramBody) > 0 {
2015-08-27 20:36:14 +05:30
r.Header("Content-Type", "application/x-www-form-urlencoded")
r.Body(paramBody)
2014-08-23 18:43:55 +05:30
}
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
url, err := url.Parse(r.url)
2014-05-06 21:20:31 +05:30
if err != nil {
return nil, err
}
2015-08-27 20:36:14 +05:30
r.req.URL = url
2014-05-06 21:20:31 +05:30
2015-08-27 20:36:14 +05:30
trans := r.setting.Transport
2014-05-06 21:20:31 +05:30
if trans == nil {
// create default transport
trans = &http.Transport{
2016-11-25 12:02:09 +05:30
TLSClientConfig: r.setting.TLSClientConfig,
2015-08-27 20:36:14 +05:30
Proxy: r.setting.Proxy,
Dial: TimeoutDialer(r.setting.ConnectTimeout, r.setting.ReadWriteTimeout),
2014-05-06 21:20:31 +05:30
}
} else {
2015-08-27 20:36:14 +05:30
// if r.transport is *http.Transport then set the settings.
2014-05-06 21:20:31 +05:30
if t, ok := trans.(*http.Transport); ok {
if t.TLSClientConfig == nil {
2016-11-25 12:02:09 +05:30
t.TLSClientConfig = r.setting.TLSClientConfig
2014-05-06 21:20:31 +05:30
}
if t.Proxy == nil {
2015-08-27 20:36:14 +05:30
t.Proxy = r.setting.Proxy
2014-05-06 21:20:31 +05:30
}
if t.Dial == nil {
2015-08-27 20:36:14 +05:30
t.Dial = TimeoutDialer(r.setting.ConnectTimeout, r.setting.ReadWriteTimeout)
2014-05-06 21:20:31 +05:30
}
}
}
2014-08-23 18:43:55 +05:30
var jar http.CookieJar
2015-08-27 20:36:14 +05:30
if r.setting.EnableCookie {
2014-08-23 18:43:55 +05:30
if defaultCookieJar == nil {
createDefaultCookie()
}
jar = defaultCookieJar
} else {
jar = nil
}
2014-05-06 21:20:31 +05:30
client := &http.Client{
Transport: trans,
2014-08-23 18:43:55 +05:30
Jar: jar,
}
2015-08-27 20:36:14 +05:30
if len(r.setting.UserAgent) > 0 && len(r.req.Header.Get("User-Agent")) == 0 {
r.req.Header.Set("User-Agent", r.setting.UserAgent)
2014-08-23 18:43:55 +05:30
}
2015-08-27 20:36:14 +05:30
if r.setting.ShowDebug {
dump, err := httputil.DumpRequest(r.req, true)
2014-08-23 18:43:55 +05:30
if err != nil {
println(err.Error())
}
println(string(dump))
2014-05-06 21:20:31 +05:30
}
2015-08-27 20:36:14 +05:30
resp, err := client.Do(r.req)
2014-05-06 21:20:31 +05:30
if err != nil {
return nil, err
}
2015-08-27 20:36:14 +05:30
r.resp = resp
2014-05-06 21:20:31 +05:30
return resp, nil
}
// String returns the body string in response.
// it calls Response inner.
2015-08-27 20:36:14 +05:30
func (r *Request) String() (string, error) {
data, err := r.Bytes()
2014-05-06 21:20:31 +05:30
if err != nil {
return "", err
}
return string(data), nil
}
// Bytes returns the body []byte in response.
// it calls Response inner.
2015-08-27 20:36:14 +05:30
func (r *Request) Bytes() ([]byte, error) {
if r.body != nil {
return r.body, nil
2014-08-23 18:43:55 +05:30
}
2015-08-27 20:36:14 +05:30
resp, err := r.getResponse()
2014-05-06 21:20:31 +05:30
if err != nil {
return nil, err
}
if resp.Body == nil {
return nil, nil
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
2015-08-27 20:36:14 +05:30
r.body = data
2014-05-06 21:20:31 +05:30
return data, nil
}
// ToFile saves the body data in response to one file.
// it calls Response inner.
2015-08-27 20:36:14 +05:30
func (r *Request) ToFile(filename string) error {
2014-05-06 21:20:31 +05:30
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
2015-08-27 20:36:14 +05:30
resp, err := r.getResponse()
2014-05-06 21:20:31 +05:30
if err != nil {
return err
}
if resp.Body == nil {
return nil
}
defer resp.Body.Close()
_, err = io.Copy(f, resp.Body)
2014-08-23 18:43:55 +05:30
return err
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// ToJSON returns the map that marshals from the body bytes as json in response .
2014-05-06 21:20:31 +05:30
// it calls Response inner.
2016-11-25 12:02:09 +05:30
func (r *Request) ToJSON(v interface{}) error {
2015-08-27 20:36:14 +05:30
data, err := r.Bytes()
2014-05-06 21:20:31 +05:30
if err != nil {
return err
}
err = json.Unmarshal(data, v)
2014-08-23 18:43:55 +05:30
return err
2014-05-06 21:20:31 +05:30
}
2016-11-25 12:02:09 +05:30
// ToXML returns the map that marshals from the body bytes as xml in response .
2014-05-06 21:20:31 +05:30
// it calls Response inner.
2016-11-25 12:02:09 +05:30
func (r *Request) ToXML(v interface{}) error {
2015-08-27 20:36:14 +05:30
data, err := r.Bytes()
2014-05-06 21:20:31 +05:30
if err != nil {
return err
}
err = xml.Unmarshal(data, v)
2014-08-23 18:43:55 +05:30
return err
2014-05-06 21:20:31 +05:30
}
// Response executes request client gets response mannually.
2015-08-27 20:36:14 +05:30
func (r *Request) Response() (*http.Response, error) {
return r.getResponse()
2014-05-06 21:20:31 +05:30
}
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
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
}
conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil
}
}