http

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package http handles interacting with HTTP clients and servers.

This package allows you to make HTTP requests through an HTTP proxy even those using an untrusted certificate (eg: signed by an internal CA).

Note that only HTTP Basic authentication is supported for proxy communication at this time.

Index

Constants

View Source
const (
	ErrParseURLFailureCode      = 2251
	ErrProxyFailureCode         = 2252
	ErrCreateRequestFailureCode = 2253
	ErrDoRequestFailureCode     = 2254
	ErrReadResponseFailureCode  = 2255
)

Object error codes (2251-2500)

View Source
const (
	// StatusPanicRecovery indicates that a panic was detected in code but automatic recovery prevented the
	// application from crashing.
	StatusPanicRecovery = 599
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// ClientCertificates is a list of certificates to pass for client authentication.
	ClientCertificates []tls.Certificate

	// DisableSSLVerification disables HTTPS certificate verification when connecting to a server. You should
	// only do this if you are *really* sure. Otherwise, add the server's certificate to the RootCertificates
	// pool.
	DisableSSLVerification bool

	// RootCertificates is a pool of root CA certificates to trust.
	RootCertificates *crypto.CertificatePool
	// contains filtered or unexported fields
}

Client represents an HTTP client.

func NewClient

func NewClient(proxyConfig ProxyConfig) *Client

NewClient returns a new HTTP client object.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, url string, headers map[string]string, body []byte) (
	*http.Response, []byte, error)

Delete performs a DELETE request for the given URL and returns the raw body byte array.

The following errors are returned by this function: ErrCreateRequestFailure, ErrDoRequestFailure, ErrReadResponseFailure, ErrStatusCodeNotOK

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, headers map[string]string) (
	*http.Response, []byte, error)

Get performs a GET request for the given URL and returns the raw body byte array.

The following errors are returned by this function: ErrCreateRequestFailure, ErrDoRequestFailure, ErrReadResponseFailure, ErrStatusCodeNotOK

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, url string, body io.Reader) (
	*http.Client, *http.Request, error)

NewRequest creates a new HTTP request object using any configured proxy information.

Note that only HTTP Basic authentication is supported for proxied requests.

The following errors are returned by this function: ErrParseUrlFailure, ErrProxyFailure, ErrCreateRequestFailure

func (*Client) Options

func (c *Client) Options(ctx context.Context, url string, headers map[string]string) (
	*http.Response, []byte, error)

Options performs an OPTIONS request for the given URL and returns the raw body byte array.

The following errors are returned by this function: ErrCreateRequestFailure, ErrDoRequestFailure, ErrReadResponseFailure, ErrStatusCodeNotOK

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, url string, headers map[string]string, body []byte) (
	*http.Response, []byte, error)

Patch performs a PATCH request for the given URL and returns the raw body byte array.

The following errors are returned by this function: ErrCreateRequestFailure, ErrDoRequestFailure, ErrReadResponseFailure, ErrStatusCodeNotOK

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, headers map[string]string, body []byte) (
	*http.Response, []byte, error)

Post performs a POST request for the given URL and returns the raw body byte array.

The following errors are returned by this function: ErrCreateRequestFailure, ErrDoRequestFailure, ErrReadResponseFailure, ErrStatusCodeNotOK

func (*Client) Put

func (c *Client) Put(ctx context.Context, url string, headers map[string]string, body []byte) (
	*http.Response, []byte, error)

Put performs a PUT request for the given URL and returns the raw body byte array.

The following errors are returned by this function: ErrCreateRequestFailure, ErrDoRequestFailure, ErrReadResponseFailure, ErrStatusCodeNotOK

type ErrCreateRequestFailure

type ErrCreateRequestFailure struct {
	Method string
	URL    string
	Err    error
}

ErrCreateRequestFailure occurs when there is an error creating a new HTTP request.

func (*ErrCreateRequestFailure) Code

func (e *ErrCreateRequestFailure) Code() int

Code returns the corresponding error code.

