From 12246581e68fae9c1635b9121e6a17bf6f3298aa Mon Sep 17 00:00:00 2001
From: Katri <katri@livepeer.org>
Date: Mon, 13 Jun 2022 15:52:43 +0000
Subject: [PATCH] Added close socket and clean buffer to Downloader URIReader
 and InputHLS

---
 lib/downloader.cpp      | 12 ++++++++++++
 lib/urireader.cpp       |  5 +++++
 src/input/input_hls.cpp |  5 +++++
 3 files changed, 22 insertions(+)

diff --git a/lib/downloader.cpp b/lib/downloader.cpp
index d74e3aff..1f430972 100644
--- a/lib/downloader.cpp
+++ b/lib/downloader.cpp
@@ -87,6 +87,7 @@ namespace HTTP{
     if (!proxied || needSSL){
       if (!getSocket() || link.host != connectedHost || link.getPort() != connectedPort || needSSL != ssl){
         getSocket().close();
+        getSocket().Received().clear();
         connectedHost = link.host;
         connectedPort = link.getPort();
 #ifdef SSL
@@ -102,6 +103,7 @@ namespace HTTP{
     }else{
       if (!getSocket() || proxyUrl.host != connectedHost || proxyUrl.getPort() != connectedPort || needSSL != ssl){
         getSocket().close();
+        getSocket().Received().clear();
         connectedHost = proxyUrl.host;
         connectedPort = proxyUrl.getPort();
         getSocket().open(connectedHost, connectedPort, true);
@@ -195,6 +197,16 @@ namespace HTTP{
         // Data! Check if we can parse it...
         if (H.Read(getSocket())){
           H.headerOnly = false;
+
+          // If the return status code is invalid, close the socket, wipe all buffers, and return false
+          if(!getStatusCode()){
+            H.headerOnly = false;
+            getSocket().close();
+            getSocket().Received().clear();
+            H.Clean();
+            return false;
+          }
+
           if (shouldContinue()){
             if (maxRecursiveDepth == 0){
               FAIL_MSG("Maximum recursion depth reached");
diff --git a/lib/urireader.cpp b/lib/urireader.cpp
index e6a49d7e..80f204c8 100644
--- a/lib/urireader.cpp
+++ b/lib/urireader.cpp
@@ -107,6 +107,9 @@ namespace HTTP{
       if (!downer.head(myURI) || !downer.isOk()){
         FAIL_MSG("Error getting URI info for '%s': %" PRIu32 " %s", myURI.getUrl().c_str(),
                  downer.getStatusCode(), downer.getStatusText().c_str());
+        // Close the socket, and clean up the buffer
+        downer.getSocket().close();
+        downer.getSocket().Received().clear();
         if (!downer.isOk()){return false;}
         supportRangeRequest = false;
         totalSize = std::string::npos;
@@ -256,6 +259,8 @@ namespace HTTP{
   void URIReader::close(){
     // Close downloader socket if open
     downer.getSocket().close();
+    downer.getSocket().Received().clear();
+    downer.getHTTP().Clean();
     // Unmap file if mapped
     if (mapped){
       munmap(mapped, totalSize);
diff --git a/src/input/input_hls.cpp b/src/input/input_hls.cpp
index f58f41b3..e51a951d 100644
--- a/src/input/input_hls.cpp
+++ b/src/input/input_hls.cpp
@@ -679,6 +679,11 @@ namespace Mist{
             break; // we have all tracks discovered, next playlist!
           }
         }while (!segDowner.atEnd());
+        if (!segDowner.atEnd()){
+          segDowner.close();
+          tsStream.clear();
+        }
+
         if (prepidCounter < pidCounter){break;}// We're done reading this playlist!
       }
     }