Merge pull request #2371 from Suor/streaming

api: make .open() stream data for Azure
This commit is contained in:
Ruslan Kuprieiev
2019-08-09 18:42:40 +03:00
committed by GitHub
7 changed files with 208 additions and 24 deletions
+37
View File
@@ -0,0 +1,37 @@
from __future__ import unicode_literals
import io
import requests
from dvc.utils.http import open_url
from tests.utils.httpd import StaticFileServer
def test_open_url(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
# Simulate bad connection
original_iter_content = requests.Response.iter_content
def bad_iter_content(self, *args, **kwargs):
it = original_iter_content(self, *args, **kwargs)
for i, chunk in enumerate(it):
# Drop connection error on second chunk if there is one
if i > 0:
raise requests.ConnectionError("Simulated connection drop")
yield chunk
monkeypatch.setattr(requests.Response, "iter_content", bad_iter_content)
# Text should be longer than default chunk to test resume,
# using twice of that plus something tests second resume,
# this is important because second response is different
text = "0123456789" * (io.DEFAULT_BUFFER_SIZE // 10 + 1)
(tmp_path / "sample.txt").write_text(text * 2)
with StaticFileServer() as httpd:
url = "http://localhost:{}/sample.txt".format(httpd.server_port)
with open_url(url) as fd:
# Test various .read() variants
assert fd.read(len(text)) == text
assert fd.read() == text
+24 -16
View File
@@ -2,31 +2,39 @@ import hashlib
import os
import threading
from dvc.utils.compat import HTTPServer, SimpleHTTPRequestHandler
from RangeHTTPServer import RangeRequestHandler
from dvc.utils.compat import HTTPServer
class ETagHandler(SimpleHTTPRequestHandler):
class TestRequestHandler(RangeRequestHandler):
checksum_header = None
def end_headers(self):
file = self.translate_path(self.path)
# RangeRequestHandler only sends Accept-Ranges header if Range header
# is present, see https://github.com/danvk/RangeHTTPServer/issues/23
if not self.headers.get("Range"):
self.send_header("Accept-Ranges", "bytes")
if not os.path.isdir(file) and os.path.exists(file):
with open(file, "r") as fd:
etag = hashlib.md5(fd.read().encode("utf8")).hexdigest()
self.send_header("ETag", etag)
# Add a checksum header
if self.checksum_header:
file = self.translate_path(self.path)
SimpleHTTPRequestHandler.end_headers(self)
if not os.path.isdir(file) and os.path.exists(file):
with open(file, "r") as fd:
encoded_text = fd.read().encode("utf8")
checksum = hashlib.md5(encoded_text).hexdigest()
self.send_header(self.checksum_header, checksum)
RangeRequestHandler.end_headers(self)
class ContentMD5Handler(SimpleHTTPRequestHandler):
def end_headers(self):
file = self.translate_path(self.path)
class ETagHandler(TestRequestHandler):
checksum_header = "ETag"
if not os.path.isdir(file) and os.path.exists(file):
with open(file, "r") as fd:
md5 = hashlib.md5(fd.read().encode("utf8")).hexdigest()
self.send_header("Content-MD5", md5)
SimpleHTTPRequestHandler.end_headers(self)
class ContentMD5Handler(TestRequestHandler):
checksum_header = "Content-MD5"
class StaticFileServer: