http2

package
v0.0.0-...-1d624c0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 9 Imported by: 0

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"

	"github.com/MarkdownNinja/stdx/proxyproto"
	h2proxy "github.com/MarkdownNinja/stdx/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,
	}

	server := h2proxy.NewServer(&http.Server{
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *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

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

NewServer creates a new HTTP server.

A nil h2 is equivalent to a zero http2.Server.

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