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.

GitFilter.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.http.server;
  11. import java.io.File;
  12. import java.text.MessageFormat;
  13. import java.util.LinkedList;
  14. import java.util.List;
  15. import javax.servlet.Filter;
  16. import javax.servlet.FilterConfig;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import org.eclipse.jgit.http.server.glue.ErrorServlet;
  21. import org.eclipse.jgit.http.server.glue.MetaFilter;
  22. import org.eclipse.jgit.http.server.glue.RegexGroupFilter;
  23. import org.eclipse.jgit.http.server.glue.ServletBinder;
  24. import org.eclipse.jgit.http.server.resolver.AsIsFileService;
  25. import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
  26. import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
  27. import org.eclipse.jgit.lib.Constants;
  28. import org.eclipse.jgit.transport.resolver.FileResolver;
  29. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  30. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  31. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  32. import org.eclipse.jgit.util.StringUtils;
  33. /**
  34. * Handles Git repository access over HTTP.
  35. * <p>
  36. * Applications embedding this filter should map a directory path within the
  37. * application to this filter. For a servlet version, see
  38. * {@link org.eclipse.jgit.http.server.GitServlet}.
  39. * <p>
  40. * Applications may wish to add additional repository action URLs to this
  41. * servlet by taking advantage of its extension from
  42. * {@link org.eclipse.jgit.http.server.glue.MetaFilter}. Callers may register
  43. * their own URL suffix translations through {@link #serve(String)}, or their
  44. * regex translations through {@link #serveRegex(String)}. Each translation
  45. * should contain a complete filter pipeline which ends with the HttpServlet
  46. * that should handle the requested action.
  47. */
  48. public class GitFilter extends MetaFilter {
  49. private volatile boolean initialized;
  50. private RepositoryResolver<HttpServletRequest> resolver;
  51. private AsIsFileService asIs = new AsIsFileService();
  52. private UploadPackFactory<HttpServletRequest> uploadPackFactory = new DefaultUploadPackFactory();
  53. private UploadPackErrorHandler uploadPackErrorHandler;
  54. private ReceivePackFactory<HttpServletRequest> receivePackFactory = new DefaultReceivePackFactory();
  55. private ReceivePackErrorHandler receivePackErrorHandler;
  56. private final List<Filter> uploadPackFilters = new LinkedList<>();
  57. private final List<Filter> receivePackFilters = new LinkedList<>();
  58. /**
  59. * New servlet that will load its base directory from {@code web.xml}.
  60. * <p>
  61. * The required parameter {@code base-path} must be configured to point to
  62. * the local filesystem directory where all served Git repositories reside.
  63. */
  64. public GitFilter() {
  65. // Initialized above by field declarations.
  66. }
  67. /**
  68. * New servlet configured with a specific resolver.
  69. *
  70. * @param resolver
  71. * the resolver to use when matching URL to Git repository. If
  72. * null the {@code base-path} parameter will be looked for in the
  73. * parameter table during init, which usually comes from the
  74. * {@code web.xml} file of the web application.
  75. */
  76. public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) {
  77. assertNotInitialized();
  78. this.resolver = resolver;
  79. }
  80. /**
  81. * Set AsIsFileService
  82. *
  83. * @param f
  84. * the filter to validate direct access to repository files
  85. * through a dumb client. If {@code null} then dumb client
  86. * support is completely disabled.
  87. */
  88. public void setAsIsFileService(AsIsFileService f) {
  89. assertNotInitialized();
  90. this.asIs = f != null ? f : AsIsFileService.DISABLED;
  91. }
  92. /**
  93. * Set upload-pack factory
  94. *
  95. * @param f
  96. * the factory to construct and configure an
  97. * {@link org.eclipse.jgit.transport.UploadPack} session when a
  98. * fetch or clone is requested by a client.
  99. */
  100. @SuppressWarnings("unchecked")
  101. public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) {
  102. assertNotInitialized();
  103. this.uploadPackFactory = f != null ? f : (UploadPackFactory<HttpServletRequest>)UploadPackFactory.DISABLED;
  104. }
  105. /**
  106. * Set a custom error handler for git-upload-pack.
  107. *
  108. * @param h
  109. * A custom error handler for git-upload-pack.
  110. */
  111. public void setUploadPackErrorHandler(UploadPackErrorHandler h) {
  112. assertNotInitialized();
  113. this.uploadPackErrorHandler = h;
  114. }
  115. /**
  116. * Add upload-pack filter
  117. *
  118. * @param filter
  119. * filter to apply before any of the UploadPack operations. The
  120. * UploadPack instance is available in the request attribute
  121. * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
  122. */
  123. public void addUploadPackFilter(Filter filter) {
  124. assertNotInitialized();
  125. uploadPackFilters.add(filter);
  126. }
  127. /**
  128. * Set the receive-pack factory
  129. *
  130. * @param f
  131. * the factory to construct and configure a
  132. * {@link org.eclipse.jgit.transport.ReceivePack} session when a
  133. * push is requested by a client.
  134. */
  135. @SuppressWarnings("unchecked")
  136. public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) {
  137. assertNotInitialized();
  138. this.receivePackFactory = f != null ? f : (ReceivePackFactory<HttpServletRequest>)ReceivePackFactory.DISABLED;
  139. }
  140. /**
  141. * Set a custom error handler for git-receive-pack.
  142. *
  143. * @param h
  144. * A custom error handler for git-receive-pack.
  145. */
  146. public void setReceivePackErrorHandler(ReceivePackErrorHandler h) {
  147. assertNotInitialized();
  148. this.receivePackErrorHandler = h;
  149. }
  150. /**
  151. * Add receive-pack filter
  152. *
  153. * @param filter
  154. * filter to apply before any of the ReceivePack operations. The
  155. * ReceivePack instance is available in the request attribute
  156. * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
  157. */
  158. public void addReceivePackFilter(Filter filter) {
  159. assertNotInitialized();
  160. receivePackFilters.add(filter);
  161. }
  162. private void assertNotInitialized() {
  163. if (initialized)
  164. throw new IllegalStateException(HttpServerText.get().alreadyInitializedByContainer);
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. public void init(FilterConfig filterConfig) throws ServletException {
  169. super.init(filterConfig);
  170. if (resolver == null) {
  171. File root = getFile(filterConfig, "base-path");
  172. boolean exportAll = getBoolean(filterConfig, "export-all");
  173. setRepositoryResolver(new FileResolver<>(root, exportAll));
  174. }
  175. initialized = true;
  176. if (uploadPackFactory != UploadPackFactory.DISABLED) {
  177. ServletBinder b = serve("*/" + GitSmartHttpTools.UPLOAD_PACK);
  178. b = b.through(new UploadPackServlet.Factory(uploadPackFactory));
  179. for (Filter f : uploadPackFilters)
  180. b = b.through(f);
  181. b.with(new UploadPackServlet(uploadPackErrorHandler));
  182. }
  183. if (receivePackFactory != ReceivePackFactory.DISABLED) {
  184. ServletBinder b = serve("*/" + GitSmartHttpTools.RECEIVE_PACK);
  185. b = b.through(new ReceivePackServlet.Factory(receivePackFactory));
  186. for (Filter f : receivePackFilters)
  187. b = b.through(f);
  188. b.with(new ReceivePackServlet(receivePackErrorHandler));
  189. }
  190. ServletBinder refs = serve("*/" + Constants.INFO_REFS);
  191. if (uploadPackFactory != UploadPackFactory.DISABLED) {
  192. refs = refs.through(new UploadPackServlet.InfoRefs(
  193. uploadPackFactory, uploadPackFilters));
  194. }
  195. if (receivePackFactory != ReceivePackFactory.DISABLED) {
  196. refs = refs.through(new ReceivePackServlet.InfoRefs(
  197. receivePackFactory, receivePackFilters));
  198. }
  199. if (asIs != AsIsFileService.DISABLED) {
  200. refs = refs.through(new IsLocalFilter());
  201. refs = refs.through(new AsIsFileFilter(asIs));
  202. refs.with(new InfoRefsServlet());
  203. } else
  204. refs.with(new ErrorServlet(HttpServletResponse.SC_NOT_ACCEPTABLE));
  205. if (asIs != AsIsFileService.DISABLED) {
  206. final IsLocalFilter mustBeLocal = new IsLocalFilter();
  207. final AsIsFileFilter enabled = new AsIsFileFilter(asIs);
  208. serve("*/" + Constants.HEAD)//
  209. .through(mustBeLocal)//
  210. .through(enabled)//
  211. .with(new TextFileServlet(Constants.HEAD));
  212. final String info_alternates = Constants.OBJECTS + "/" + Constants.INFO_ALTERNATES;
  213. serve("*/" + info_alternates)//
  214. .through(mustBeLocal)//
  215. .through(enabled)//
  216. .with(new TextFileServlet(info_alternates));
  217. final String http_alternates = Constants.OBJECTS + "/" + Constants.INFO_HTTP_ALTERNATES;
  218. serve("*/" + http_alternates)//
  219. .through(mustBeLocal)//
  220. .through(enabled)//
  221. .with(new TextFileServlet(http_alternates));
  222. serve("*/objects/info/packs")//
  223. .through(mustBeLocal)//
  224. .through(enabled)//
  225. .with(new InfoPacksServlet());
  226. serveRegex("^/(.*)/objects/([0-9a-f]{2}/[0-9a-f]{38})$")//
  227. .through(mustBeLocal)//
  228. .through(enabled)//
  229. .through(new RegexGroupFilter(2))//
  230. .with(new ObjectFileServlet.Loose());
  231. serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.pack)$")//
  232. .through(mustBeLocal)//
  233. .through(enabled)//
  234. .through(new RegexGroupFilter(2))//
  235. .with(new ObjectFileServlet.Pack());
  236. serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.idx)$")//
  237. .through(mustBeLocal)//
  238. .through(enabled)//
  239. .through(new RegexGroupFilter(2))//
  240. .with(new ObjectFileServlet.PackIdx());
  241. }
  242. }
  243. private static File getFile(FilterConfig cfg, String param)
  244. throws ServletException {
  245. String n = cfg.getInitParameter(param);
  246. if (n == null || "".equals(n))
  247. throw new ServletException(MessageFormat.format(HttpServerText.get().parameterNotSet, param));
  248. File path = new File(n);
  249. if (!path.exists())
  250. throw new ServletException(MessageFormat.format(HttpServerText.get().pathForParamNotFound, path, param));
  251. return path;
  252. }
  253. private static boolean getBoolean(FilterConfig cfg, String param)
  254. throws ServletException {
  255. String n = cfg.getInitParameter(param);
  256. if (n == null)
  257. return false;
  258. try {
  259. return StringUtils.toBoolean(n);
  260. } catch (IllegalArgumentException err) {
  261. throw new ServletException(MessageFormat.format(HttpServerText.get().invalidBoolean, param, n));
  262. }
  263. }
  264. /** {@inheritDoc} */
  265. @Override
  266. protected ServletBinder register(ServletBinder binder) {
  267. if (resolver == null)
  268. throw new IllegalStateException(HttpServerText.get().noResolverAvailable);
  269. binder = binder.through(new NoCacheFilter());
  270. binder = binder.through(new RepositoryFilter(resolver));
  271. return binder;
  272. }
  273. }