http

package
v5.19.4 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 24 Imported by: 1,511 modules, 1,157 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package http implements the HTTP transport protocol.

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultClient is the default HTTP client, which uses a net/http client configured
	// with http.DefaultTransport.
	DefaultClient = NewClient(nil)
)

Functions

func NewClient

func NewClient(c *http.Client) transport.Transport

NewClient creates a new client with a custom net/http client. See `InstallProtocol` to install and override default http client. If the net/http client is nil or empty, it will use a net/http client configured with http.DefaultTransport.

Note that for HTTP client cannot distinguish between private repositories and unexistent repositories on GitHub. So it returns `ErrAuthorizationRequired` for both.

func NewClientWithOptions added in v5.7.0

func NewClientWithOptions(c *http.Client, opts *ClientOptions) transport.Transport

NewClientWithOptions returns a new client configured with the provided net/http client and other custom options specific to the client. If the net/http client is nil or empty, it will use a net/http client configured with http.DefaultTransport.

Credentials this client adds where the transport cannot see them are not subject to the redirect stripping described on AuthMethod: a RoundTripper injects after the hop is decided, and Client.Jar is consulted after CheckRedirect, so a domain cookie still follows a redirect to a subdomain the transport counts as another origin. Apply them in an AuthMethod instead if that is not wanted. A CheckRedirect hook set on this client runs alongside the transport's own, but any header it adds when a redirect leaves the repository's origin is discarded the same way.

A RoundTripper is therefore also how to authenticate to a new origin a redirect has moved the repository to: match on the request URL and inject the credential only for that origin, so it is not sent anywhere else. The transport keeps its own CheckRedirect on the copy it makes of this client, so the policy and the origin checks still apply.

None of this applies to a RoundTripper that follows redirects itself: CheckRedirect is not consulted then, so no stripping happens at all.

func NewErr

func NewErr(r *http.Response) error

NewErr returns a new Err based on a http response and closes response body if needed

Types

type AuthMethod

type AuthMethod interface {
	transport.AuthMethod
	SetAuth(r *http.Request)
}

AuthMethod is concrete implementation of common.AuthMethod for HTTP services

Headers SetAuth adds are dropped when a redirect leaves the repository's origin: only the headers the transport sets itself survive that boundary. This applies to non-credential headers too, so an implementation that adds a trace or tenant header loses it on such a hop.

That filter matches header names, not values. An implementation that writes a credential into a name the transport also uses - User-Agent, Host, Accept, Content-Type, Content-Length - has that value carried across the boundary with the name. Such a credential is also sent to the origin and to any proxy in path, so it should not be placed there whether or not a redirect follows.

type BasicAuth

type BasicAuth struct {
	Username, Password string
}

BasicAuth represent a HTTP basic auth

func (*BasicAuth) Name

func (a *BasicAuth) Name() string

Name is name of the auth

func (*BasicAuth) SetAuth

func (a *BasicAuth) SetAuth(r *http.Request)

func (*BasicAuth) String

func (a *BasicAuth) String() string

type ClientOptions added in v5.7.0

type ClientOptions struct {
	// CacheMaxEntries is the max no. of entries that the transport objects
	// cache will hold at any given point of time. It must be a positive integer.
	// Calling `client.addTransport()` after the cache has reached the specified
	// size, will result in the least recently used transport getting deleted
	// before the provided transport is added to the cache.
	CacheMaxEntries int

	// RedirectPolicy controls redirect handling. Supported values are
	// "true", "false", and "initial". The zero value defaults to
	// "initial", matching Git's http.followRedirects default.
	RedirectPolicy RedirectPolicy
}

ClientOptions holds user configurable options for the client.

type Err

type Err struct {
	Response *http.Response
	Reason   string
}

Err is a dedicated error to return errors based on status code

func (*Err) Error

func (e *Err) Error() string

func (*Err) StatusCode

func (e *Err) StatusCode() int

StatusCode returns the status code of the response

type RedirectPolicy added in v5.18.0

type RedirectPolicy string

RedirectPolicy controls how the HTTP transport follows redirects.

The values mirror Git's http.followRedirects config: "true" follows redirects for all requests, "false" treats redirects as errors, and "initial" follows redirects only for the initial /info/refs discovery request. The zero value defaults to "initial".

const (
	FollowInitialRedirects RedirectPolicy = "initial"
	FollowRedirects        RedirectPolicy = "true"
	NoFollowRedirects      RedirectPolicy = "false"
)

type TokenAuth

type TokenAuth struct {
	Token string
}

TokenAuth implements an http.AuthMethod that can be used with http transport to authenticate with HTTP token authentication (also known as bearer authentication).

IMPORTANT: If you are looking to use OAuth tokens with popular servers (e.g. GitHub, Bitbucket, GitLab) you should use BasicAuth instead. These servers use basic HTTP authentication, with the OAuth token as user or password. Check the documentation of your git server for details.

func (*TokenAuth) Name

func (a *TokenAuth) Name() string

Name is name of the auth

func (*TokenAuth) SetAuth

func (a *TokenAuth) SetAuth(r *http.Request)

func (*TokenAuth) String

func (a *TokenAuth) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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