From fdc8a257b8047656921b002664db2e9a29500f21 Mon Sep 17 00:00:00 2001
From: ian <ian@virtualfunc.com>
Date: Fri, 26 Jul 2019 15:03:31 -0500
Subject: [PATCH 08/11] recognize server closing http connection

---
 libavformat/http.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 579debc..3bf2434 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -332,15 +332,25 @@ int ff_http_do_new_request(URLContext *h, const char *uri)
         return AVERROR(EINVAL);
     }
 
+    // read headers if not already loaded (this is needed to check if connection is closing)
+    if (!s->end_header) {
+      int new_location;
+      http_read_header(h, &new_location);
+
+      if (s->willclose) {
+        ret = ffurl_closep(&s->hd);
+
+        if (ret < 0)
+          return ret;
+      }
+    }
+
     if (!s->end_chunked_post) {
         ret = http_shutdown(h, h->flags);
         if (ret < 0)
             return ret;
     }
 
-    if (s->willclose)
-        return AVERROR_EOF;
-
     s->end_chunked_post = 0;
     s->chunkend      = 0;
     s->off           = 0;
@@ -990,7 +1000,7 @@ static int process_line(URLContext *h, char *line, int line_count,
         } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
             ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
         } else if (!av_strcasecmp(tag, "Connection")) {
-            if (!strcmp(p, "close"))
+            if (strstr(p, "close") != NULL)
                 s->willclose = 1;
         } else if (!av_strcasecmp(tag, "Server")) {
             if (!av_strcasecmp(p, "AkamaiGHost")) {
@@ -1637,6 +1647,7 @@ static int http_shutdown(URLContext *h, int flags)
             s->hd->flags |= AVIO_FLAG_NONBLOCK;
             read_ret = ffurl_read(s->hd, buf, sizeof(buf));
             s->hd->flags &= ~AVIO_FLAG_NONBLOCK;
+
             if (read_ret < 0 && read_ret != AVERROR(EAGAIN)) {
                 av_log(h, AV_LOG_ERROR, "URL read error: %s\n", av_err2str(read_ret));
                 ret = read_ret;
-- 
2.7.4

