-
Notifications
You must be signed in to change notification settings - Fork 14
/
url_replace_handler.go
88 lines (72 loc) · 2.83 KB
/
url_replace_handler.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
package nethttplibrary
import (
abstractions "github.com/microsoft/kiota-abstractions-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"net/http"
"strings"
)
var urlReplaceOptionKey = abstractions.RequestOptionKey{Key: "UrlReplaceOptionKey"}
// UrlReplaceHandler is a middleware handler that replaces url segments in the uri path.
type UrlReplaceHandler struct {
options UrlReplaceOptions
}
// NewUrlReplaceHandler creates a configuration object for the CompressionHandler
func NewUrlReplaceHandler(enabled bool, replacementPairs map[string]string) *UrlReplaceHandler {
return &UrlReplaceHandler{UrlReplaceOptions{Enabled: enabled, ReplacementPairs: replacementPairs}}
}
// UrlReplaceOptions is a configuration object for the UrlReplaceHandler middleware
type UrlReplaceOptions struct {
Enabled bool
ReplacementPairs map[string]string
}
// GetKey returns UrlReplaceOptions unique name in context object
func (u *UrlReplaceOptions) GetKey() abstractions.RequestOptionKey {
return urlReplaceOptionKey
}
// GetReplacementPairs reads ReplacementPairs settings from UrlReplaceOptions
func (u *UrlReplaceOptions) GetReplacementPairs() map[string]string {
return u.ReplacementPairs
}
// IsEnabled reads Enabled setting from UrlReplaceOptions
func (u *UrlReplaceOptions) IsEnabled() bool {
return u.Enabled
}
type urlReplaceOptionsInt interface {
abstractions.RequestOption
IsEnabled() bool
GetReplacementPairs() map[string]string
}
// Intercept is invoked by the middleware pipeline to either move the request/response
// to the next middleware in the pipeline
func (c *UrlReplaceHandler) Intercept(pipeline Pipeline, middlewareIndex int, req *http.Request) (*http.Response, error) {
reqOption, ok := req.Context().Value(urlReplaceOptionKey).(urlReplaceOptionsInt)
if !ok {
reqOption = &c.options
}
obsOptions := GetObservabilityOptionsFromRequest(req)
ctx := req.Context()
var span trace.Span
if obsOptions != nil {
ctx, span = otel.GetTracerProvider().Tracer(obsOptions.GetTracerInstrumentationName()).Start(ctx, "UrlReplaceHandler_Intercept")
span.SetAttributes(attribute.Bool("com.microsoft.kiota.handler.url_replacer.enable", true))
defer span.End()
req = req.WithContext(ctx)
}
if !reqOption.IsEnabled() || len(reqOption.GetReplacementPairs()) == 0 {
return pipeline.Next(req, middlewareIndex)
}
req.URL.Path = ReplacePathTokens(req.URL.Path, reqOption.GetReplacementPairs())
if span != nil {
span.SetAttributes(attribute.String("http.request_url", req.RequestURI))
}
return pipeline.Next(req, middlewareIndex)
}
// ReplacePathTokens invokes token replacement logic on the given url path
func ReplacePathTokens(path string, replacementPairs map[string]string) string {
for key, value := range replacementPairs {
path = strings.Replace(path, key, value, 1)
}
return path
}