You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RawServlet.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Copyright 2014 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.servlet;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.net.URLEncoder;
  22. import java.text.MessageFormat;
  23. import java.text.ParseException;
  24. import java.util.ArrayList;
  25. import java.util.Date;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.TreeMap;
  30. import javax.servlet.ServletContext;
  31. import javax.servlet.ServletException;
  32. import javax.servlet.http.HttpServlet;
  33. import javax.servlet.http.HttpServletRequest;
  34. import javax.servlet.http.HttpServletResponse;
  35. import org.apache.tika.Tika;
  36. import org.eclipse.jgit.lib.FileMode;
  37. import org.eclipse.jgit.lib.MutableObjectId;
  38. import org.eclipse.jgit.lib.ObjectLoader;
  39. import org.eclipse.jgit.lib.ObjectReader;
  40. import org.eclipse.jgit.lib.Repository;
  41. import org.eclipse.jgit.revwalk.RevCommit;
  42. import org.eclipse.jgit.revwalk.RevWalk;
  43. import org.eclipse.jgit.treewalk.TreeWalk;
  44. import org.eclipse.jgit.treewalk.filter.PathFilter;
  45. import org.slf4j.Logger;
  46. import org.slf4j.LoggerFactory;
  47. import com.gitblit.Constants;
  48. import com.gitblit.Keys;
  49. import com.gitblit.manager.IRepositoryManager;
  50. import com.gitblit.manager.IRuntimeManager;
  51. import com.gitblit.models.PathModel;
  52. import com.gitblit.utils.ByteFormat;
  53. import com.gitblit.utils.JGitUtils;
  54. import com.gitblit.utils.MarkdownUtils;
  55. import com.gitblit.utils.StringUtils;
  56. import com.google.inject.Inject;
  57. import com.google.inject.Singleton;
  58. /**
  59. * Serves the content of a branch.
  60. *
  61. * @author James Moger
  62. *
  63. */
  64. @Singleton
  65. public class RawServlet extends HttpServlet {
  66. // Forward slash character
  67. static final char FSC = '!';
  68. private static final long serialVersionUID = 1L;
  69. private transient Logger logger = LoggerFactory.getLogger(RawServlet.class);
  70. private final IRuntimeManager runtimeManager;
  71. private final IRepositoryManager repositoryManager;
  72. @Inject
  73. public RawServlet(
  74. IRuntimeManager runtimeManager,
  75. IRepositoryManager repositoryManager) {
  76. this.runtimeManager = runtimeManager;
  77. this.repositoryManager = repositoryManager;
  78. }
  79. /**
  80. * Returns an url to this servlet for the specified parameters.
  81. *
  82. * @param baseURL
  83. * @param repository
  84. * @param branch
  85. * @param path
  86. * @return an url
  87. */
  88. public static String asLink(String baseURL, String repository, String branch, String path) {
  89. if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
  90. baseURL = baseURL.substring(0, baseURL.length() - 1);
  91. }
  92. if (repository.length() > 0 && repository.charAt(repository.length() - 1) == '/') {
  93. repository = repository.substring(0, repository.length() - 1);
  94. }
  95. if (repository.length() > 0 && repository.charAt(0) == '/') {
  96. repository = repository.substring(1);
  97. }
  98. char fsc = GitblitContext.getManager(IRuntimeManager.class).getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
  99. if (fsc == '/') {
  100. fsc = FSC;
  101. }
  102. if (branch != null) {
  103. branch = Repository.shortenRefName(branch).replace('/', fsc);
  104. }
  105. if (path != null && path.length() > 0 && path.charAt(0) == '/') {
  106. path = path.substring(1);
  107. }
  108. String encodedPath = path == null ? "" : path.replace('/', fsc);
  109. return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath));
  110. }
  111. /**
  112. * Find and return the name of a branch from a given repository in a HTTP request path info.
  113. * The branch name returned is transformed to the form in the repository, i.e. a transformation
  114. * of the forward slash character in the URL is reversed.
  115. *
  116. * @param repository
  117. * Path of repository, no leading slash, no trailing slash
  118. * @param pathInfo
  119. * The sanitised path info from a HTTP request, i.e. without the leading slash.
  120. *
  121. * @return The name of the branch from the path info, unescaped.
  122. */
  123. String getBranch(String repository, String pathInfo)
  124. {
  125. if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) return "";
  126. String branch = pathInfo.substring(pathInfo.indexOf(repository) + repository.length() + 1);
  127. int fs = branch.indexOf('/');
  128. if (fs > -1) {
  129. branch = branch.substring(0, fs);
  130. }
  131. char c = runtimeManager.getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
  132. return branch.replace('!', '/').replace(c, '/');
  133. }
  134. /**
  135. * Find and return the path from a given repository and given branch in a HTTP request path info.
  136. * The path string returned is transformed to the form in the repository, i.e. a transformation
  137. * of the forward slash character in the URL is reversed.
  138. *
  139. * @param repository
  140. * Path of repository, no leading slash, no trailing slash
  141. * @param branch
  142. * Branch name from the repository, i.e. with forward slash character, no leading slash, no trailing slash.
  143. * @param pathInfo
  144. * The sanitised path info from a HTTP request, i.e. without the leading slash.
  145. *
  146. * @return The file/folder path part from the path info, in unescaped form.
  147. */
  148. String getPath(String repository, String branch, String pathInfo)
  149. {
  150. if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) return "";
  151. // Make the branch look like in the URL, or else it won't match later in the `indexOf`.
  152. char c = runtimeManager.getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
  153. char fsc = (c == '/') ? FSC : c;
  154. String base = repository + "/" + Repository.shortenRefName(branch).replace('/', fsc);
  155. // 'repository/' or 'repository/branch' or 'repository/branch/'
  156. if (pathInfo.equals(base)) {
  157. return "";
  158. }
  159. // I have no idea why 'indexOf(base)' is used, which assumes something could come before 'base' in
  160. // the pathInfo string. But since it is here, we handle it until we completly refactor the paths used
  161. // in Gitblit to something sensible.
  162. // 'leadin/repository/'
  163. // 'leadin/repository/branch'
  164. int pathStart = pathInfo.indexOf(base) + base.length();
  165. // 'leadin/repository/branch/'
  166. if (pathStart < pathInfo.length() && pathInfo.charAt(pathStart) == '/') pathStart++;
  167. if (pathInfo.length() == pathStart) return "";
  168. // 'leadin/repository/branch/path'
  169. String path = pathInfo.substring(pathStart);
  170. path = path.replace('!', '/').replace(c, '/');
  171. // 'repository/branch/path/'
  172. // 'leadin/repository/branch/path/'
  173. if (path.endsWith("/")) {
  174. path = path.substring(0, path.length() - 1);
  175. }
  176. return path;
  177. }
  178. protected boolean renderIndex() {
  179. return false;
  180. }
  181. /**
  182. * Retrieves the specified resource from the specified branch of the
  183. * repository.
  184. *
  185. * @param request
  186. * @param response
  187. * @throws javax.servlet.ServletException
  188. * @throws java.io.IOException
  189. */
  190. private void processRequest(HttpServletRequest request, HttpServletResponse response)
  191. throws ServletException, IOException {
  192. String path = request.getPathInfo();
  193. if (path.toLowerCase().endsWith(".git")) {
  194. // forward to url with trailing /
  195. // this is important for relative pages links
  196. response.sendRedirect(request.getServletPath() + path + "/");
  197. return;
  198. }
  199. if (path.charAt(0) == '/') {
  200. // strip leading /
  201. path = path.substring(1);
  202. }
  203. // determine repository and resource from url
  204. String repository = path;
  205. Repository r = null;
  206. int terminator = repository.length();
  207. do {
  208. repository = repository.substring(0, terminator);
  209. r = repositoryManager.getRepository(repository, false);
  210. terminator = repository.lastIndexOf('/');
  211. } while (r == null && terminator > -1 );
  212. ServletContext context = request.getSession().getServletContext();
  213. try {
  214. if (r == null) {
  215. // repository not found!
  216. String mkd = MessageFormat.format(
  217. "# Error\nSorry, no valid **repository** specified in this url: {0}!",
  218. path);
  219. error(response, mkd);
  220. return;
  221. }
  222. // identify the branch
  223. String branch = getBranch(repository, path);
  224. if (StringUtils.isEmpty(branch)) {
  225. branch = r.getBranch();
  226. if (branch == null) {
  227. // no branches found! empty?
  228. String mkd = MessageFormat.format(
  229. "# Error\nSorry, no valid **branch** specified in this url: {0}!",
  230. path);
  231. error(response, mkd);
  232. } else {
  233. // redirect to default branch
  234. String base = request.getRequestURI();
  235. String url = base + branch + "/";
  236. response.sendRedirect(url);
  237. }
  238. return;
  239. }
  240. // identify the requested path
  241. String requestedPath = getPath(repository, branch, path);
  242. // identify the commit
  243. RevCommit commit = JGitUtils.getCommit(r, branch);
  244. if (commit == null) {
  245. // branch not found!
  246. String mkd = MessageFormat.format(
  247. "# Error\nSorry, the repository {0} does not have a **{1}** branch!",
  248. repository, branch);
  249. error(response, mkd);
  250. return;
  251. }
  252. Map<String, String> quickContentTypes = new HashMap<>();
  253. quickContentTypes.put("html", "text/html");
  254. quickContentTypes.put("htm", "text/html");
  255. quickContentTypes.put("xml", "application/xml");
  256. quickContentTypes.put("json", "application/json");
  257. List<PathModel> pathEntries = JGitUtils.getFilesInPath(r, requestedPath, commit);
  258. if (pathEntries.isEmpty()) {
  259. // requested a specific resource
  260. String file = StringUtils.getLastPathElement(requestedPath);
  261. try {
  262. String ext = StringUtils.getFileExtension(file).toLowerCase();
  263. // We can't parse out an extension for classic "dotfiles", so make a general assumption that
  264. // they're text files to allow presenting them in browser instead of only for download.
  265. //
  266. // However, that only holds for files with no other extension included, for files that happen
  267. // to start with a dot but also include an extension, process the extension normally.
  268. // This logic covers .gitattributes, .gitignore, .zshrc, etc., but does not cover .mongorc.js, .zshrc.bak
  269. boolean isExtensionlessDotfile = file.charAt(0) == '.' && (file.length() == 1 || file.indexOf('.', 1) < 0);
  270. String contentType = isExtensionlessDotfile ? "text/plain" : quickContentTypes.get(ext);
  271. if (contentType == null) {
  272. List<String> exts = runtimeManager.getSettings().getStrings(Keys.web.prettyPrintExtensions);
  273. if (exts.contains(ext)) {
  274. // extension is a registered text type for pretty printing
  275. contentType = "text/plain";
  276. } else {
  277. // query Tika for the content type
  278. Tika tika = new Tika();
  279. contentType = tika.detect(file);
  280. }
  281. }
  282. if (contentType == null) {
  283. // ask the container for the content type
  284. contentType = context.getMimeType(requestedPath);
  285. if (contentType == null) {
  286. // still unknown content type, assume binary
  287. contentType = "application/octet-stream";
  288. }
  289. }
  290. if (isTextType(contentType) || isTextDataType(contentType)) {
  291. // load, interpret, and serve text content as UTF-8
  292. String [] encodings = runtimeManager.getSettings().getStrings(Keys.web.blobEncodings).toArray(new String[0]);
  293. String content = JGitUtils.getStringContent(r, commit.getTree(), requestedPath, encodings);
  294. if (content == null) {
  295. logger.error("RawServlet Failed to load {} {} {}", repository, commit.getName(), path);
  296. notFound(response, requestedPath, branch);
  297. return;
  298. }
  299. byte [] bytes = content.getBytes(Constants.ENCODING);
  300. setContentType(response, contentType);
  301. response.setContentLength(bytes.length);
  302. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  303. sendContent(response, JGitUtils.getCommitDate(commit), is);
  304. } else {
  305. // stream binary content directly from the repository
  306. if (!streamFromRepo(request, response, r, commit, requestedPath)) {
  307. logger.error("RawServlet Failed to load {} {} {}", repository, commit.getName(), path);
  308. notFound(response, requestedPath, branch);
  309. }
  310. }
  311. return;
  312. } catch (Exception e) {
  313. logger.error(null, e);
  314. }
  315. } else {
  316. // path request
  317. if (!request.getPathInfo().endsWith("/")) {
  318. // redirect to trailing '/' url
  319. response.sendRedirect(request.getServletPath() + request.getPathInfo() + "/");
  320. return;
  321. }
  322. if (renderIndex()) {
  323. // locate and render an index file
  324. Map<String, String> names = new TreeMap<String, String>();
  325. for (PathModel entry : pathEntries) {
  326. names.put(entry.name.toLowerCase(), entry.name);
  327. }
  328. List<String> extensions = new ArrayList<String>();
  329. extensions.add("html");
  330. extensions.add("htm");
  331. String content = null;
  332. for (String ext : extensions) {
  333. String key = "index." + ext;
  334. if (names.containsKey(key)) {
  335. String fileName = names.get(key);
  336. String fullPath = fileName;
  337. if (!requestedPath.isEmpty()) {
  338. fullPath = requestedPath + "/" + fileName;
  339. }
  340. String [] encodings = runtimeManager.getSettings().getStrings(Keys.web.blobEncodings).toArray(new String[0]);
  341. String stringContent = JGitUtils.getStringContent(r, commit.getTree(), fullPath, encodings);
  342. if (stringContent == null) {
  343. continue;
  344. }
  345. content = stringContent;
  346. requestedPath = fullPath;
  347. break;
  348. }
  349. }
  350. response.setContentType("text/html; charset=" + Constants.ENCODING);
  351. byte [] bytes = content.getBytes(Constants.ENCODING);
  352. response.setContentLength(bytes.length);
  353. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  354. sendContent(response, JGitUtils.getCommitDate(commit), is);
  355. return;
  356. }
  357. }
  358. // no content, document list or 404 page
  359. if (pathEntries.isEmpty()) {
  360. // default 404 page
  361. notFound(response, requestedPath, branch);
  362. return;
  363. } else {
  364. //
  365. // directory list
  366. //
  367. response.setContentType("text/html");
  368. response.getWriter().append("<style>table th, table td { min-width: 150px; text-align: left; }</style>");
  369. response.getWriter().append("<table>");
  370. response.getWriter().append("<thead><tr><th>path</th><th>mode</th><th>size</th></tr>");
  371. response.getWriter().append("</thead>");
  372. response.getWriter().append("<tbody>");
  373. String pattern = "<tr><td><a href=\"{0}/{1}\">{1}</a></td><td>{2}</td><td>{3}</td></tr>";
  374. final ByteFormat byteFormat = new ByteFormat();
  375. if (!pathEntries.isEmpty()) {
  376. if (pathEntries.get(0).path.indexOf('/') > -1) {
  377. // we are in a subdirectory, add parent directory link
  378. String pp = URLEncoder.encode(requestedPath, Constants.ENCODING);
  379. pathEntries.add(0, new PathModel("..", pp + "/..", null, 0, FileMode.TREE.getBits(), null, null));
  380. }
  381. }
  382. String basePath = request.getServletPath() + request.getPathInfo();
  383. if (basePath.charAt(basePath.length() - 1) == '/') {
  384. // strip trailing slash
  385. basePath = basePath.substring(0, basePath.length() - 1);
  386. }
  387. for (PathModel entry : pathEntries) {
  388. String pp = URLEncoder.encode(entry.name, Constants.ENCODING);
  389. response.getWriter().append(MessageFormat.format(pattern, basePath, pp,
  390. JGitUtils.getPermissionsFromMode(entry.mode),
  391. entry.isFile() ? byteFormat.format(entry.size) : ""));
  392. }
  393. response.getWriter().append("</tbody>");
  394. response.getWriter().append("</table>");
  395. }
  396. } catch (Throwable t) {
  397. logger.error("Failed to write page to client", t);
  398. } finally {
  399. r.close();
  400. }
  401. }
  402. protected boolean isTextType(String contentType) {
  403. if (contentType.startsWith("text/")
  404. || "application/json".equals(contentType)
  405. || "application/xml".equals(contentType)) {
  406. return true;
  407. }
  408. return false;
  409. }
  410. protected boolean isTextDataType(String contentType) {
  411. if ("image/svg+xml".equals(contentType)) {
  412. return true;
  413. }
  414. return false;
  415. }
  416. /**
  417. * Override all text types to be plain text.
  418. *
  419. * @param response
  420. * @param contentType
  421. */
  422. protected void setContentType(HttpServletResponse response, String contentType) {
  423. if (isTextType(contentType)) {
  424. response.setContentType("text/plain");
  425. } else {
  426. response.setContentType(contentType);
  427. }
  428. }
  429. protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository,
  430. RevCommit commit, String requestedPath) throws IOException {
  431. boolean served = false;
  432. RevWalk rw = new RevWalk(repository);
  433. TreeWalk tw = new TreeWalk(repository);
  434. try {
  435. tw.reset();
  436. tw.addTree(commit.getTree());
  437. PathFilter f = PathFilter.create(requestedPath);
  438. tw.setFilter(f);
  439. tw.setRecursive(true);
  440. MutableObjectId id = new MutableObjectId();
  441. ObjectReader reader = tw.getObjectReader();
  442. while (tw.next()) {
  443. FileMode mode = tw.getFileMode(0);
  444. if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
  445. continue;
  446. }
  447. tw.getObjectId(id, 0);
  448. String filename = StringUtils.getLastPathElement(requestedPath);
  449. try {
  450. String userAgent = request.getHeader("User-Agent");
  451. if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
  452. response.setHeader("Content-Disposition", "filename=\""
  453. + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
  454. } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
  455. response.setHeader("Content-Disposition", "attachment; filename=\""
  456. + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
  457. } else {
  458. response.setHeader("Content-Disposition", "attachment; filename=\""
  459. + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\"");
  460. }
  461. }
  462. catch (UnsupportedEncodingException e) {
  463. response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
  464. }
  465. long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB);
  466. setContentType(response, "application/octet-stream");
  467. response.setIntHeader("Content-Length", (int) len);
  468. ObjectLoader ldr = repository.open(id);
  469. ldr.copyTo(response.getOutputStream());
  470. served = true;
  471. }
  472. } finally {
  473. tw.close();
  474. rw.dispose();
  475. }
  476. response.flushBuffer();
  477. return served;
  478. }
  479. protected void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException {
  480. try {
  481. byte[] tmp = new byte[8192];
  482. int len = 0;
  483. while ((len = is.read(tmp)) > -1) {
  484. response.getOutputStream().write(tmp, 0, len);
  485. }
  486. } finally {
  487. is.close();
  488. }
  489. response.flushBuffer();
  490. }
  491. protected void notFound(HttpServletResponse response, String requestedPath, String branch)
  492. throws ParseException, ServletException, IOException {
  493. String str = MessageFormat.format(
  494. "# Error\nSorry, the requested resource **{0}** was not found in **{1}**.",
  495. requestedPath, branch);
  496. response.setStatus(HttpServletResponse.SC_NOT_FOUND);
  497. error(response, str);
  498. }
  499. private void error(HttpServletResponse response, String mkd) throws ServletException,
  500. IOException, ParseException {
  501. String content = MarkdownUtils.transformMarkdown(mkd);
  502. response.setContentType("text/html; charset=" + Constants.ENCODING);
  503. response.getWriter().write(content);
  504. }
  505. @Override
  506. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  507. throws ServletException, IOException {
  508. processRequest(request, response);
  509. }
  510. @Override
  511. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  512. throws ServletException, IOException {
  513. processRequest(request, response);
  514. }
  515. }