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