From 4a18855ac5001e6cad466de876758ad0b061a631 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Wed, 23 Apr 2014 21:22:18 +0200
Subject: [PATCH] OO-1072: don't let HEAD request be a WebDAV monopoly

---
 .../olat/core/servlets/OpenOLATServlet.java   |  9 ++++++-
 .../services/webdav/WebDAVCommandsTest.java   | 27 +++++++++++++++++++
 .../services/webdav/WebDAVConnection.java     |  8 ++++++
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/olat/core/servlets/OpenOLATServlet.java b/src/main/java/org/olat/core/servlets/OpenOLATServlet.java
index 9650d407ace..ff85f70c1e7 100644
--- a/src/main/java/org/olat/core/servlets/OpenOLATServlet.java
+++ b/src/main/java/org/olat/core/servlets/OpenOLATServlet.java
@@ -226,7 +226,14 @@ public class OpenOLATServlet extends HttpServlet {
 	@Override
 	protected void doHead(HttpServletRequest req, HttpServletResponse resp)
 	throws ServletException, IOException {
-		webDAVDispatcher.execute(req, resp);
+		String subContext = DispatcherModule.getFirstPath(req);
+		if("/".equals(subContext)) {
+			webDAVDispatcher.execute(req, resp);
+		} else if("/webdav".equals(subContext) || "/webdav/".equals(subContext)) {
+			webDAVDispatcher.execute(req, resp);
+		} else {
+			executeUserRequest(req, resp);
+		}
 	}
 
 	/**
diff --git a/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java b/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java
index d98ac07d9a6..18ea6f29abd 100644
--- a/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java
+++ b/src/test/java/org/olat/core/commons/services/webdav/WebDAVCommandsTest.java
@@ -120,6 +120,33 @@ public class WebDAVCommandsTest extends WebDAVTestCase {
 		IOUtils.closeQuietly(conn);
 	}
 	
+	@Test
+	public void testHead()
+	throws IOException, URISyntaxException {
+		//create a user
+		Identity user = JunitTestHelper.createAndPersistIdentityAsUser("webdav-2-" + UUID.randomUUID().toString());
+
+		WebDAVConnection conn = new WebDAVConnection();
+		conn.setCredentials(user.getName(), "A6B7C8");
+		
+		//create a file
+		String publicPath = FolderConfig.getUserHomes() + "/" + user.getName() + "/public";
+		VFSContainer vfsPublic = new OlatRootFolderImpl(publicPath, null);
+		createFile(vfsPublic, "test_head.txt");
+		
+		//head file
+		URI publicUri = conn.getBaseURI().path("webdav").path("home").path("public").path("test_head.txt").build();
+		HttpResponse response = conn.head(publicUri);
+		Header lengthHeader = response.getFirstHeader("Content-Length");
+		Assert.assertNotNull(lengthHeader);
+		Assert.assertEquals("0", lengthHeader.getValue());
+		Header typeHeader = response.getFirstHeader("Content-Type");
+		Assert.assertNotNull(typeHeader);
+		Assert.assertEquals("text/plain", typeHeader.getValue());
+		EntityUtils.consume(response.getEntity());
+		IOUtils.closeQuietly(conn);
+	}
+	
 	@Test
 	public void testPropFind()
 	throws IOException, URISyntaxException {
diff --git a/src/test/java/org/olat/core/commons/services/webdav/WebDAVConnection.java b/src/test/java/org/olat/core/commons/services/webdav/WebDAVConnection.java
index 4eda8952887..414e44d6979 100644
--- a/src/test/java/org/olat/core/commons/services/webdav/WebDAVConnection.java
+++ b/src/test/java/org/olat/core/commons/services/webdav/WebDAVConnection.java
@@ -38,6 +38,7 @@ import org.apache.http.client.CookieStore;
 import org.apache.http.client.CredentialsProvider;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
 import org.apache.http.client.methods.HttpOptions;
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.client.methods.HttpUriRequest;
@@ -93,6 +94,13 @@ public class WebDAVConnection implements Closeable {
 		return response;
 	}
 	
+	public HttpResponse head(URI uri) throws IOException, URISyntaxException {
+		HttpHead propfind = new HttpHead(uri);
+		HttpResponse response = execute(propfind);
+		Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+		return response;
+	}
+	
 	public String propfind(URI uri, int depth) throws IOException, URISyntaxException {
 		HttpPropFind propfind = new HttpPropFind(uri);
 		propfind.addHeader("Depth", Integer.toString(depth));
-- 
GitLab