-
Notifications
You must be signed in to change notification settings - Fork 1
/
context-req.go
69 lines (59 loc) · 1.46 KB
/
context-req.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
package goa
import (
"bytes"
"io/ioutil"
"net"
"strings"
)
const reqBodyKey = "requestBody"
func (c *ContextBeforeLookup) ParseForm() error {
c.RequestBody() // record the request body
return c.Request.ParseForm()
}
func (c *ContextBeforeLookup) RequestBody() ([]byte, error) {
if data, ok := c.data[reqBodyKey]; ok {
if body, ok := data.([]byte); ok {
return body, nil
}
return nil, nil
}
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.SetError(err)
return nil, err
}
if c.data == nil {
c.data = make(map[string]interface{})
}
c.data[reqBodyKey] = body
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return body, nil
}
func (c *ContextBeforeLookup) Scheme() string {
if proto := c.Request.Header.Get("X-Forwarded-Proto"); proto != `` {
return proto
}
return `http`
}
func (c *ContextBeforeLookup) Origin() string {
return c.Scheme() + "://" + c.Request.Host
}
func (c *ContextBeforeLookup) Url() string {
return c.Origin() + c.Request.URL.RequestURI()
}
func (c *ContextBeforeLookup) ClientAddr() string {
if addrs := c.Request.Header.Get("X-Forwarded-For"); addrs != `` {
addr := strings.SplitN(addrs, `, `, 2)[0]
if addr != `unknown` {
return addr
}
}
if addr := c.Request.Header.Get("X-Real-IP"); addr != `` && addr != `unknown` {
return addr
}
host, _, _ := net.SplitHostPort(c.RemoteAddr)
return host
}
func (c *ContextBeforeLookup) RequestId() string {
return c.Request.Header.Get("X-Request-Id")
}