web

package standard library
go1.23.3 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Overview

Package web defines minimal helper routines for accessing HTTP/HTTPS resources without requiring external dependencies on the net package.

If the cmd_go_bootstrap build tag is present, web avoids the use of the net package and returns errors for all network operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableTestHooks added in go1.20

func DisableTestHooks()

func EnableTestHooks added in go1.20

func EnableTestHooks(interceptors []Interceptor) error

func GetBytes added in go1.13

func GetBytes(u *url.URL) ([]byte, error)

GetBytes returns the body of the requested resource, or an error if the response status was not http.StatusOK.

GetBytes is a convenience wrapper around Get and Response.Err.

func IsLocalHost added in go1.20

func IsLocalHost(u *url.URL) bool

IsLocalHost reports whether the given URL refers to a local (loopback) host, such as "localhost" or "127.0.0.1:8080".

func Join added in go1.13

func Join(u *url.URL, path string) *url.URL

Join returns the result of adding the slash-separated path elements to the end of u's path.

func OpenBrowser

func OpenBrowser(url string) (opened bool)

OpenBrowser attempts to open the requested URL in a web browser.

Types

type HTTPError

type HTTPError struct {
	URL        string // redacted
	Status     string
	StatusCode int
	Err        error  // underlying error, if known
	Detail     string // limited to maxErrorDetailLines and maxErrorDetailBytes
}

An HTTPError describes an HTTP error response (non-200 result).

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) Is added in go1.13

func (e *HTTPError) Is(target error) bool

func (*HTTPError) Unwrap added in go1.14

func (e *HTTPError) Unwrap() error

type Interceptor added in go1.20

type Interceptor struct {
	Scheme   string
	FromHost string
	ToHost   string
	Client   *http.Client
}

type Response added in go1.13

type Response struct {
	URL        string // redacted
	Status     string
	StatusCode int
	Header     map[string][]string
	Body       io.ReadCloser // Either the original body or &errorDetail.
	// contains filtered or unexported fields
}

func Get

func Get(security SecurityMode, u *url.URL) (*Response, error)

Get returns the body of the HTTP or HTTPS resource specified at the given URL.

If the URL does not include an explicit scheme, Get first tries "https". If the server does not respond under that scheme and the security mode is Insecure, Get then tries "http". The URL included in the response indicates which scheme was actually used, and it is a redacted URL suitable for use in error messages.

For the "https" scheme only, credentials are attached using the cmd/go/internal/auth package. If the URL itself includes a username and password, it will not be attempted under the "http" scheme unless the security mode is Insecure.

Get returns a non-nil error only if the request did not receive a response under any applicable scheme. (A non-2xx response does not cause an error.)

func (*Response) Err added in go1.13

func (r *Response) Err() error

Err returns an *HTTPError corresponding to the response r. If the response r has StatusCode 200 or 0 (unset), Err returns nil. Otherwise, Err may read from r.Body in order to extract relevant error detail.

type SecurityMode

type SecurityMode int

SecurityMode specifies whether a function should make network calls using insecure transports (eg, plain text HTTP). The zero value is "secure".

const (
	SecureOnly      SecurityMode = iota // Reject plain HTTP; validate HTTPS.
	DefaultSecurity                     // Allow plain HTTP if explicit; validate HTTPS.
	Insecure                            // Allow plain HTTP if not explicitly HTTPS; skip HTTPS validation.
)

Jump to

Keyboard shortcuts

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