Live video on the AT Protocol
1package misttriggers
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/url"
8
9 "github.com/golang/glog"
10 "stream.place/streamplace/pkg/errors"
11)
12
13type PushRewritePayload struct {
14 FullURL string
15 URL *url.URL
16 Hostname string
17 StreamName string
18}
19
20func ParsePushRewritePayload(payload MistTriggerBody) (PushRewritePayload, error) {
21 lines := payload.Lines()
22 if len(lines) != 3 {
23 return PushRewritePayload{}, fmt.Errorf("expected 3 lines in PUSH_REWRITE payload but got %d. Payload: %s", len(lines), payload)
24 }
25
26 u, err := url.Parse(lines[0])
27 if err != nil {
28 return PushRewritePayload{}, fmt.Errorf("unparsable URL in PUSH_REWRITE payload err=%s payload=%s", err, payload)
29 }
30
31 return PushRewritePayload{
32 FullURL: lines[0],
33 URL: u,
34 Hostname: lines[1],
35 StreamName: lines[2],
36 }, nil
37}
38
39func (d *MistCallbackHandlersCollection) TriggerPushRewrite(ctx context.Context, w http.ResponseWriter, req *http.Request, body MistTriggerBody) {
40 payload, err := ParsePushRewritePayload(body)
41 if err != nil {
42 glog.Infof("Error parsing PUSH_REWRITE payload error=%q payload=%q", err, string(body))
43 errors.WriteHTTPBadRequest(w, "Error parsing PUSH_REWRITE payload", err)
44 return
45 }
46 resp, err := d.broker.TriggerPushRewrite(ctx, &payload)
47 if err != nil {
48 glog.Infof("Error handling PUSH_REWRITE payload error=%q payload=%q", err, string(body))
49 errors.WriteHTTPInternalServerError(w, "Error handling PUSH_REWRITE payload", err)
50 return
51 }
52 // Flushing necessary here for Mist to handle an empty response body
53 flusher := w.(http.Flusher)
54 flusher.Flush()
55 w.Write([]byte(resp)) // nolint:errcheck
56}