request

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

DefaultIPSourcePriority 默认IP来源解析顺序(按可信度从高到低)

Functions

func FormDataDecode

func FormDataDecode[T any](r *http.Request, sizes ...int64) (*T, error)

func FormDecode

func FormDecode(r *http.Request, dst any) error

FormDecode 解析 application/x-www-form-urlencoded 编码的 form 数据

func GetHeaderValueFormatInt

func GetHeaderValueFormatInt(header http.Header, fields ...string) (map[string]int, error)

GetHeaderValueFormatInt 获取多个header字段,并转为int

func GetQueryValueFromQuery2Int

func GetQueryValueFromQuery2Int(qs url.Values, fields string, defaultValue ...int) (int, bool)

func GetQueryValueFromRequest2Int

func GetQueryValueFromRequest2Int(r *http.Request, fields string, defaultValue ...int) (int, bool)

func Getip

func Getip(r *http.Request, policys ...IPSource) string

Getip 获取客户端IP noinspection SpellCheckingInspection

func JsonDecode

func JsonDecode[T any](r *http.Request) (T, error)

JsonDecode 解析json数据 值类型,适合小结构体,当字段少于10的时候,缺点是返回时会复制整个结构体

func JsonDecodePtr

func JsonDecodePtr[T interface{ *E }, E any](r *http.Request, target ...T) (T, error)

JsonDecodePtr 解析json数据 处理指针类型(调用方需确保T是指针类型,如 *YourStruct)

func QueryDecode

func QueryDecode(u url.Values, dst any) error

QueryDecode 解析query数据

Types

type File

type File struct {
	Filename string
	Size     int64
	Header   textproto.MIMEHeader
	Body     *bytes.Buffer
}

type Files

type Files []File

type IPParsePolicy

type IPParsePolicy []IPSource

func (IPParsePolicy) GetFromIPSources

func (p IPParsePolicy) GetFromIPSources(sources *IPSources) string

func (IPParsePolicy) GetIP

func (p IPParsePolicy) GetIP(r *http.Request) string

type IPSource

type IPSource string
const (
	RemoteAddr IPSource = "RemoteAddr"

	//noinspection no 标准或广泛使用的头部
	HeaderForwarded      IPSource = "Forwarded"        // RFC 7239
	HeaderXForwardedFor  IPSource = "X-Forwarded-For"  // 事实标准
	HeaderXRealIP        IPSource = "X-Real-IP"        // Nginx等
	HeaderCFConnectingIP IPSource = "CF-Connecting-IP" // CloudFlare
	HeaderTrueClientIP   IPSource = "True-Client-IP"   // Akamai等

	//noinspection no 常见但非标准的变体(保持向后兼容)
	HeaderHTTPClientIP      IPSource = "HTTP_CLIENT_IP"
	HeaderHTTPXForwardedFor IPSource = "HTTP_X_FORWARDED_FOR"
	HeaderXForwardedHost    IPSource = "X-Forwarded-Host"
	HeaderXForwardedProto   IPSource = "X-Forwarded-Proto"

	//noinspection no 特殊代理头部
	HeaderVia IPSource = "Via" // 代理服务器链
)

IPHeader 定义标准的HTTP头部字段名 noinspection no

func (IPSource) String

func (s IPSource) String() string

type IPSources

type IPSources struct {
	RemoteAddr string `json:"remote_addr"` // 直接连接信息

	// 标准头部
	Forwarded      string `json:"forwarded,omitempty"`        // RFC 7239
	XForwardedFor  string `json:"x_forwarded_for,omitempty"`  // 事实标准
	XRealIP        string `json:"x_real_ip,omitempty"`        // Nginx等
	CFConnectingIP string `json:"cf_connecting_ip,omitempty"` // CloudFlare
	TrueClientIP   string `json:"true_client_ip,omitempty"`   // Akamai等

	// 非标准变体(向后兼容)
	HTTPClientIP      string `json:"http_client_ip,omitempty"`
	HTTPXForwardedFor string `json:"http_x_forwarded_for,omitempty"`

	// 其他相关头部
	XForwardedHost  string `json:"x_forwarded_host,omitempty"`
	XForwardedProto string `json:"x_forwarded_proto,omitempty"`
	Via             string `json:"via,omitempty"`
}

func ParseRequestIPSources

func ParseRequestIPSources(r *http.Request) IPSources

ParseRequestIPSources 解析请求中的所有IP相关信息

Jump to

Keyboard shortcuts

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