func (*ErrCreateRequestFailure) Error

func (e *ErrCreateRequestFailure) Error() string

Error returns the string version of the error.

func (*ErrCreateRequestFailure) InternalError

func (e *ErrCreateRequestFailure) InternalError() error

InternalError returns the internal standard error object if there is one or nil if none is set.

type ErrDoRequestFailure

type ErrDoRequestFailure struct {
	Method string
	URL    string
	Err    error
}

ErrDoRequestFailure occurs when there is an error executing a request.

func (*ErrDoRequestFailure) Code

func (e *ErrDoRequestFailure) Code() int

Code returns the corresponding error code.

func (*ErrDoRequestFailure) Error

func (e *ErrDoRequestFailure) Error() string

Error returns the string version of the error.

func (*ErrDoRequestFailure) InternalError

func (e *ErrDoRequestFailure) InternalError() error

InternalError returns the internal standard error object if there is one or nil if none is set.

type ErrParseURLFailure

type ErrParseURLFailure struct {
	URL string
	Err error
}

ErrParseURLFailure occurs when there is an error parsing a URL.

func (*ErrParseURLFailure) Code

func (e *ErrParseURLFailure) Code() int

Code returns the corresponding error code.

func (*ErrParseURLFailure) Error

func (e *ErrParseURLFailure) Error() string

Error returns the string version of the error.

func (*ErrParseURLFailure) InternalError

func (e *ErrParseURLFailure) InternalError() error

InternalError returns the internal standard error object if there is one or nil if none is set.

type ErrProxyFailure

type ErrProxyFailure struct {
	URL string
	Err error
}

ErrProxyFailure occurs when there is an error retrieving a proxy URL.

func (*ErrProxyFailure) Code

func (e *ErrProxyFailure) Code() int

Code returns the corresponding error code.

func (*ErrProxyFailure) Error

func (e *ErrProxyFailure) Error() string

Error returns the string version of the error.

func (*ErrProxyFailure) InternalError

func (e *ErrProxyFailure) InternalError() error

InternalError returns the internal standard error object if there is one or nil if none is set.

type ErrReadResponseFailure

type ErrReadResponseFailure struct {
	Err error
}

ErrReadResponseFailure occurs when there is an error reading a response body.

func (*ErrReadResponseFailure) Code

func (e *ErrReadResponseFailure) Code() int

Code returns the corresponding error code.

func (*ErrReadResponseFailure) Error

func (e *ErrReadResponseFailure) Error() string

Error returns the string version of the error.

func (*ErrReadResponseFailure) InternalError

func (e *ErrReadResponseFailure) InternalError() error

InternalError returns the internal standard error object if there is one or nil if none is set.

type ErrStatusCodeNotOK

type ErrStatusCodeNotOK struct {
	StatusCode int
}

ErrStatusCodeNotOK occurs when an HTTP status code of 400 or greater is returned.

func (*ErrStatusCodeNotOK) Code

func (e *ErrStatusCodeNotOK) Code() int

Code returns the corresponding error code.

func (*ErrStatusCodeNotOK) Error

func (e *ErrStatusCodeNotOK) Error() string

Error returns the string version of the error.

func (*ErrStatusCodeNotOK) InternalError

func (e *ErrStatusCodeNotOK) InternalError() error

InternalError returns the internal standard error object if there is one or nil if none is set.

type ProxyConfig

type ProxyConfig struct {
	httpproxy.Config

	// HTTPProxyUser is the username for proxy authentication for HTTP URLs.
	HTTPProxyUser string

	// HTTPProxyPass is the password for proxy authentication for HTTP URLs.
	HTTPProxyPass string

	// HTTPSProxyUser is the username for proxy authentication for HTTPS URLs.
	HTTPSProxyUser string

	// HTTPSProxyPass is the password for proxy authentication for HTTPS URLs.
	HTTPSProxyPass string
}

ProxyConfig holds the full configuration for proxy settings used by HTTP clients.

Jump to

Keyboard shortcuts

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