httpx

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package httpx includes various HTTP utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(w http.ResponseWriter, res *http.Response) error

Copy writes the http.Response to the http.ResponseWriter. Do not modify the body of the http.Response after calling this methog. http.ResponseWriter is assumed to be empty.

func DecodeDeepObject

func DecodeDeepObject(values url.Values, obj any) error

DecodeDeepObject decodes an object from a string representation with bracketed nested fields names. For example, color[R]=100&color[G]=200&color[B]=150 .

func EncodeDeepObject

func EncodeDeepObject(obj any) (url.Values, error)

EncodeDeepObject encodes an object into string representation with bracketed nested fields names. For example, color[R]=100&color[G]=200&color[B]=150 .

func ExtractPathArguments

func ExtractPathArguments(spec string, path string) (args url.Values, err error)

ExtractPathArguments extracts path arguments from a URL or path given a spec such as /obj/{id}/{} that identified them. Unnamed args are assigned the names path1, path2, etc.

func FillPathArguments

func FillPathArguments(u string) (resolved string, err error)

FillPathArguments transfers query arguments into path arguments, if present.

func InsertPathArguments

func InsertPathArguments(u string, values QArgs) string

InsertPathArguments fills URL path arguments such as {arg} from the named value. If the argument is not named, e.g. {}, then a default name of path1, path2, etc. is assumed.

func IsLocalhostAddress

func IsLocalhostAddress(r *http.Request) bool

IsLocalhostAddress checks if the request's remote address is the local host.

func IsPrivateIPAddress

func IsPrivateIPAddress(r *http.Request) bool

IsPrivateIPAddress checks if the request's remote address is on the local subnets, e.g. 192.168.X.X or 10.X.X.X.

func JoinHostAndPath

func JoinHostAndPath(host string, path string) string

JoinHostAndPath combines the path shorthand with a hostname.

func MustNewRequest

func MustNewRequest(method string, url string, body any) *http.Request

MustNewRequest wraps NewRequestWithContext with the background context. It panics on error.

func MustNewRequestWithContext

func MustNewRequestWithContext(ctx context.Context, method string, url string, body any) *http.Request

MustNewRequestWithContext returns a new http.Request given a method, URL, and optional body. It panics on error. Arguments of type io.Reader, io.ReadCloser, []byte and string are serialized in binary form. url.Values and QArgs are serialized as form data. All other types are serialized as JSON. The Content-Type Content-Length headers will be set to match the body if they can be determined and unless already set.

func NewRequest

func NewRequest(method string, url string, body any) (*http.Request, error)

NewRequest wraps NewRequestWithContext with the background context.

func NewRequestWithContext

func NewRequestWithContext(ctx context.Context, method string, url string, body any) (*http.Request, error)

NewRequestWithContext returns a new http.Request given a method, URL, and optional body. Arguments of type io.Reader, io.ReadCloser, []byte and string are serialized in binary form. url.Values and QArgs are serialized as form data. All other types are serialized as JSON. The Content-Type Content-Length headers will be set to match the body if they can be determined and unless already set.

func ParseRequestBody

func ParseRequestBody(r *http.Request, data any) error

ParseRequestBody parses the body of an incoming request and populates the fields of a data object. It supports JSON and URL-encoded form data content types. Use json tags to designate the name of the argument to map to each field.

func ParseRequestData

func ParseRequestData(r *http.Request, data any) error

ParseRequestData parses the body and query arguments of an incoming request and populates the fields of a data object. Use json tags to designate the name of the argument to map to each field. An argument name can be hierarchical using either notation "a[b][c]" or "a.b.c", in which case it is read into the corresponding nested field.

func ParseURL

func ParseURL(rawURL string) (canonical *url.URL, err error)

ParseURL returns a canonical version of the parsed URL with the scheme and port filled in if omitted.

func ResolveURL

func ResolveURL(base string, relative string) (resolved string, err error)

ResolveURL resolves a URL in relation to the endpoint's base path.

func SetRequestBody

func SetRequestBody(r *http.Request, body any) error

SetRequestBody sets the body of the request. Arguments of type io.Reader, io.ReadCloser, []byte and string are serialized in binary form. url.Values and QArgs are serialized as form data. All other types are serialized as JSON. The Content-Type Content-Length headers will be set to match the body if they can be determined and unless already set.

Types

type BodyReader

type BodyReader struct {
	// contains filtered or unexported fields
}

BodyReader is used to wrap bytes in a closer+reader while allowing access to the underlying bytes.

func NewBodyReader

func NewBodyReader(b []byte) *BodyReader

NewBodyReader creates a new closer+reader for the request body while allowing access to the underlying bytes.

func (*BodyReader) Bytes

func (br *BodyReader) Bytes() []byte

Bytes gives access to the underlying bytes.

func (*BodyReader) Close

func (br *BodyReader) Close() error

Read implements the io.Closer interface.

func (*BodyReader) Read

func (br *BodyReader) Read(p []byte) (n int, err error)

