http

package module
v0.0.0-...-f31a14c Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

http is listening on two sockets if the server is set to use a wildcard bind to accept both IPv4 and IPv6 traffic. This enables the proper use of ListenAndServe from net/http of the standard library in a portable approach.

By default, OpenBSD, NetBSD, DragonflyBSD and others do not route IPv4 traffic to AF_INET6 sockets. This default behavior intentionally violates RFC 2553 for security reasons.

It is recommended to listen to two sockets, one for AF_INET and another for AF_INET6, when you would like to accept both IPv4 and IPv6 traffic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServeIPv4

func ListenAndServeIPv4(config *http.Server) error

ListenAndServeIPv4 uses IPv4only.

Example

ListenAndServeIPv4 with specific IPv4 for http on non-default port 8080.

package main

import (
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	if err := httpd.ListenAndServeIPv4(&http.Server{Addr: "192.0.2.10:8080"}); err != nil {
		panic(err)
	}
}
Output:

func ListenAndServeIPv4TLS

func ListenAndServeIPv4TLS(config *http.Server, certFile, keyFile string) error

ListenAndServeIPv4TLS uses IPv4only and uses TLS.

func ListenAndServeIPv6

func ListenAndServeIPv6(config *http.Server) error

ListenAndServeIPv6 uses IPv6only.

Example

ListenAndServeIPv6 with specific IPv6 for http on non-default port 8080.

package main

import (
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	if err := httpd.ListenAndServeIPv6(&http.Server{Addr: "[2001:db8::1010]:8080"}); err != nil {
		panic(err)
	}
}
Output:

func ListenAndServeIPv6TLS

func ListenAndServeIPv6TLS(config *http.Server, certFile, keyFile string) error

ListenAndServeIPv6TLS uses IPv6only and uses TLS.

Types

type Server

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

Server holds the *http.Server config and all dual-stack information.

func New

func New(config *http.Server) *Server

New taks a *http.Server config and returns a *Server pointer.

func (*Server) Bind

func (s *Server) Bind(addr string) error

Bind is primarily set via *http.Server config, but can be manipulated by passing a addr:port string (192.0.2.10:8443 / [2001:db8::1010]:8080) which disables the other network stack that the address is not attributable to.

Example

Bind to IPv6 localhost only on port 8080.

package main

import (
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	web := httpd.New(&http.Server{})

	if err := web.Bind("[::1]:8080"); err != nil {
		panic(err)
	}

	if err := web.ListenAndServe(); err != nil {
		panic(err)
	}
}
Output:

func (*Server) DisableIPv4

func (s *Server) DisableIPv4()

DisableIPv4 turns off the IPv4 listening and serve.

Example
package main

import (
	"fmt"
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	web := httpd.New(&http.Server{})

	web.DisableIPv4()
	fmt.Println(web.Stack())
}
Output:

false true

func (*Server) DisableIPv6

func (s *Server) DisableIPv6()

DisableIPv6 turns off the IPv6 listening and serve.

Example
package main

import (
	"fmt"
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	web := httpd.New(&http.Server{})

	web.DisableIPv6()
	fmt.Println(web.Stack())
}
Output:

true false

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe

https://pkg.go.dev/net/http#ListenAndServe
Example

ListenAndServe with wildcard on http default port 80.

package main

import (
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	web := httpd.New(&http.Server{})

	if err := web.ListenAndServe(); err != nil {
		panic(err)
	}
}
Output:

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS() error

ListenAndServeTLS

https://pkg.go.dev/net/http#ListenAndServeTLS

func (*Server) Stack

func (s *Server) Stack() (bool, bool)

Stack returns the dual-stack state with two boolean values respectively IPv4, IPv6.

Example
package main

import (
	"fmt"
	"net/http"

	httpd "catinello.eu/http"
)

func main() {
	web := httpd.New(&http.Server{})

	fmt.Println(web.Stack())
}
Output:

true true

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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