Skip to content

Commit b21a44c

Browse files
committed
go-aah/aah#21, go-aah/aah#13 update and added headers for upcoming
1 parent 2e27636 commit b21a44c

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed

header.go

+43-38
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,49 @@ import (
1717

1818
// HTTP Header names
1919
const (
20-
HeaderAccept = "Accept"
21-
HeaderAcceptEncoding = "Accept-Encoding"
22-
HeaderAcceptLanguage = "Accept-Language"
23-
HeaderAllow = "Allow"
24-
HeaderAuthorization = "Authorization"
25-
HeaderCacheControl = "Cache-Control"
26-
HeaderConnection = "Connection"
27-
HeaderContentDisposition = "Content-Disposition"
28-
HeaderContentLength = "Content-Length"
29-
HeaderContentType = "Content-Type"
30-
HeaderContentEncoding = "Content-Encoding"
31-
HeaderCookie = "Cookie"
32-
HeaderHost = "Host"
33-
HeaderIfModifiedSince = "If-Modified-Since"
34-
HeaderLocation = "Location"
35-
HeaderLastModified = "Last-Modified"
36-
HeaderMethod = "Method"
37-
HeaderReferer = "Referer"
38-
HeaderServer = "Server"
39-
HeaderSetCookie = "Set-Cookie"
40-
HeaderStatus = "Status"
41-
HeaderOrigin = "Origin"
42-
HeaderTransferEncoding = "Transfer-Encoding"
43-
HeaderUpgrade = "Upgrade"
44-
HeaderUserAgent = "User-Agent"
45-
HeaderVary = "Vary"
46-
HeaderWWWAuthenticate = "WWW-Authenticate"
47-
HeaderXContentTypeOptions = "X-Content-Type-Options"
48-
HeaderXForwardedFor = "X-Forwarded-For"
49-
HeaderXForwardedHost = "X-Forwarded-Host"
50-
HeaderXForwardedPort = "X-Forwarded-Port"
51-
HeaderXForwardedProto = "X-Forwarded-Proto"
52-
HeaderXForwardedServer = "X-Forwarded-Server"
53-
HeaderXFrameOptions = "X-Frame-Options"
54-
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
55-
HeaderXRealIP = "X-Real-Ip"
56-
HeaderXRequestedWith = "X-Requested-With"
57-
HeaderXXSSProtection = "X-XSS-Protection"
20+
HeaderAccept = "Accept"
21+
HeaderAcceptEncoding = "Accept-Encoding"
22+
HeaderAcceptLanguage = "Accept-Language"
23+
HeaderAllow = "Allow"
24+
HeaderAuthorization = "Authorization"
25+
HeaderCacheControl = "Cache-Control"
26+
HeaderConnection = "Connection"
27+
HeaderContentDisposition = "Content-Disposition"
28+
HeaderContentEncoding = "Content-Encoding"
29+
HeaderContentLength = "Content-Length"
30+
HeaderContentType = "Content-Type"
31+
HeaderContentSecurityPolicy = "Content-Security-Policy"
32+
HeaderCookie = "Cookie"
33+
HeaderHost = "Host"
34+
HeaderIfModifiedSince = "If-Modified-Since"
35+
HeaderIfUnmodifiedSince = "If-Unmodified-Since"
36+
HeaderLocation = "Location"
37+
HeaderLastModified = "Last-Modified"
38+
HeaderMethod = "Method"
39+
HeaderReferer = "Referer"
40+
HeaderServer = "Server"
41+
HeaderSetCookie = "Set-Cookie"
42+
HeaderStatus = "Status"
43+
HeaderStrictTransportSecurity = "Strict-Transport-Security"
44+
HeaderOrigin = "Origin"
45+
HeaderTransferEncoding = "Transfer-Encoding"
46+
HeaderUpgrade = "Upgrade"
47+
HeaderUserAgent = "User-Agent"
48+
HeaderVary = "Vary"
49+
HeaderWWWAuthenticate = "WWW-Authenticate"
50+
HeaderXContentTypeOptions = "X-Content-Type-Options"
51+
HeaderXCSRFToken = "X-CSRF-Token"
52+
HeaderXForwardedFor = "X-Forwarded-For"
53+
HeaderXForwardedHost = "X-Forwarded-Host"
54+
HeaderXForwardedPort = "X-Forwarded-Port"
55+
HeaderXForwardedProto = "X-Forwarded-Proto"
56+
HeaderXForwardedServer = "X-Forwarded-Server"
57+
HeaderXFrameOptions = "X-Frame-Options"
58+
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
59+
HeaderXRealIP = "X-Real-Ip"
60+
HeaderXRequestedWith = "X-Requested-With"
61+
HeaderXRequestID = "X-Request-Id"
62+
HeaderXXSSProtection = "X-XSS-Protection"
5863
)
5964

6065
type (

request.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type (
3434

3535
// Payload holds the value from HTTP request for `Content-Type`
3636
// JSON and XML.
37-
Payload string
37+
Payload []byte
3838

3939
// ContentType the parsed HTTP header `Content-Type`.
4040
ContentType *ContentType
@@ -48,7 +48,7 @@ type (
4848

4949
// AcceptEncoding negotiated value from HTTP Header the `Accept-Encoding`
5050
// Most quailfied one based on quality factor.
51-
AcceptEncoding AcceptSpec
51+
AcceptEncoding *AcceptSpec
5252

5353
// Params contains values from Path, Query, Form and File.
5454
Params *Params
@@ -102,10 +102,10 @@ func ParseRequest(r *http.Request, req *Request) *Request {
102102
req.Params = &Params{Query: r.URL.Query()}
103103
req.Referer = getReferer(r.Header)
104104
req.UserAgent = r.Header.Get(HeaderUserAgent)
105-
req.ClientIP = ClientIP(r)
105+
req.ClientIP = clientIP(r)
106106
req.Locale = NegotiateLocale(r)
107107
req.IsJSONP = isJSONPReqeust(r)
108-
req.IsGzipAccepted = isGzipAccepted(r)
108+
req.IsGzipAccepted = isGzipAccepted(req, r)
109109
req.Raw = r
110110

111111
return req
@@ -115,11 +115,26 @@ func ParseRequest(r *http.Request, req *Request) *Request {
115115
// Request methods
116116
//___________________________________
117117

118+
// Cookie method returns a named cookie from HTTP request otherwise error.
119+
func (r *Request) Cookie(name string) (*http.Cookie, error) {
120+
return r.Raw.Cookie(name)
121+
}
122+
123+
// Cookies method returns all the cookies from HTTP request.
124+
func (r *Request) Cookies() []*http.Cookie {
125+
return r.Raw.Cookies()
126+
}
127+
118128
// Reset method resets request instance for reuse.
119129
func (r *Request) Reset() {
130+
r.Host = ""
131+
r.Method = ""
132+
r.Path = ""
120133
r.Header = nil
134+
r.Payload = nil
121135
r.ContentType = nil
122136
r.AcceptContentType = nil
137+
r.AcceptEncoding = nil
123138
r.Params = nil
124139
r.Referer = ""
125140
r.UserAgent = ""
@@ -194,10 +209,10 @@ func (p *Params) FormFile(key string) (multipart.File, *multipart.FileHeader, er
194209
// Unexported methods
195210
//___________________________________
196211

197-
// ClientIP returns IP address from HTTP request, typically known as Client IP or
212+
// clientIP returns IP address from HTTP request, typically known as Client IP or
198213
// Remote IP. It parses the IP in the order of X-Forwarded-For, X-Real-IP
199214
// and finally `http.Request.RemoteAddr`.
200-
func ClientIP(req *http.Request) string {
215+
func clientIP(req *http.Request) string {
201216
// Header X-Forwarded-For
202217
if fwdFor := req.Header.Get(HeaderXForwardedFor); !ess.IsStrEmpty(fwdFor) {
203218
index := strings.Index(fwdFor, ",")
@@ -236,9 +251,10 @@ func isJSONPReqeust(r *http.Request) bool {
236251
return !ess.IsStrEmpty(callback)
237252
}
238253

239-
func isGzipAccepted(req *http.Request) bool {
240-
specs := ParseAcceptEncoding(req)
254+
func isGzipAccepted(req *Request, r *http.Request) bool {
255+
specs := ParseAcceptEncoding(r)
241256
if specs != nil {
257+
req.AcceptEncoding = specs.MostQualified()
242258
for _, v := range specs {
243259
if v.Value == "gzip" {
244260
return true

0 commit comments

Comments
 (0)