httphost

package
v1.2.116 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Example
package main

import (
	"log"
	"net/http"

	http_ "github.com/searKing/golang/go/net/http"
	"github.com/searKing/golang/go/net/http/httphost"

	_ "github.com/searKing/golang/go/net/resolver/passthrough"
)

func main() {
	req, _ := http.NewRequest("GET", "http://example.com", nil)
	proxy := &httphost.Host{
		HostTarget: "127.0.0.1",
	}
	req = req.WithContext(httphost.WithHost(req.Context(), proxy))

	err := http_.HostFuncFromContext(req)
	if err != nil {
		log.Fatal(err)
	}

	_, err = http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTargetUrl

func ParseTargetUrl(host string) (*url.URL, error)

func WithHost

func WithHost(ctx context.Context, host *Host) context.Context

WithHost returns a new context based on the provided parent ctx. HTTP client requests made with the returned context will use the provided host hooks

Types

type Host

type Host struct {
	// HostTarget is as like gRPC Naming for host service discovery, with Host in TargetUrl replaced if not empty.
	HostTarget string
	// resolve HostTarget to host and replace host if HostTarget resolved
	ReplaceHostInRequest bool

	// HostTargetAddrResolved is the host's addr resolved and picked from resolver.
	HostTargetAddrResolved resolver.Address
}

Host specifies TargetUrl and HostTarget to return a dynamic host.

func ContextHost

func ContextHost(ctx context.Context) *Host

ContextHost returns the Host associated with the provided context. If none, it returns nil.

Jump to

Keyboard shortcuts

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