2014-05-06 19:50:31 +04:00
// 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"
2021-07-14 17:43:13 +03:00
"context"
2014-05-06 19:50:31 +04:00
"crypto/tls"
"encoding/xml"
"io"
2014-11-18 23:13:08 +03:00
"log"
2014-08-23 17:13:55 +04:00
"mime/multipart"
2014-05-06 19:50:31 +04:00
"net"
"net/http"
2014-08-23 17:13:55 +04:00
"net/http/cookiejar"
2014-05-06 19:50:31 +04:00
"net/http/httputil"
"net/url"
"os"
"strings"
2014-08-23 17:13:55 +04:00
"sync"
2014-05-06 19:50:31 +04:00
"time"
2021-03-02 00:08:10 +03:00
2021-07-24 19:03:58 +03:00
"code.gitea.io/gitea/modules/json"
2014-05-06 19:50:31 +04:00
)
2018-01-22 23:37:14 +03:00
var defaultSetting = Settings { false , "GiteaServer" , 60 * time . Second , 60 * time . Second , nil , nil , nil , false }
2014-08-23 17:13:55 +04:00
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 09:32:09 +03:00
// SetDefaultSetting overwrites default settings
2015-08-27 18:06:14 +03:00
func SetDefaultSetting ( setting Settings ) {
2014-08-23 17:13:55 +04:00
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 09:32:09 +03:00
// newRequest returns *Request with specific method
2015-10-26 16:16:24 +03:00
func newRequest ( url , method string ) * Request {
2014-08-23 17:13:55 +04:00
var resp http . Response
req := http . Request {
Method : method ,
Header : make ( http . Header ) ,
Proto : "HTTP/1.1" ,
ProtoMajor : 1 ,
ProtoMinor : 1 ,
}
2015-08-27 18:06:14 +03:00
return & Request { url , & req , map [ string ] string { } , map [ string ] string { } , defaultSetting , & resp , nil }
2014-08-23 17:13:55 +04:00
}
2014-05-06 19:50:31 +04:00
2017-04-19 06:45:01 +03:00
// NewRequest returns *Request with specific method
func NewRequest ( url , method string ) * Request {
return newRequest ( url , method )
}
2015-08-27 18:06:14 +03:00
// Get returns *Request with GET method.
func Get ( url string ) * Request {
2015-10-26 16:16:24 +03:00
return newRequest ( url , "GET" )
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
// Post returns *Request with POST method.
func Post ( url string ) * Request {
2015-10-26 16:16:24 +03:00
return newRequest ( url , "POST" )
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
// Put returns *Request with PUT method.
func Put ( url string ) * Request {
2015-10-26 16:16:24 +03:00
return newRequest ( url , "PUT" )
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
// Delete returns *Request DELETE method.
func Delete ( url string ) * Request {
2015-10-26 16:16:24 +03:00
return newRequest ( url , "DELETE" )
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
// Head returns *Request with HEAD method.
func Head ( url string ) * Request {
2015-10-26 16:16:24 +03:00
return newRequest ( url , "HEAD" )
2014-08-23 17:13:55 +04:00
}
2016-11-25 09:32:09 +03:00
// Settings is the default settings for http client
2015-08-27 18:06:14 +03:00
type Settings struct {
2014-08-23 17:13:55 +04:00
ShowDebug bool
UserAgent string
ConnectTimeout time . Duration
ReadWriteTimeout time . Duration
2016-11-25 09:32:09 +03:00
TLSClientConfig * tls . Config
2014-08-23 17:13:55 +04:00
Proxy func ( * http . Request ) ( * url . URL , error )
Transport http . RoundTripper
EnableCookie bool
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// Request provides more useful methods for requesting one url than http.Request.
2015-08-27 18:06:14 +03:00
type Request struct {
2014-08-23 17:13:55 +04:00
url string
req * http . Request
params map [ string ] string
files map [ string ] string
2015-08-27 18:06:14 +03:00
setting Settings
2014-08-23 17:13:55 +04:00
resp * http . Response
body [ ] byte
}
2016-11-25 09:32:09 +03:00
// Setting changes request settings
2015-08-27 18:06:14 +03:00
func ( r * Request ) Setting ( setting Settings ) * Request {
r . setting = setting
return r
2014-08-23 17:13:55 +04:00
}
2021-07-14 17:43:13 +03:00
// SetContext sets the request's Context
func ( r * Request ) SetContext ( ctx context . Context ) * Request {
r . req = r . req . WithContext ( ctx )
return r
}
2014-08-23 17:13:55 +04:00
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetBasicAuth ( username , password string ) * Request {
r . req . SetBasicAuth ( username , password )
return r
2014-08-23 17:13:55 +04:00
}
// SetEnableCookie sets enable/disable cookiejar
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetEnableCookie ( enable bool ) * Request {
r . setting . EnableCookie = enable
return r
2014-08-23 17:13:55 +04:00
}
// SetUserAgent sets User-Agent header field
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetUserAgent ( useragent string ) * Request {
r . setting . UserAgent = useragent
return r
2014-05-06 19:50:31 +04:00
}
// Debug sets show debug or not when executing request.
2015-08-27 18:06:14 +03:00
func ( r * Request ) Debug ( isdebug bool ) * Request {
r . setting . ShowDebug = isdebug
return r
2014-05-06 19:50:31 +04:00
}
// SetTimeout sets connect time out and read-write time out for BeegoRequest.
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetTimeout ( connectTimeout , readWriteTimeout time . Duration ) * Request {
r . setting . ConnectTimeout = connectTimeout
r . setting . ReadWriteTimeout = readWriteTimeout
return r
2014-05-06 19:50:31 +04:00
}
// SetTLSClientConfig sets tls connection configurations if visiting https url.
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetTLSClientConfig ( config * tls . Config ) * Request {
2016-11-25 09:32:09 +03:00
r . setting . TLSClientConfig = config
2015-08-27 18:06:14 +03:00
return r
2014-05-06 19:50:31 +04:00
}
// Header add header item string in request.
2015-08-27 18:06:14 +03:00
func ( r * Request ) Header ( key , value string ) * Request {
r . req . Header . Set ( key , value )
return r
}
2018-01-31 01:09:16 +03:00
// HeaderWithSensitiveCase add header item in request and keep the case of the header key.
func ( r * Request ) HeaderWithSensitiveCase ( key , value string ) * Request {
r . req . Header [ key ] = [ ] string { value }
return r
}
2016-11-25 09:32:09 +03:00
// Headers returns headers in request.
2015-08-27 18:06:14 +03:00
func ( r * Request ) Headers ( ) http . Header {
return r . req . Header
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// SetProtocolVersion sets the protocol version for incoming requests.
2014-08-23 17:13:55 +04:00
// Client requests always use HTTP/1.1.
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetProtocolVersion ( vers string ) * Request {
2014-08-23 17:13:55 +04:00
if len ( vers ) == 0 {
vers = "HTTP/1.1"
}
major , minor , ok := http . ParseHTTPVersion ( vers )
if ok {
2015-08-27 18:06:14 +03:00
r . req . Proto = vers
r . req . ProtoMajor = major
r . req . ProtoMinor = minor
2014-08-23 17:13:55 +04:00
}
2015-08-27 18:06:14 +03:00
return r
2014-08-23 17:13:55 +04:00
}
2014-05-06 19:50:31 +04:00
// SetCookie add cookie into request.
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetCookie ( cookie * http . Cookie ) * Request {
r . req . Header . Add ( "Cookie" , cookie . String ( ) )
return r
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// SetTransport sets transport to
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetTransport ( transport http . RoundTripper ) * Request {
r . setting . Transport = transport
return r
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// SetProxy sets http proxy
2014-05-06 19:50:31 +04:00
// example:
//
// func(req *http.Request) (*url.URL, error) {
// u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
// return u, nil
// }
2015-08-27 18:06:14 +03:00
func ( r * Request ) SetProxy ( proxy func ( * http . Request ) ( * url . URL , error ) ) * Request {
r . setting . Proxy = proxy
return r
2014-05-06 19:50:31 +04:00
}
// Param adds query param in to request.
// params build query string as ?key1=value1&key2=value2...
2015-08-27 18:06:14 +03:00
func ( r * Request ) Param ( key , value string ) * Request {
r . params [ key ] = value
return r
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// PostFile uploads file via http
2015-08-27 18:06:14 +03:00
func ( r * Request ) PostFile ( formname , filename string ) * Request {
r . files [ formname ] = filename
return r
2014-08-23 17:13:55 +04:00
}
2014-05-06 19:50:31 +04:00
// Body adds request raw body.
// it supports string and []byte.
2015-08-27 18:06:14 +03:00
func ( r * Request ) Body ( data interface { } ) * Request {
2014-05-06 19:50:31 +04:00
switch t := data . ( type ) {
case string :
bf := bytes . NewBufferString ( t )
2021-09-22 08:38:34 +03:00
r . req . Body = io . NopCloser ( bf )
2015-08-27 18:06:14 +03:00
r . req . ContentLength = int64 ( len ( t ) )
2014-05-06 19:50:31 +04:00
case [ ] byte :
bf := bytes . NewBuffer ( t )
2021-09-22 08:38:34 +03:00
r . req . Body = io . NopCloser ( bf )
2015-08-27 18:06:14 +03:00
r . req . ContentLength = int64 ( len ( t ) )
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
return r
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
func ( r * Request ) getResponse ( ) ( * http . Response , error ) {
if r . resp . StatusCode != 0 {
return r . resp , nil
2014-08-23 17:13:55 +04:00
}
2014-05-06 19:50:31 +04:00
var paramBody string
2015-08-27 18:06:14 +03:00
if len ( r . params ) > 0 {
2014-05-06 19:50:31 +04:00
var buf bytes . Buffer
2015-08-27 18:06:14 +03:00
for k , v := range r . params {
2014-05-06 19:50:31 +04:00
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 18:06:14 +03:00
if r . req . Method == "GET" && len ( paramBody ) > 0 {
2019-06-12 22:41:28 +03:00
if strings . Contains ( r . url , "?" ) {
2015-08-27 18:06:14 +03:00
r . url += "&" + paramBody
2014-05-06 19:50:31 +04:00
} else {
2015-08-27 18:06:14 +03:00
r . url = r . url + "?" + paramBody
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
} else if r . req . Method == "POST" && r . req . Body == nil {
if len ( r . files ) > 0 {
2014-11-18 23:13:08 +03:00
pr , pw := io . Pipe ( )
bodyWriter := multipart . NewWriter ( pw )
go func ( ) {
2015-08-27 18:06:14 +03:00
for formname , filename := range r . files {
2014-11-18 23:13:08 +03:00
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 17:13:55 +04:00
}
2015-08-27 18:06:14 +03:00
for k , v := range r . params {
2019-06-12 22:41:28 +03:00
err := bodyWriter . WriteField ( k , v )
if err != nil {
log . Fatal ( err )
}
2014-08-23 17:13:55 +04:00
}
2019-06-12 22:41:28 +03:00
_ = bodyWriter . Close ( )
_ = pw . Close ( )
2014-11-18 23:13:08 +03:00
} ( )
2015-08-27 18:06:14 +03:00
r . Header ( "Content-Type" , bodyWriter . FormDataContentType ( ) )
2021-09-22 08:38:34 +03:00
r . req . Body = io . NopCloser ( pr )
2014-11-18 23:13:08 +03:00
} else if len ( paramBody ) > 0 {
2015-08-27 18:06:14 +03:00
r . Header ( "Content-Type" , "application/x-www-form-urlencoded" )
r . Body ( paramBody )
2014-08-23 17:13:55 +04:00
}
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
url , err := url . Parse ( r . url )
2014-05-06 19:50:31 +04:00
if err != nil {
return nil , err
}
2015-08-27 18:06:14 +03:00
r . req . URL = url
2014-05-06 19:50:31 +04:00
2015-08-27 18:06:14 +03:00
trans := r . setting . Transport
2014-05-06 19:50:31 +04:00
if trans == nil {
// create default transport
2017-03-02 03:36:42 +03:00
proxy := r . setting . Proxy
if proxy == nil {
proxy = http . ProxyFromEnvironment
}
2014-05-06 19:50:31 +04:00
trans = & http . Transport {
2016-11-25 09:32:09 +03:00
TLSClientConfig : r . setting . TLSClientConfig ,
2017-03-02 03:36:42 +03:00
Proxy : proxy ,
2021-07-14 17:43:13 +03:00
DialContext : TimeoutDialer ( r . setting . ConnectTimeout ) ,
2014-05-06 19:50:31 +04:00
}
2019-06-12 22:41:28 +03:00
} else if t , ok := trans . ( * http . Transport ) ; ok {
if t . TLSClientConfig == nil {
t . TLSClientConfig = r . setting . TLSClientConfig
}
if t . Proxy == nil {
t . Proxy = r . setting . Proxy
}
2021-07-14 17:43:13 +03:00
if t . DialContext == nil {
t . DialContext = TimeoutDialer ( r . setting . ConnectTimeout )
2014-05-06 19:50:31 +04:00
}
}
2014-08-23 17:13:55 +04:00
var jar http . CookieJar
2015-08-27 18:06:14 +03:00
if r . setting . EnableCookie {
2014-08-23 17:13:55 +04:00
if defaultCookieJar == nil {
createDefaultCookie ( )
}
jar = defaultCookieJar
} else {
jar = nil
}
2014-05-06 19:50:31 +04:00
client := & http . Client {
Transport : trans ,
2014-08-23 17:13:55 +04:00
Jar : jar ,
2021-04-25 21:48:12 +03:00
Timeout : r . setting . ReadWriteTimeout ,
2014-08-23 17:13:55 +04:00
}
2015-08-27 18:06:14 +03:00
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 17:13:55 +04:00
}
2015-08-27 18:06:14 +03:00
if r . setting . ShowDebug {
dump , err := httputil . DumpRequest ( r . req , true )
2014-08-23 17:13:55 +04:00
if err != nil {
println ( err . Error ( ) )
}
println ( string ( dump ) )
2014-05-06 19:50:31 +04:00
}
2015-08-27 18:06:14 +03:00
resp , err := client . Do ( r . req )
2014-05-06 19:50:31 +04:00
if err != nil {
return nil , err
}
2015-08-27 18:06:14 +03:00
r . resp = resp
2014-05-06 19:50:31 +04:00
return resp , nil
}
// String returns the body string in response.
// it calls Response inner.
2015-08-27 18:06:14 +03:00
func ( r * Request ) String ( ) ( string , error ) {
data , err := r . Bytes ( )
2014-05-06 19:50:31 +04:00
if err != nil {
return "" , err
}
return string ( data ) , nil
}
// Bytes returns the body []byte in response.
// it calls Response inner.
2015-08-27 18:06:14 +03:00
func ( r * Request ) Bytes ( ) ( [ ] byte , error ) {
if r . body != nil {
return r . body , nil
2014-08-23 17:13:55 +04:00
}
2015-08-27 18:06:14 +03:00
resp , err := r . getResponse ( )
2014-05-06 19:50:31 +04:00
if err != nil {
return nil , err
}
if resp . Body == nil {
return nil , nil
}
defer resp . Body . Close ( )
2021-09-22 08:38:34 +03:00
data , err := io . ReadAll ( resp . Body )
2014-05-06 19:50:31 +04:00
if err != nil {
return nil , err
}
2015-08-27 18:06:14 +03:00
r . body = data
2014-05-06 19:50:31 +04:00
return data , nil
}
// ToFile saves the body data in response to one file.
// it calls Response inner.
2015-08-27 18:06:14 +03:00
func ( r * Request ) ToFile ( filename string ) error {
2014-05-06 19:50:31 +04:00
f , err := os . Create ( filename )
if err != nil {
return err
}
defer f . Close ( )
2015-08-27 18:06:14 +03:00
resp , err := r . getResponse ( )
2014-05-06 19:50:31 +04:00
if err != nil {
return err
}
if resp . Body == nil {
return nil
}
defer resp . Body . Close ( )
_ , err = io . Copy ( f , resp . Body )
2014-08-23 17:13:55 +04:00
return err
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// ToJSON returns the map that marshals from the body bytes as json in response .
2014-05-06 19:50:31 +04:00
// it calls Response inner.
2016-11-25 09:32:09 +03:00
func ( r * Request ) ToJSON ( v interface { } ) error {
2015-08-27 18:06:14 +03:00
data , err := r . Bytes ( )
2014-05-06 19:50:31 +04:00
if err != nil {
return err
}
err = json . Unmarshal ( data , v )
2014-08-23 17:13:55 +04:00
return err
2014-05-06 19:50:31 +04:00
}
2016-11-25 09:32:09 +03:00
// ToXML returns the map that marshals from the body bytes as xml in response .
2014-05-06 19:50:31 +04:00
// it calls Response inner.
2016-11-25 09:32:09 +03:00
func ( r * Request ) ToXML ( v interface { } ) error {
2015-08-27 18:06:14 +03:00
data , err := r . Bytes ( )
2014-05-06 19:50:31 +04:00
if err != nil {
return err
}
err = xml . Unmarshal ( data , v )
2014-08-23 17:13:55 +04:00
return err
2014-05-06 19:50:31 +04:00
}
2017-03-15 03:52:01 +03:00
// Response executes request client gets response manually.
2015-08-27 18:06:14 +03:00
func ( r * Request ) Response ( ) ( * http . Response , error ) {
return r . getResponse ( )
2014-05-06 19:50:31 +04:00
}
// TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
2021-07-14 17:43:13 +03:00
func TimeoutDialer ( cTimeout time . Duration ) func ( ctx context . Context , net , addr string ) ( c net . Conn , err error ) {
return func ( ctx context . Context , netw , addr string ) ( net . Conn , error ) {
d := net . Dialer { Timeout : cTimeout }
conn , err := d . DialContext ( ctx , netw , addr )
2014-05-06 19:50:31 +04:00
if err != nil {
return nil , err
}
2021-04-25 21:48:12 +03:00
return conn , nil
2014-05-06 19:50:31 +04:00
}
}