From cda0e3d18fc1631f44e9ff042c141740acd2d296 Mon Sep 17 00:00:00 2001 From: Erikas Rudinskas Date: Sat, 27 Mar 2021 21:41:09 +0200 Subject: [PATCH] fix some channels not loading at all using HLS service --- hls/content_m3u8.go | 2 +- hls/content_media.go | 2 +- hls/utils.go | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/hls/content_m3u8.go b/hls/content_m3u8.go index abaf4c5..a91d756 100644 --- a/hls/content_m3u8.go +++ b/hls/content_m3u8.go @@ -34,7 +34,7 @@ func handleEstablishedContentM3U8(w http.ResponseWriter, r *http.Request, cr *Co switch { case contentType == "application/vnd.apple.mpegurl" || contentType == "application/x-mpegurl": // M3U8 metadata content := rewriteLinks(&resp.Body, prefix, cr.Channel.LinkM3u8Ref.linkRoot) - addHeaders(resp.Header, w.Header()) + addHeaders(resp.Header, w.Header(), false) w.WriteHeader(http.StatusOK) fmt.Fprint(w, content) default: // media (or anything else) diff --git a/hls/content_media.go b/hls/content_media.go index c31ff3d..bcf3aa4 100644 --- a/hls/content_media.go +++ b/hls/content_media.go @@ -18,7 +18,7 @@ func handleContentMedia(w http.ResponseWriter, r *http.Request, cr *ContentReque func handleEstablishedContentMedia(w http.ResponseWriter, r *http.Request, cr *ContentRequest, resp *http.Response) { cr.Channel.Mux.Unlock() // So other clients can watch it too - addHeaders(resp.Header, w.Header()) + addHeaders(resp.Header, w.Header(), true) w.WriteHeader(resp.StatusCode) io.Copy(w, resp.Body) cr.Channel.Mux.Lock() // To prevent runtime error because we use 'defer' to unlock mux diff --git a/hls/utils.go b/hls/utils.go index 22047e0..9f90a06 100644 --- a/hls/utils.go +++ b/hls/utils.go @@ -126,7 +126,7 @@ func response(link string) (*http.Response, error) { return nil, errors.New(link + " returned HTTP code " + strconv.Itoa(resp.StatusCode)) } -func addHeaders(from, to http.Header) { +func addHeaders(from, to http.Header, contentLength bool) { for k, v := range from { switch k { case "Connection": @@ -140,7 +140,13 @@ func addHeaders(from, to http.Header) { case "Date": to.Set("Date", strings.Join(v, "; ")) case "Content-Length": - to.Set("Content-Length", strings.Join(v, "; ")) + // This is only useful for unaltered media files. It should not be copied for M3U8 requests because + // players will not attempt to receive more bytes from HTTP server than are set here, therefore some M3U8 + // contents would not load. E.g. CURL would display error "curl: (18) transfer closed with 83 bytes remaining to read" + // if set for M3U8 requests. + if contentLength { + to.Set("Content-Length", strings.Join(v, "; ")) + } } } }