/
headers.go
55 lines (45 loc) · 1.2 KB
/
headers.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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package http
import (
"context"
"github.com/cloudevents/sdk-go/v2/binding"
"net/http"
"net/textproto"
"strings"
"unicode"
"github.com/cloudevents/sdk-go/v2/binding/spec"
)
var attributeHeadersMapping map[string]string
type customHeaderKey int
const (
headerKey customHeaderKey = iota
)
func init() {
attributeHeadersMapping = make(map[string]string)
for _, v := range specs.Versions() {
for _, a := range v.Attributes() {
if a.Kind() == spec.DataContentType {
attributeHeadersMapping[a.Name()] = ContentType
} else {
attributeHeadersMapping[a.Name()] = textproto.CanonicalMIMEHeaderKey(prefix + a.Name())
}
}
}
}
func extNameToHeaderName(name string) string {
var b strings.Builder
b.Grow(len(name) + len(prefix))
b.WriteString(prefix)
b.WriteRune(unicode.ToUpper(rune(name[0])))
b.WriteString(name[1:])
return b.String()
}
func HeaderFrom(ctx context.Context) http.Header {
return binding.GetOrDefaultFromCtx(ctx, headerKey, make(http.Header)).(http.Header)
}
func WithCustomHeader(ctx context.Context, header http.Header) context.Context {
return context.WithValue(ctx, headerKey, header)
}