-
Notifications
You must be signed in to change notification settings - Fork 1
/
petition.go
executable file
·126 lines (115 loc) · 3.68 KB
/
petition.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package gridas
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"code.google.com/p/go-uuid/uuid"
"labix.org/v2/mgo/bson"
"github.com/crbrox/gridas/mylog"
)
//Petition is a representation from the request received. Header fields are cooked to represent
//the final request meant to be sent to the target host. The relayer's own fields are removed
type Petition struct {
ID string `json:"id"`
TraceID string `json:"traceid"`
TargetHost string `json:"targethost"`
TargetScheme string `json:"targetscheme"`
Method string `json:"method"` // GET, POST, PUT, etc.
URL *url.URL `json:"-"`
URLString string `json:"urlstring"`
Proto string `json:"proto"` // "HTTP/1.0"
Header http.Header `json:"header"`
Trailer http.Header `json:"trailer"`
Body []byte `json:"body"`
RemoteAddr string `json:"remoteaddr"`
RequestURI string `json:"requesturi"`
Host string `json:"host"`
Created time.Time `json:"created"`
}
//newPetition creates a petition from an http.Request. It checks header fields and make necessary transformations.
//The body is read and saved as a slice of byte.
func newPetition(original *http.Request) (*Petition, error) {
targetHost := original.Header.Get(RelayerHost)
if targetHost == "" {
return nil, fmt.Errorf("gridas: Missing mandatory header %s", RelayerHost)
}
original.Header.Del(RelayerHost)
scheme := strings.ToLower(original.Header.Get(RelayerProtocol))
switch scheme {
case "http", "https":
case "":
scheme = "http"
default:
mylog.Debug("unsupported protocol", scheme)
return nil, fmt.Errorf("gridas: unsupported protocol %s", scheme)
}
original.Header.Del(RelayerProtocol)
traceID := original.Header.Get(RelayerTraceID)
if traceID == "" {
//Just in case an older version client using "Topic"
traceID = original.Header.Get(RelayerTopic)
}
original.Header.Del(RelayerTraceID)
original.Header.Del(RelayerTopic)
//Delete older header fields, ignore them, do nothing yet
original.Header.Del(RelayerProxy)
original.Header.Del(RelayerRetry)
{
//Hack for clients of older version
const HTTPS = "https://"
const HTTPSLen = len(HTTPS)
const HTTP = "http://"
const HTTPLen = len(HTTP)
if strings.HasPrefix(targetHost, HTTPS) {
targetHost = targetHost[HTTPSLen:]
scheme = "https"
} else if strings.HasPrefix(targetHost, HTTP) {
targetHost = targetHost[HTTPLen:]
scheme = "http"
}
}
//save body content
body, err := ioutil.ReadAll(original.Body)
if err != nil {
mylog.Debugf("error reading body request %v %+v", err, original)
return nil, err
}
id := uuid.New()
relayedRequest := &Petition{
ID: id,
Body: body,
Method: original.Method,
URL: original.URL,
Proto: original.Proto, // "HTTP/1.0"
Header: original.Header,
Trailer: original.Trailer,
RemoteAddr: original.RemoteAddr,
RequestURI: original.RequestURI,
TargetHost: targetHost,
TargetScheme: scheme,
Created: bson.Now(),
TraceID: traceID}
return relayedRequest, nil
}
//Request returns the original http.Request with the body restored as a CloserReader
//so it can be used to do a request to the original target host
func (p *Petition) Request() (*http.Request, error) {
p.URL.Host = p.TargetHost
p.URL.Scheme = p.TargetScheme
p.URLString = p.URL.String()
req, err := http.NewRequest(
p.Method,
p.URLString,
ioutil.NopCloser(bytes.NewReader(p.Body))) //Restore body as ReadCloser
if err != nil {
mylog.Debugf("error restoring request %v %+v", err, p)
return nil, err
}
req.Header = p.Header
req.Trailer = p.Trailer
return req, nil
}