Skip to content

Commit

Permalink
webrtc, hls: use absolute paths in Location header (#3195) (#3177) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Apr 14, 2024
1 parent a18bebf commit 665e11a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 111 deletions.
22 changes: 0 additions & 22 deletions internal/protocols/httpp/location_with_trailing_slash.go

This file was deleted.

43 changes: 0 additions & 43 deletions internal/protocols/httpp/location_with_trailing_slash_test.go

This file was deleted.

44 changes: 20 additions & 24 deletions internal/protocols/webrtc/whip_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *WHIPClient) Publish(
videoTrack format.Format,
audioTrack format.Format,
) ([]*OutgoingTrack, error) {
iceServers, err := c.optionsICEServers(ctx, c.URL.String())
iceServers, err := c.optionsICEServers(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (c *WHIPClient) Publish(
return nil, err
}

res, err := c.postOffer(ctx, c.URL.String(), offer)
res, err := c.postOffer(ctx, offer)
if err != nil {
c.pc.Close()
return nil, err
Expand All @@ -81,7 +81,7 @@ func (c *WHIPClient) Publish(

err = c.pc.SetAnswer(res.Answer)
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}
Expand All @@ -93,9 +93,9 @@ outer:
for {
select {
case ca := <-c.pc.NewLocalCandidate():
err := c.patchCandidate(ctx, c.URL.String(), offer, res.ETag, ca)
err := c.patchCandidate(ctx, offer, res.ETag, ca)
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}
Expand All @@ -106,7 +106,7 @@ outer:
break outer

case <-t.C:
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, fmt.Errorf("deadline exceeded while waiting connection")
}
Expand All @@ -117,7 +117,7 @@ outer:

// Read reads tracks.
func (c *WHIPClient) Read(ctx context.Context) ([]*IncomingTrack, error) {
iceServers, err := c.optionsICEServers(ctx, c.URL.String())
iceServers, err := c.optionsICEServers(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func (c *WHIPClient) Read(ctx context.Context) ([]*IncomingTrack, error) {
return nil, err
}

res, err := c.postOffer(ctx, c.URL.String(), offer)
res, err := c.postOffer(ctx, offer)
if err != nil {
c.pc.Close()
return nil, err
Expand All @@ -162,22 +162,22 @@ func (c *WHIPClient) Read(ctx context.Context) ([]*IncomingTrack, error) {
var sdp sdp.SessionDescription
err = sdp.Unmarshal([]byte(res.Answer.SDP))
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}

// check that there are at most two tracks
_, err = TrackCount(sdp.MediaDescriptions)
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}

err = c.pc.SetAnswer(res.Answer)
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}
Expand All @@ -189,9 +189,9 @@ outer:
for {
select {
case ca := <-c.pc.NewLocalCandidate():
err := c.patchCandidate(ctx, c.URL.String(), offer, res.ETag, ca)
err := c.patchCandidate(ctx, offer, res.ETag, ca)
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}
Expand All @@ -202,15 +202,15 @@ outer:
break outer

case <-t.C:
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, fmt.Errorf("deadline exceeded while waiting connection")
}
}

tracks, err := c.pc.GatherIncomingTracks(ctx, 0)
if err != nil {
c.deleteSession(context.Background(), c.URL.String()) //nolint:errcheck
c.deleteSession(context.Background()) //nolint:errcheck
c.pc.Close()
return nil, err
}
Expand All @@ -220,7 +220,7 @@ outer:

// Close closes the client.
func (c *WHIPClient) Close() error {
err := c.deleteSession(context.Background(), c.URL.String())
err := c.deleteSession(context.Background())
c.pc.Close()
return err
}
Expand All @@ -238,9 +238,8 @@ func (c *WHIPClient) Wait(ctx context.Context) error {

func (c *WHIPClient) optionsICEServers(
ctx context.Context,
ur string,
) ([]webrtc.ICEServer, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodOptions, ur, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodOptions, c.URL.String(), nil)
if err != nil {
return nil, err
}
Expand All @@ -266,10 +265,9 @@ type whipPostOfferResponse struct {

func (c *WHIPClient) postOffer(
ctx context.Context,
ur string,
offer *webrtc.SessionDescription,
) (*whipPostOfferResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ur, bytes.NewReader([]byte(offer.SDP)))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.URL.String(), bytes.NewReader([]byte(offer.SDP)))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -322,7 +320,6 @@ func (c *WHIPClient) postOffer(

func (c *WHIPClient) patchCandidate(
ctx context.Context,
ur string,
offer *webrtc.SessionDescription,
etag string,
candidate *webrtc.ICECandidateInit,
Expand All @@ -332,7 +329,7 @@ func (c *WHIPClient) patchCandidate(
return err
}

req, err := http.NewRequestWithContext(ctx, http.MethodPatch, ur, bytes.NewReader(frag))
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, c.URL.String(), bytes.NewReader(frag))
if err != nil {
return err
}
Expand All @@ -355,9 +352,8 @@ func (c *WHIPClient) patchCandidate(

func (c *WHIPClient) deleteSession(
ctx context.Context,
ur string,
) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, ur, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.URL.String(), nil)
if err != nil {
return err
}
Expand Down
10 changes: 9 additions & 1 deletion internal/servers/hls/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ var hlsIndex []byte
//go:embed hls.min.js
var hlsMinJS []byte

func mergePathAndQuery(path string, rawQuery string) string {
res := path
if rawQuery != "" {
res += "?" + rawQuery
}
return res
}

type httpServer struct {
address string
encryption bool
Expand Down Expand Up @@ -134,7 +142,7 @@ func (s *httpServer) onRequest(ctx *gin.Context) {
dir, fname = pa, ""

if !strings.HasSuffix(dir, "/") {
ctx.Writer.Header().Set("Location", httpp.LocationWithTrailingSlash(ctx.Request.URL))
ctx.Writer.Header().Set("Location", mergePathAndQuery(ctx.Request.URL.Path+"/", ctx.Request.URL.RawQuery))
ctx.Writer.WriteHeader(http.StatusMovedPermanently)
return
}
Expand Down

0 comments on commit 665e11a

Please sign in to comment.