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