HLS: support paths with slashes (#381)

This commit is contained in:
aler9 2021-05-29 16:05:44 +02:00
parent 99323ba740
commit d0c76d778c
3 changed files with 30 additions and 21 deletions

View File

@ -103,11 +103,11 @@ func ipEqualOrInRange(ip net.IP, ips []interface{}) bool {
// Request is an HTTP request received by an HLS server.
type Request struct {
Path string
Subpath string
Req *http.Request
W http.ResponseWriter
Res chan io.Reader
Path string
FileName string
Req *http.Request
W http.ResponseWriter
Res chan io.Reader
}
type trackIDPayloadPair struct {
@ -559,7 +559,7 @@ func (c *Converter) runRequestHandler(terminate chan struct{}, done chan struct{
}
switch {
case req.Subpath == "stream.m3u8":
case req.FileName == "stream.m3u8":
func() {
c.tsMutex.Lock()
defer c.tsMutex.Unlock()
@ -582,8 +582,8 @@ func (c *Converter) runRequestHandler(terminate chan struct{}, done chan struct{
req.Res <- bytes.NewReader([]byte(cnt))
}()
case strings.HasSuffix(req.Subpath, ".ts"):
base := strings.TrimSuffix(req.Subpath, ".ts")
case strings.HasSuffix(req.FileName, ".ts"):
base := strings.TrimSuffix(req.FileName, ".ts")
c.tsMutex.Lock()
f, ok := c.tsByName[base]
@ -597,7 +597,7 @@ func (c *Converter) runRequestHandler(terminate chan struct{}, done chan struct{
req.Res <- f.buf.NewReader()
case req.Subpath == "":
case req.FileName == "":
req.W.Header().Add("Access-Control-Allow-Origin", "*")
req.Res <- bytes.NewReader([]byte(index))

View File

@ -5,6 +5,7 @@ import (
"io"
"net"
"net/http"
"path"
"strings"
"sync"
"time"
@ -143,27 +144,35 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.Log(logger.Info, "[conn %v] %s %s", r.RemoteAddr, r.Method, r.URL.Path)
// remove leading prefix
path := r.URL.Path[1:]
pa := r.URL.Path[1:]
if path == "" || path == "favicon.ico" {
if pa == "" || pa == "favicon.ico" {
w.WriteHeader(http.StatusNotFound)
return
}
parts := strings.SplitN(path, "/", 2)
if len(parts) < 2 {
w.Header().Add("Location", parts[0]+"/")
pa, fname := func() (string, string) {
if strings.HasSuffix(pa, ".ts") || strings.HasSuffix(pa, ".m3u8") {
return path.Dir(pa), path.Base(pa)
}
return pa, ""
}()
if fname == "" && !strings.HasSuffix(pa, "/") {
w.Header().Add("Location", "/"+pa+"/")
w.WriteHeader(http.StatusMovedPermanently)
return
}
pa = strings.TrimSuffix(pa, "/")
cres := make(chan io.Reader)
hreq := hlsconverter.Request{
Path: parts[0],
Subpath: parts[1],
Req: r,
W: w,
Res: cres,
Path: pa,
FileName: fname,
Req: r,
W: w,
Res: cres,
}
select {

View File

@ -18,7 +18,7 @@ func TestClientHLSRead(t *testing.T) {
"-i", "emptyvideo.mkv",
"-c", "copy",
"-f", "rtsp",
"rtsp://" + ownDockerIP + ":8554/teststream",
"rtsp://" + ownDockerIP + ":8554/test/stream",
})
require.NoError(t, err)
defer cnt1.close()
@ -26,7 +26,7 @@ func TestClientHLSRead(t *testing.T) {
time.Sleep(1 * time.Second)
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-i", "http://" + ownDockerIP + ":8888/teststream/stream.m3u8",
"-i", "http://" + ownDockerIP + ":8888/test/stream/stream.m3u8",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",