request-reader.go 1.3 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
package common

import (
	"net/http"
	"net/url"
)

type RequestReader struct {
	*Header
	*BodyRequestHandler
	req *http.Request
}

func (r *RequestReader) Proto() string {
	return r.req.Proto
}

func NewRequestReader(req *http.Request) *RequestReader {
	r := new(RequestReader)
	r.req = req
	r.ParseRequest()
	return r
}
func (r *RequestReader) ParseRequest() () {

	r.Header = NewHeader(r.req.Header)

	body := make([]byte, r.req.ContentLength, r.req.ContentLength)
	i, err := r.req.Body.Read(body)
	_ = r.req.Body.Close()
	if err != nil && int64(i) == r.req.ContentLength {
		r.BodyRequestHandler = NewBodyRequestHandler(r.req.Header.Get("Content-Type"), body)

	} else {
		r.BodyRequestHandler = NewBodyRequestHandler(r.req.Header.Get("Content-Type"), nil)
	}

	// todo
}

func (r *RequestReader) Cookie(name string) (*http.Cookie, error) {
	return r.req.Cookie(name)
}

func (r *RequestReader) Cookies() []*http.Cookie {
	return r.req.Cookies()
}

func (r *RequestReader) Method() string {
	return r.req.Method
}

func (r *RequestReader) URL() *url.URL {
	return r.req.URL
}

func (r *RequestReader) RequestURI() string {
	return r.req.RequestURI
}

func (r *RequestReader) Host() string {
	return r.req.Host
}

func (r *RequestReader) RemoteAddr() string {
	return r.req.RemoteAddr
}