From 38dbe7600e39731cc603e8adb211b81bd2fee2c0 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 11 Feb 2015 10:13:56 -0500
Subject: [PATCH] Add 'UTF8' encoding alias
---
src/main/java/com/gitblit/servlet/RawServlet.java | 108 ++++++++++++++++++++++++++----------------------------
1 files changed, 52 insertions(+), 56 deletions(-)
diff --git a/src/main/java/com/gitblit/servlet/RawServlet.java b/src/main/java/com/gitblit/servlet/RawServlet.java
index 0cda59a..ca41d0a 100644
--- a/src/main/java/com/gitblit/servlet/RawServlet.java
+++ b/src/main/java/com/gitblit/servlet/RawServlet.java
@@ -95,21 +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);
+ branch = Repository.shortenRefName(branch).replace('/', fsc);
}
- String encodedPath = path == null ? "" : path.replace(' ', '-');
- try {
- encodedPath = URLEncoder.encode(encodedPath, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- }
- return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + (path == null ? "" : encodedPath)));
+ String encodedPath = path == null ? "" : path.replace('/', fsc);
+ return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath));
}
protected String getBranch(String repository, HttpServletRequest request) {
@@ -133,7 +129,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() {
@@ -174,10 +171,7 @@
} else {
repository = path.substring(0, slash);
}
- offset += slash;
- if (offset == 0) {
- offset++;
- }
+ offset = ( slash + 1 );
r = repositoryManager.getRepository(repository, false);
if (repository.equals(path)) {
// either only repository in url or no repository found
@@ -250,8 +244,6 @@
}
}
- setContentType(response, contentType);
-
if (isTextType(contentType)) {
// load, interpret, and serve text content as UTF-8
@@ -259,41 +251,22 @@
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);
+ notFound(response, requestedPath, branch);
return;
}
byte [] bytes = content.getBytes(Constants.ENCODING);
+ setContentType(response, contentType);
response.setContentLength(bytes.length);
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
sendContent(response, JGitUtils.getCommitDate(commit), is);
} else {
- // serve binary content
- String filename = StringUtils.getLastPathElement(requestedPath);
- try {
- String userAgent = request.getHeader("User-Agent");
- if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
- response.setHeader("Content-Disposition", "filename=\""
- + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
- } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
- response.setHeader("Content-Disposition", "attachment; filename=\""
- + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
- } else {
- response.setHeader("Content-Disposition", "attachment; filename=\""
- + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\"");
- }
- }
- catch (UnsupportedEncodingException e) {
- response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
- }
-
// stream binary content directly from the repository
- streamFromRepo(response, r, commit, requestedPath);
+ if (!streamFromRepo(request, response, r, commit, requestedPath)) {
+ logger.error("RawServlet Failed to load {} {} {}", repository, commit.getName(), path);
+ notFound(response, requestedPath, branch);
+ }
}
return;
} catch (Exception e) {
@@ -353,11 +326,7 @@
// no content, document list or 404 page
if (pathEntries.isEmpty()) {
// default 404 page
- String str = MessageFormat.format(
- "# Error\nSorry, the requested resource **{0}** was not found.",
- requestedPath);
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
- error(response, str);
+ notFound(response, requestedPath, branch);
return;
} else {
//
@@ -423,12 +392,10 @@
}
}
- private void streamFromRepo(HttpServletResponse response, Repository repository,
+ protected boolean streamFromRepo(HttpServletRequest request, 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");
-
+ boolean served = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
@@ -446,10 +413,30 @@
}
tw.getObjectId(id, 0);
+ String filename = StringUtils.getLastPathElement(requestedPath);
+ try {
+ String userAgent = request.getHeader("User-Agent");
+ if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
+ response.setHeader("Content-Disposition", "filename=\""
+ + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
+ } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
+ response.setHeader("Content-Disposition", "attachment; filename=\""
+ + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
+ } else {
+ response.setHeader("Content-Disposition", "attachment; filename=\""
+ + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\"");
+ }
+ }
+ catch (UnsupportedEncodingException e) {
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
+ }
+
long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB);
+ setContentType(response, "application/octet-stream");
response.setIntHeader("Content-Length", (int) len);
ObjectLoader ldr = repository.open(id);
ldr.copyTo(response.getOutputStream());
+ served = true;
}
} finally {
tw.release();
@@ -457,11 +444,11 @@
}
response.flushBuffer();
+ return served;
}
- 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;
@@ -474,6 +461,15 @@
response.flushBuffer();
}
+ protected void notFound(HttpServletResponse response, String requestedPath, String branch)
+ throws ParseException, ServletException, IOException {
+ String str = MessageFormat.format(
+ "# Error\nSorry, the requested resource **{0}** was not found in **{1}**.",
+ requestedPath, branch);
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ error(response, str);
+ }
+
private void error(HttpServletResponse response, String mkd) throws ServletException,
IOException, ParseException {
String content = MarkdownUtils.transformMarkdown(mkd);
--
Gitblit v1.9.1