gf/g/net/ghttp/http_request.go

257 lines
6.5 KiB
Go
Raw Normal View History

2017-12-29 16:03:30 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
2017-12-31 18:19:58 +08:00
2017-11-23 10:21:28 +08:00
package ghttp
import (
"io/ioutil"
2017-12-31 12:15:04 +08:00
"net/http"
"net/url"
2017-12-31 11:09:16 +08:00
"gitee.com/johng/gf/g/util/gconv"
2017-12-31 12:15:04 +08:00
"gitee.com/johng/gf/g/encoding/gjson"
2017-11-23 10:21:28 +08:00
)
2017-12-31 12:15:04 +08:00
// 请求对象
type Request struct {
2017-12-31 12:15:04 +08:00
http.Request
parsedPost bool // POST参数是否已经解析
getvals *url.Values // GET参数
Id int // 请求id(唯一)
Server *Server // 请求关联的服务器对象
Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全)
Session *Session // 与当前请求绑定的Session对象(并发安全)
Response *Response // 对应请求的返回数据操作对象
}
// 初始化GET请求参数
func (r *Request) initGet() {
2017-11-23 10:21:28 +08:00
if r.getvals == nil {
2018-01-03 10:23:37 +08:00
values := r.URL.Query()
2017-11-23 10:21:28 +08:00
r.getvals = &values
}
}
// 初始化POST请求参数
func (r *Request) initPost() {
if !r.parsedPost {
// 快速保存,尽量避免并发问题
r.parsedPost = true
// MultiMedia表单请求解析允许最大使用内存1GB
r.ParseMultipartForm(1024*1024*1024)
}
}
// 获得指定名称的get参数列表
func (r *Request) GetQuery(k string) []string {
r.initGet()
2017-11-23 10:21:28 +08:00
if v, ok := (*r.getvals)[k]; ok {
return v
}
return nil
}
func (r *Request) GetQueryBool(k string) bool {
2017-12-31 11:09:16 +08:00
return gconv.Bool(r.GetQueryString(k))
}
func (r *Request) GetQueryInt(k string) int {
2017-12-31 11:09:16 +08:00
return gconv.Int(r.GetQueryString(k))
}
func (r *Request) GetQueryUint(k string) uint {
2017-12-31 11:09:16 +08:00
return gconv.Uint(r.GetQueryString(k))
}
func (r *Request) GetQueryFloat32(k string) float32 {
2017-12-31 11:09:16 +08:00
return gconv.Float32(r.GetQueryString(k))
}
func (r *Request) GetQueryFloat64(k string) float64 {
2017-12-31 11:09:16 +08:00
return gconv.Float64(r.GetQueryString(k))
2017-12-08 09:50:11 +08:00
}
func (r *Request) GetQueryString(k string) string {
2017-11-23 10:21:28 +08:00
v := r.GetQuery(k)
if v == nil {
return ""
} else {
return v[0]
}
}
func (r *Request) GetQueryArray(k string) []string {
2017-12-31 11:09:16 +08:00
return r.GetQuery(k)
2017-11-23 10:21:28 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
func (r *Request) GetQueryMap(defaultMap...map[string]string) map[string]string {
r.initGet()
2017-12-28 17:28:32 +08:00
m := make(map[string]string)
if len(defaultMap) == 0 {
for k, v := range *r.getvals {
m[k] = v[0]
}
} else {
for k, v := range defaultMap[0] {
v2 := r.GetQueryArray(k)
if v2 == nil {
m[k] = v
} else {
m[k] = v2[0]
}
2017-11-23 10:21:28 +08:00
}
}
return m
}
// 获得post参数
func (r *Request) GetPost(k string) []string {
r.initPost()
2017-11-23 10:21:28 +08:00
if v, ok := r.PostForm[k]; ok {
return v
}
return nil
}
func (r *Request) GetPostBool(k string) bool {
2017-12-31 11:09:16 +08:00
return gconv.Bool(r.GetPostString(k))
}
func (r *Request) GetPostInt(k string) int {
2017-12-31 11:09:16 +08:00
return gconv.Int(r.GetPostString(k))
}
func (r *Request) GetPostUint(k string) uint {
2017-12-31 11:09:16 +08:00
return gconv.Uint(r.GetPostString(k))
}
func (r *Request) GetPostFloat32(k string) float32 {
2017-12-31 11:09:16 +08:00
return gconv.Float32(r.GetPostString(k))
}
func (r *Request) GetPostFloat64(k string) float64 {
2017-12-31 11:09:16 +08:00
return gconv.Float64(r.GetPostString(k))
2017-12-08 09:50:11 +08:00
}
func (r *Request) GetPostString(k string) string {
2017-11-23 10:21:28 +08:00
v := r.GetPost(k)
if v == nil {
return ""
} else {
return v[0]
}
}
func (r *Request) GetPostArray(k string) []string {
2017-12-31 11:09:16 +08:00
return r.GetPost(k)
2017-11-23 10:21:28 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
2017-12-28 17:28:32 +08:00
// 需要注意的是如果其中一个字段为数组形式那么只会返回第一个元素如果需要获取全部的元素请使用GetPostArray获取特定字段内容
func (r *Request) GetPostMap(defaultMap...map[string]string) map[string]string {
r.initPost()
2017-12-28 17:28:32 +08:00
m := make(map[string]string)
if len(defaultMap) == 0 {
for k, v := range r.PostForm {
m[k] = v[0]
}
} else {
for k, v := range defaultMap[0] {
if v2, ok := r.PostForm[k]; ok {
m[k] = v2[0]
} else {
m[k] = v
}
2017-11-23 10:21:28 +08:00
}
}
return m
}
// 获得post或者get提交的参数如果有同名参数那么按照get->post优先级进行覆盖
func (r *Request) GetRequest(k string) []string {
2017-11-23 10:21:28 +08:00
v := r.GetQuery(k)
if v == nil {
return r.GetPost(k)
}
return v
}
func (r *Request) GetRequestString(k string) string {
2017-11-23 10:21:28 +08:00
v := r.GetRequest(k)
if v == nil {
return ""
} else {
return v[0]
}
}
func (r *Request) GetRequestBool(k string) bool {
2017-12-31 11:09:16 +08:00
return gconv.Bool(r.GetRequestString(k))
}
func (r *Request) GetRequestInt(k string) int {
2017-12-31 11:09:16 +08:00
return gconv.Int(r.GetRequestString(k))
}
func (r *Request) GetRequestUint(k string) uint {
2017-12-31 11:09:16 +08:00
return gconv.Uint(r.GetRequestString(k))
}
func (r *Request) GetRequestFloat32(k string) float32 {
2017-12-31 11:09:16 +08:00
return gconv.Float32(r.GetRequestString(k))
}
func (r *Request) GetRequestFloat64(k string) float64 {
2017-12-31 11:09:16 +08:00
return gconv.Float64(r.GetRequestString(k))
}
func (r *Request) GetRequestArray(k string) []string {
2017-12-31 11:09:16 +08:00
return r.GetRequest(k)
2017-11-23 10:21:28 +08:00
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
2017-12-28 17:28:32 +08:00
// 需要注意的是如果其中一个字段为数组形式那么只会返回第一个元素如果需要获取全部的元素请使用GetRequestArray获取特定字段内容
func (r *Request) GetRequestMap(defaultMap...map[string]string) map[string]string {
m := r.GetQueryMap()
if len(defaultMap) == 0 {
for k, v := range r.GetPostMap() {
if _, ok := m[k]; !ok {
m[k] = v
}
}
} else {
for k, v := range defaultMap[0] {
v2 := r.GetRequest(k)
if v2 != nil {
m[k] = v2[0]
} else {
m[k] = v
}
2017-11-23 10:21:28 +08:00
}
}
return m
}
// 获取原始请求输入字符串,注意:只能获取一次,读完就没了
func (r *Request) GetRaw() []byte {
result, _ := ioutil.ReadAll(r.Body)
return result
2017-11-23 10:21:28 +08:00
}
2017-12-31 11:09:16 +08:00
// 获取原始json请求输入字符串并解析为json对象
func (r *Request) GetJson() *gjson.Json {
2017-11-23 10:21:28 +08:00
data := r.GetRaw()
if data != nil {
if j, err := gjson.DecodeToJson(data); err == nil {
return j
}
2017-11-23 10:21:28 +08:00
}
return nil
}