// Handler to initiate streaming (or not) func urlHandler(w http.ResponseWriter, r *http.Request) { log.Printf("Connection from %s", r.RemoteAddr) if url, ok := urls[r.URL.Path]; ok { if canAccess(url, r.RemoteAddr) { log.Printf("Serving source %s at %s", r.URL.Path, url.Source) // Track number of connected users statsChannel <- true defer func() { statsChannel <- false }() // Source address will be parsed for sure here. @see conf.configValid() parsedUrl, _ := netUrl.Parse(url.Source) if parsedUrl.Scheme == "udp" { url.Source = parsedUrl.Host stream.ChannelStream(w, Sources[r.URL.Path]) } else if parsedUrl.Scheme == "http" { stream.HttpStream(w, url) } else { log.Printf("Unsupported stream protocol: ", parsedUrl.Scheme) response.NotFound(w) return } log.Printf("Stream ended") } else { log.Printf("User at %s cannot access %s", r.RemoteAddr, url.Source) http.ServeFile(w, r, *fakeStream) } } else { log.Printf("Source not found for URL %s", r.URL.Path) response.NotFound(w) } }
// Perform actual unicast streaming func HttpStream(w http.ResponseWriter, url conf.Url) { r, err := http.Get(url.Source) if err != nil { log.Printf("Failed to open HTTP stream %s: %s", url.Source, err) response.NotFound(w) return } defer r.Body.Close() io.Copy(w, r.Body) }