Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webrtc: fix error when charset is in Content-Type (#3126) #3318

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/protocols/httpp/content_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package httpp

import "strings"

// ParseContentType parses a Content-Type header and returns the content type.
func ParseContentType(v string) string {
return strings.TrimSpace(strings.Split(v, ";")[0])

Check warning on line 7 in internal/protocols/httpp/content_type.go

View check run for this annotation

Codecov / codecov/patch

internal/protocols/httpp/content_type.go#L6-L7

Added lines #L6 - L7 were not covered by tests
}
6 changes: 3 additions & 3 deletions internal/protocols/webrtc/whip_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"

"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/protocols/httpp"
)

// WHIPClient is a WHIP client.
Expand Down Expand Up @@ -286,8 +286,8 @@
return nil, fmt.Errorf("bad status code: %v", res.StatusCode)
}

contentType := res.Header.Get("Content-Type")
if strings.TrimSpace(strings.Split(contentType, ";")[0]) != "application/sdp" {
contentType := httpp.ParseContentType(req.Header.Get("Content-Type"))
if contentType != "application/sdp" {

Check warning on line 290 in internal/protocols/webrtc/whip_client.go

View check run for this annotation

Codecov / codecov/patch

internal/protocols/webrtc/whip_client.go#L289-L290

Added lines #L289 - L290 were not covered by tests
return nil, fmt.Errorf("bad Content-Type: expected 'application/sdp', got '%s'", contentType)
}

Expand Down
6 changes: 4 additions & 2 deletions internal/servers/webrtc/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func (s *httpServer) onWHIPOptions(ctx *gin.Context, pathName string, publish bo
}

func (s *httpServer) onWHIPPost(ctx *gin.Context, pathName string, publish bool) {
if ctx.Request.Header.Get("Content-Type") != "application/sdp" {
contentType := httpp.ParseContentType(ctx.Request.Header.Get("Content-Type"))
if contentType != "application/sdp" {
writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid Content-Type"))
return
}
Expand Down Expand Up @@ -215,7 +216,8 @@ func (s *httpServer) onWHIPPatch(ctx *gin.Context, pathName string, rawSecret st
return
}

if ctx.Request.Header.Get("Content-Type") != "application/trickle-ice-sdpfrag" {
contentType := httpp.ParseContentType(ctx.Request.Header.Get("Content-Type"))
if contentType != "application/trickle-ice-sdpfrag" {
writeError(ctx, http.StatusBadRequest, fmt.Errorf("invalid Content-Type"))
return
}
Expand Down
Loading