-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
85 lines (70 loc) · 2.55 KB
/
request.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/ws source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ws
import (
"fmt"
"net/http"
"net/url"
"aahframework.org/ahttp.v0"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Request struct and its methods
//______________________________________________________________________________
// Request struct holds information for successful WebSocket connection made.
type Request struct {
// ID aah assigns Globally Unique Identifier (GUID) using Mongo Object ID
// algorithm for every WebSocket connection made to aah server.
//
// You may use it for tracking, tracing or identifying WebSocket client.
ID string
// Host value of the HTTP 'Host' header (e.g. 'example.com:8080').
Host string
// Path the request URL Path e.g. `/chatroom/aahframework`.
Path string
// Header holds the values of HTTP headers when WebSocket connection made.
Header http.Header
pathParams ahttp.PathParams
raw *http.Request
}
// URL method returns HTTP request URL instance.
func (r *Request) URL() *url.URL {
return r.raw.URL
}
// PathValue method returns value for given Path param key otherwise empty string.
// For eg.: `/discussion/:roomName` => `PathValue("roomName")`.
func (r *Request) PathValue(key string) string {
if r.pathParams != nil {
return r.pathParams.Get(key)
}
return ""
}
// QueryValue method returns value for given URL query param key
// otherwise empty string.
func (r *Request) QueryValue(key string) string {
return r.URL().Query().Get(key)
}
// QueryArrayValue method returns array value for given URL query param key
// otherwise empty string slice.
func (r *Request) QueryArrayValue(key string) []string {
if values, found := r.URL().Query()[key]; found {
return values
}
return []string{}
}
// ClientIP method returns remote Client IP address aka Remote IP.
// It parses in the order of given set of headers otherwise it uses default
// default header set `X-Forwarded-For`, `X-Real-IP`, "X-Appengine-Remote-Addr"
// and finally `http.Request.RemoteAddr`.
func (r *Request) ClientIP() string {
return ahttp.ClientIP(r.raw)
}
// String request stringer interface.
func (r Request) String() string {
return fmt.Sprintf("request(id:%s host:%s path:%s querystring:%s)",
r.ID,
r.Host,
r.Path,
r.URL().Query().Encode(),
)
}