Skip to content

Commit

Permalink
Only add Connection: close header for go-IPFS v0.4.18- (cf ipfs-shi…
Browse files Browse the repository at this point in the history
  • Loading branch information
ntninja committed Jun 16, 2019
1 parent c9ac81d commit c0a38ad
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
10 changes: 9 additions & 1 deletion ipfshttpclient/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ def connect(addr=DEFAULT_ADDR, base=DEFAULT_BASE,
client = Client(addr, base, chunk_size, session, **defaults)

# Query version number from daemon and validate it
assert_version(client.version()['Version'])
version_str = client.version()["Version"]
assert_version(version_str)

# Apply workarounds based on daemon version
version = tuple(map(int, version_str.split('-', 1)[0].split('.')))
if version < (0, 4, 19): # pragma: no cover (workaround)
#WORKAROUND: Go-IPFS randomly fucks up streaming requests if they are not
# `Connection: close` (https://github.com/ipfs/go-ipfs/issues/5168)
client._workarounds.add("close_conn_on_upload")

return client

Expand Down
4 changes: 3 additions & 1 deletion ipfshttpclient/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,6 @@ def __init__(self, addr=DEFAULT_ADDR, base=DEFAULT_BASE,

self._client = self._clientfactory(addr, base, **defaults)
if session:
self._client.open_session()
self._client.open_session()

self._workarounds = self._client.workarounds
11 changes: 10 additions & 1 deletion ipfshttpclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class HTTPClient(object):
The address where the IPFS daemon may be reached
base : str
The path prefix for API calls
workarounds : Set[str]
List of daemon workarounds to apply
timeout : Union[numbers.Real, Tuple[numbers.Real, numbers.Real], NoneType]
The default number of seconds to wait when establishing a connection to
the daemon and waiting for returned data before throwing
Expand All @@ -164,7 +166,7 @@ class HTTPClient(object):

__metaclass__ = abc.ABCMeta

def __init__(self, addr, base, **defaults):
def __init__(self, addr, base, workarounds=None, **defaults):
addr = multiaddr.Multiaddr(addr)
addr_iter = iter(addr.items())

Expand Down Expand Up @@ -222,6 +224,8 @@ def __init__(self, addr, base, **defaults):

self.defaults = defaults
self._session = None

self.workarounds = workarounds if workarounds else set()

def open_session(self):
"""Open a persistent backend session that allows reusing HTTP
Expand Down Expand Up @@ -282,6 +286,11 @@ def _do_raise_for_status(self, response):

def _request(self, method, url, params, parser, stream=False, files=None,
headers={}, data=None, timeout=120):
if "close_conn_on_upload" in self.workarounds \
and method.upper() not in ("GET", "HEAD"): # pragma: no cover (workaround)
headers = headers.copy()
headers["Connection"] = "close"

# Do HTTP request (synchronously)
res = self._do_request(method, url, params=params, stream=stream,
files=files, headers=headers, data=data,
Expand Down
4 changes: 0 additions & 4 deletions ipfshttpclient/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ def __init__(self, name, chunk_size=default_chunk_size):
self._headers = content_disposition_headers(name, disptype='form-data')
self._headers.update(multipart_content_type_headers(self._boundary, subtype='form-data'))

#WORKAROUND: Go-IPFS randomly fucks up streaming requests if they are not
# `Connection: close` (https://github.com/ipfs/go-ipfs/issues/5168)
self._headers["Connection"] = "close"

super(StreamBase, self).__init__()

def headers(self):
Expand Down
3 changes: 1 addition & 2 deletions test/unit/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ def test__gen_headers(self):
name = "test_name"
generator = StreamBaseSub(name)

expected = b'Connection: close\r\n' \
+ b'Content-Disposition: form-data; filename="test_name"\r\n' \
expected = b'Content-Disposition: form-data; filename="test_name"\r\n' \
+ b'Content-Type: multipart/form-data; ' \
+ b'boundary="' + generator._boundary.encode() + b'"\r\n\r\n'

Expand Down

0 comments on commit c0a38ad

Please sign in to comment.