From 08725c857b4dfc013ab5c9be752ab51a6d0045c6 Mon Sep 17 00:00:00 2001 From: Shawn Pearce Date: Sat, 26 Apr 2014 19:20:15 -0700 Subject: [PATCH] Accept HEAD in RestApiServlet HEAD should be treated like GET, but omit the content body. Change-Id: I7f83829f4850e8717191ca3f1772af2792a0adee --- .../gerrit/httpd/restapi/RestApiServlet.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java index ce450dc3c2..b6310fbc7f 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java @@ -206,7 +206,7 @@ public class RestApiServlet extends HttpServlet { RestResource rsrc = TopLevelResource.INSTANCE; ViewData viewData = new ViewData(null, null); if (path.isEmpty()) { - if ("GET".equals(req.getMethod())) { + if (isGetOrHead(req)) { viewData = new ViewData(null, rc.list()); } else if (rc instanceof AcceptsPost && "POST".equals(req.getMethod())) { @SuppressWarnings("unchecked") @@ -247,7 +247,7 @@ public class RestApiServlet extends HttpServlet { (RestCollection) viewData.view; if (path.isEmpty()) { - if ("GET".equals(req.getMethod())) { + if (isGetOrHead(req)) { viewData = new ViewData(null, c.list()); } else if (c instanceof AcceptsPost && "POST".equals(req.getMethod())) { @SuppressWarnings("unchecked") @@ -363,7 +363,7 @@ public class RestApiServlet extends HttpServlet { } private static boolean notModified(HttpServletRequest req, RestResource rsrc) { - if (!"GET".equals(req.getMethod())) { + if (!isGetOrHead(req)) { return false; } @@ -386,7 +386,7 @@ public class RestApiServlet extends HttpServlet { private static void configureCaching(HttpServletRequest req, HttpServletResponse res, RestResource rsrc, CacheControl c) { - if ("GET".equals(req.getMethod())) { + if (isGetOrHead(req)) { switch (c.getType()) { case NONE: default: @@ -716,11 +716,13 @@ public class RestApiServlet extends HttpServlet { res.setHeader("Content-Length", Long.toString(len)); } - OutputStream dst = res.getOutputStream(); - try { - bin.writeTo(dst); - } finally { - dst.close(); + if (!"HEAD".equals(req.getMethod())) { + OutputStream dst = res.getOutputStream(); + try { + bin.writeTo(dst); + } finally { + dst.close(); + } } } finally { appResult.close(); @@ -787,6 +789,8 @@ public class RestApiServlet extends HttpServlet { // is chosen, look for the projection based upon GET as the method as // the client thinks it is a nested collection. method = "GET"; + } else if ("HEAD".equals(method)) { + method = "GET"; } List p = splitProjection(projection); @@ -879,9 +883,12 @@ public class RestApiServlet extends HttpServlet { user.setAccessPath(AccessPath.REST_API); } + private static boolean isGetOrHead(HttpServletRequest req) { + return "GET".equals(req.getMethod()) || "HEAD".equals(req.getMethod()); + } + private static boolean isStateChange(HttpServletRequest req) { - String method = req.getMethod(); - return !("GET".equals(method) || "HEAD".equals(method)); + return !isGetOrHead(req); } private void checkRequiresCapability(ViewData viewData) throws AuthException { @@ -918,7 +925,7 @@ public class RestApiServlet extends HttpServlet { static void replyText(@Nullable HttpServletRequest req, HttpServletResponse res, String text) throws IOException { - if ((req == null || "GET".equals(req.getMethod())) && isMaybeHTML(text)) { + if ((req == null || isGetOrHead(req)) && isMaybeHTML(text)) { replyJson(req, res, ImmutableMultimap.of("pp", "0"), new JsonPrimitive(text)); } else { if (!text.endsWith("\n")) {