Documentation
¶
Overview ¶
Package https provides a net/http-compatible HTTPS client for TinyGo.
TinyGo ships a stub crypto/tls, so net/http cannot reach https URLs. This package fills that gap by performing TLS through the TLS stack the host OS already provides, and by exposing the familiar net/http surface:
resp, err := https.Get("https://example.com/")
Request and response types are the standard net/http types, so replacing http.Get with https.Get is usually the only change an application needs.
Implementation selection ¶
- standard Go builds delegate to net/http and crypto/tls
- TinyGo on macOS uses Network.framework
- TinyGo on Linux uses vendored mbedTLS
- TinyGo on Windows uses Schannel
- other TinyGo targets return ErrPlatformNotSupported
- go build -tags force_tinygo_logic forces the native backend on host Go, which is how the native code is tested without a TinyGo toolchain
A build never falls back to plaintext or to an unverified connection.
Configuration ¶
Config is deliberately not crypto/tls.Config, because TinyGo builds must not link crypto/tls. It accepts PEM bytes, which every backend understands:
client := https.NewClient(
https.WithRootCAFile("/etc/ssl/private-ca.pem"),
)
The zero value verifies the peer chain and hostname against the system trust store and requires TLS 1.2 or later. WithInsecureSkipVerify disables that and is for testing only.
Errors ¶
Native status codes are mapped onto sentinel errors, so application code can branch identically on every platform:
if errors.Is(err, https.ErrUntrustedRoot) { ... }
Index ¶
- Variables
- func DialPlain(ctx context.Context, host, port string) (net.Conn, error)
- func Get(url string) (*http.Response, error)
- func Head(url string) (*http.Response, error)
- func NewClient(opts ...Option) *http.Client
- func Post(url, contentType string, body io.Reader) (*http.Response, error)
- func PostForm(url string, data url.Values) (*http.Response, error)
- func Upgrade(ctx context.Context, conn net.Conn, host string, cfg *Config) (net.Conn, error)
- type Config
- type Error
- type KeyPair
- type Option
- func WithClientCertificate(certPEM, keyPEM []byte) Option
- func WithInsecureSkipVerify(skip bool) Option
- func WithMinVersion(v Version) Option
- func WithRootCAFile(path string) Option
- func WithRootCAPEM(pemBytes []byte) Option
- func WithRootCAsOnly(only bool) Option
- func WithServerName(name string) Option
- type Transport
- type UpgradableConn
- type Version
Constants ¶
This section is empty.
Variables ¶
var ( ErrHandshakeFailed = errors.New("https: TLS handshake failed") ErrCertificateInvalid = errors.New("https: certificate invalid") ErrCertificateExpired = errors.New("https: certificate expired") ErrHostnameMismatch = errors.New("https: certificate hostname mismatch") ErrUntrustedRoot = errors.New("https: certificate signed by untrusted root") ErrClientCertificateRejected = errors.New("https: client certificate rejected") ErrProtocolVersion = errors.New("https: TLS protocol version not supported") ErrPlatformNotSupported = errors.New("https: platform not supported") // ErrClientCertificateUnsupported reports that the active backend cannot // offer a client certificate. Network.framework needs a SecIdentityRef, // which requires importing the key into a keychain. ErrClientCertificateUnsupported = errors.New("https: client certificates not supported by this backend") )
Sentinel errors. Native backend status codes are mapped onto these so application code can branch with errors.Is regardless of platform.
var DefaultClient = &http.Client{Transport: DefaultTransport}
DefaultClient is an http.Client using DefaultTransport.
var DefaultTransport = &Transport{}
DefaultTransport is used by DefaultClient and the package-level helpers.
var ErrNotUpgradable = errNotUpgradable
ErrNotUpgradable reports that a connection carries no descriptor, so TLS cannot be started on it. Use DialPlain to obtain one that can.
Functions ¶
func DialPlain ¶ added in v1.0.6
DialPlain opens a plaintext TCP connection suitable for Upgrade.
On this path it is an ordinary net.Dial, so the returned connection is a *net.TCPConn with the standard library's deadline and cancellation behavior.
func Get ¶
Get issues a GET request, mirroring net/http.Get.
An error is returned if the request could not be made. Any returned response has a non-nil Body which the caller must close.
func PostForm ¶
PostForm issues a POST with data URL-encoded as the request body, mirroring net/http.PostForm.
func Upgrade ¶ added in v1.0.6
Upgrade starts TLS on a connection that has already carried plaintext, and returns a net.Conn whose bytes are plaintext again.
host is the SNI name and the name verified against the certificate; Config.ServerName overrides it. Verification completes before Upgrade returns, so a returned connection is a verified peer.
On success the returned connection owns conn and closing it closes conn. On failure conn is left open and untouched, so a caller that wants to continue in plaintext still can.
This path is crypto/tls, so it accepts any net.Conn.
Types ¶
type Config ¶
type Config struct {
// RootCAs are additional PEM-encoded trust anchors.
RootCAs [][]byte
// RootCAsOnly ignores the system trust store, trusting only RootCAs.
RootCAsOnly bool
// Certificates are client certificates offered for mutual TLS.
Certificates []KeyPair
// InsecureSkipVerify disables chain and hostname verification.
// It is for testing only.
InsecureSkipVerify bool
// ServerName overrides the SNI name and the name checked against the
// certificate. It defaults to the host in the request URL.
ServerName string
// MinVersion is the minimum acceptable TLS version. Zero means TLS 1.2.
MinVersion Version
// contains filtered or unexported fields
}
Config holds client TLS settings. The zero value verifies the peer chain and hostname against the system trust store and requires TLS 1.2 or later.
Config is deliberately not crypto/tls.Config: the native backends accept PEM bytes, and TinyGo builds must not link crypto/tls.
type Error ¶
type Error struct {
Op string // "dial", "handshake", "read", "write"
Host string
Backend string
Code int // native status code, for diagnosis
Err error
}
Error carries the native status code alongside a mapped sentinel.
type Option ¶
type Option func(*Config)
Option configures a Config.
func WithClientCertificate ¶
WithClientCertificate offers a PEM-encoded client certificate for mutual TLS.
func WithInsecureSkipVerify ¶
WithInsecureSkipVerify disables certificate verification. Testing only.
func WithMinVersion ¶
WithMinVersion sets the minimum TLS version.
func WithRootCAFile ¶
WithRootCAFile adds trust anchors read from a PEM file. A read failure is reported when the Config is first used.
func WithRootCAPEM ¶
WithRootCAPEM adds PEM-encoded trust anchors.
func WithRootCAsOnly ¶
WithRootCAsOnly ignores the system trust store.
func WithServerName ¶
WithServerName overrides the SNI and verified host name.
type Transport ¶
type Transport struct {
// Config holds the TLS settings. Nil means the secure defaults.
Config *Config
// DialTimeout bounds connection setup and the TLS handshake.
// Zero means 30 seconds.
DialTimeout time.Duration
// ResponseTimeout bounds reading the response headers and body.
// Zero means no limit beyond the request context.
ResponseTimeout time.Duration
// MaxIdleConnsPerHost is how many idle connections are kept per
// destination for reuse. Zero means 2.
MaxIdleConnsPerHost int
// IdleConnTimeout is how long a connection may sit idle and still be
// reused. Zero means 20 seconds.
//
// The native path cannot detect that a peer closed an idle connection, so
// this bounds how long a connection is assumed live rather than merely
// releasing memory. Keep it below the server's own idle timeout.
IdleConnTimeout time.Duration
// DisableKeepAlives closes every connection after a single request,
// which is what this package did before connection reuse existed.
DisableKeepAlives bool
// contains filtered or unexported fields
}
Transport is an http.RoundTripper that speaks HTTPS through the host OS TLS stack. In standard Go builds it delegates to net/http.
Transport is safe for concurrent use.
func NewTransport ¶
NewTransport builds a Transport from Config options.
func (*Transport) CloseIdleConnections ¶ added in v1.1.2
func (t *Transport) CloseIdleConnections()
CloseIdleConnections closes connections that are being kept for reuse. It mirrors net/http.Transport.CloseIdleConnections and does not affect requests still in flight.
type UpgradableConn ¶ added in v1.0.6
type UpgradableConn interface {
net.Conn
// Fd returns the underlying descriptor. Upgrade takes ownership of it on
// success, so callers must not close it themselves afterwards.
Fd() int
}
UpgradableConn is a plaintext connection that can be handed to Upgrade.
TinyGo's net package does not implement SyscallConn, so a descriptor cannot be recovered from an arbitrary net.Conn. A connection therefore has to carry its own descriptor, which is what DialPlain returns.
Standard Go builds do not need the descriptor and accept any net.Conn.