Documentation
¶
Overview ¶
Package httpKit
请求头 || 响应头 key: 不区分大小写
Index ¶
- Constants
- Variables
- func AddHeader(header http.Header, key, value string)
- func AddHeaderIfMissing(header http.Header, key, value string)
- func AddHeaderIfMissingIgnoreCase(header http.Header, key, value string)
- func AssertHttpUrl(httpUrl string) error
- func DelHeader(header http.Header, key string)
- func GetClientIP(r *http.Request) (string, error)
- func GetFormFile(r *http.Request, key string, maxMemory int64) (multipart.File, *multipart.FileHeader, error)
- func GetHeader(header http.Header, key string) string
- func GetHeaderValues(header http.Header, key string) []string
- func GetOrigin(header http.Header) string
- func GetProto(req *http.Request) string
- func GetRequestUrl(req *http.Request) string
- func GetRoute(req *http.Request) string
- func GetRouteWithQuery(req *http.Request) string
- func GetURLRawQuery(req *http.Request) string
- func GetUserAgent(header http.Header) string
- func HeaderContainsValue(header http.Header, key, value string) bool
- func HeaderContainsValueIgnoreCase(header http.Header, key, value string) bool
- func HeaderToMap(header http.Header) map[string]interface{}
- func IsProxyDialError(err error) bool
- func MakeRequestBodySeekable(req *http.Request) error
- func ObtainGetParam(r *http.Request, key string) string
- func ObtainPostParam(r *http.Request, key string) (string, error)
- func OverrideRequestBody(req *http.Request, m map[string][]string)
- func PolyfillContextPath(relativePath string) string
- func Proxy(w http.ResponseWriter, r *http.Request, targetAddr string, ...) error
- func ResetRequestBody(req *http.Request) error
- func RespondData(w http.ResponseWriter, code int, contentType string, data []byte) error
- func RespondFile(w http.ResponseWriter, r *http.Request, code int, filePath, fileName string) error
- func RespondJson(w http.ResponseWriter, code int, obj any) error
- func RespondString(w http.ResponseWriter, code int, format string, values ...any) error
- func RespondStringData(w http.ResponseWriter, code int, data []byte) error
- func SetCacheControlNoCache(header http.Header)
- func SetCacheControlNoStore(header http.Header)
- func SetHeader(header http.Header, key, value string)
- func SetHeaderIfMissing(header http.Header, key, value string)
- func SetHeaderIfMissingIgnoreCase(header http.Header, key, value string)
- func Status(w http.ResponseWriter, code int)
- func ToRequestBodyString(m map[string][]string) string
- type ProxyOption
- type Repeat
Constants ¶
const ( // PlainContentType 纯文本格式. PlainContentType = "text/plain; charset=utf-8" // JsonContentType JSON数据格式. JsonContentType = "application/json; charset=utf-8" // XmlContentType XML数据格式. XmlContentType = "text/xml; charset=utf-8" // FormUrlencodedContentType 适用于 POST 请求. /* application/x-www-form-urlencoded: 将键值对的参数用&连接起来,如果有空格,将空格转换为+加号;有特殊符号,将特殊符号转换为ASCII HEX值. application/x-www-form-urlencoded是浏览器默认的编码格式,对于Get请求,是将参数转换?key=value&key=value格式,连接到url后. */ FormUrlencodedContentType = "application/x-www-form-urlencoded; charset=utf-8" // OctetStreamContentType 二进制流数据(如常见的文件下载). /* 参考:https://www.runoob.com/http/http-content-type.html */ OctetStreamContentType = "application/octet-stream; charset=utf-8" )
Variables ¶
var ( // GetRequestPublicIp 获取http请求ip. GetRequestPublicIp func(req *http.Request) string = netutil.GetRequestPublicIp )
Functions ¶
func AddHeader ¶
AddHeader
e.g.
header := make(map[string][]string) AddHeader(header, "k", "0") fmt.Println(header) // map[K:[0]] AddHeader(header, "k", "1") fmt.Println(header) // map[K:[0 1]] AddHeader(header, "k", "1") fmt.Println(header) // map[K:[0 1 1]]
func AssertHttpUrl ¶
func GetClientIP ¶ added in v2.8.128
GetClientIP Deprecated: Use GetRequestPublicIp instead.
!!!: 请注意,恶意用户可以创建伪造的 X-REAL-IP 和 X-FORWARDED-FOR 标头。因此,在处理这些头部信息时需要谨慎。如果可能,应该使用一种方法来验证IP地址的真实性。
流程: (1) 这个函数首先尝试获取X-Real-IP头部信息,如果这个头部信息存在并且是一个有效的IP地址,那么就返回这个IP。 (2) 如果X-Real-IP头部信息不存在或者不是一个有效的IP地址,那么函数会尝试获取X-Forwarded-For头部信息,这个头部信息包含了一个IP地址列表,每个经过的代理服务器都会添加一个IP。 (3) 如果X-Forwarded-For头部信息中存在有效的IP地址,那么就返回这个IP。 (4) 如果以上两个头部信息都不存在有效的IP地址,那么函数会尝试获取RemoteAddr,这个是Web服务器从其接收连接并将响应发送到的实际物理IP地址。
func GetFormFile ¶
func GetFormFile(r *http.Request, key string, maxMemory int64) (multipart.File, *multipart.FileHeader, error)
GetFormFile
[Go] golang获取http中的get传递的参数
https://www.cnblogs.com/taoshihan/p/12943118.html
@param maxMemory 可以为-1,此时将采用默认值
func GetRouteWithQuery ¶
GetRouteWithQuery 获取: 路由(带query).
e.g.
http://127.0.0.1/a/b?1=1&2=2 => "/a/b?1=1&2=2"
func GetUserAgent ¶
GetUserAgent 获取http请求头中"User Agent"的值.
参考: https://www.sunzhongwei.com/golang-gin-for-user-agent-in-http-request-header-value
e.g. Chrome浏览器: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36 Safari浏览器: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
func HeaderContainsValueIgnoreCase ¶
HeaderContainsValueIgnoreCase Header中,指定key对应的value切片是否包含指定value?\
PS: 不区分大小写.
func HeaderToMap ¶
HeaderToMap http.Header(即 map[string][]string) => map[string]interface{}
func IsProxyDialError ¶
IsProxyDialError 代理请求返回的error,是否是因为dial目标地址失败?
func MakeRequestBodySeekable ¶
MakeRequestBodySeekable
Go语言: 如何让 request.Body 可以多次读取
https://www.cnblogs.com/ayanmw/p/17191530.html
PS: (1) 一般与 proxy() 搭配使用; (2) 某个路由涉及代理(请求转发)的话,需要在handler里面 首先 调用此方法.
func ObtainGetParam ¶ added in v2.8.123
ObtainGetParam
[Go] golang获取http中的get传递的参数
https://www.cnblogs.com/taoshihan/p/12943118.html
func ObtainPostParam ¶ added in v2.8.123
ObtainPostParam
[Go] golang获取http中的get传递的参数
https://www.cnblogs.com/taoshihan/p/12943118.html
func OverrideRequestBody ¶
OverrideRequestBody 覆盖request body.
条件: (1) POST (2) x-www-form-urlencoded
func PolyfillContextPath ¶
PolyfillContextPath *
- e.g.
- "" => ""
- "/" => "/"
- "//" => "/"
- "///" => "/"
- "/c/////c//" => "/c/c/" *
- @return 优化过的ContextPath
func Proxy ¶
func Proxy(w http.ResponseWriter, r *http.Request, targetAddr string, options ...ProxyOption) error
Proxy 代理请求(反向代理,请求转发).
PS: 转发请求前如果想变更请求头(Request Header),可以在调用此函数前设置请求头.
@param w e.g.ctx.Writer @param r e.g.ctx.Request
func ResetRequestBody ¶
ResetRequestBody 重置请求体,以防: 已经读完body了,请求转发给别人,别人收到的请求没内容.
PS: req.Body可以为nil.
func RespondFile ¶
RespondFile 响应文件
参考: gin里面的 Context.File() 和 Context.FileAttachment() .
@param filePath 文件路径 @param fileName 文件名(可以为"",此时将从 传参filePath 中获取) @return 如果不为nil,建议输出到控制台
func RespondJson ¶
func RespondJson(w http.ResponseWriter, code int, obj any) error
RespondJson
参考: gin里面的 Context.JSON() .
func RespondStringData ¶
func RespondStringData(w http.ResponseWriter, code int, data []byte) error
func SetCacheControlNoCache ¶
SetCacheControlNoCache 实际上是有缓存的)浏览器对请求回来的response做缓存,但是每次在向客户端(浏览器)提供响应数据时,缓存都要向服务器评估缓存响应的有效性。
PS: (1) 一般情况下, "no-cache" 就够了; (2) 详见"Web.docx".
func SetHeader ¶
SetHeader
e.g.
header := make(map[string][]string) httpKit.AddHeader(header, "k", "0") fmt.Println(header) // map[K:[0]] httpKit.AddHeader(header, "k", "1") fmt.Println(header) // map[K:[0 1]] httpKit.SetHeader(header, "k", "2") fmt.Println(header) // map[K:[2]] httpKit.SetHeader(header, "k", "") fmt.Println(header) // map[]
func Status ¶
func Status(w http.ResponseWriter, code int)
Status 设置响应的http状态码
PS: (1) 不建议多次设置 http状态码; (2) 如果多次设置的话,感觉 第一次设置的值 会生效.
@param code -1: 不设置http状态码
func ToRequestBodyString ¶
ToRequestBodyString
条件: (1) POST (2) x-www-form-urlencoded
e.g.
m := map[string][]string{ "a": []string{"test"}, "b": []string{"测试"}, } fmt.Println(ToRequestBodyString(m)) // a=test&b=%E6%B5%8B%E8%AF%95
Types ¶
type ProxyOption ¶
type ProxyOption func(opts *proxyOptions)
func WithErrorLogger ¶
func WithErrorLogger(errorLogger *log.Logger) ProxyOption
func WithQueryParams ¶
func WithQueryParams(queryParams map[string][]string) ProxyOption
func WithReqUrlPath ¶
func WithReqUrlPath(reqUrlPath *string) ProxyOption
WithReqUrlPath
@param reqUrlPath 不带query
func WithScheme ¶
func WithScheme(scheme string) ProxyOption