2021-09-12 21:32:42 +03:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core"
|
|
|
|
"github.com/owncast/owncast/core/data"
|
|
|
|
"github.com/owncast/owncast/router/middleware"
|
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HandleHLSRequest will manage all requests to HLS content.
|
|
|
|
func HandleHLSRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Sanity check to limit requests to HLS file types.
|
|
|
|
if filepath.Ext(r.URL.Path) != ".m3u8" && filepath.Ext(r.URL.Path) != ".ts" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedPath := r.URL.Path
|
|
|
|
relativePath := strings.Replace(requestedPath, "/hls/", "", 1)
|
|
|
|
fullPath := filepath.Join(config.HLSStoragePath, relativePath)
|
|
|
|
|
|
|
|
// If using external storage then only allow requests for the
|
|
|
|
// master playlist at stream.m3u8, no variants or segments.
|
|
|
|
if data.GetS3Config().Enabled && relativePath != "stream.m3u8" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle playlists
|
|
|
|
if path.Ext(r.URL.Path) == ".m3u8" {
|
|
|
|
// Playlists should never be cached.
|
|
|
|
middleware.DisableCache(w)
|
|
|
|
|
2021-11-01 05:20:56 +03:00
|
|
|
// Force the correct content type
|
|
|
|
w.Header().Set("Content-Type", "application/x-mpegURL")
|
|
|
|
|
2021-09-12 21:32:42 +03:00
|
|
|
// Use this as an opportunity to mark this viewer as active.
|
|
|
|
id := utils.GenerateClientIDFromRequest(r)
|
|
|
|
core.SetViewerIDActive(id)
|
|
|
|
} else {
|
|
|
|
cacheTime := utils.GetCacheDurationSecondsForPath(relativePath)
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(cacheTime))
|
|
|
|
}
|
2021-11-01 05:20:56 +03:00
|
|
|
|
2021-10-01 22:40:13 +03:00
|
|
|
middleware.EnableCors(w)
|
2021-09-12 21:32:42 +03:00
|
|
|
http.ServeFile(w, r, fullPath)
|
|
|
|
}
|