Read implements the io.Reader interface.

func (*BodyReader) Reset

func (br *BodyReader) Reset()

Reset resets the underlying reader to the beginning.

type DefragRequest

type DefragRequest struct {
	// contains filtered or unexported fields
}

DefragRequest merges together multiple fragments back into a single HTTP request

func NewDefragRequest

func NewDefragRequest() *DefragRequest

NewDefragRequest creates a new request integrator.

func (*DefragRequest) Add

func (st *DefragRequest) Add(r *http.Request) error

Add a fragment to be integrated.

func (*DefragRequest) Integrated

func (st *DefragRequest) Integrated() (integrated *http.Request, err error)

Integrated indicates if all the fragments have been collected and if so returns them as a single HTTP request.

func (*DefragRequest) LastActivity

func (st *DefragRequest) LastActivity() time.Duration

LastActivity indicates how long ago was the last fragment added.

type DefragResponse

type DefragResponse struct {
	// contains filtered or unexported fields
}

DefragResponse merges together multiple fragments back into a single HTTP response

func NewDefragResponse

func NewDefragResponse() *DefragResponse

NewDefragResponse creates a new response integrator.

func (*DefragResponse) Add

func (st *DefragResponse) Add(r *http.Response) error

Add a fragment to be integrated.

func (*DefragResponse) Integrated

func (st *DefragResponse) Integrated() (integrated *http.Response, err error)

Integrated indicates if all the fragments have been collected and if so returns them as a single HTTP response.

func (*DefragResponse) LastActivity

func (st *DefragResponse) LastActivity() time.Duration

LastActivity indicates how long ago was the last fragment added.

type FragRequest

type FragRequest struct {
	// contains filtered or unexported fields
}

FragRequest transforms an HTTP request into one or more fragments that do not exceed a given size. Fragmenting is needed because NATS imposes a maximum size for messages

func NewFragRequest

func NewFragRequest(r *http.Request, fragmentSize int64) (*FragRequest, error)

NewFragRequest creates a new request fragmentor

func (*FragRequest) Fragment

func (fr *FragRequest) Fragment(index int) (f *http.Request, err error)

Fragment returns the 1-indexed fragment

func (*FragRequest) N

func (fr *FragRequest) N() int

N is the number of fragments

type FragResponse

type FragResponse struct {
	// contains filtered or unexported fields
}

FragResponse transforms an HTTP response into one or more fragments that do not exceed a given size. Fragmenting is needed because NATS imposes a maximum size for messages

func NewFragResponse

func NewFragResponse(r *http.Response, fragmentSize int64) (*FragResponse, error)

NewFragResponse creates a new response fragmentor

func (*FragResponse) Fragment

func (fr *FragResponse) Fragment(index int) (f *http.Response, err error)

Fragment returns the 1-indexed fragment

func (*FragResponse) N

func (fr *FragResponse) N() int

N is the number of fragments

type QArgs

type QArgs map[string]any

QArgs faciliates the creation of URL query arguments for the common case where there is only one value per key. Values of any type are converted to a string. Keys are case-sensitive.

Usage:

u := "https://example.com/path?"+QArgs{
	"hello": "World",
	"number": 6,
	"id", key
}.Encode()

func (QArgs) Encode

func (q QArgs) Encode() string

Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.

func (QArgs) String

func (q QArgs) String() string

String encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.

func (QArgs) URLValues

func (q QArgs) URLValues() url.Values

URLValues generates a standard URL values map.

type ResponseRecorder

type ResponseRecorder struct {
	// contains filtered or unexported fields
}

ResponseRecorder is used to record HTTP responses.

func NewResponseRecorder

func NewResponseRecorder() *ResponseRecorder

NewResponseRecorder creates a new response recorder. The recorder is modifiable even after the body was written because it keeps the entire response in memory.

func (*ResponseRecorder) Clear

func (rr *ResponseRecorder) Clear()

Clear deletes all the content recorded to date.

func (*ResponseRecorder) ContentLength

func (rr *ResponseRecorder) ContentLength() int

ContentLength returns the total number of bytes written to the body of the response.

func (*ResponseRecorder) Header

func (rr *ResponseRecorder) Header() http.Header

Header enables setting headers. It implements the http.ResponseWriter interface.

func (*ResponseRecorder) Result

func (rr *ResponseRecorder) Result() *http.Response

Result returns the response generated by the recorder.

func (*ResponseRecorder) StatusCode

func (rr *ResponseRecorder) StatusCode() int

StatusCode returns the status code set for the response. If unset, the default is http.StatusOK 200.

func (*ResponseRecorder) Write

func (rr *ResponseRecorder) Write(b []byte) (int, error)

Write writes bytes to the body of the response. It implements the http.ResponseWriter interface.

func (*ResponseRecorder) WriteHeader

func (rr *ResponseRecorder) WriteHeader(statusCode int)

WriteHeader writes the header to the response. It implements the http.ResponseWriter interface.

Jump to

Keyboard shortcuts

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