httpx

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 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-Real-IP、X-Forwarded-For(自右向左跳过可信代理)中取得真实地址; 否则返回对端地址。

func Error

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

Error 输出错误 JSON。

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 SetProxyTrust added in v0.5.0

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

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

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 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