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 @Override
168 public void doPost(final HttpServletRequest req,
169 final HttpServletResponse rsp) throws IOException {
170 if (!UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType())) {
171 rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE);
172 return;
173 }
174
175 int[] version = parseVersion(req.getHeader(HDR_USER_AGENT));
176 if (hasChunkedEncodingRequestBug(version, req)) {
177 GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n"
178 + HttpServerText.get().clientHas175ChunkedEncodingBug);
179 return;
180 }
181
182 SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
183 @Override
184 public void flush() throws IOException {
185 doFlush();
186 }
187 };
188
189 UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
190 try {
191 up.setBiDirectionalPipe(false);
192 rsp.setContentType(UPLOAD_PACK_RESULT_TYPE);
193
194 up.upload(getInputStream(req), out, null);
195 out.close();
196
197 } catch (ServiceMayNotContinueException e) {
198 if (e.isOutput()) {
199 consumeRequestBody(req);
200 out.close();
201 } else if (!rsp.isCommitted()) {
202 rsp.reset();
203 sendError(req, rsp, e.getStatusCode(), e.getMessage());
204 }
205 return;
206
207 } catch (UploadPackInternalServerErrorException e) {
208
209 log(up.getRepository(), e.getCause());
210 consumeRequestBody(req);
211 out.close();
212
213 } catch (Throwable e) {
214 log(up.getRepository(), e);
215 if (!rsp.isCommitted()) {
216 rsp.reset();
217 sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
218 }
219 return;
220 }
221 }
222
223 private void log(Repository git, Throwable e) {
224 getServletContext().log(MessageFormat.format(
225 HttpServerText.get().internalErrorDuringUploadPack,
226 ServletUtils.identify(git)), e);
227 }
228 }