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