summaryrefslogtreecommitdiff
path: root/methods/basehttp.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2017-07-26 18:35:42 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2017-07-26 19:07:56 +0200
commitf2f8e89f08cdf01c83a0b8ab053c65329d85ca90 (patch)
tree7ca9caf3e9dfa5caaad5dda836c0d95278a57204 /methods/basehttp.cc
parentd7518dba50e2285c41c7002a1d86f876401fd9ea (diff)
downloadapt-f2f8e89f08cdf01c83a0b8ab053c65329d85ca90.tar.gz
fail early in http if server answer is too small as well
Failing on too much data is good, but we can do better by checking for exact filesizes as we know with hashsums how large a file should be, so if we get a file which has a size we do not expect we can drop it directly, regardless of if the file is larger or smaller than what we expect which should catch most cases which would end up as hashsum errors later now a lot sooner.
Diffstat (limited to 'methods/basehttp.cc')
-rw-r--r--methods/basehttp.cc35
1 files changed, 33 insertions, 2 deletions
diff --git a/methods/basehttp.cc b/methods/basehttp.cc
index 47dabf960..cc5039c75 100644
--- a/methods/basehttp.cc
+++ b/methods/basehttp.cc
@@ -660,8 +660,39 @@ int BaseHttpMethod::Loop()
// so instead we use the size of the biggest item in the queue
Req.MaximumSize = FindMaximumObjectSizeInQueue();
- if (Req.HaveContent)
- Result = Server->RunData(Req);
+ if (Req.HaveContent)
+ {
+ /* If the server provides Content-Length we can figure out with it if
+ this satisfies any request we have made so far (in the pipeline).
+ If not we can kill the connection as whatever file the server is trying
+ to send to us would be rejected with a hashsum mismatch later or triggers
+ a maximum size error. We don't run the data to /dev/null as this can be MBs
+ of junk data we would waste bandwidth on and instead just close the connection
+ to reopen a fresh one which should be more cost/time efficient */
+ if (Req.DownloadSize > 0)
+ {
+ decltype(Queue->ExpectedHashes.FileSize()) const filesize = Req.StartPos + Req.DownloadSize;
+ bool found = false;
+ for (FetchItem const *I = Queue; I != 0 && I != QueueBack; I = I->Next)
+ {
+ auto const fs = I->ExpectedHashes.FileSize();
+ if (fs == 0 || fs == filesize)
+ {
+ found = true;
+ break;
+ }
+ }
+ if (found == false)
+ {
+ SetFailReason("MaximumSizeExceeded");
+ _error->Error(_("File has unexpected size (%llu != %llu). Mirror sync in progress?"),
+ filesize, Queue->ExpectedHashes.FileSize());
+ Result = false;
+ }
+ }
+ if (Result)
+ Result = Server->RunData(Req);
+ }
/* If the server is sending back sizeless responses then fill in
the size now */