src/Tweeper.php: make enclosure validate when there is no Content-Length
authorAntonio Ospite <ao2@ao2.it>
Fri, 16 Nov 2018 22:06:38 +0000 (23:06 +0100)
committerAntonio Ospite <ao2@ao2.it>
Fri, 16 Nov 2018 22:06:38 +0000 (23:06 +0100)
When the server does not provide a Content-Length header, curl_getinfo()
would return a negative value for "download_content_length".

However RSS recommends to use 0 when the enclosure's size cannot be
determined.

See: https://www.feedvalidator.org/docs/error/UseZeroForUnknown.html

src/Tweeper.php

index 8fda9b1..09bd7cc 100644 (file)
@@ -206,10 +206,17 @@ class Tweeper {
     // See http://sourceforge.net/p/feedvalidator/bugs/72/
     $http_url = preg_replace("/^https/", "http", $url_info['url']);
 
     // See http://sourceforge.net/p/feedvalidator/bugs/72/
     $http_url = preg_replace("/^https/", "http", $url_info['url']);
 
+    // When the server does not provide a Content-Length header,
+    // curl_getinfo() would return a negative value for
+    // "download_content_length", however RSS recommends to use 0 when the
+    // enclosure's size cannot be determined.
+    // See: https://www.feedvalidator.org/docs/error/UseZeroForUnknown.html
+    $length = max($url_info['download_content_length'], 0);
+
     $dom = new DOMDocument();
     $enc = $dom->createElement('enclosure');
     $enc->setAttribute('url', $http_url);
     $dom = new DOMDocument();
     $enc = $dom->createElement('enclosure');
     $enc->setAttribute('url', $http_url);
-    $enc->setAttribute('length', $url_info['download_content_length']);
+    $enc->setAttribute('length', $length);
     $enc->setAttribute('type', $url_info['content_type']);
 
     return $enc;
     $enc->setAttribute('type', $url_info['content_type']);
 
     return $enc;