http2

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 10 Imported by: 6 modules, 6 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 http2 provides helpers for HTTP/2.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

Server is an HTTP server accepting both regular and proxied, both HTTP/1 and HTTP/2 connections.

HTTP/2 is negotiated using TLS ALPN, either directly via a tls.Conn, either indirectly via the PROXY protocol. When the PROXY protocol is used, the TLS-terminating proxy in front of the server must be configured to accept the "h2" TLS ALPN protocol.

The server is closed when the http.Server is.

Example
package main

import (
	"log"
	"net"
	"net/http"
	"time"

	"github.com/pires/go-proxyproto"
	h2proxy "github.com/pires/go-proxyproto/helper/http2"
)

func main() {
	ln, err := net.Listen("tcp", "localhost:80")
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	proxyLn := &proxyproto.Listener{
		Listener: ln,
		// This server accepts both proxied and non-proxied connections, so the
		// PROXY header is optional. Restrict who may send one before exposing
		// such a listener to untrusted clients (see proxyproto.DefaultPolicy).
		ConnPolicy: func(proxyproto.ConnPolicyOptions) (proxyproto.Policy, error) {
			return proxyproto.USE, nil
		},
	}

	server := h2proxy.NewServerWithConfig(&http.Server{
		ReadHeaderTimeout: 5 * time.Second,
		Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
			_, _ = w.Write([]byte("Hello world!\n"))
		}),
	}, nil)
	if err := server.Serve(proxyLn); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}

func NewServer deprecated

func NewServer(h1 *http.Server, h2 *http2.Server) *Server

NewServer creates an HTTP server using the legacy x/net HTTP/2 configuration. A nil h2 is equivalent to a zero http2.Server.

Deprecated: Use NewServerWithConfig with net/http.HTTP2Config instead.

func NewServerWithConfig added in v0.16.0

func NewServerWithConfig(h1 *http.Server, h2 *http.HTTP2Config) *Server

NewServerWithConfig creates an HTTP server using standard-library HTTP/2. A non-nil h2 overrides h1.HTTP2. Otherwise h1.HTTP2 is preserved. Configure h1 before calling NewServerWithConfig; the helper installs its context hooks and enables HTTP/1, TLS HTTP/2, and unencrypted HTTP/2.

func (*Server) Serve

func (srv *Server) Serve(ln net.Listener) error

Serve accepts incoming connections on the listener ln.

Jump to

Keyboard shortcuts

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