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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.server;
  44. import java.io.File;
  45. import java.text.MessageFormat;
  46. import java.util.LinkedList;
  47. import java.util.List;
  48. import javax.servlet.Filter;
  49. import javax.servlet.FilterConfig;
  50. import javax.servlet.ServletException;
  51. import javax.servlet.http.HttpServletRequest;
  52. import javax.servlet.http.HttpServletResponse;
  53. import org.eclipse.jgit.http.server.glue.ErrorServlet;
  54. import org.eclipse.jgit.http.server.glue.MetaFilter;
  55. import org.eclipse.jgit.http.server.glue.RegexGroupFilter;
  56. import org.eclipse.jgit.http.server.glue.ServletBinder;
  57. import org.eclipse.jgit.http.server.resolver.AsIsFileService;
  58. import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
  59. import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.transport.resolver.FileResolver;
  62. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  63. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  64. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  65. import org.eclipse.jgit.util.StringUtils;
  66. /**
  67. * Handles Git repository access over HTTP.
  68. * <p>
  69. * Applications embedding this filter should map a directory path within the
  70. * application to this filter. For a servlet version, see
  71. * {@link org.eclipse.jgit.http.server.GitServlet}.
  72. * <p>
  73. * Applications may wish to add additional repository action URLs to this
  74. * servlet by taking advantage of its extension from
  75. * {@link org.eclipse.jgit.http.server.glue.MetaFilter}. Callers may register
  76. * their own URL suffix translations through {@link #serve(String)}, or their
  77. * regex translations through {@link #serveRegex(String)}. Each translation
  78. * should contain a complete filter pipeline which ends with the HttpServlet
  79. * that should handle the requested action.
  80. */
  81. public class GitFilter extends MetaFilter {
  82. private volatile boolean initialized;
  83. private RepositoryResolver<HttpServletRequest> resolver;
  84. private AsIsFileService asIs = new AsIsFileService();
  85. private UploadPackFactory<HttpServletRequest> uploadPackFactory = new DefaultUploadPackFactory();
  86. private ReceivePackFactory<HttpServletRequest> receivePackFactory = new DefaultReceivePackFactory();
  87. private final List<Filter> uploadPackFilters = new LinkedList<>();
  88. private final List<Filter> receivePackFilters = new LinkedList<>();
  89. /**
  90. * New servlet that will load its base directory from {@code web.xml}.
  91. * <p>
  92. * The required parameter {@code base-path} must be configured to point to
  93. * the local filesystem directory where all served Git repositories reside.
  94. */
  95. public GitFilter() {
  96. // Initialized above by field declarations.
  97. }
  98. /**
  99. * New servlet configured with a specific resolver.
  100. *
  101. * @param resolver
  102. * the resolver to use when matching URL to Git repository. If
  103. * null the {@code base-path} parameter will be looked for in the
  104. * parameter table during init, which usually comes from the
  105. * {@code web.xml} file of the web application.
  106. */
  107. public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) {
  108. assertNotInitialized();
  109. this.resolver = resolver;
  110. }
  111. /**
  112. * Set AsIsFileService
  113. *
  114. * @param f
  115. * the filter to validate direct access to repository files
  116. * through a dumb client. If {@code null} then dumb client
  117. * support is completely disabled.
  118. */
  119. public void setAsIsFileService(AsIsFileService f) {
  120. assertNotInitialized();
  121. this.asIs = f != null ? f : AsIsFileService.DISABLED;
  122. }
  123. /**
  124. * Set upload-pack factory
  125. *
  126. * @param f
  127. * the factory to construct and configure an
  128. * {@link org.eclipse.jgit.transport.UploadPack} session when a
  129. * fetch or clone is requested by a client.
  130. */
  131. @SuppressWarnings("unchecked")
  132. public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) {
  133. assertNotInitialized();
  134. this.uploadPackFactory = f != null ? f : (UploadPackFactory<HttpServletRequest>)UploadPackFactory.DISABLED;
  135. }
  136. /**
  137. * Add upload-pack filter
  138. *
  139. * @param filter
  140. * filter to apply before any of the UploadPack operations. The
  141. * UploadPack instance is available in the request attribute
  142. * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
  143. */
  144. public void addUploadPackFilter(Filter filter) {
  145. assertNotInitialized();
  146. uploadPackFilters.add(filter);
  147. }
  148. /**
  149. * Set the receive-pack factory
  150. *
  151. * @param f
  152. * the factory to construct and configure a
  153. * {@link org.eclipse.jgit.transport.ReceivePack} session when a
  154. * push is requested by a client.
  155. */
  156. @SuppressWarnings("unchecked")
  157. public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) {
  158. assertNotInitialized();
  159. this.receivePackFactory = f != null ? f : (ReceivePackFactory<HttpServletRequest>)ReceivePackFactory.DISABLED;
  160. }
  161. /**
  162. * Add receive-pack filter
  163. *
  164. * @param filter
  165. * filter to apply before any of the ReceivePack operations. The
  166. * ReceivePack instance is available in the request attribute
  167. * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
  168. */
  169. public void addReceivePackFilter(Filter filter) {
  170. assertNotInitialized();
  171. receivePackFilters.add(filter);
  172. }
  173. private void assertNotInitialized() {
  174. if (initialized)
  175. throw new IllegalStateException(HttpServerText.get().alreadyInitializedByContainer);
  176. }
  177. /** {@inheritDoc} */
  178. @Override
  179. public void init(FilterConfig filterConfig) throws ServletException {
  180. super.init(filterConfig);
  181. if (resolver == null) {
  182. File root = getFile(filterConfig, "base-path");
  183. boolean exportAll = getBoolean(filterConfig, "export-all");
  184. setRepositoryResolver(new FileResolver<>(root, exportAll));
  185. }
  186. initialized = true;
  187. if (uploadPackFactory != UploadPackFactory.DISABLED) {
  188. ServletBinder b = serve("*/" + GitSmartHttpTools.UPLOAD_PACK);
  189. b = b.through(new UploadPackServlet.Factory(uploadPackFactory));
  190. for (Filter f : uploadPackFilters)
  191. b = b.through(f);
  192. b.with(new UploadPackServlet());
  193. }
  194. if (receivePackFactory != ReceivePackFactory.DISABLED) {
  195. ServletBinder b = serve("*/" + GitSmartHttpTools.RECEIVE_PACK);
  196. b = b.through(new ReceivePackServlet.Factory(receivePackFactory));
  197. for (Filter f : receivePackFilters)
  198. b = b.through(f);
  199. b.with(new ReceivePackServlet());
  200. }
  201. ServletBinder refs = serve("*/" + Constants.INFO_REFS);
  202. if (uploadPackFactory != UploadPackFactory.DISABLED) {
  203. refs = refs.through(new UploadPackServlet.InfoRefs(
  204. uploadPackFactory, uploadPackFilters));
  205. }
  206. if (receivePackFactory != ReceivePackFactory.DISABLED) {
  207. refs = refs.through(new ReceivePackServlet.InfoRefs(
  208. receivePackFactory, receivePackFilters));
  209. }
  210. if (asIs != AsIsFileService.DISABLED) {
  211. refs = refs.through(new IsLocalFilter());
  212. refs = refs.through(new AsIsFileFilter(asIs));
  213. refs.with(new InfoRefsServlet());
  214. } else
  215. refs.with(new ErrorServlet(HttpServletResponse.SC_NOT_ACCEPTABLE));
  216. if (asIs != AsIsFileService.DISABLED) {
  217. final IsLocalFilter mustBeLocal = new IsLocalFilter();
  218. final AsIsFileFilter enabled = new AsIsFileFilter(asIs);
  219. serve("*/" + Constants.HEAD)//
  220. .through(mustBeLocal)//
  221. .through(enabled)//
  222. .with(new TextFileServlet(Constants.HEAD));
  223. final String info_alternates = "objects/info/alternates";
  224. serve("*/" + info_alternates)//
  225. .through(mustBeLocal)//
  226. .through(enabled)//
  227. .with(new TextFileServlet(info_alternates));
  228. final String http_alternates = "objects/info/http-alternates";
  229. serve("*/" + http_alternates)//
  230. .through(mustBeLocal)//
  231. .through(enabled)//
  232. .with(new TextFileServlet(http_alternates));
  233. serve("*/objects/info/packs")//
  234. .through(mustBeLocal)//
  235. .through(enabled)//
  236. .with(new InfoPacksServlet());
  237. serveRegex("^/(.*)/objects/([0-9a-f]{2}/[0-9a-f]{38})$")//
  238. .through(mustBeLocal)//
  239. .through(enabled)//
  240. .through(new RegexGroupFilter(2))//
  241. .with(new ObjectFileServlet.Loose());
  242. serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.pack)$")//
  243. .through(mustBeLocal)//
  244. .through(enabled)//
  245. .through(new RegexGroupFilter(2))//
  246. .with(new ObjectFileServlet.Pack());
  247. serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.idx)$")//
  248. .through(mustBeLocal)//
  249. .through(enabled)//
  250. .through(new RegexGroupFilter(2))//
  251. .with(new ObjectFileServlet.PackIdx());
  252. }
  253. }
  254. private static File getFile(FilterConfig cfg, String param)
  255. throws ServletException {
  256. String n = cfg.getInitParameter(param);
  257. if (n == null || "".equals(n))
  258. throw new ServletException(MessageFormat.format(HttpServerText.get().parameterNotSet, param));
  259. File path = new File(n);
  260. if (!path.exists())
  261. throw new ServletException(MessageFormat.format(HttpServerText.get().pathForParamNotFound, path, param));
  262. return path;
  263. }
  264. private static boolean getBoolean(FilterConfig cfg, String param)
  265. throws ServletException {
  266. String n = cfg.getInitParameter(param);
  267. if (n == null)
  268. return false;
  269. try {
  270. return StringUtils.toBoolean(n);
  271. } catch (IllegalArgumentException err) {
  272. throw new ServletException(MessageFormat.format(HttpServerText.get().invalidBoolean, param, n));
  273. }
  274. }
  275. /** {@inheritDoc} */
  276. @Override
  277. protected ServletBinder register(ServletBinder binder) {
  278. if (resolver == null)
  279. throw new IllegalStateException(HttpServerText.get().noResolverAvailable);
  280. binder = binder.through(new NoCacheFilter());
  281. binder = binder.through(new RepositoryFilter(resolver));
  282. return binder;
  283. }
  284. }