forked from birkirb/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
488 lines (424 loc) · 13 KB
/
http.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"context"
"mime/multipart"
"path/filepath"
)
// Request is Revel's HTTP request object structure
type Request struct {
In ServerRequest // The server request
Header *RevelHeader // The revel header
ContentType string // The content type
Format string // The output format "html", "xml", "json", or "txt"
AcceptLanguages AcceptLanguages // The languages to accept
Locale string // THe locale
WebSocket ServerWebSocket // The websocket
Method string // The method
RemoteAddr string // The remote address
Host string // The host
// URL request path from the server (built)
URL *url.URL // The url
// DEPRECATED use GetForm()
Form url.Values // The Form
// DEPRECATED use GetMultipartForm()
MultipartForm *MultipartForm // The multipart form
controller *Controller // The controller, so some of this data can be fetched
}
var FORM_NOT_FOUND = errors.New("Form Not Found")
var httpLog = RevelLog.New("section", "http")
// Response is Revel's HTTP response object structure
type Response struct {
Status int
ContentType string
Out OutResponse
writer io.Writer
}
// The output response
type OutResponse struct {
// internalHeader.Server Set by ServerResponse.Get(HTTP_SERVER_HEADER), saves calling the get every time the header needs to be written to
internalHeader *RevelHeader // The internal header
Server ServerResponse // The server response
response *Response // The response
}
// The header defined in Revel
type RevelHeader struct {
Server ServerHeader // The server
}
// NewResponse wraps ServerResponse inside a Revel's Response and returns it
func NewResponse(w ServerResponse) (r *Response) {
r = &Response{Out: OutResponse{Server: w, internalHeader: &RevelHeader{}}}
r.Out.response = r
return r
}
// NewRequest returns a Revel's HTTP request instance with given HTTP instance
func NewRequest(r ServerRequest) *Request {
req := &Request{Header: &RevelHeader{}}
if r != nil {
req.SetRequest(r)
}
return req
}
func (req *Request) SetRequest(r ServerRequest) {
req.In = r
if h, e := req.In.Get(HTTP_SERVER_HEADER); e == nil {
req.Header.Server = h.(ServerHeader)
}
req.URL, _ = req.GetValue(HTTP_URL).(*url.URL)
req.ContentType = ResolveContentType(req)
req.Format = ResolveFormat(req)
req.AcceptLanguages = ResolveAcceptLanguage(req)
req.Method, _ = req.GetValue(HTTP_METHOD).(string)
req.RemoteAddr, _ = req.GetValue(HTTP_REMOTE_ADDR).(string)
req.Host, _ = req.GetValue(HTTP_HOST).(string)
}
// Returns a cookie
func (req *Request) Cookie(key string) (ServerCookie, error) {
if req.Header.Server != nil {
return req.Header.Server.GetCookie(key)
}
return nil, http.ErrNoCookie
}
// Fetch the requested URI
func (req *Request) GetRequestURI() string {
uri, _ := req.GetValue(HTTP_REQUEST_URI).(string)
return uri
}
// Fetch the query
func (req *Request) GetQuery() (v url.Values) {
v, _ = req.GetValue(ENGINE_PARAMETERS).(url.Values)
return
}
// Fetch the path
func (req *Request) GetPath() (path string) {
path, _ = req.GetValue(ENGINE_PATH).(string)
return
}
// Fetch the body
func (req *Request) GetBody() (body io.Reader) {
body, _ = req.GetValue(HTTP_BODY).(io.Reader)
return
}
// Fetch the context
func (req *Request) Context() (c context.Context) {
c, _ = req.GetValue(HTTP_REQUEST_CONTEXT).(context.Context)
return
}
// Deprecated use controller.Params.Get()
func (req *Request) FormValue(key string) (value string) {
return req.controller.Params.Get(key)
}
// Deprecated use controller.Params.Form[Key]
func (req *Request) PostFormValue(key string) (value string) {
valueList := req.controller.Params.Form[key]
if len(valueList) > 0 {
value = valueList[0]
}
return
}
// Deprecated use GetForm() instead
func (req *Request) ParseForm() (e error) {
if req.Form == nil {
req.Form, e = req.GetForm()
}
return
}
func (req *Request) GetForm() (url.Values, error) {
if form, err := req.In.Get(HTTP_FORM); err != nil {
return nil, err
} else if values, found := form.(url.Values); found {
req.Form = values
return values, nil
}
return nil, FORM_NOT_FOUND
}
// Deprecated for backwards compatibility only
type MultipartForm struct {
File map[string][]*multipart.FileHeader
Value url.Values
origin ServerMultipartForm
}
func (req *Request) MultipartReader() (*multipart.Reader, error) {
return nil, errors.New("MultipartReader not supported, use controller.Param")
}
// Deprecated for backwards compatibility only
func newMultipareForm(s ServerMultipartForm) (f *MultipartForm) {
return &MultipartForm{File: s.GetFiles(), Value: s.GetValues(), origin: s}
}
// Deprecated use GetMultipartForm() instead
func (req *Request) ParseMultipartForm(_ int64) (e error) {
var s ServerMultipartForm
if s, e = req.GetMultipartForm(); e == nil {
req.MultipartForm = newMultipareForm(s)
}
return
}
// Return the args for the controller
func (req *Request) Args() map[string]interface{} {
return req.controller.Args
}
// Return a multipart form
func (req *Request) GetMultipartForm() (ServerMultipartForm, error) {
if form, err := req.In.Get(HTTP_MULTIPART_FORM); err != nil {
return nil, err
} else if values, found := form.(ServerMultipartForm); found {
return values, nil
}
return nil, FORM_NOT_FOUND
}
// Destroy the request
func (req *Request) Destroy() {
req.In = nil
req.ContentType = ""
req.Format = ""
req.AcceptLanguages = nil
req.Method = ""
req.RemoteAddr = ""
req.Host = ""
req.Header.Destroy()
req.URL = nil
req.Form = nil
req.MultipartForm = nil
}
// Set the server response
func (resp *Response) SetResponse(r ServerResponse) {
resp.Out.Server = r
if h, e := r.Get(HTTP_SERVER_HEADER); e == nil {
resp.Out.internalHeader.Server, _ = h.(ServerHeader)
}
}
// Destroy the output response
func (o *OutResponse) Destroy() {
o.response = nil
o.internalHeader.Destroy()
}
// Destroy the RevelHeader
func (h *RevelHeader) Destroy() {
h.Server = nil
}
// Destroy the Response
func (resp *Response) Destroy() {
resp.Out.Destroy()
resp.Status = 0
resp.ContentType = ""
resp.writer = nil
}
// UserAgent returns the client's User-Agent header string.
func (r *Request) UserAgent() string {
return r.Header.Get("User-Agent")
}
// Referer returns the client's Referer header string.
func (req *Request) Referer() string {
return req.Header.Get("Referer")
}
// Return the httpheader for the key
func (req *Request) GetHttpHeader(key string) string {
return req.Header.Get(key)
}
// Return the value from the server
func (r *Request) GetValue(key int) (value interface{}) {
value, _ = r.In.Get(key)
return
}
// WriteHeader writes the header (for now, just the status code).
// The status may be set directly by the application (c.Response.Status = 501).
// If it isn't, then fall back to the provided status code.
func (resp *Response) WriteHeader(defaultStatusCode int, defaultContentType string) {
if resp.ContentType == "" {
resp.ContentType = defaultContentType
}
resp.Out.internalHeader.Set("Content-Type", resp.ContentType)
if resp.Status == 0 {
resp.Status = defaultStatusCode
}
resp.SetStatus(resp.Status)
}
func (resp *Response) SetStatus(statusCode int) {
if resp.Out.internalHeader.Server != nil {
resp.Out.internalHeader.Server.SetStatus(statusCode)
} else {
resp.Out.Server.Set(ENGINE_RESPONSE_STATUS, statusCode)
}
}
// Return the writer
func (resp *Response) GetWriter() (writer io.Writer) {
writer = resp.writer
if writer == nil {
if w, e := resp.Out.Server.Get(ENGINE_WRITER); e == nil {
writer, resp.writer = w.(io.Writer), w.(io.Writer)
}
}
return
}
// Replace the writer
func (resp *Response) SetWriter(writer io.Writer) bool {
resp.writer = writer
// Leave it up to the engine to flush and close the writer
return resp.Out.Server.Set(ENGINE_WRITER, writer)
}
// Passes full control to the response to the caller - terminates any initial writes
func (resp *Response) GetStreamWriter() (writer StreamWriter) {
if w, e := resp.Out.Server.Get(HTTP_STREAM_WRITER); e == nil {
writer = w.(StreamWriter)
}
return
}
// Return the header
func (o *OutResponse) Header() *RevelHeader {
return o.internalHeader
}
// Write the header out
func (o *OutResponse) Write(data []byte) (int, error) {
return o.response.GetWriter().Write(data)
}
// Set a value in the header
func (h *RevelHeader) Set(key, value string) {
if h.Server != nil {
h.Server.Set(key, value)
}
}
// Add a key to the header
func (h *RevelHeader) Add(key, value string) {
if h.Server != nil {
h.Server.Add(key, value)
}
}
// Set a cookie in the header
func (h *RevelHeader) SetCookie(cookie string) {
if h.Server != nil {
h.Server.SetCookie(cookie)
}
}
// Set the status for the header
func (h *RevelHeader) SetStatus(status int) {
if h.Server != nil {
h.Server.SetStatus(status)
}
}
// Get a key from the header
func (h *RevelHeader) Get(key string) (value string) {
values := h.GetAll(key)
if len(values) > 0 {
value = values[0]
}
return
}
// GetAll returns []string of items (the header split by a comma)
func (h *RevelHeader) GetAll(key string) (values []string) {
if h.Server != nil {
values = h.Server.Get(key)
}
return
}
// ResolveContentType gets the content type.
// e.g. From "multipart/form-data; boundary=--" to "multipart/form-data"
// If none is specified, returns "text/html" by default.
func ResolveContentType(req *Request) string {
contentType := req.Header.Get("Content-Type")
if contentType == "" {
return "text/html"
}
return strings.ToLower(strings.TrimSpace(strings.Split(contentType, ";")[0]))
}
// ResolveFormat maps the request's Accept MIME type declaration to
// a Request.Format attribute, specifically "html", "xml", "json", or "txt",
// returning a default of "html" when Accept header cannot be mapped to a
// value above.
func ResolveFormat(req *Request) string {
ext := strings.ToLower(filepath.Ext(req.GetPath()))
switch ext {
case ".html":
return "html"
case ".json":
return "json"
case ".xml":
return "xml"
case ".txt":
return "txt"
}
accept := req.GetHttpHeader("accept")
switch {
case accept == "",
strings.HasPrefix(accept, "*/*"), // */
strings.Contains(accept, "application/xhtml"),
strings.Contains(accept, "text/html"):
return "html"
case strings.Contains(accept, "application/json"),
strings.Contains(accept, "text/javascript"),
strings.Contains(accept, "application/javascript"):
return "json"
case strings.Contains(accept, "application/xml"),
strings.Contains(accept, "text/xml"):
return "xml"
case strings.Contains(accept, "text/plain"):
return "txt"
}
return "html"
}
// AcceptLanguage is a single language from the Accept-Language HTTP header.
type AcceptLanguage struct {
Language string
Quality float32
}
// AcceptLanguages is collection of sortable AcceptLanguage instances.
type AcceptLanguages []AcceptLanguage
func (al AcceptLanguages) Len() int { return len(al) }
func (al AcceptLanguages) Swap(i, j int) { al[i], al[j] = al[j], al[i] }
func (al AcceptLanguages) Less(i, j int) bool { return al[i].Quality > al[j].Quality }
func (al AcceptLanguages) String() string {
output := bytes.NewBufferString("")
for i, language := range al {
if _, err := output.WriteString(fmt.Sprintf("%s (%1.1f)", language.Language, language.Quality)); err != nil {
httpLog.Error("String: WriteString failed:", "error", err)
}
if i != len(al)-1 {
if _, err := output.WriteString(", "); err != nil {
httpLog.Error("String: WriteString failed:", "error", err)
}
}
}
return output.String()
}
// ResolveAcceptLanguage returns a sorted list of Accept-Language
// header values.
//
// The results are sorted using the quality defined in the header for each
// language range with the most qualified language range as the first
// element in the slice.
//
// See the HTTP header fields specification
// (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4) for more details.
func ResolveAcceptLanguage(req *Request) AcceptLanguages {
header := req.Header.Get("Accept-Language")
if header == "" {
return req.AcceptLanguages
}
acceptLanguageHeaderValues := strings.Split(header, ",")
acceptLanguages := make(AcceptLanguages, len(acceptLanguageHeaderValues))
for i, languageRange := range acceptLanguageHeaderValues {
if qualifiedRange := strings.Split(languageRange, ";q="); len(qualifiedRange) == 2 {
quality, err := strconv.ParseFloat(qualifiedRange[1], 32)
if err != nil {
httpLog.Warn("Detected malformed Accept-Language header quality in assuming quality is 1", "languageRange", languageRange)
acceptLanguages[i] = AcceptLanguage{qualifiedRange[0], 1}
} else {
acceptLanguages[i] = AcceptLanguage{qualifiedRange[0], float32(quality)}
}
} else {
acceptLanguages[i] = AcceptLanguage{languageRange, 1}
}
}
sort.Sort(acceptLanguages)
return acceptLanguages
}