/
httphandler.go
255 lines (221 loc) · 6.41 KB
/
httphandler.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cocaine12
import (
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
"github.com/ugorji/go/codec"
)
var (
mh codec.MsgpackHandle
h = &mh
)
var (
mhHTTPReq codec.MsgpackHandle
hHTTPReq = &mhHTTPReq
)
// Headers are packed as a tuple of tuples HTTP headers from Cocaine
type Headers [][2]string
// type HTTPReq struct {
// *http.Request
// }
// UnpackProxyRequest unpacks a HTTPRequest from a serialized cocaine form
func UnpackProxyRequest(raw []byte) (*http.Request, error) {
var v struct {
Method string
URI string
Version string
Headers Headers
Body []byte
}
if err := codec.NewDecoderBytes(raw, hHTTPReq).Decode(&v); err != nil {
return nil, err
}
req, err := http.NewRequest(v.Method, v.URI, bytes.NewBuffer(v.Body))
if err != nil {
return nil, err
}
req.Header = HeadersCocaineToHTTP(v.Headers)
req.Host = req.Header.Get("Host")
if xRealIP := req.Header.Get("X-Real-IP"); xRealIP != "" {
req.RemoteAddr = xRealIP
}
// If body is compressed it will be decompressed
// Inspired by https://github.com/golang/go/blob/master/src/net/http/transport.go#L883
hasBody := req != nil && req.Method != "HEAD" && req.ContentLength != 0
if hasBody && req.Header.Get("Content-Encoding") == "gzip" {
req.Header.Del("Content-Encoding")
req.Header.Del("Content-Length")
req.ContentLength = -1
req.Body = &gzipReader{body: req.Body}
}
return req, nil
}
// WriteHead converts the HTTP status code and the headers to the cocaine format
func WriteHead(code int, headers Headers) []byte {
var out []byte
codec.NewEncoderBytes(&out, h).Encode([]interface{}{code, headers})
return out
}
// HeadersHTTPtoCocaine converts net/http.Header to Cocaine representation
func HeadersHTTPtoCocaine(header http.Header) Headers {
hdr := make(Headers, 0, len(header))
for headerName, headerValues := range header {
for _, headerValue := range headerValues {
hdr = append(hdr, [2]string{headerName, headerValue})
}
}
return hdr
}
// HeadersCocaineToHTTP converts Cocaine representation of headers to net/http.Header
func HeadersCocaineToHTTP(hdr Headers) http.Header {
header := make(http.Header, len(hdr))
for _, hdrValues := range hdr {
header.Add(hdrValues[0], hdrValues[1])
}
return header
}
// WrapHandler provides opportunity for using Go web frameworks, which supports http.Handler interface
//
// Trivial example which is used martini web framework
//
// import (
// "github.com/cocaine/cocaine-framework-go/cocaine"
// "github.com/codegangsta/martini"
// )
//
//
// func main() {
// m := martini.Classic()
// m.Get("", func() string {
// return "This is an example server"
// })
//
// m.Get("/hw", func() string {
// return "Hello world!"
// })
//
// binds := map[string]cocaine.EventHandler{
// "example": cocaine.WrapHandler(m, nil),
// }
// if worker, err := cocaine.NewWorker(); err == nil{
// worker.Loop(binds)
// }else{
// panic(err)
// }
// }
func WrapHandler(handler http.Handler) EventHandler {
return func(ctx context.Context, request Request, response Response) {
defer response.Close()
w, httpRequest, err := convertToHTTPFunc(ctx, request, response)
if err != nil {
return
}
handler.ServeHTTP(w, httpRequest)
w.finishRequest()
}
}
func WrapHTTPFunc(handler func(ctx context.Context, w http.ResponseWriter, req *http.Request)) EventHandler {
return func(ctx context.Context, request Request, response Response) {
defer response.Close()
w, httpRequest, err := convertToHTTPFunc(ctx, request, response)
if err != nil {
return
}
handler(ctx, w, httpRequest)
w.finishRequest()
}
}
func WrapHTTPFuncs(hfs map[string]func(ctx context.Context, w http.ResponseWriter, req *http.Request)) map[string]EventHandler {
handlers := make(map[string]EventHandler, len(hfs))
for key, hf := range hfs {
handlers[key] = WrapHTTPFunc(hf)
}
return handlers
}
func convertToHTTPFunc(ctx context.Context, request Request, response Response) (*ResponseWriter, *http.Request, error) {
// Read the first chunk
// It consists of method, uri, httpversion, headers, body.
// They are packed by msgpack
msg, err := request.Read(ctx)
if err != nil {
if ctx.Err() != nil {
response.Write(WriteHead(http.StatusRequestTimeout, Headers{}))
response.Write([]byte("request was not received during a timeout"))
return nil, nil, ctx.Err()
}
response.Write(WriteHead(http.StatusBadRequest, Headers{}))
response.Write([]byte("cannot process request " + err.Error()))
return nil, nil, err
}
httpRequest, err := UnpackProxyRequest(msg)
if err != nil {
response.Write(WriteHead(http.StatusBadRequest, Headers{}))
response.Write([]byte("malformed request"))
return nil, nil, err
}
w := &ResponseWriter{
cRes: response,
req: httpRequest,
handlerHeader: make(http.Header),
contentLength: -1,
wroteHeader: false,
}
return w, httpRequest, nil
}
// WrapHandlerFunc provides opportunity for using Go web frameworks, which supports http.HandlerFunc interface
//
// Trivial example is
//
// import (
// "net/http"
// "github.com/cocaine/cocaine-framework-go/cocaine"
// )
//
// func handler(w http.ResponseWriter, req *http.Request) {
// w.Header().Set("Content-Type", "text/plain")
// w.Write([]byte("This is an example server.\n"))
// }
//
// func main() {
// binds := map[string]cocaine.EventHandler{
// "example": cocaine.WrapHandlerFunc(handler, nil),
// }
// if worker, err := cocaine.NewWorker(); err == nil{
// worker.Loop(binds)
// }else{
// panic(err)
// }
// }
//
func WrapHandlerFunc(hf http.HandlerFunc) EventHandler {
return WrapHandler(http.HandlerFunc(hf))
}
// WrapHandleFuncs wraps the bunch of http.Handlers to allow them handle requests via Worker
func WrapHandleFuncs(hfs map[string]http.HandlerFunc) map[string]EventHandler {
handlers := make(map[string]EventHandler, len(hfs))
for key, hf := range hfs {
handlers[key] = WrapHandlerFunc(hf)
}
return handlers
}
// inspired by https://github.com/golang/go/blob/master/src/net/http/transport.go#L1238
// gzipReader wraps a response body so it can lazily
// call gzip.NewReader on the first call to Read
type gzipReader struct {
body io.ReadCloser // underlying Response.Body
zr io.Reader // lazily-initialized gzip reader
}
func (gz *gzipReader) Read(p []byte) (n int, err error) {
if gz.zr == nil {
gz.zr, err = gzip.NewReader(gz.body)
if err != nil {
return 0, err
}
}
return gz.zr.Read(p)
}
func (gz *gzipReader) Close() error {
return gz.body.Close()
}