Documentation ¶
Index ¶
- Constants
- Variables
- func CheckConnect(ip string, port int, timeout int) (localIP string, e error)
- func ConstructRangeStr(rangeStr string) string
- func Do(url string, headers map[string]string, timeout time.Duration) (string, error)
- func Get(url string, timeout time.Duration) (int, []byte, error)
- func GetValidURLSchemas() string
- func GetWithHeaders(url string, headers map[string]string, timeout time.Duration) (code int, resBody []byte, err error)
- func HTTPGet(url string, headers map[string]string) (*http.Response, error)
- func HTTPGetTimeout(url string, headers map[string]string, timeout time.Duration) (*http.Response, error)
- func HTTPGetWithTLS(url string, headers map[string]string, timeout time.Duration, cacerts []string, ...) (*http.Response, error)
- func HTTPStatusOk(code int) bool
- func HTTPWithHeaders(method, url string, headers map[string]string, timeout time.Duration, ...) (*http.Response, error)
- func ParseQuery(query interface{}) string
- func PostJSON(url string, body interface{}, timeout time.Duration) (int, []byte, error)
- func PostJSONWithHeaders(url string, headers map[string]string, body interface{}, timeout time.Duration) (int, []byte, error)
- func RegisterProtocol(scheme string, rt http.RoundTripper)
- func RegisterProtocolOnTransport(tr *http.Transport)
- type MockHTTPClient
- func (m *MockHTTPClient) CreateGetFunc(code int, res []byte, e error) getFunc
- func (m *MockHTTPClient) CreateGetWithHeadersFunc(code int, res []byte, e error) getWithHeadersFunc
- func (m *MockHTTPClient) CreatePostJSONFunc(code int, res []byte, e error) postJSONFunc
- func (m *MockHTTPClient) CreatePostJSONWithHeadersFunc(code int, res []byte, e error) postJSONWithHeadersFunc
- func (m *MockHTTPClient) Get(url string, timeout time.Duration) (int, []byte, error)
- func (m *MockHTTPClient) GetWithHeaders(url string, headers map[string]string, timeout time.Duration) (int, []byte, error)
- func (m *MockHTTPClient) PostJSON(url string, body interface{}, timeout time.Duration) (int, []byte, error)
- func (m *MockHTTPClient) PostJSONWithHeaders(url string, headers map[string]string, body interface{}, timeout time.Duration) (int, []byte, error)
- func (m *MockHTTPClient) Reset()
- type RangeStruct
- type SimpleHTTPClient
Constants ¶
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 )
const (
ApplicationJSONUtf8Value = "application/json;charset=utf-8"
)
http content types
Variables ¶
var ( // DefaultBuiltInTransport is the transport for HTTPWithHeaders. DefaultBuiltInTransport *http.Transport // DefaultBuiltInHTTPClient is the http client for HTTPWithHeaders. DefaultBuiltInHTTPClient *http.Client )
Functions ¶
func CheckConnect ¶
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 ¶
ConstructRangeStr wraps the rangeStr as a HTTP Range header value.
func Do ¶
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 ¶
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 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 ¶
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 ¶
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
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) 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.
type RangeStruct ¶
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.