| | |
| | | import java.text.ParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.TreeMap; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.http.HttpServlet; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | |
| | | |
| | | import com.gitblit.Constants; |
| | | import com.gitblit.Keys; |
| | | import com.gitblit.dagger.DaggerServlet; |
| | | import com.gitblit.manager.IRepositoryManager; |
| | | import com.gitblit.manager.IRuntimeManager; |
| | | import com.gitblit.models.PathModel; |
| | |
| | | import com.gitblit.utils.JGitUtils; |
| | | import com.gitblit.utils.MarkdownUtils; |
| | | import com.gitblit.utils.StringUtils; |
| | | |
| | | import dagger.ObjectGraph; |
| | | import com.google.inject.Inject; |
| | | import com.google.inject.Singleton; |
| | | |
| | | /** |
| | | * Serves the content of a branch. |
| | |
| | | * @author James Moger |
| | | * |
| | | */ |
| | | public class RawServlet extends DaggerServlet { |
| | | @Singleton |
| | | public class RawServlet extends HttpServlet { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | private transient Logger logger = LoggerFactory.getLogger(RawServlet.class); |
| | | |
| | | private IRuntimeManager runtimeManager; |
| | | private final IRuntimeManager runtimeManager; |
| | | |
| | | private IRepositoryManager repositoryManager; |
| | | private final IRepositoryManager repositoryManager; |
| | | |
| | | @Override |
| | | protected void inject(ObjectGraph dagger) { |
| | | this.runtimeManager = dagger.get(IRuntimeManager.class); |
| | | this.repositoryManager = dagger.get(IRepositoryManager.class); |
| | | @Inject |
| | | public RawServlet( |
| | | IRuntimeManager runtimeManager, |
| | | IRepositoryManager repositoryManager) { |
| | | |
| | | this.runtimeManager = runtimeManager; |
| | | this.repositoryManager = repositoryManager; |
| | | } |
| | | |
| | | /** |
| | |
| | | if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') { |
| | | baseURL = baseURL.substring(0, baseURL.length() - 1); |
| | | } |
| | | String encodedPath = path.replace(' ', '-'); |
| | | try { |
| | | encodedPath = URLEncoder.encode(encodedPath, "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | |
| | | char fsc = '!'; |
| | | char c = GitblitContext.getManager(IRuntimeManager.class).getSettings().getChar(Keys.web.forwardSlashCharacter, '/'); |
| | | if (c != '/') { |
| | | fsc = c; |
| | | } |
| | | return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + (path == null ? "" : encodedPath))); |
| | | if (branch != null) { |
| | | branch = Repository.shortenRefName(branch).replace('/', fsc); |
| | | } |
| | | |
| | | String encodedPath = path == null ? "" : path.replace('/', fsc); |
| | | return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath)); |
| | | } |
| | | |
| | | protected String getBranch(String repository, HttpServletRequest request) { |
| | |
| | | if (fs > -1) { |
| | | branch = branch.substring(0, fs); |
| | | } |
| | | return branch; |
| | | char c = runtimeManager.getSettings().getChar(Keys.web.forwardSlashCharacter, '/'); |
| | | return branch.replace('!', '/').replace(c, '/'); |
| | | } |
| | | |
| | | protected String getPath(String repository, String branch, HttpServletRequest request) { |
| | |
| | | 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() { |
| | |
| | | } |
| | | |
| | | // determine repository and resource from url |
| | | String repository = ""; |
| | | String repository = path; |
| | | Repository r = null; |
| | | int offset = 0; |
| | | while (r == null) { |
| | | int slash = path.indexOf('/', offset); |
| | | if (slash == -1) { |
| | | repository = path; |
| | | } else { |
| | | repository = path.substring(0, slash); |
| | | } |
| | | offset += slash; |
| | | int terminator = repository.length(); |
| | | do { |
| | | repository = repository.substring(0, terminator); |
| | | r = repositoryManager.getRepository(repository, false); |
| | | if (repository.equals(path)) { |
| | | // either only repository in url or no repository found |
| | | break; |
| | | } |
| | | } |
| | | terminator = repository.lastIndexOf('/'); |
| | | } while (r == null && terminator > -1 ); |
| | | |
| | | ServletContext context = request.getSession().getServletContext(); |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | Map<String, String> quickContentTypes = new HashMap<>(); |
| | | quickContentTypes.put("html", "text/html"); |
| | | quickContentTypes.put("htm", "text/html"); |
| | | quickContentTypes.put("xml", "application/xml"); |
| | | quickContentTypes.put("json", "application/json"); |
| | | |
| | | List<PathModel> pathEntries = JGitUtils.getFilesInPath(r, requestedPath, commit); |
| | | if (pathEntries.isEmpty()) { |
| | | // requested a specific resource |
| | | String file = StringUtils.getLastPathElement(requestedPath); |
| | | try { |
| | | // query Tika for the content type |
| | | Tika tika = new Tika(); |
| | | String contentType = tika.detect(file); |
| | | |
| | | String ext = StringUtils.getFileExtension(file).toLowerCase(); |
| | | // We can't parse out an extension for classic "dotfiles", so make a general assumption that |
| | | // they're text files to allow presenting them in browser instead of only for download. |
| | | // |
| | | // However, that only holds for files with no other extension included, for files that happen |
| | | // to start with a dot but also include an extension, process the extension normally. |
| | | // This logic covers .gitattributes, .gitignore, .zshrc, etc., but does not cover .mongorc.js, .zshrc.bak |
| | | boolean isExtensionlessDotfile = file.charAt(0) == '.' && (file.length() == 1 || file.indexOf('.', 1) < 0); |
| | | String contentType = isExtensionlessDotfile ? "text/plain" : quickContentTypes.get(ext); |
| | | |
| | | if (contentType == null) { |
| | | List<String> exts = runtimeManager.getSettings().getStrings(Keys.web.prettyPrintExtensions); |
| | | if (exts.contains(ext)) { |
| | | // extension is a registered text type for pretty printing |
| | | contentType = "text/plain"; |
| | | } else { |
| | | // query Tika for the content type |
| | | Tika tika = new Tika(); |
| | | contentType = tika.detect(file); |
| | | } |
| | | } |
| | | |
| | | if (contentType == null) { |
| | | // ask the container for the content type |
| | |
| | | } |
| | | } |
| | | |
| | | setContentType(response, contentType); |
| | | |
| | | if (isTextType(contentType)) { |
| | | if (isTextType(contentType) || isTextDataType(contentType)) { |
| | | |
| | | // 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); |
| | | 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) { |
| | |
| | | // 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 { |
| | | // |
| | |
| | | return false; |
| | | } |
| | | |
| | | protected boolean isTextDataType(String contentType) { |
| | | if ("image/svg+xml".equals(contentType)) { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * Override all text types to be plain text. |
| | | * |
| | |
| | | } |
| | | } |
| | | |
| | | 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 { |
| | |
| | | } |
| | | 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(); |
| | | tw.close(); |
| | | rw.dispose(); |
| | | } |
| | | |
| | | 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; |
| | |
| | | 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); |