9
9
"fmt"
10
10
"io"
11
11
"mime/multipart"
12
- "net"
13
12
"net/http"
14
13
"net/url"
15
14
"os"
@@ -21,10 +20,8 @@ import (
21
20
)
22
21
23
22
const (
24
- jsonpReqParamKey = "callback"
25
- ajaxHeaderValue = "XMLHttpRequest"
26
- wsHdrVal = "websocket"
27
- connHdrValPartial = "upgrade"
23
+ jsonpReqParamKey = "callback"
24
+ ajaxHeaderValue = "XMLHttpRequest"
28
25
)
29
26
30
27
var requestPool = & sync.Pool {New : func () interface {} { return & Request {} }}
@@ -36,8 +33,8 @@ var requestPool = &sync.Pool{New: func() interface{} { return &Request{} }}
36
33
// ParseRequest method populates the given aah framework `ahttp.Request`
37
34
// instance from Go HTTP request.
38
35
func ParseRequest (r * http.Request , req * Request ) * Request {
39
- req .Scheme = identifyScheme (r )
40
- req .Host = host (r )
36
+ req .Scheme = IdentifyScheme (r )
37
+ req .Host = IdentifyHost (r )
41
38
req .Proto = r .Proto
42
39
req .Method = r .Method
43
40
req .Path = r .URL .Path
@@ -153,26 +150,7 @@ func (r *Request) SetAcceptEncoding(encoding *AcceptSpec) *Request {
153
150
// could configure headers of their choice. For e.g.
154
151
// X-Appengine-Remote-Addr, etc.
155
152
func (r * Request ) ClientIP () string {
156
- // Header X-Forwarded-For
157
- if fwdFor := r .Header .Get (HeaderXForwardedFor ); ! ess .IsStrEmpty (fwdFor ) {
158
- index := strings .Index (fwdFor , "," )
159
- if index == - 1 {
160
- return strings .TrimSpace (fwdFor )
161
- }
162
- return strings .TrimSpace (fwdFor [:index ])
163
- }
164
-
165
- // Header X-Real-Ip
166
- if realIP := r .Header .Get (HeaderXRealIP ); ! ess .IsStrEmpty (realIP ) {
167
- return strings .TrimSpace (realIP )
168
- }
169
-
170
- // Remote Address
171
- if remoteAddr , _ , err := net .SplitHostPort (r .Unwrap ().RemoteAddr ); err == nil {
172
- return strings .TrimSpace (remoteAddr )
173
- }
174
-
175
- return ""
153
+ return ClientIP (r .Unwrap ())
176
154
}
177
155
178
156
// Cookie method returns a named cookie from HTTP request otherwise error.
@@ -226,12 +204,6 @@ func (r *Request) IsAJAX() bool {
226
204
return r .Header .Get (HeaderXRequestedWith ) == ajaxHeaderValue
227
205
}
228
206
229
- // IsWebSocket method returns true if request is WebSocket otherwise false.
230
- func (r * Request ) IsWebSocket () bool {
231
- return r .Header .Get (HeaderUpgrade ) == wsHdrVal &&
232
- strings .Contains (strings .ToLower (r .Header .Get (HeaderConnection )), connHdrValPartial )
233
- }
234
-
235
207
// URL method return underlying request URL instance.
236
208
func (r * Request ) URL () * url.URL {
237
209
return r .Unwrap ().URL
@@ -452,29 +424,6 @@ func (p *Params) FormFile(key string) (multipart.File, *multipart.FileHeader, er
452
424
// Unexported methods
453
425
//___________________________________
454
426
455
- // identifyScheme method is to identify value of protocol value. It's is derived
456
- // one, Go language doesn't provide directly.
457
- // - `X-Forwarded-Proto` is not empty return value as is
458
- // - `http.Request.TLS` is not nil value is `https`
459
- // - `http.Request.TLS` is nil value is `http`
460
- func identifyScheme (r * http.Request ) string {
461
- scheme := r .Header .Get (HeaderXForwardedProto )
462
- if scheme == "" {
463
- if r .TLS == nil {
464
- return SchemeHTTP // "http"
465
- }
466
- return SchemeHTTPS // "https"
467
- }
468
- return scheme
469
- }
470
-
471
- func host (r * http.Request ) string {
472
- if r .URL .Host == "" {
473
- return r .Host
474
- }
475
- return r .URL .Host
476
- }
477
-
478
427
func getReferer (hdr http.Header ) string {
479
428
referer := hdr .Get (HeaderReferer )
480
429
if referer == "" {
0 commit comments