-
Notifications
You must be signed in to change notification settings - Fork 171
/
metadata.go
175 lines (156 loc) · 4.79 KB
/
metadata.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
/*
* Copyright (c) 2019-2021. 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 metadata
import (
"context"
"strings"
"golang.org/x/net/http/httpguts"
"github.com/pydio/cells/v4/common"
)
type metadataKey struct{}
type Metadata map[string]string
// FromContextCopy returns as COPY of the internal metadata, that can be subsequently modified
func FromContextCopy(ctx context.Context) (Metadata, bool) {
return fromContextCopy(ctx)
}
// FromContextRead returns internal metadata as is. Beware of not modifying it to avoid r/w concurrent access,
// in that case you must use FromContextCopy instead.
func FromContextRead(ctx context.Context) (Metadata, bool) {
return fromContext(ctx)
}
func fromContext(ctx context.Context) (Metadata, bool) {
val := ctx.Value(metadataKey{})
if val == nil {
return nil, false
}
md, ok := ctx.Value(metadataKey{}).(Metadata)
return md, ok
}
func fromContextCopy(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(metadataKey{}).(Metadata)
if !ok {
return nil, ok
}
// copy
mm := make(Metadata, len(md))
for k, v := range md {
mm[k] = v
}
return mm, ok
}
func NewContext(ctx context.Context, md map[string]string) context.Context {
// Make sure that keys are ok
for k, v := range md {
if k != strings.Title(k) {
delete(md, k)
md[strings.Title(k)] = v
}
}
return context.WithValue(ctx, metadataKey{}, Metadata(md))
}
// MinioMetaFromContext prepares metadata for minio client. If forEvents is passed, it replies with the complete set
// of metadata
func MinioMetaFromContext(ctx context.Context, forEvents ...bool) (md map[string]string, ok bool) {
eventsHeaders := false
if len(forEvents) > 0 && forEvents[0] {
eventsHeaders = true
}
md = make(map[string]string)
if meta, mOk := fromContext(ctx); mOk {
for k, v := range meta {
if !eventsHeaders && (strings.HasPrefix(strings.ToLower(k), "x-pydio") || strings.EqualFold(k, "CtxWorkspaceUuid")) {
continue
}
if strings.ToLower(k) == "x-pydio-claims" {
continue
}
if httpguts.ValidHeaderFieldName(k) && httpguts.ValidHeaderFieldValue(v) {
md[k] = v
}
}
}
if user := ctx.Value(common.PydioContextUserKey); user != nil && eventsHeaders {
md[common.PydioContextUserKey] = user.(string)
}
return md, len(md) > 0
}
// WithUserNameMetadata appends a username to both the context metadata and as context key.
func WithUserNameMetadata(ctx context.Context, userName string) context.Context {
md := make(map[string]string)
if meta, ok := fromContext(ctx); ok {
for k, v := range meta {
//k = strings.Title(k)
if strings.EqualFold(k, common.PydioContextUserKey) {
continue
}
md[k] = v
}
}
md[common.PydioContextUserKey] = userName
ctx = NewContext(ctx, md)
// Add it as value for easier use inside the gateway, but this will not be transmitted
ctx = context.WithValue(ctx, common.PydioContextUserKey, userName)
return ctx
}
// CanonicalMeta extract header name or its lowercase version
func CanonicalMeta(ctx context.Context, name string) (string, bool) {
if md, o := fromContext(ctx); o {
if val, ok := md[strings.Title(name)]; ok {
return val, true
}
}
return "", false
}
// WithAdditionalMetadata retrieves existing meta, adds new key/values to the map and produces a new context
// It enforces case-conflicts on all keys
func WithAdditionalMetadata(ctx context.Context, meta map[string]string) context.Context {
md := make(map[string]string)
if mm, ok := fromContext(ctx); ok {
for k, v := range mm {
ignore := false
for nk := range meta {
if strings.EqualFold(nk, k) {
ignore = true
break
}
}
if ignore {
continue
}
md[k] = v
}
}
for k, v := range meta {
md[k] = v
}
return NewContext(ctx, md)
}
func NewBackgroundWithUserKey(userName string) context.Context {
return NewContext(context.Background(), map[string]string{
common.PydioContextUserKey: userName,
})
}
func NewBackgroundWithMetaCopy(ctx context.Context) context.Context {
bgCtx := context.Background()
if ctxMeta, ok := fromContextCopy(ctx); ok {
bgCtx = NewContext(bgCtx, ctxMeta)
}
return bgCtx
}