commit b2f1b3944eb63bd7c3c383e35802a325584e84a8 Author: Karsten Loesing karsten.loesing@gmx.net Date: Tue Jan 9 17:47:15 2018 +0100
Avoid sending an error after a (partial) response.
Also take out some unused code.
Fixes #24823. --- CHANGELOG.md | 6 +++++ .../metrics/web/ResearchStatsServlet.java | 30 +++++----------------- 2 files changed, 13 insertions(+), 23 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f4adf1..adb518d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# Changes in version ???? + + * Minor changes + - Avoid sending an error after a (partial) response. + + # Changes in version 1.0.3 - 2017-12-20
* Major changes diff --git a/src/main/java/org/torproject/metrics/web/ResearchStatsServlet.java b/src/main/java/org/torproject/metrics/web/ResearchStatsServlet.java index 415e5b5..4d06241 100644 --- a/src/main/java/org/torproject/metrics/web/ResearchStatsServlet.java +++ b/src/main/java/org/torproject/metrics/web/ResearchStatsServlet.java @@ -56,26 +56,16 @@ public class ResearchStatsServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - String requestUri = request.getRequestURI(); - if (requestUri.equals("/metrics/stats/")) { - this.writeDirectoryListing(request, response); + File statsFile = this.determineStatsFile(request); + if (statsFile == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else if (!statsFile.exists()) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } else { - File statsFile = this.determineStatsFile(request); - if (statsFile == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; - } else if (!this.writeStatsFile(statsFile, response)) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } + this.writeStatsFile(statsFile, response); } }
- private void writeDirectoryListing(HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException { - response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); - response.setHeader("Location", "/?type=dt&level=ad"); - } - private File determineStatsFile(HttpServletRequest request) { String requestedStatsFile = request.getRequestURI(); if (requestedStatsFile.endsWith(".csv")) { @@ -93,11 +83,8 @@ public class ResearchStatsServlet extends HttpServlet { } }
- private boolean writeStatsFile(File statsFile, + private void writeStatsFile(File statsFile, HttpServletResponse response) throws IOException, ServletException { - if (!statsFile.exists()) { - return false; - } response.setContentType("text/csv"); response.setHeader("Content-Length", String.valueOf( statsFile.length())); @@ -112,10 +99,7 @@ public class ResearchStatsServlet extends HttpServlet { while ((length = bis.read(buffer)) > 0) { bos.write(buffer, 0, length); } - } catch (IOException e) { - return false; } - return true; } }