Browse Source

Merged #154 "Raw servlet returns 0-length files instead of 404s"

tags/v1.6.1
James Moger 9 years ago
parent
commit
f1f4197727

+ 2
- 2
src/main/java/com/gitblit/servlet/PagesServlet.java View File

} }
@Override @Override
protected void streamFromRepo(HttpServletResponse response, Repository repository,
protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository,
RevCommit commit, String requestedPath) throws IOException { RevCommit commit, String requestedPath) throws IOException {
response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime()); response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());
response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate"); response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
super.streamFromRepo(response, repository, commit, requestedPath);
return super.streamFromRepo(request, response, repository, commit, requestedPath);
} }
@Override @Override

+ 40
- 34
src/main/java/com/gitblit/servlet/RawServlet.java View File

fsc = c; fsc = c;
} }
if (branch != null) { if (branch != null) {
branch = branch.replace('/', fsc);
branch = Repository.shortenRefName(branch).replace('/', fsc);
} }


String encodedPath = path == null ? "" : path.replace(' ', '-'); String encodedPath = path == null ? "" : path.replace(' ', '-');
} }
} }


setContentType(response, contentType);

if (isTextType(contentType)) { if (isTextType(contentType)) {


// load, interpret, and serve text content as UTF-8 // load, interpret, and serve text content as UTF-8
String content = JGitUtils.getStringContent(r, commit.getTree(), requestedPath, encodings); String content = JGitUtils.getStringContent(r, commit.getTree(), requestedPath, encodings);
if (content == null) { if (content == null) {
logger.error("RawServlet Failed to load {} {} {}", repository, commit.getName(), path); 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; return;
} }


byte [] bytes = content.getBytes(Constants.ENCODING); byte [] bytes = content.getBytes(Constants.ENCODING);
setContentType(response, contentType);
response.setContentLength(bytes.length); response.setContentLength(bytes.length);
ByteArrayInputStream is = new ByteArrayInputStream(bytes); ByteArrayInputStream is = new ByteArrayInputStream(bytes);
sendContent(response, JGitUtils.getCommitDate(commit), is); sendContent(response, JGitUtils.getCommitDate(commit), is);


} else { } 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 // 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; return;
} catch (Exception e) { } catch (Exception e) {
// no content, document list or 404 page // no content, document list or 404 page
if (pathEntries.isEmpty()) { if (pathEntries.isEmpty()) {
// default 404 page // 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; return;
} else { } else {
// //
} }
} }


protected void streamFromRepo(HttpServletResponse response, Repository repository,
protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository,
RevCommit commit, String requestedPath) throws IOException { RevCommit commit, String requestedPath) throws IOException {


boolean served = false;
RevWalk rw = new RevWalk(repository); RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository); TreeWalk tw = new TreeWalk(repository);
try { try {
} }
tw.getObjectId(id, 0); 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); long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB);
setContentType(response, "application/octet-stream");
response.setIntHeader("Content-Length", (int) len); response.setIntHeader("Content-Length", (int) len);
ObjectLoader ldr = repository.open(id); ObjectLoader ldr = repository.open(id);
ldr.copyTo(response.getOutputStream()); ldr.copyTo(response.getOutputStream());
served = true;
} }
} finally { } finally {
tw.release(); tw.release();
} }


response.flushBuffer(); response.flushBuffer();
return served;
} }


protected void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException { protected void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException {
response.flushBuffer(); 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, private void error(HttpServletResponse response, String mkd) throws ServletException,
IOException, ParseException { IOException, ParseException {
String content = MarkdownUtils.transformMarkdown(mkd); String content = MarkdownUtils.transformMarkdown(mkd);

Loading…
Cancel
Save