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.UnpackException;
80 import org.eclipse.jgit.lib.Repository;
81 import org.eclipse.jgit.transport.InternalHttpServerGlue;
82 import org.eclipse.jgit.transport.ReceivePack;
83 import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
84 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
85 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
86 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
87
88
89 class ReceivePackServlet extends HttpServlet {
90 private static final long serialVersionUID = 1L;
91
92 static class InfoRefs extends SmartServiceInfoRefs {
93 private final ReceivePackFactory<HttpServletRequest> receivePackFactory;
94
95 InfoRefs(ReceivePackFactory<HttpServletRequest> receivePackFactory,
96 List<Filter> filters) {
97 super(RECEIVE_PACK, filters);
98 this.receivePackFactory = receivePackFactory;
99 }
100
101 @Override
102 protected void begin(HttpServletRequest req, Repository db)
103 throws IOException, ServiceNotEnabledException,
104 ServiceNotAuthorizedException {
105 ReceivePack rp = receivePackFactory.create(req, db);
106 InternalHttpServerGlue.setPeerUserAgent(
107 rp,
108 req.getHeader(HDR_USER_AGENT));
109 req.setAttribute(ATTRIBUTE_HANDLER, rp);
110 }
111
112 @Override
113 protected void advertise(HttpServletRequest req,
114 PacketLineOutRefAdvertiser pck) throws IOException,
115 ServiceNotEnabledException, ServiceNotAuthorizedException {
116 ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER);
117 try {
118 rp.sendAdvertisedRefs(pck);
119 } finally {
120 rp.getRevWalk().close();
121 }
122 }
123 }
124
125 static class Factory implements Filter {
126 private final ReceivePackFactory<HttpServletRequest> receivePackFactory;
127
128 Factory(ReceivePackFactory<HttpServletRequest> receivePackFactory) {
129 this.receivePackFactory = receivePackFactory;
130 }
131
132 public void doFilter(ServletRequest request, ServletResponse response,
133 FilterChain chain) throws IOException, ServletException {
134 HttpServletRequest req = (HttpServletRequest) request;
135 HttpServletResponse rsp = (HttpServletResponse) response;
136 ReceivePack rp;
137 try {
138 rp = receivePackFactory.create(req, getRepository(req));
139 } catch (ServiceNotAuthorizedException e) {
140 rsp.sendError(SC_UNAUTHORIZED);
141 return;
142
143 } catch (ServiceNotEnabledException e) {
144 sendError(req, rsp, SC_FORBIDDEN);
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 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 }