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 static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
47 import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
48 import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
49 import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
50 import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
51 import static org.eclipse.jgit.http.server.ClientVersionUtil.hasChunkedEncodingRequestBug;
52 import static org.eclipse.jgit.http.server.ClientVersionUtil.parseVersion;
53 import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK;
54 import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_REQUEST_TYPE;
55 import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_RESULT_TYPE;
56 import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
57 import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER;
58 import static org.eclipse.jgit.http.server.ServletUtils.consumeRequestBody;
59 import static org.eclipse.jgit.http.server.ServletUtils.getInputStream;
60 import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
61 import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
62
63 import java.io.IOException;
64 import java.text.MessageFormat;
65 import java.util.List;
66
67 import javax.servlet.Filter;
68 import javax.servlet.FilterChain;
69 import javax.servlet.FilterConfig;
70 import javax.servlet.ServletException;
71 import javax.servlet.ServletRequest;
72 import javax.servlet.ServletResponse;
73 import javax.servlet.http.HttpServlet;
74 import javax.servlet.http.HttpServletRequest;
75 import javax.servlet.http.HttpServletResponse;
76
77 import org.eclipse.jgit.lib.Repository;
78 import org.eclipse.jgit.transport.InternalHttpServerGlue;
79 import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
80 import org.eclipse.jgit.transport.ServiceMayNotContinueException;
81 import org.eclipse.jgit.transport.UploadPack;
82 import org.eclipse.jgit.transport.UploadPackInternalServerErrorException;
83 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
84 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
85 import org.eclipse.jgit.transport.resolver.UploadPackFactory;
86
87
88 class UploadPackServlet extends HttpServlet {
89 private static final long serialVersionUID = 1L;
90
91 static class InfoRefs extends SmartServiceInfoRefs {
92 private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
93
94 InfoRefs(UploadPackFactory<HttpServletRequest> uploadPackFactory,
95 List<Filter> filters) {
96 super(UPLOAD_PACK, filters);
97 this.uploadPackFactory = uploadPackFactory;
98 }
99
100 @Override
101 protected void begin(HttpServletRequest req, Repository db)
102 throws IOException, ServiceNotEnabledException,
103 ServiceNotAuthorizedException {
104 UploadPack up = uploadPackFactory.create(req, db);
105 InternalHttpServerGlue.setPeerUserAgent(
106 up,
107 req.getHeader(HDR_USER_AGENT));
108 req.setAttribute(ATTRIBUTE_HANDLER, up);
109 }
110
111 @Override
112 protected void advertise(HttpServletRequest req,
113 PacketLineOutRefAdvertiser pck) throws IOException,
114 ServiceNotEnabledException, ServiceNotAuthorizedException {
115 UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
116 try {
117 up.setBiDirectionalPipe(false);
118 up.sendAdvertisedRefs(pck);
119 } finally {
120 up.getRevWalk().close();
121 }
122 }
123 }
124
125 static class Factory implements Filter {
126 private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
127
128 Factory(UploadPackFactory<HttpServletRequest> uploadPackFactory) {
129 this.uploadPackFactory = uploadPackFactory;
130 }
131
132 @Override
133 public void doFilter(ServletRequest request, ServletResponse response,
134 FilterChain chain) throws IOException, ServletException {
135 HttpServletRequest req = (HttpServletRequest) request;
136 HttpServletResponse rsp = (HttpServletResponse) response;
137 UploadPack rp;
138 try {
139 rp = uploadPackFactory.create(req, getRepository(req));
140 } catch (ServiceNotAuthorizedException e) {
141 rsp.sendError(SC_UNAUTHORIZED, e.getMessage());
142 return;
143 } catch (ServiceNotEnabledException e) {
144 sendError(req, rsp, SC_FORBIDDEN, e.getMessage());
145 return;
146 }
147
148 try {
149 req.setAttribute(ATTRIBUTE_HANDLER, rp);
150 chain.doFilter(req, rsp);
151 } finally {
152 req.removeAttribute(ATTRIBUTE_HANDLER);
153 }
154 }
155
156 @Override
157 public void init(FilterConfig filterConfig) throws ServletException {
158
159 }
160
161 @Override
162 public void destroy() {
163
164 }
165 }
166
167
168 @Override
169 public void doPost(final HttpServletRequest req,
170 final HttpServletResponse rsp) throws IOException {
171 if (!UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType())) {
172 rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE);
173 return;
174 }
175
176 int[] version = parseVersion(req.getHeader(HDR_USER_AGENT));
177 if (hasChunkedEncodingRequestBug(version, req)) {
178 GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n"
179 + HttpServerText.get().clientHas175ChunkedEncodingBug);
180 return;
181 }
182
183 SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
184 @Override
185 public void flush() throws IOException {
186 doFlush();
187 }
188 };
189
190 UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
191 try {
192 up.setBiDirectionalPipe(false);
193 rsp.setContentType(UPLOAD_PACK_RESULT_TYPE);
194
195 up.upload(getInputStream(req), out, null);
196 out.close();
197
198 } catch (ServiceMayNotContinueException e) {
199 if (e.isOutput()) {
200 consumeRequestBody(req);
201 out.close();
202 } else if (!rsp.isCommitted()) {
203 rsp.reset();
204 sendError(req, rsp, e.getStatusCode(), e.getMessage());
205 }
206 return;
207
208 } catch (UploadPackInternalServerErrorException e) {
209
210 log(up.getRepository(), e.getCause());
211 consumeRequestBody(req);
212 out.close();
213
214 } catch (Throwable e) {
215 log(up.getRepository(), e);
216 if (!rsp.isCommitted()) {
217 rsp.reset();
218 sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
219 }
220 return;
221 }
222 }
223
224 private void log(Repository git, Throwable e) {
225 getServletContext().log(MessageFormat.format(
226 HttpServerText.get().internalErrorDuringUploadPack,
227 ServletUtils.identify(git)), e);
228 }
229 }