server

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Overview

Package server implements Web Packager HTTP Server (webpkgserver).

If you are interested in building and running webpkgserver as a binary, see cmd/webpkgserver/README.md instead.

Basic Use

FromTOMLConfig creates a Server that can be used out of the box:

c, err := tomlconfig.ReadFromFile("your.toml")
if err != nil {
	log.Fatal(err)
}
s, err := server.FromTOMLConfig(c)
if err != nil {
	log.Fatal(err)
}
s.ListenAndServe() // Use ListenAndServeTLS to enable TLS.

Define Custom Parameters

If you want to define custom parameters/sections in TOML, define a struct with a tomlconfig.Config embedded:

type Config struct {
	tomlconfig.Config
	Foo FooConfig
}

With the example above, your TOML config can contain the [Foo] section in addition to the standard ones.

You need to call toml.Unmarshal by yourself. Also be sure to call Verify on the tomlconfig.Config embedding; otherwise FromTOMLConfig may panic with invalid config values.

data, err := ioutil.ReadFile("your.toml")
if err != nil {
	log.Fatal(err)
}
var c Config
if err := toml.Unmarshal(data, &c); err != nil {
	log.Fatal(err)
}
if err := c.Verify(); err != nil {
	log.Fatal(err)
}
s, err := server.FromTOMLConfig(&c.Config)
if err != nil {
	log.Fatal(err)
}
// ... (mutate s.Packager and s.CertManager to apply FooConfig settings)
s.ListenAndServe() // Use ListenAndServeTLS to enable TLS.

Handler Internals

Handler is composed of three child handlers: doc handler, cert handler, and validity handler.

The doc handler produces a signed exchange for the given URL. The request looks like:

/priv/doc/https://example.com/index.html
    -- or --
/priv/doc?sign=https%3A%2F%2Fexample.com%2Findex.html

where "/priv/doc" and "sign" can be customized through DocPath and SignParam in tomlconfig.ServerConfig respectively.

The cert handler serves AugmentedChains in the application/cert-chain+cbor format. The request looks like:

/webpkg/cert/47DEQpj8HBSa-_TImW+5JCeuQeRkm5NMpJWZG3hSuFUK

where "/webpkg/cert" can be customized through CertPath and "47DEQpj8..." is an example of unique stable identifier, which is RawChain.Digest of the served AugmentedChain.

The validity handler serves validity data. Currently, it constantly returns an empty CBOR map (a single byte of 0xa0), which is interpreted as "no update available." The request looks like:

/webpkg/validity

where "/webpkg/validity" can be customized through ValidityPath. It does not take any argument, such as the document URL, at this moment.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Packager is used to produce signed exchanges. ExchangeFactory should
	// be an ExchangeMetaFactory set with CertManager (the following field)
	// to keep the signing certificate and the cert-url parameter consistent
	// with this Handler.
	Packager *webpackager.Packager

	// CertManager provides the AugmentedChain to serve from this Handler.
	CertManager *certmanager.Manager

	// AllowTestCert indicates if it's ok to allow test certs.
	AllowTestCert bool

	// ServerConfig specifies the endpoints. All fields must contain a valid
	// value as described in cmd/webpkgserver/webpkgserver.example.toml.
	tomlconfig.ServerConfig
}

Config holds the parameters to NewHandler.

type ExchangeConfig

type ExchangeConfig struct {
	// Version specifies the signed exchange version. If Version is empty,
	// ExchangeMetaFactory uses exchange.DefaultVersion.
	Version version.Version

	// MIRecordSize specifies Merkle Integrity record size. The value must
	// be positive, or zero to use exchange.DefaultMIRecordSize. It must not
	// exceed 16384 (16 KiB) to be compliant with the specification.
	MIRecordSize int

	// CertManager specifies an AugmentedChain provider. ExchangeMetaFactory
	// does not start or stop this CertManager automatically; the caller is
	// responsible to make the CertManager active before ExchangeMetaFactory
	// receives the first call of Get. CertManager may not be nil.
	CertManager *certmanager.Manager

	// CertURLBase specifies the base URL for the cert-url parameter in the
	// signature. ExchangeMetaFactory appends RawChain.Digest to CertURLBase,
	// as a stable unique identifier of the AugmentedChain, to construct the
	// cert-url parameter. CertURLBase may not be nil.
	CertURLBase *url.URL

	// PrivateKey specifies the private key used for signing. PrivateKey may
	// not be nil.
	PrivateKey crypto.PrivateKey

	// KeepNonSXGPreloads instructs Factory to include preload link headers
	// that don't have the corresponding allowed-alt-sxg with a valid
	// header-integrity.
	KeepNonSXGPreloads bool
}

ExchangeConfig configures ExchangeMetaFactory.

type ExchangeMetaFactory

type ExchangeMetaFactory struct {
	ExchangeConfig
}

ExchangeMetaFactory is an exchange.FactoryProvider designed to be used with Handler.

func NewExchangeMetaFactory

func NewExchangeMetaFactory(c ExchangeConfig) *ExchangeMetaFactory

NewExchangeMetaFactory creates a new ExchangeMetaFactory.

func (*ExchangeMetaFactory) Get

Get returns a new exchange.Factory set with the current AugmentedChain from e.CertManager.

type Handler

type Handler struct {
	Config
	// contains filtered or unexported fields
}

Handler handles HTTP requests. See the package GoDoc for details.

func NewHandler

func NewHandler(c Config) *Handler

NewHandler creates and initializes a new Handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler.

type Server

type Server struct {
	*http.Server
	Config
}

Server encapsulates http.Server and Config so it can start and stop CertManager automatically in Serve.

func FromTOMLConfig

func FromTOMLConfig(c *tomlconfig.Config) (*Server, error)

FromTOMLConfig creates and initializes a Server from TOML config.

func NewServer

func NewServer(s *http.Server, c Config) *Server

NewServer creates a new Server. s.Handler is replaced with NewHandler(c).

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe wraps s.Server.ListenAndServe to start/stop s.CertManager automatically.

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS wraps s.Server.ListenAndServeTLS to start/stop s.CertManager automatically.

func (*Server) Serve

func (s *Server) Serve(l net.Listener) error

Serve wraps s.Server.Serve to start/stop s.CertManager automatically.

func (*Server) ServeTLS

func (s *Server) ServeTLS(l net.Listener, certFile, keyFile string) error

ServeTLS wraps s.Server.ServeTLS to start/stop s.CertManager automatically.

Directories

Path Synopsis
Package tomlconfig defines the TOML config for Web Packager HTTP Server.
Package tomlconfig defines the TOML config for Web Packager HTTP Server.

Jump to

Keyboard shortcuts

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