[hlsnative] Support test parameter
[youtube-dl] / youtube_dl / downloader / http.py
index 7e1c1d7156fa9a5986dcdb7d41f4c19ab9c790b4..f62555ce0e33353f5eac848e3956b263f9d43bcb 100644 (file)
@@ -14,8 +14,6 @@ from ..utils import (
 
 
 class HttpFD(FileDownloader):
-    _TEST_FILE_SIZE = 10241
-
     def real_download(self, filename, info_dict):
         url = info_dict['url']
         tmpfilename = self.temp_name(filename)
@@ -27,8 +25,16 @@ class HttpFD(FileDownloader):
             headers['Youtubedl-user-agent'] = info_dict['user_agent']
         if 'http_referer' in info_dict:
             headers['Referer'] = info_dict['http_referer']
-        basic_request = compat_urllib_request.Request(url, None, headers)
-        request = compat_urllib_request.Request(url, None, headers)
+        add_headers = info_dict.get('http_headers')
+        if add_headers:
+            headers.update(add_headers)
+        data = info_dict.get('http_post_data')
+        http_method = info_dict.get('http_method')
+        basic_request = compat_urllib_request.Request(url, data, headers)
+        request = compat_urllib_request.Request(url, data, headers)
+        if http_method is not None:
+            basic_request.get_method = lambda: http_method
+            request.get_method = lambda: http_method
 
         is_test = self.params.get('test', False)
 
@@ -110,7 +116,7 @@ class HttpFD(FileDownloader):
         # However, for a test we still would like to download just a piece of a file.
         # To achieve this we limit data_len to _TEST_FILE_SIZE and manually control
         # block size when downloading a file.
-        if is_test and data_len > self._TEST_FILE_SIZE:
+        if is_test and (data_len is None or int(data_len) > self._TEST_FILE_SIZE):
             data_len = self._TEST_FILE_SIZE
 
         if data_len is not None:
@@ -185,7 +191,8 @@ class HttpFD(FileDownloader):
             self.to_stderr(u"\n")
             self.report_error(u'Did not get any data blocks')
             return False
-        stream.close()
+        if tmpfilename != u'-':
+            stream.close()
         self.report_finish(data_len_str, (time.time() - start))
         if data_len is not None and byte_counter != data_len:
             raise ContentTooShortError(byte_counter, int(data_len))