]> source.dussan.org Git - gitblit.git/commitdiff
Fix raw serving of files/directories that do not exist in a branch/ref 54/154/1
authorJames Moger <james.moger@gitblit.com>
Thu, 4 Sep 2014 20:05:09 +0000 (16:05 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 4 Sep 2014 20:05:09 +0000 (16:05 -0400)
src/main/java/com/gitblit/servlet/PagesServlet.java
src/main/java/com/gitblit/servlet/RawServlet.java

index a8e834183fc7286fa2e56f6775b10bc27dce2ebc..defd9e630a60036ef9e9bdb52035eb6d0939bafc 100644 (file)
@@ -84,13 +84,13 @@ public class PagesServlet extends RawServlet {
        }\r
 \r
        @Override\r
-       protected void streamFromRepo(HttpServletResponse response, Repository repository,\r
+       protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository,\r
                        RevCommit commit, String requestedPath) throws IOException {\r
 \r
                response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());\r
                response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");\r
 \r
-               super.streamFromRepo(response, repository, commit, requestedPath);\r
+               return super.streamFromRepo(request, response, repository, commit, requestedPath);\r
        }\r
 \r
        @Override\r
index 30d33fd758bc78b742990d1633447fd68bd3d7b3..7504b4bec39cda7af2d0a54c93b4cc47283dbd32 100644 (file)
@@ -101,7 +101,7 @@ public class RawServlet extends DaggerServlet {
                        fsc = c;
                }
                if (branch != null) {
-                       branch = branch.replace('/', fsc);
+                       branch = Repository.shortenRefName(branch).replace('/', fsc);
                }
 
                String encodedPath = path == null ? "" : path.replace(' ', '-');
@@ -248,8 +248,6 @@ public class RawServlet extends DaggerServlet {
                                                }
                                        }
 
-                                       setContentType(response, contentType);
-
                                        if (isTextType(contentType)) {
 
                                                // load, interpret, and serve text content as UTF-8
@@ -257,41 +255,22 @@ public class RawServlet extends DaggerServlet {
                                                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) {
@@ -351,11 +330,7 @@ public class RawServlet extends DaggerServlet {
                        // 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 {
                                //
@@ -421,9 +396,10 @@ public class RawServlet extends DaggerServlet {
                }
        }
 
-       protected void streamFromRepo(HttpServletResponse response, Repository repository,
+       protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository,
                        RevCommit commit, String requestedPath) throws IOException {
 
+               boolean served = false;
                RevWalk rw = new RevWalk(repository);
                TreeWalk tw = new TreeWalk(repository);
                try {
@@ -441,10 +417,30 @@ public class RawServlet extends DaggerServlet {
                                }
                                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();
@@ -452,6 +448,7 @@ public class RawServlet extends DaggerServlet {
                }
 
                response.flushBuffer();
+               return served;
        }
 
        protected void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException {
@@ -468,6 +465,15 @@ public class RawServlet extends DaggerServlet {
                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);