diff mercurial/httppeer.py @ 39484:98995b689e03

httppeer: use util.readexactly() to abort on incomplete responses Plain resp.read(n) may not return exactly n bytes when we need, and to detect such cases before trying to interpret whatever has been read, we can use util.readexactly(), which raises an Abort when stream ends unexpectedly. In the first case here, readexactly() prevents a traceback with struct.error, in the second it avoids looking for invalid compression engines. In this test case, _wraphttpresponse doesn't catch the problem (presumably because it doesn't know transfer encoding), and the code continues reading the response until it gets to compression engine data. Maybe there should be checks before the execution gets there, but I'm not sure where (httplib?)
author Anton Shestakov <av6@dwimlabs.net>
date Sat, 08 Sep 2018 21:58:51 +0800
parents 1fc39367eafd
children 089fc0db0954
line wrap: on
line diff
--- a/mercurial/httppeer.py	Sat Sep 08 23:57:07 2018 +0800
+++ b/mercurial/httppeer.py	Sat Sep 08 21:58:51 2018 +0800
@@ -401,8 +401,8 @@
     elif version_info == (0, 2):
         # application/mercurial-0.2 always identifies the compression
         # engine in the payload header.
-        elen = struct.unpack('B', resp.read(1))[0]
-        ename = resp.read(elen)
+        elen = struct.unpack('B', util.readexactly(resp, 1))[0]
+        ename = util.readexactly(resp, elen)
         engine = util.compengines.forwiretype(ename)
 
         resp = engine.decompressorreader(resp)