From b25fa38a6dbfc084babefbd526310a712028d7f3 Mon Sep 17 00:00:00 2001 From: langhuihui <178529795@qq.com> Date: Wed, 26 Nov 2025 11:15:11 +0800 Subject: [PATCH] chore: update gotask to 1.0.3 --- go.mod | 2 +- go.sum | 4 ++-- plugin/flv/index.go | 8 ++++---- plugin/hls/index.go | 2 +- plugin/mp4/index.go | 2 +- server_http.go | 30 +++++++++++++++++++++++++++--- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 185b752..4951120 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/icholy/digest v1.1.0 github.com/jinzhu/copier v0.4.0 github.com/kerberos-io/onvif v1.0.0 - github.com/langhuihui/gotask v1.0.2 + github.com/langhuihui/gotask v1.0.3 github.com/mark3labs/mcp-go v0.27.0 github.com/mattn/go-sqlite3 v1.14.24 github.com/mcuadros/go-defaults v1.2.0 diff --git a/go.sum b/go.sum index 14f7121..654583d 100644 --- a/go.sum +++ b/go.sum @@ -152,8 +152,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/langhuihui/gomem v0.0.0-20251001011839-023923cf7683 h1:lITBgMb71ad6OUU9gycsheCw9PpMbXy3/QA8T0V0dVM= github.com/langhuihui/gomem v0.0.0-20251001011839-023923cf7683/go.mod h1:BTPq1+4YUP4i7w8VHzs5AUIdn3T5gXjIUXbxgHW9TIQ= -github.com/langhuihui/gotask v1.0.2 h1:vcQ/9yD0+x2DkSrDBkYpufOyJxXs7i5fd7wUD8gvLLs= -github.com/langhuihui/gotask v1.0.2/go.mod h1:2zNqwV8M1pHoO0b5JC/A37oYpdtXrfL10Qof9AvR5IE= +github.com/langhuihui/gotask v1.0.3 h1:QiL0YDkvJVPp44KkFYcTcj/GkYjGPW+0WxR0N/j1/JE= +github.com/langhuihui/gotask v1.0.3/go.mod h1:2zNqwV8M1pHoO0b5JC/A37oYpdtXrfL10Qof9AvR5IE= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= diff --git a/plugin/flv/index.go b/plugin/flv/index.go index b4de010..8c5d168 100644 --- a/plugin/flv/index.go +++ b/plugin/flv/index.go @@ -47,12 +47,12 @@ func (plugin *FLVPlugin) Start() (err error) { } func (plugin *FLVPlugin) ServeHTTP(w http.ResponseWriter, r *http.Request) { - rawPath := strings.TrimPrefix(r.URL.Path, "/") - if plugin.Server != nil && plugin.Server.RedirectIfNeeded(w, r, "http", rawPath) { - plugin.Debug("redirect issued", "protocol", "http", "path", rawPath) + redirectPath := strings.TrimPrefix(r.URL.Path, "/") + if plugin.Server != nil && plugin.Server.RedirectIfNeeded(w, r, "flv", redirectPath) { + plugin.Debug("redirect issued", "protocol", "http", "path", redirectPath) return } - streamPath := strings.TrimSuffix(rawPath, ".flv") + streamPath := strings.TrimSuffix(redirectPath, ".flv") var err error defer func() { if err != nil { diff --git a/plugin/hls/index.go b/plugin/hls/index.go index fffdf09..de88bb7 100644 --- a/plugin/hls/index.go +++ b/plugin/hls/index.go @@ -208,7 +208,7 @@ func (config *HLSPlugin) vod(w http.ResponseWriter, r *http.Request) { func (config *HLSPlugin) ServeHTTP(w http.ResponseWriter, r *http.Request) { redirectPath := strings.TrimPrefix(r.URL.Path, "/") - if config.Server.RedirectIfNeeded(w, r, "http", redirectPath) { + if config.Server.RedirectIfNeeded(w, r, "hls", redirectPath) { config.Debug("redirect issued", "protocol", "http", "path", redirectPath) return } diff --git a/plugin/mp4/index.go b/plugin/mp4/index.go index 6d99445..2941440 100644 --- a/plugin/mp4/index.go +++ b/plugin/mp4/index.go @@ -99,7 +99,7 @@ func (p *MP4Plugin) Start() (err error) { func (p *MP4Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) { redirectPath := strings.TrimPrefix(r.URL.Path, "/") - if p.Server != nil && p.Server.RedirectIfNeeded(w, r, "http", redirectPath) { + if p.Server != nil && p.Server.RedirectIfNeeded(w, r, "mp4", redirectPath) { p.Debug("redirect issued", "protocol", "http", "path", redirectPath) return } diff --git a/server_http.go b/server_http.go index ad64278..d1f0b5f 100644 --- a/server_http.go +++ b/server_http.go @@ -34,7 +34,10 @@ func (s *Server) GetRedirectAdvisor() RedirectAdvisor { } // RedirectIfNeeded evaluates redirect advice for HTTP-based protocols and issues redirects when appropriate. -func (s *Server) RedirectIfNeeded(w http.ResponseWriter, r *http.Request, protocol, redirectPath string) bool { +// The prefix parameter is the plugin name (e.g., "flv", "mp4", "hls", "webrtc") used to restore the path prefix. +// The redirectPath parameter is the stream path without prefix, used for matching redirect rules. +// This function automatically restores the path prefix (e.g., /flv/, /mp4/) for building the redirect URL. +func (s *Server) RedirectIfNeeded(w http.ResponseWriter, r *http.Request, prefix, redirectPath string) bool { if s == nil { return false } @@ -42,13 +45,35 @@ func (s *Server) RedirectIfNeeded(w http.ResponseWriter, r *http.Request, protoc if advisor == nil { return false } - targetHost, statusCode, ok := advisor.GetRedirectTarget(protocol, redirectPath, r.Host) + + // Save current path and restore it with the prefix for building redirect URL + currentPath := r.URL.Path + pathRestored := false + + // Restore path with prefix if needed + if prefix != "" { + pathPrefix := "/" + prefix + "/" + if !strings.HasPrefix(currentPath, pathPrefix) { + r.URL.Path = pathPrefix + redirectPath + pathRestored = true + } + } + + // Restore original path after redirect (if it was modified) + defer func() { + if pathRestored { + r.URL.Path = currentPath + } + }() + + targetHost, statusCode, ok := advisor.GetRedirectTarget(prefix, redirectPath, r.Host) if !ok || targetHost == "" { return false } if statusCode == 0 { statusCode = http.StatusFound } + // Use r.URL.Path (which has been restored with prefix) to build redirect URL redirectURL := buildRedirectURL(r, targetHost) http.Redirect(w, r, redirectURL, statusCode) return true @@ -183,4 +208,3 @@ func (s *Server) annexB(w http.ResponseWriter, r *http.Request) { return ctx.Flush() }) } -