From ab5fea9216be82071e0bca5c489ab4db15d4e7bb Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 21 Jun 2019 16:03:20 +0100 Subject: [PATCH] 416 if piece not downloaded --- .../com/muwire/core/download/Pieces.groovy | 4 ++++ .../muwire/core/upload/ContentUploader.groovy | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/download/Pieces.groovy b/core/src/main/groovy/com/muwire/core/download/Pieces.groovy index 1bcac9f3..243c1c14 100644 --- a/core/src/main/groovy/com/muwire/core/download/Pieces.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Pieces.groovy @@ -74,4 +74,8 @@ class Pieces { synchronized int donePieces() { done.cardinality() } + + synchronized boolean isDownloaded(int piece) { + done.get(piece) + } } diff --git a/core/src/main/groovy/com/muwire/core/upload/ContentUploader.groovy b/core/src/main/groovy/com/muwire/core/upload/ContentUploader.groovy index aad30f5c..ef33e8fd 100644 --- a/core/src/main/groovy/com/muwire/core/upload/ContentUploader.groovy +++ b/core/src/main/groovy/com/muwire/core/upload/ContentUploader.groovy @@ -19,19 +19,31 @@ class ContentUploader extends Uploader { private final File file private final ContentRequest request private final Mesh mesh + private final int pieceSize - ContentUploader(File file, ContentRequest request, Endpoint endpoint, Mesh mesh) { + ContentUploader(File file, ContentRequest request, Endpoint endpoint, Mesh mesh, int pieceSize) { super(endpoint) this.file = file this.request = request this.mesh = mesh + this.pieceSize = pieceSize } @Override void respond() { OutputStream os = endpoint.getOutputStream() Range range = request.getRange() - if (range.start >= file.length() || range.end >= file.length()) { + boolean satisfiable = true + final long length = file.length() + if (range.start >= length || range.end >= length) + satisfiable = false + if (satisfiable) { + int startPiece = length / range.start + int endPiece = length / range.end + for (int i = startPiece; i < endPiece; i++) + satisfiable &= mesh.pieces.isDownloaded(i) + } + if (!satisfiable) { os.write("416 Range Not Satisfiable\r\n".getBytes(StandardCharsets.US_ASCII)) writeMesh(request.downloader) os.write("\r\n".getBytes(StandardCharsets.US_ASCII))