forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-to-meta.go
118 lines (106 loc) · 3.55 KB
/
http-to-meta.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package servicecontext
import (
"context"
"net/http"
"strings"
"time"
"github.com/micro/go-micro/metadata"
"github.com/pydio/cells/common"
)
const (
HttpMetaExtracted = "HttpMetaExtracted"
HttpMetaRemoteAddress = "RemoteAddress"
HttpMetaRequestMethod = "RequestMethod"
HttpMetaRequestURI = "RequestURI"
HttpMetaProtocol = "HttpProtocol"
HttpMetaUserAgent = "UserAgent"
HttpMetaContentType = "ContentType"
HttpMetaCookiesString = "CookiesString"
ClientTime = "ClientTime"
ServerTime = "ServerTime"
CtxWorkspaceUuid = "CtxWorkspaceUuid"
)
// HttpRequestInfoToMetadata extracts as much HTTP metadata as possible and stores it in the context as metadata.
func HttpRequestInfoToMetadata(ctx context.Context, req *http.Request) context.Context {
meta := metadata.Metadata{}
if existing, ok := metadata.FromContext(ctx); ok {
if _, already := existing[HttpMetaExtracted]; already {
return ctx
}
for k, v := range existing {
meta[k] = v
}
}
meta[HttpMetaExtracted] = HttpMetaExtracted
layout := "2006-01-02T15:04-0700"
t := time.Now()
meta[ServerTime] = t.Format(layout)
// We currently use server time instead of client time. TODO: Retrieve client time and locale and set it here.
meta[ClientTime] = t.Format(layout)
// We might want to also support new standard "Forwarded" header.
if h, ok := req.Header["X-Forwarded-For"]; ok {
ips := strings.Split(strings.Join(h, ""), ",")
meta[HttpMetaRemoteAddress] = ips[0]
} else if req.RemoteAddr != "" {
meta[HttpMetaRemoteAddress] = req.RemoteAddr
}
if h, ok := req.Header["User-Agent"]; ok {
meta[HttpMetaUserAgent] = strings.Join(h, "")
}
if h, ok := req.Header["Content-Type"]; ok {
meta[HttpMetaContentType] = strings.Join(h, "")
}
if h, ok := req.Header["X-Pydio-Span-Id"]; ok {
meta[SpanMetadataId] = strings.Join(h, "")
}
for _, key := range common.XSpecialPydioHeaders {
if h, ok := req.Header[key]; ok && len(h) > 0 {
meta[key] = h[0]
}
}
if req.RequestURI != "" {
meta[HttpMetaRequestURI] = req.RequestURI
}
if req.Method != "" {
meta[HttpMetaRequestMethod] = req.Method
}
if req.Proto != "" {
meta[HttpMetaProtocol] = req.Proto
}
if len(req.Cookies()) > 0 {
var cString []string
for _, c := range req.Cookies() {
if c.String() != "" {
cString = append(cString, c.String())
}
}
meta[HttpMetaCookiesString] = strings.Join(cString, "//")
}
return metadata.NewContext(ctx, meta)
}
// HttpMetaExtractorWrapper extracts data from the request and puts it in a context Metadata field.
func HttpMetaExtractorWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(HttpRequestInfoToMetadata(r.Context(), r))
h.ServeHTTP(w, r)
})
}