httputils

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2020 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestTag is the tag name for parsing structure to query parameters.
	// see function ParseQuery.
	RequestTag = "request"

	// DefaultTimeout is the default timeout to check connect.
	DefaultTimeout = 500 * time.Millisecond
)
View Source
const (
	ApplicationJSONUtf8Value = "application/json;charset=utf-8"
)

http content types

Variables

View Source
var (
	// DefaultBuiltInTransport is the transport for HTTPWithHeaders.
	DefaultBuiltInTransport *http.Transport

	// DefaultBuiltInHTTPClient is the http client for HTTPWithHeaders.
	DefaultBuiltInHTTPClient *http.Client
)

Functions

func CheckConnect

func CheckConnect(ip string, port int, timeout int) (localIP string, e error)

CheckConnect checks the network connectivity between local and remote. param timeout: its unit is milliseconds, reset to 500 ms if <= 0 returns localIP

func ConstructRangeStr

func ConstructRangeStr(rangeStr string) string

ConstructRangeStr wraps the rangeStr as a HTTP Range header value.

func Do

func Do(url string, headers map[string]string, timeout time.Duration) (string, error)

Do performs the given http request and fills the given http response. When timeout <= 0, it will block until receiving response from server.

func Get

func Get(url string, timeout time.Duration) (int, []byte, error)

Get sends a GET request to server. When timeout <= 0, it will block until receiving response from server.

func GetValidURLSchemas added in v1.0.1

func GetValidURLSchemas() string

func GetWithHeaders

func GetWithHeaders(url string, headers map[string]string, timeout time.Duration) (code int, resBody []byte, err error)

GetWithHeaders sends a GET request to server. When timeout <= 0, it will block until receiving response from server.

func HTTPGet

func HTTPGet(url string, headers map[string]string) (*http.Response, error)

HTTPGet sends an HTTP GET request with headers.

func HTTPGetTimeout

func HTTPGetTimeout(url string, headers map[string]string, timeout time.Duration) (*http.Response, error)

HTTPGetTimeout sends an HTTP GET request with timeout.

func HTTPGetWithTLS added in v1.0.0

func HTTPGetWithTLS(url string, headers map[string]string, timeout time.Duration, cacerts []string, insecure bool) (*http.Response, error)

HTTPGetWithTLS sends an HTTP GET request with TLS config.

func HTTPStatusOk

func HTTPStatusOk(code int) bool

HTTPStatusOk reports whether the http response code is 200.

func HTTPWithHeaders

func HTTPWithHeaders(method, url string, headers map[string]string, timeout time.Duration, tlsConfig *tls.Config) (*http.Response, error)

HTTPWithHeaders sends an HTTP request with headers and specified method.

func ParseQuery

func ParseQuery(query interface{}) string

ParseQuery only parses the fields with tag 'request' of the query to parameters. query must be a pointer to a struct.

func PostJSON

func PostJSON(url string, body interface{}, timeout time.Duration) (int, []byte, error)

PostJSON sends a POST request whose content-type is 'application/json;charset=utf-8'.

func PostJSONWithHeaders

func PostJSONWithHeaders(url string, headers map[string]string, body interface{}, timeout time.Duration) (int, []byte, error)

PostJSONWithHeaders sends a POST request whose content-type is 'application/json;charset=utf-8'.

func RegisterProtocol added in v1.0.1

func RegisterProtocol(scheme string, rt http.RoundTripper)

RegisterProtocol registers custom protocols in global variable "protocols" which will be used in dfget and supernode Example:

protocols := "helloworld"
newTransport := funcNewTransport
httputils.RegisterProtocol(protocols, newTransport)

RegisterProtocol must be called before initialise dfget or supernode instances.

func RegisterProtocolOnTransport added in v1.0.1

func RegisterProtocolOnTransport(tr *http.Transport)

RegisterProtocolOnTransport registers all new protocols in "protocols" for a special Transport this function will be used in supernode and dfwget

Types

type MockHTTPClient

type MockHTTPClient struct {
	PostJSONFunc            postJSONFunc
	GetFunc                 getFunc
	PostJSONWithHeadersFunc postJSONWithHeadersFunc
	GetWithHeadersFunc      getWithHeadersFunc
}

MockHTTPClient fakes a customized implementation of util.SimpleHTTPClient.

func NewMockHTTPClient

func NewMockHTTPClient() *MockHTTPClient

NewMockHTTPClient returns a new MockHTTPClient instance.

func (*MockHTTPClient) CreateGetFunc

func (m *MockHTTPClient) CreateGetFunc(code int, res []byte, e error) getFunc

CreateGetFunc returns a mock getFunc func which will always return the specific results.

func (*MockHTTPClient) CreateGetWithHeadersFunc

func (m *MockHTTPClient) CreateGetWithHeadersFunc(code int, res []byte, e error) getWithHeadersFunc

CreateGetWithHeadersFunc returns a mock getWithHeadersFunc func which will always return the specific results.

func (*MockHTTPClient) CreatePostJSONFunc

func (m *MockHTTPClient) CreatePostJSONFunc(code int, res []byte, e error) postJSONFunc

CreatePostJSONFunc returns a mock postJSONFunc func which will always return the specific results.

func (*MockHTTPClient) CreatePostJSONWithHeadersFunc

func (m *MockHTTPClient) CreatePostJSONWithHeadersFunc(code int, res []byte, e error) postJSONWithHeadersFunc

CreatePostJSONWithHeadersFunc returns a mock postJSONWithHeadersFunc func which will always return the specific results.

func (*MockHTTPClient) Get

func (m *MockHTTPClient) Get(url string, timeout time.Duration) (int, []byte, error)

Get mocks base method.

func (*MockHTTPClient) GetWithHeaders

func (m *MockHTTPClient) GetWithHeaders(url string, headers map[string]string, timeout time.Duration) (
	int, []byte, error)

GetWithHeaders mocks base method.

func (*MockHTTPClient) PostJSON

func (m *MockHTTPClient) PostJSON(url string, body interface{}, timeout time.Duration) (
	int, []byte, error)

PostJSON mocks base method.

func (*MockHTTPClient) PostJSONWithHeaders

func (m *MockHTTPClient) PostJSONWithHeaders(url string, headers map[string]string, body interface{}, timeout time.Duration) (
	int, []byte, error)

PostJSONWithHeaders mocks base method.

func (*MockHTTPClient) Reset

func (m *MockHTTPClient) Reset()

Reset the MockHTTPClient.

type RangeStruct

type RangeStruct struct {
	StartIndex int64
	EndIndex   int64
}

RangeStruct contains the start and end of a http header range.

func GetRangeSE

func GetRangeSE(rangeHTTPHeader string, length int64) ([]*RangeStruct, error)

GetRangeSE parses the start and the end from range HTTP header and returns them.

type SimpleHTTPClient

type SimpleHTTPClient interface {
	PostJSON(url string, body interface{}, timeout time.Duration) (code int, res []byte, e error)
	Get(url string, timeout time.Duration) (code int, res []byte, e error)
	PostJSONWithHeaders(url string, headers map[string]string, body interface{}, timeout time.Duration) (code int, resBody []byte, err error)
	GetWithHeaders(url string, headers map[string]string, timeout time.Duration) (code int, resBody []byte, err error)
}

SimpleHTTPClient defines some http functions used frequently.

var DefaultHTTPClient SimpleHTTPClient = &defaultHTTPClient{}

DefaultHTTPClient is the default implementation of SimpleHTTPClient.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL