-
Notifications
You must be signed in to change notification settings - Fork 1
/
RpcHttpRequest.go
182 lines (147 loc) · 4.35 KB
/
RpcHttpRequest.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package jrm1
import (
"encoding/json"
"log"
"net/http"
"time"
mime "github.com/vault-thirteen/auxie/MIME"
"github.com/vault-thirteen/auxie/header"
)
// RpcHttpRequest is an RPC request originated from an HTTP request.
type RpcHttpRequest struct {
// Processor.
p *Processor
// Processor settings.
settings *ProcessorSettings
// HTTP request from which the RPC request was received.
req *http.Request
// HTTP response writer used for communication with client.
rw http.ResponseWriter
// Time of start of the request processing.
tStart time.Time
// Time spent on processing a successful function call.
// Is set in milliseconds.
tDuration int64
// RPC request.
rr *RpcRequest
// RPC response.
resp *RpcResponse
}
// NewRpcHttpRequest is a simple constructor of an RPC request originated from
// an HTTP request.
func NewRpcHttpRequest(p *Processor, settings *ProcessorSettings, req *http.Request, rw http.ResponseWriter) (rhr *RpcHttpRequest) {
rhr = &RpcHttpRequest{
p: p,
settings: settings,
req: req,
rw: rw,
}
return rhr
}
// init reads a request from HTTP body, checks it and searches for the
// requested function. If error occurs, it responds to the client via HTTP.
// If request is correct and ready to be processed further, 'True' is returned.
// When 'False' is returned, the caller must stop serving the request.
func (r *RpcHttpRequest) init() (proceed bool) {
r.startTimer()
r.p.incAllRequestsCounter()
if !checkHttpRequest(r.rw, r.req) {
return false
}
r.resp = NewRpcResponse()
var err error
r.rr, err = NewRpcRequest(r.req.Body)
if err != nil {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_RequestIsNotReadable)
r.respond()
return false
}
r.resp.Id = r.rr.Id
var ok bool
ok = r.rr.HasAllRootFields()
if !ok {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_InvalidRequest)
r.respond()
return false
}
err = r.rr.CheckProtocolVersion()
if err != nil {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_UnsupportedProtocol)
r.respond()
return false
}
err = r.p.FindFunc(*r.rr.Method)
if err != nil {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_UnknownMethod)
r.respond()
return false
}
return true
}
// startTimer starts the timer.
func (r *RpcHttpRequest) startTimer() {
if r.settings.isDurationEnabled() {
r.tStart = time.Now()
}
}
// run calls the requested function and collects the result. If error occurs,
// it responds to the client via HTTP. If everything is correct and ready to be
// processed further, 'True' is returned. When 'False' is returned, the caller
// must stop serving the request.
func (r *RpcHttpRequest) run() (proceed bool) {
var err error
if r.settings.isRequestIdShown() {
err = r.resp.Meta.AddField(*r.settings.RequestIdFieldName, *r.rr.Id)
if err != nil {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_InternalRpcError)
r.respond()
return false
}
}
r.resp.Result, r.resp.Error = r.p.RunFunc(*r.rr.Method, r.rr.Parameters, r.resp.Meta)
if r.settings.isRequestIdShown() {
err = r.resp.Meta.RemoveField(*r.settings.RequestIdFieldName)
if err != nil {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_InternalRpcError)
r.respond()
return false
}
}
if !r.stopTimer() {
return false
}
return true
}
// stopTimer stops the timer and saves the duration as a meta-data field. If
// error occurs, it responds to the client via HTTP. If everything is correct
// and ready to be processed further, 'True' is returned. When 'False' is
// returned, the caller must stop serving the request.
func (r *RpcHttpRequest) stopTimer() (proceed bool) {
if r.settings.isDurationEnabled() {
r.tDuration = time.Now().Sub(r.tStart).Milliseconds()
err := r.resp.Meta.AddField(*r.p.settings.DurationFieldName, r.tDuration)
if err != nil {
r.resp.Error = NewRpcErrorFast(RpcErrorCode_InternalRpcError)
r.respond()
return false
}
}
return true
}
// respond analyses the result and responds to the client via HTTP. The caller
// must stop serving the request after this function returns.
func (r *RpcHttpRequest) respond() {
// Empty meta-data set must not be shown.
if len(*r.resp.Meta) == 0 {
r.resp.Meta = nil
}
if !r.resp.hasError() {
r.resp.OK = true
r.p.incSuccessfulRequestsCounter()
}
r.rw.Header().Set(header.HttpHeaderContentType, mime.TypeApplicationJson)
err := json.NewEncoder(r.rw).Encode(r.resp)
if err != nil {
log.Println(err)
}
}