Skip to content

Commit

Permalink
Handle too big requests by recursively splitting requests on HTTP 413…
Browse files Browse the repository at this point in the history
… responses. (#7)
  • Loading branch information
swistakm authored and Changaco committed May 22, 2019
1 parent 288ce3b commit e810b70
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions git_lfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
try:
from urllib.parse import urlsplit, urlunsplit
from urllib.request import Request, urlopen
from urllib.error import HTTPError
except ImportError:
from urllib2 import Request, urlopen
from urllib2 import HTTPError
from urlparse import urlsplit, urlunsplit

from .utils import force_link, ignore_missing_file, in_dir, TempDir, TempFile
Expand Down Expand Up @@ -121,13 +123,32 @@ def read_lfs_metadata(checkout_dir):
def fetch_urls(lfs_url, lfs_auth_info, oid_list):
"""Fetch the URLs of the files from the Git LFS endpoint
"""
objects = []
data = json.dumps({'operation': 'download', 'objects': oid_list})
headers = dict(POST_HEADERS)
headers.update(lfs_auth_info)
req = Request(lfs_url+'/objects/batch', data.encode('ascii'), headers)
resp = json.loads(urlopen(req).read().decode('ascii'))
assert 'objects' in resp, resp
return resp['objects']

try:
resp = json.loads(urlopen(req).read().decode('ascii'))
assert 'objects' in resp, resp
objects.extend(resp['objects'])

except HTTPError as err:
if err.code != 413:
raise

# It is possible for remote server to respond with 413 Request Entity
# Too Large. If that happens try to request for two sets of oids so
# request is smaller.
objects.extend(
fetch_urls(lfs_url, lfs_auth_info, oid_list[:len(oid_list) // 2])
)
objects.extend(
fetch_urls(lfs_url, lfs_auth_info, oid_list[len(oid_list) // 2:])
)

return objects


def fetch(git_repo, checkout_dir=None, verbose=0):
Expand Down

2 comments on commit e810b70

@lostanlen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! @Changaco could you please make a v1.6 release with this enhancement?

@Changaco
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.