request-reader.go 1.4 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3
package common

import (
Y
Your Name 已提交
4
	"io/ioutil"
E
eoLinker API Management 已提交
5 6 7 8
	"net/http"
	"net/url"
)

Y
Your Name 已提交
9
//RequestReader 请求reader
E
eoLinker API Management 已提交
10 11 12 13 14 15
type RequestReader struct {
	*Header
	*BodyRequestHandler
	req *http.Request
}

Y
Your Name 已提交
16
//Proto 获取协议
E
eoLinker API Management 已提交
17 18 19 20
func (r *RequestReader) Proto() string {
	return r.req.Proto
}

Y
Your Name 已提交
21
//NewRequestReader 创建RequestReader
E
eoLinker API Management 已提交
22 23 24 25 26 27
func NewRequestReader(req *http.Request) *RequestReader {
	r := new(RequestReader)
	r.req = req
	r.ParseRequest()
	return r
}
Y
Your Name 已提交
28 29 30

//ParseRequest 解析请求
func (r *RequestReader) ParseRequest() {
E
eoLinker API Management 已提交
31 32

	r.Header = NewHeader(r.req.Header)
Y
Your Name 已提交
33
	body, err := ioutil.ReadAll(r.req.Body)
E
eoLinker API Management 已提交
34
	_ = r.req.Body.Close()
Y
Your Name 已提交
35
	if err != nil {
E
eoLinker API Management 已提交
36
		r.BodyRequestHandler = NewBodyRequestHandler(r.req.Header.Get("Content-Type"), nil)
Y
Your Name 已提交
37 38
	} else {
		r.BodyRequestHandler = NewBodyRequestHandler(r.req.Header.Get("Content-Type"), body)
E
eoLinker API Management 已提交
39 40 41
	}
}

Y
Your Name 已提交
42
//Cookie 获取cookie
E
eoLinker API Management 已提交
43 44 45 46
func (r *RequestReader) Cookie(name string) (*http.Cookie, error) {
	return r.req.Cookie(name)
}

Y
Your Name 已提交
47
//Cookies 获取cookies
E
eoLinker API Management 已提交
48 49 50 51
func (r *RequestReader) Cookies() []*http.Cookie {
	return r.req.Cookies()
}

Y
Your Name 已提交
52
//Method 获取请求方式
E
eoLinker API Management 已提交
53 54 55 56
func (r *RequestReader) Method() string {
	return r.req.Method
}

Y
Your Name 已提交
57
//URL url
E
eoLinker API Management 已提交
58 59 60 61
func (r *RequestReader) URL() *url.URL {
	return r.req.URL
}

Y
Your Name 已提交
62
//RequestURI 获取请求URI
E
eoLinker API Management 已提交
63 64 65 66
func (r *RequestReader) RequestURI() string {
	return r.req.RequestURI
}

Y
Your Name 已提交
67
//Host 获取host
E
eoLinker API Management 已提交
68 69 70 71
func (r *RequestReader) Host() string {
	return r.req.Host
}

Y
Your Name 已提交
72
//RemoteAddr 远程地址
E
eoLinker API Management 已提交
73 74 75
func (r *RequestReader) RemoteAddr() string {
	return r.req.RemoteAddr
}