From 3436af75bfdebb747a02fb26b86f1389e478fc15 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 10 Apr 2020 08:04:00 +0100 Subject: [PATCH] remove redundant header parsing code --- .../com/muwire/core/upload/Request.groovy | 55 +------------------ 1 file changed, 2 insertions(+), 53 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/upload/Request.groovy b/core/src/main/groovy/com/muwire/core/upload/Request.groovy index b57a5693..90febd40 100644 --- a/core/src/main/groovy/com/muwire/core/upload/Request.groovy +++ b/core/src/main/groovy/com/muwire/core/upload/Request.groovy @@ -22,7 +22,7 @@ class Request { static Request parseContentRequest(InfoHash infoHash, InputStream is) throws IOException { - Map headers = parseHeaders(is) + Map headers = DataUtil.readAllHeaders(is) if (!headers.containsKey("Range")) throw new IOException("Range header not found") @@ -60,7 +60,7 @@ class Request { } static Request parseHashListRequest(InfoHash infoHash, InputStream is) throws IOException { - Map headers = parseHeaders(is) + Map headers = DataUtil.readAllHeaders(is) Persona downloader = null if (headers.containsKey("X-Persona")) { def encoded = headers["X-Persona"].trim() @@ -69,55 +69,4 @@ class Request { } new HashListRequest(infoHash : infoHash, headers : headers, downloader : downloader) } - - private static Map parseHeaders(InputStream is) { - Map headers = new HashMap<>() - byte [] tmp = new byte[Constants.MAX_HEADER_SIZE] - while(headers.size() < Constants.MAX_HEADERS) { - boolean r = false - boolean n = false - int idx = 0 - while (true) { - byte read = is.read() - if (read == -1) - throw new IOException("Stream closed") - - if (!r && read == N) - throw new IOException("Received N before R") - if (read == R) { - if (r) - throw new IOException("double R") - r = true - continue - } - - if (r && !n) { - if (read != N) - throw new IOException("R not followed by N") - n = true - break - } - if (idx == 0x1 << 14) - throw new IOException("Header too long") - tmp[idx++] = read - } - - if (idx == 0) - break - - String header = new String(tmp, 0, idx, StandardCharsets.US_ASCII) - log.fine("Read header $header") - - int keyIdx = header.indexOf(":") - if (keyIdx < 1) - throw new IOException("Header key not found") - if (keyIdx == header.length()) - throw new IOException("Header value not found") - String key = header.substring(0, keyIdx) - String value = header.substring(keyIdx + 1) - headers.put(key, value) - } - headers - } - }