Documentation ¶
Overview ¶
Package httpKit
请求头 || 响应头 key: 不区分大小写
Index ¶
- Constants
- func AddHeader(header http.Header, key, value string)
- func ClearCookies(req *http.Request)
- func DelHeader(header http.Header, key string)
- func GetContentType(data []byte) string
- 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 GetPostParam(r *http.Request, key string) (string, error)
- func GetProto(req *http.Request) string
- func GetRequestRoute(req *http.Request) string
- func GetRequestUrl(req *http.Request) string
- func GetUrlParam(r *http.Request, key string) string
- func GetUrlParam1(r *http.Request, key string) (string, error)
- func GetUserAgent(header http.Header) string
- func OverridePostRequestBody(req *http.Request, m map[string]string)
- func PolyfillContextPath(relativePath string) string
- func Proxy(w http.ResponseWriter, r *http.Request, errorLogger *log.Logger, ...) (err 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 Status(w http.ResponseWriter, code int)
- type Repeat
Constants ¶
const ( // PlainContentType 纯文本格式 PlainContentType = "text/plain; charset=utf-8" // JsonContentType JSON数据格式 JsonContentType = "application/json; charset=utf-8" // OctetStreamContentType 二进制流数据(如常见的文件下载) /* 参考:https://www.runoob.com/http/http-content-type.html */ OctetStreamContentType = "application/octet-stream; charset=utf-8" )
Variables ¶
This section is empty.
Functions ¶
func AddHeader ¶
AddHeader
e.g.
header := make(map[string][]string) fmt.Println(header) // map[] httpKit.AddHeader(header, "k", "0") fmt.Println(header) // map[K:[0]] httpKit.AddHeader(header, "k", "1") fmt.Println(header) // map[K:[0 1]]
func GetContentType ¶
GetContentType 获取 ContentType(即MimeType).
@return 保底 "application/octet-stream"
e.g. ([]byte(nil)) => "text/plain; charset=utf-8" ([]byte{}) => "text/plain; charset=utf-8"
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 GetPostParam ¶
GetPostParam
[Go] golang获取http中的get传递的参数
https://www.cnblogs.com/taoshihan/p/12943118.html
func GetUrlParam ¶
GetUrlParam
[Go] golang获取http中的get传递的参数
https://www.cnblogs.com/taoshihan/p/12943118.html
func GetUrlParam1 ¶
GetUrlParam1
[Go] golang获取http中的get传递的参数
https://www.cnblogs.com/taoshihan/p/12943118.html
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 OverridePostRequestBody ¶
OverridePostRequestBody 覆盖POST请求的请求体.
func PolyfillContextPath ¶
PolyfillContextPath *
- e.g.
- "" => ""
- "/" => "/"
- "//" => "/"
- "///" => "/"
- "/c/////c//" => "/c/c/" *
- @return 优化过的ContextPath
func Proxy ¶ added in v1.30.16
func Proxy(w http.ResponseWriter, r *http.Request, errorLogger *log.Logger, scheme, addr string, reqUrlPath *string, extraQuery map[string]string) (err error)
Proxy 代理请求(反向代理,请求转发)
@param errLogger 可以为nil,但不建议这么干,因为错误会输出到控制台(通过 log.Printf()),不利于错误定位 @param scheme "http" || "https" @param addr e.g."127.0.0.1:8888" @param reqUrlPath (1) 可以为nil(此时不修改 req.URL.Path)
(2) 非nil的话,个人感觉: 字符串的第一个字符应该是"/"
@param extraQuery 可以为nil @return 可能是 context.Canceled(可以用==进行比较)
更多可参考: httputil.NewSingleHostReverseProxy() https://m.bilibili.com/video/BV1H64y1u7D7?buvid=Y44D4D448DC195994A5A88CED2DA982C60DF&is_story_h5=false&mid=5%2BiuUUrTqJQOdIa1r3VR0g%3D%3D&p=1&plat_id=114&share_from=ugc&share_medium=iphone&share_plat=ios&share_session_id=8B36D2C9-4DCB-4BE5-80AD-F7D49E292B5F&share_source=WEIXIN&share_tag=s_i×tamp=1680160438&unique_k=16bK0gz&up_id=456307879
PS: (1) 支持代理的协议: https、http、wss、ws... (2) 如果请求转发的目标有效,但处理此请求需要花费大量时间(比如20+min),此时如果请求的客户端终端了请求(e.g.浏览器页面被直接关闭了),将返回 context.Canceled. (3) addr有效,reqUrlPath非nil但事实上不存在该路由的情况,返回值为nil && 原始客户端得到404(404 page not found).
e.g. 将 https://127.0.0.1:8888/test 转发给 http://127.0.0.1:8889/test 传参可以是: (1) scheme=http addr=127.0.0.1:8889 reqUrlPath=nil (2) scheme=http addr=127.0.0.1:8889 reqUrlPath=&"/test" 传参不能是: (1) scheme=http addr=127.0.0.1:8889 reqUrlPath=&"test" (400 Bad Request)
e.g.1 将 https://127.0.0.1:8888/test 转发给 http://127.0.0.1:8889/test1 传参可以是: (1) scheme=http addr=127.0.0.1:8889 reqUrlPath=&"/test1" 传参不能是: (1) scheme=http addr=127.0.0.1:8889 reqUrlPath=&"test1"
e.g.2 将 https://127.0.0.1:8888/group/test 转发给 http://127.0.0.1:8889/test1 scheme="http" addr="127.0.0.1:8889" reqUrlPath=strKit.GetStringPtr("/test1")
e.g.3 将 https://127.0.0.1:8888/group/test 转发给 http://127.0.0.1:8889/group1/test1 scheme="http" addr="127.0.0.1:8889" reqUrlPath=strKit.GetStringPtr("/group1/test1")
e.g.4 将 wss://127.0.0.1:8888/test 转发给 ws://127.0.0.1:80/ws/connect scheme="http" addr="127.0.0.1:80" reqUrlPath=strKit.GetStringPtr("/ws/connect")
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) fmt.Println(header) // map[] 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状态码