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.

GitServlet.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 javax.servlet.ServletConfig;
  47. import javax.servlet.ServletException;
  48. import javax.servlet.http.HttpServletRequest;
  49. import javax.servlet.http.HttpServletResponse;
  50. import org.eclipse.jgit.http.server.glue.ErrorServlet;
  51. import org.eclipse.jgit.http.server.glue.MetaServlet;
  52. import org.eclipse.jgit.http.server.glue.RegexGroupFilter;
  53. import org.eclipse.jgit.http.server.glue.ServletBinder;
  54. import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
  55. import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
  56. import org.eclipse.jgit.http.server.resolver.AsIsFileService;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.transport.ReceivePack;
  59. import org.eclipse.jgit.transport.UploadPack;
  60. import org.eclipse.jgit.transport.resolver.FileResolver;
  61. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  62. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  63. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  64. import org.eclipse.jgit.util.StringUtils;
  65. /**
  66. * Handles Git repository access over HTTP.
  67. * <p>
  68. * Applications embedding this servlet should map a directory path within the
  69. * application to this servlet, for example:
  70. *
  71. * <pre>
  72. * &lt;servlet&gt;
  73. * &lt;servlet-name&gt;GitServlet&lt;/servlet-name&gt;
  74. * &lt;servlet-class&gt;org.eclipse.jgit.http.server.GitServlet&lt;/servlet-class&gt;
  75. * &lt;init-param&gt;
  76. * &lt;param-name&gt;base-path&lt;/param-name&gt;
  77. * &lt;param-value&gt;/var/srv/git&lt;/param-value&gt;
  78. * &lt;/init-param&gt;
  79. * &lt;init-param&gt;
  80. * &lt;param-name&gt;export-all&lt;/param-name&gt;
  81. * &lt;param-value&gt;0&lt;/param-value&gt;
  82. * &lt;/init-param&gt;
  83. * &lt;/servlet&gt;
  84. * &lt;servlet-mapping&gt;
  85. * &lt;servlet-name&gt;GitServlet&lt;/servlet-name&gt;
  86. * &lt;url-pattern&gt;/git/*&lt;/url-pattern&gt;
  87. * &lt;/servlet-mapping&gt;
  88. * </pre>
  89. *
  90. * <p>
  91. * Applications may wish to add additional repository action URLs to this
  92. * servlet by taking advantage of its extension from {@link MetaServlet}.
  93. * Callers may register their own URL suffix translations through
  94. * {@link #serve(String)}, or their regex translations through
  95. * {@link #serveRegex(String)}. Each translation should contain a complete
  96. * filter pipeline which ends with the HttpServlet that should handle the
  97. * requested action.
  98. */
  99. public class GitServlet extends MetaServlet {
  100. private static final long serialVersionUID = 1L;
  101. private volatile boolean initialized;
  102. private RepositoryResolver<HttpServletRequest> resolver;
  103. private AsIsFileService asIs = new AsIsFileService();
  104. private UploadPackFactory<HttpServletRequest> uploadPackFactory = new DefaultUploadPackFactory();
  105. private ReceivePackFactory<HttpServletRequest> receivePackFactory = new DefaultReceivePackFactory();
  106. /**
  107. * New servlet that will load its base directory from {@code web.xml}.
  108. * <p>
  109. * The required parameter {@code base-path} must be configured to point to
  110. * the local filesystem directory where all served Git repositories reside.
  111. */
  112. public GitServlet() {
  113. // Initialized above by field declarations.
  114. }
  115. /**
  116. * New servlet configured with a specific resolver.
  117. *
  118. * @param resolver
  119. * the resolver to use when matching URL to Git repository. If
  120. * null the {@code base-path} parameter will be looked for in the
  121. * parameter table during init, which usually comes from the
  122. * {@code web.xml} file of the web application.
  123. */
  124. public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) {
  125. assertNotInitialized();
  126. this.resolver = resolver;
  127. }
  128. /**
  129. * @param f
  130. * the filter to validate direct access to repository files
  131. * through a dumb client. If {@code null} then dumb client
  132. * support is completely disabled.
  133. */
  134. public void setAsIsFileService(AsIsFileService f) {
  135. assertNotInitialized();
  136. this.asIs = f != null ? f : AsIsFileService.DISABLED;
  137. }
  138. /**
  139. * @param f
  140. * the factory to construct and configure an {@link UploadPack}
  141. * session when a fetch or clone is requested by a client.
  142. */
  143. @SuppressWarnings("unchecked")
  144. public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) {
  145. assertNotInitialized();
  146. this.uploadPackFactory = f != null ? f : (UploadPackFactory<HttpServletRequest>)UploadPackFactory.DISABLED;
  147. }
  148. /**
  149. * @param f
  150. * the factory to construct and configure a {@link ReceivePack}
  151. * session when a push is requested by a client.
  152. */
  153. @SuppressWarnings("unchecked")
  154. public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) {
  155. assertNotInitialized();
  156. this.receivePackFactory = f != null ? f : (ReceivePackFactory<HttpServletRequest>)ReceivePackFactory.DISABLED;
  157. }
  158. private void assertNotInitialized() {
  159. if (initialized)
  160. throw new IllegalStateException(HttpServerText.get().alreadyInitializedByContainer);
  161. }
  162. @Override
  163. public void init(final ServletConfig config) throws ServletException {
  164. super.init(config);
  165. if (resolver == null) {
  166. final File root = getFile("base-path");
  167. final boolean exportAll = getBoolean("export-all");
  168. setRepositoryResolver(new FileResolver<HttpServletRequest>(root, exportAll));
  169. }
  170. initialized = true;
  171. if (uploadPackFactory != UploadPackFactory.DISABLED) {
  172. serve("*/git-upload-pack")//
  173. .with(new UploadPackServlet(uploadPackFactory));
  174. }
  175. if (receivePackFactory != ReceivePackFactory.DISABLED) {
  176. serve("*/git-receive-pack")//
  177. .with(new ReceivePackServlet(receivePackFactory));
  178. }
  179. ServletBinder refs = serve("*/" + Constants.INFO_REFS);
  180. if (uploadPackFactory != UploadPackFactory.DISABLED) {
  181. refs = refs.through(//
  182. new UploadPackServlet.InfoRefs(uploadPackFactory));
  183. }
  184. if (receivePackFactory != ReceivePackFactory.DISABLED) {
  185. refs = refs.through(//
  186. new ReceivePackServlet.InfoRefs(receivePackFactory));
  187. }
  188. if (asIs != AsIsFileService.DISABLED) {
  189. refs = refs.through(new IsLocalFilter());
  190. refs = refs.through(new AsIsFileFilter(asIs));
  191. refs.with(new InfoRefsServlet());
  192. } else
  193. refs.with(new ErrorServlet(HttpServletResponse.SC_FORBIDDEN));
  194. if (asIs != AsIsFileService.DISABLED) {
  195. final IsLocalFilter mustBeLocal = new IsLocalFilter();
  196. final AsIsFileFilter enabled = new AsIsFileFilter(asIs);
  197. serve("*/" + Constants.HEAD)//
  198. .through(mustBeLocal)//
  199. .through(enabled)//
  200. .with(new TextFileServlet(Constants.HEAD));
  201. final String info_alternates = "objects/info/alternates";
  202. serve("*/" + info_alternates)//
  203. .through(mustBeLocal)//
  204. .through(enabled)//
  205. .with(new TextFileServlet(info_alternates));
  206. final String http_alternates = "objects/info/http-alternates";
  207. serve("*/" + http_alternates)//
  208. .through(mustBeLocal)//
  209. .through(enabled)//
  210. .with(new TextFileServlet(http_alternates));
  211. serve("*/objects/info/packs")//
  212. .through(mustBeLocal)//
  213. .through(enabled)//
  214. .with(new InfoPacksServlet());
  215. serveRegex("^/(.*)/objects/([0-9a-f]{2}/[0-9a-f]{38})$")//
  216. .through(mustBeLocal)//
  217. .through(enabled)//
  218. .through(new RegexGroupFilter(2))//
  219. .with(new ObjectFileServlet.Loose());
  220. serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.pack)$")//
  221. .through(mustBeLocal)//
  222. .through(enabled)//
  223. .through(new RegexGroupFilter(2))//
  224. .with(new ObjectFileServlet.Pack());
  225. serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.idx)$")//
  226. .through(mustBeLocal)//
  227. .through(enabled)//
  228. .through(new RegexGroupFilter(2))//
  229. .with(new ObjectFileServlet.PackIdx());
  230. }
  231. }
  232. private File getFile(final String param) throws ServletException {
  233. String n = getInitParameter(param);
  234. if (n == null || "".equals(n))
  235. throw new ServletException(MessageFormat.format(HttpServerText.get().parameterNotSet, param));
  236. File path = new File(n);
  237. if (!path.exists())
  238. throw new ServletException(MessageFormat.format(HttpServerText.get().pathForParamNotFound, path, param));
  239. return path;
  240. }
  241. private boolean getBoolean(String param) throws ServletException {
  242. String n = getInitParameter(param);
  243. if (n == null)
  244. return false;
  245. try {
  246. return StringUtils.toBoolean(n);
  247. } catch (IllegalArgumentException err) {
  248. throw new ServletException(MessageFormat.format(HttpServerText.get().invalidBoolean, param, n));
  249. }
  250. }
  251. @Override
  252. protected ServletBinder register(ServletBinder binder) {
  253. if (resolver == null)
  254. throw new IllegalStateException(HttpServerText.get().noResolverAvailable);
  255. binder = binder.through(new NoCacheFilter());
  256. binder = binder.through(new RepositoryFilter(resolver));
  257. return binder;
  258. }
  259. }