-
Notifications
You must be signed in to change notification settings - Fork 76
/
client_side_helpers.go
54 lines (46 loc) · 1.88 KB
/
client_side_helpers.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
package relay
import (
"bytes"
"encoding/base64"
"net/http"
"net/http/httptest"
"strconv"
"github.com/launchdarkly/ld-relay/v8/internal/basictypes"
"github.com/launchdarkly/ld-relay/v8/internal/browser"
"github.com/launchdarkly/ld-relay/v8/internal/events"
"github.com/launchdarkly/ld-relay/v8/internal/middleware"
"github.com/launchdarkly/ld-relay/v8/internal/util"
ldevents "github.com/launchdarkly/go-sdk-events/v3"
)
func getEventsImage(w http.ResponseWriter, req *http.Request) {
clientCtx := middleware.GetEnvContextInfo(req.Context())
if clientCtx.Env.GetEventDispatcher() == nil {
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write(util.ErrorJSONMsg("Event proxy is not enabled for this environment"))
return
}
handler := clientCtx.Env.GetEventDispatcher().GetHandler(basictypes.JSClientSDK, ldevents.AnalyticsEventDataKind)
if handler == nil { // COVERAGE: abnormal condition that can't be caused in unit tests
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write(util.ErrorJSONMsg("Event proxy for browser clients is not enabled for this environment"))
return
}
d := req.URL.Query().Get("d")
if d != "" {
go func() {
nullW := httptest.NewRecorder()
eventData, _ := base64.StdEncoding.DecodeString(d)
eventsReq, _ := http.NewRequest("POST", "", bytes.NewBuffer(eventData))
eventsReq.Header.Add("Content-Type", "application/json")
eventsReq.Header.Add("X-LaunchDarkly-User-Agent", eventsReq.Header.Get("X-LaunchDarkly-User-Agent"))
eventsReq.Header.Add(events.EventSchemaHeader, strconv.Itoa(events.SummaryEventsSchemaVersion))
handler(nullW, eventsReq)
}()
}
w.Header().Set("Content-Type", "image/gif")
_, _ = w.Write(browser.Transparent1PixelImageData)
}
func getGoals(w http.ResponseWriter, req *http.Request) {
clientCtx := middleware.GetEnvContextInfo(req.Context())
clientCtx.Env.GetJSClientContext().Proxy.ServeHTTP(w, req)
}