forwardedheader

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 5 Imported by: 1

README

test Go Reference

forwarded-header

Parser of HTTP Forward Header defined by RFC 7239: Forwarded HTTP Extension.

Synopsis

package main

import (
	"fmt"
	"net/http"
	"net/netip"
	"os"

	forwardedheader "github.com/shogo82148/forwarded-header"
)

func main() {
	// build the Forwarded Header
	forwarded := []*forwardedheader.Forwarded{
		{
			For: forwardedheader.Node{
				IP: netip.MustParseAddr("192.0.2.60"),
			},
			Proto: "http",
			By: forwardedheader.Node{
				IP: netip.MustParseAddr("203.0.113.43"),
			},
		},
	}
	header := make(http.Header)
	header.Set("Forwarded", forwardedheader.Encode(forwarded))
	header.Write(os.Stdout)
	// Output:
	// Forwarded: by=203.0.113.43;for=192.0.2.60;proto=http

	// parse the Forwarded Header
	parsed, err := forwardedheader.Parse(header.Values("Forwarded"))
	if err != nil {
		panic(err)
	}
	for _, f := range parsed {
		fmt.Println(f)
	}
	// Output:
	// by=203.0.113.43;for=192.0.2.60;proto=http
}

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(f []*Forwarded) string

Encode encodes the Forwarded header.

Types

type Error

type Error struct {
	Message string
	Index   int
	Pos     int
}

Error represents an error that occurred while parsing the Forwarded header.

func (*Error) Error

func (e *Error) Error() string

type Forwarded

type Forwarded struct {
	// By is used to disclose the interface where the request came in to the proxy server.
	By Node

	// For is used to disclose information about the client that initiated the request and subsequent proxies in a chain of proxies.
	For Node

	// Host is used to forward the original value of the "Host" header field.
	Host string

	// Proto is the value of the used protocol type.
	Proto string
}

Forwarded represents the Forwarded header.

func Parse

func Parse(h []string) ([]*Forwarded, error)

Parse parses the Forwarded header.

func (*Forwarded) String

func (f *Forwarded) String() string

String returns the string representation of the Forwarded header. The returned string is a valid Forwarded header.

type Node

type Node struct {
	IP             netip.Addr
	Port           int
	ObfuscatedNode string
	ObfuscatedPort string
}

A Node represents a node identifier in a Forwarded header

func (Node) String

func (n Node) String() string

Jump to

Keyboard shortcuts

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