From 9951ffbead909c537b6c76aec1ba17425222f5fe Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 04 Sep 2014 13:13:11 -0400
Subject: [PATCH] Merged #148 "Do not stamp raw servlet responses with cache-control headers"

---
 src/main/java/com/gitblit/servlet/RawServlet.java |   40 +++++++++++++++++++++++-----------------
 1 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/src/main/java/com/gitblit/servlet/RawServlet.java b/src/main/java/com/gitblit/servlet/RawServlet.java
index 15e036e..30d33fd 100644
--- a/src/main/java/com/gitblit/servlet/RawServlet.java
+++ b/src/main/java/com/gitblit/servlet/RawServlet.java
@@ -95,20 +95,17 @@
 			baseURL = baseURL.substring(0, baseURL.length() - 1);
 		}
 
+		char fsc = '!';
+		char c = GitblitContext.getManager(IRuntimeManager.class).getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
+		if (c != '/') {
+			fsc = c;
+		}
 		if (branch != null) {
-			char fsc = '!';
-			char c = GitblitContext.getManager(IRuntimeManager.class).getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
-			if (c != '/') {
-				fsc = c;
-			}
 			branch = branch.replace('/', fsc);
 		}
 
 		String encodedPath = path == null ? "" : path.replace(' ', '-');
-		try {
-			encodedPath = URLEncoder.encode(encodedPath, "UTF-8");
-		} catch (UnsupportedEncodingException e) {
-		}
+		encodedPath = encodedPath.replace('/', fsc);
 		return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + (path == null ? "" : encodedPath)));
 	}
 
@@ -133,7 +130,8 @@
 		if (path.endsWith("/")) {
 			path = path.substring(0, path.length() - 1);
 		}
-		return path;
+		char c = runtimeManager.getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
+		return path.replace('!', '/').replace(c, '/');
 	}
 
 	protected boolean renderIndex() {
@@ -175,6 +173,9 @@
 				repository = path.substring(0, slash);
 			}
 			offset += slash;
+			if (offset == 0) {
+				offset++;
+			}
 			r = repositoryManager.getRepository(repository, false);
 			if (repository.equals(path)) {
 				// either only repository in url or no repository found
@@ -254,6 +255,15 @@
 						// load, interpret, and serve text content as UTF-8
 						String [] encodings = runtimeManager.getSettings().getStrings(Keys.web.blobEncodings).toArray(new String[0]);
 						String content = JGitUtils.getStringContent(r, commit.getTree(), requestedPath, encodings);
+						if (content == null) {
+							logger.error("RawServlet Failed to load {} {} {}", repository, commit.getName(), path);
+							String str = MessageFormat.format(
+									"# Error\nSorry, the requested resource **{0}** was not found.",
+									requestedPath);
+							response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+							error(response, str);
+							return;
+						}
 
 						byte [] bytes = content.getBytes(Constants.ENCODING);
 						response.setContentLength(bytes.length);
@@ -411,11 +421,8 @@
 		}
 	}
 
-	private void streamFromRepo(HttpServletResponse response, Repository repository,
+	protected void streamFromRepo(HttpServletResponse response, Repository repository,
 			RevCommit commit, String requestedPath) throws IOException {
-
-		response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());
-		response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
 
 		RevWalk rw = new RevWalk(repository);
 		TreeWalk tw = new TreeWalk(repository);
@@ -447,9 +454,8 @@
 		response.flushBuffer();
 	}
 
-	private void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException {
-		response.setDateHeader("Last-Modified", date.getTime());
-		response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
+	protected void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException {
+
 		try {
 			byte[] tmp = new byte[8192];
 			int len = 0;

--
Gitblit v1.9.1