httpx

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpx 提供管理 API 共用的 JSON 读写与错误响应。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP 返回请求的客户端地址。对端为可信代理时,从 X-Forwarded-For(自右向左跳过可信代理)与 X-Real-IP 中取得真实地址; 否则返回对端地址。

两个请求头只有一个时直接使用。两个都有且不一致时,说明其中一个是客户端伪造后被代理原样转发的: 只追加 X-Forwarded-For 的代理(haproxy option forwardfor、Caddy 等)会原样转发客户端自带的 X-Real-IP, 只写 X-Real-IP 的代理(只配置了 proxy_set_header X-Real-IP 的 nginx)会原样转发客户端自带的 X-Forwarded-For。 单看一次请求无法分辨是哪一种,因此:

  • 一个是内网地址、另一个是公网地址时取公网地址,伪造内网地址无法绕过“仅内网访问”;
  • 其余情况以 X-Forwarded-For 为准,它的最右一项由最近的代理追加,客户端无法改写。

func Error

func Error(w http.ResponseWriter, status int, format string, args ...any)

Error 输出错误 JSON。

func MarkWAFBlocked added in v0.6.3

func MarkWAFBlocked(r *http.Request)

MarkWAFBlocked 标记请求已被 WAF 拦截;请求上下文中没有标记时忽略。

func OK

func OK(w http.ResponseWriter, v any)

OK 输出 200 JSON。

func ParsePrefixes added in v0.5.0

func ParsePrefixes(list []string) ([]netip.Prefix, error)

ParsePrefixes 把 IP 或 CIDR 列表解析为地址段,单个 IP 视为 /32 或 /128。

func PeerIP added in v0.5.0

func PeerIP(r *http.Request) string

PeerIP 返回 TCP 对端地址,不读取任何请求头。

func ReadJSON

func ReadJSON(r *http.Request, v any) error

ReadJSON 解析请求体到 v,拒绝未知字段以尽早暴露前后端不一致。 要求 Content-Type 为 application/json:浏览器跨源发送该类型会触发预检,可阻止表单类 CSRF。

func SameOrigin

func SameOrigin(next http.Handler) http.Handler

SameOrigin 拒绝来自其他源的状态变更请求(同站不同端口、兄弟子域同样被拒绝)。 浏览器会为跨源请求带上 Origin 或 Sec-Fetch-Site;命令行工具通常两者都不带,予以放行。

func SessionUser added in v0.6.3

func SessionUser(r *http.Request) string

SessionUser 返回请求所带后台登录态的用户名;没有登录态、登录态无效或未注册校验函数时返回空串。 登录态由服务端密钥签名,访客无法伪造,可放心用作日志标记。

func SetProxyTrust added in v0.5.0

func SetProxyTrust(enable bool, extra []netip.Prefix)

SetProxyTrust 设置是否信任本机代理传来的客户端地址,extra 为额外信任的代理地址段。

func SetSessionCheck added in v0.6.3

func SetSessionCheck(f func(*http.Request) string)

SetSessionCheck 注册后台登录态校验函数:请求带有本实例有效的后台登录态时返回用户名,否则返回空串。 传入 nil 取消注册。

func ViaOf added in v0.5.3

func ViaOf(r *http.Request) string

ViaOf 返回 WithVia 标记的代理名称,未标记时为空。

func WithVia added in v0.5.3

func WithVia(r *http.Request, via string) *http.Request

WithVia 标记请求经由指定的代理转发(例如飞牛统一网关)。这类代理给出的客户端地址无法核实, 审计日志据此显示来源,避免把转发来的地址当作已验证的地址。

func WithWAFFlag added in v0.6.3

func WithWAFFlag(r *http.Request) (*http.Request, *atomic.Bool)

WithWAFFlag 在请求上下文中放入 WAF 拦截标记,WAF 拦截时由 MarkWAFBlocked 置位, Web 服务据此在访问日志与统计中记录 deny=waf,不再按状态码推断(雷池可返回 403 以外的状态码,后端自己的 403 也不算)。

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON 以指定状态码输出 JSON。

Types

type ErrorBody

type ErrorBody struct {
	Error string `json:"error"`
}

ErrorBody 是统一的错误响应结构。

Jump to

Keyboard shortcuts

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