1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.http.server;
45
46 import java.io.File;
47 import java.text.MessageFormat;
48 import java.util.LinkedList;
49 import java.util.List;
50
51 import javax.servlet.Filter;
52 import javax.servlet.FilterConfig;
53 import javax.servlet.ServletException;
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56
57 import org.eclipse.jgit.http.server.glue.ErrorServlet;
58 import org.eclipse.jgit.http.server.glue.MetaFilter;
59 import org.eclipse.jgit.http.server.glue.RegexGroupFilter;
60 import org.eclipse.jgit.http.server.glue.ServletBinder;
61 import org.eclipse.jgit.http.server.resolver.AsIsFileService;
62 import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
63 import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
64 import org.eclipse.jgit.lib.Constants;
65 import org.eclipse.jgit.transport.resolver.FileResolver;
66 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
67 import org.eclipse.jgit.transport.resolver.RepositoryResolver;
68 import org.eclipse.jgit.transport.resolver.UploadPackFactory;
69 import org.eclipse.jgit.util.StringUtils;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public class GitFilter extends MetaFilter {
87 private volatile boolean initialized;
88
89 private RepositoryResolver<HttpServletRequest> resolver;
90
91 private AsIsFileServiceolver/AsIsFileService.html#AsIsFileService">AsIsFileService asIs = new AsIsFileService();
92
93 private UploadPackFactory<HttpServletRequest> uploadPackFactory = new DefaultUploadPackFactory();
94
95 private ReceivePackFactory<HttpServletRequest> receivePackFactory = new DefaultReceivePackFactory();
96
97 private final List<Filter> uploadPackFilters = new LinkedList<>();
98
99 private final List<Filter> receivePackFilters = new LinkedList<>();
100
101
102
103
104
105
106
107 public GitFilter() {
108
109 }
110
111
112
113
114
115
116
117
118
119
120 public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) {
121 assertNotInitialized();
122 this.resolver = resolver;
123 }
124
125
126
127
128
129
130
131
132
133 public void setAsIsFileService(AsIsFileService f) {
134 assertNotInitialized();
135 this.asIs = f != null ? f : AsIsFileService.DISABLED;
136 }
137
138
139
140
141
142
143
144
145
146 @SuppressWarnings("unchecked")
147 public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) {
148 assertNotInitialized();
149 this.uploadPackFactory = f != null ? f : (UploadPackFactory<HttpServletRequest>)UploadPackFactory.DISABLED;
150 }
151
152
153
154
155
156
157
158
159
160 public void addUploadPackFilter(Filter filter) {
161 assertNotInitialized();
162 uploadPackFilters.add(filter);
163 }
164
165
166
167
168
169
170
171
172
173 @SuppressWarnings("unchecked")
174 public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) {
175 assertNotInitialized();
176 this.receivePackFactory = f != null ? f : (ReceivePackFactory<HttpServletRequest>)ReceivePackFactory.DISABLED;
177 }
178
179
180
181
182
183
184
185
186
187 public void addReceivePackFilter(Filter filter) {
188 assertNotInitialized();
189 receivePackFilters.add(filter);
190 }
191
192 private void assertNotInitialized() {
193 if (initialized)
194 throw new IllegalStateException(HttpServerText.get().alreadyInitializedByContainer);
195 }
196
197
198 @Override
199 public void init(FilterConfig filterConfig) throws ServletException {
200 super.init(filterConfig);
201
202 if (resolver == null) {
203 File root = getFile(filterConfig, "base-path");
204 boolean exportAll = getBoolean(filterConfig, "export-all");
205 setRepositoryResolver(new FileResolver<>(root, exportAll));
206 }
207
208 initialized = true;
209
210 if (uploadPackFactory != UploadPackFactory.DISABLED) {
211 ServletBinder b = serve("*/" + GitSmartHttpTools.UPLOAD_PACK);
212 b = b.through(new UploadPackServlet.Factory(uploadPackFactory));
213 for (Filter f : uploadPackFilters)
214 b = b.through(f);
215 b.with(new UploadPackServlet());
216 }
217
218 if (receivePackFactory != ReceivePackFactory.DISABLED) {
219 ServletBinder b = serve("*/" + GitSmartHttpTools.RECEIVE_PACK);
220 b = b.through(new ReceivePackServlet.Factory(receivePackFactory));
221 for (Filter f : receivePackFilters)
222 b = b.through(f);
223 b.with(new ReceivePackServlet());
224 }
225
226 ServletBinder refs = serve("*/" + Constants.INFO_REFS);
227 if (uploadPackFactory != UploadPackFactory.DISABLED) {
228 refs = refs.through(new UploadPackServlet.InfoRefs(
229 uploadPackFactory, uploadPackFilters));
230 }
231 if (receivePackFactory != ReceivePackFactory.DISABLED) {
232 refs = refs.through(new ReceivePackServlet.InfoRefs(
233 receivePackFactory, receivePackFilters));
234 }
235 if (asIs != AsIsFileService.DISABLED) {
236 refs = refs.through(new IsLocalFilter());
237 refs = refs.through(new AsIsFileFilter(asIs));
238 refs.with(new InfoRefsServlet());
239 } else
240 refs.with(new ErrorServlet(HttpServletResponse.SC_NOT_ACCEPTABLE));
241
242 if (asIs != AsIsFileService.DISABLED) {
243 final IsLocalFilterilter.html#IsLocalFilter">IsLocalFilter mustBeLocal = new IsLocalFilter();
244 final AsIsFileFilterileFilter.html#AsIsFileFilter">AsIsFileFilter enabled = new AsIsFileFilter(asIs);
245
246 serve("*/" + Constants.HEAD)
247 .through(mustBeLocal)
248 .through(enabled)
249 .with(new TextFileServlet(Constants.HEAD));
250
251 final String info_alternates = Constants.OBJECTS + "/" + Constants.INFO_ALTERNATES;
252 serve("*/" + info_alternates)
253 .through(mustBeLocal)
254 .through(enabled)
255 .with(new TextFileServlet(info_alternates));
256
257 final String http_alternates = Constants.OBJECTS + "/" + Constants.INFO_HTTP_ALTERNATES;
258 serve("*/" + http_alternates)
259 .through(mustBeLocal)
260 .through(enabled)
261 .with(new TextFileServlet(http_alternates));
262
263 serve("*/objects/info/packs")
264 .through(mustBeLocal)
265 .through(enabled)
266 .with(new InfoPacksServlet());
267
268 serveRegex("^/(.*)/objects/([0-9a-f]{2}/[0-9a-f]{38})$")
269 .through(mustBeLocal)
270 .through(enabled)
271 .through(new RegexGroupFilter(2))
272 .with(new ObjectFileServlet.Loose());
273
274 serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.pack)$")
275 .through(mustBeLocal)
276 .through(enabled)
277 .through(new RegexGroupFilter(2))
278 .with(new ObjectFileServlet.Pack());
279
280 serveRegex("^/(.*)/objects/(pack/pack-[0-9a-f]{40}\\.idx)$")
281 .through(mustBeLocal)
282 .through(enabled)
283 .through(new RegexGroupFilter(2))
284 .with(new ObjectFileServlet.PackIdx());
285 }
286 }
287
288 private static File getFile(FilterConfig cfg, String param)
289 throws ServletException {
290 String n = cfg.getInitParameter(param);
291 if (n == null || "".equals(n))
292 throw new ServletException(MessageFormat.format(HttpServerText.get().parameterNotSet, param));
293
294 File path = new File(n);
295 if (!path.exists())
296 throw new ServletException(MessageFormat.format(HttpServerText.get().pathForParamNotFound, path, param));
297 return path;
298 }
299
300 private static boolean getBoolean(FilterConfig cfg, String param)
301 throws ServletException {
302 String n = cfg.getInitParameter(param);
303 if (n == null)
304 return false;
305 try {
306 return StringUtils.toBoolean(n);
307 } catch (IllegalArgumentException err) {
308 throw new ServletException(MessageFormat.format(HttpServerText.get().invalidBoolean, param, n));
309 }
310 }
311
312
313 @Override
314 protected ServletBinder../../../org/eclipse/jgit/http/server/glue/ServletBinder.html#ServletBinder">ServletBinder register(ServletBinder binder) {
315 if (resolver == null)
316 throw new IllegalStateException(HttpServerText.get().noResolverAvailable);
317 binder = binder.through(new NoCacheFilter());
318 binder = binder.through(new RepositoryFilter(resolver));
319 return binder;
320 }
321 }