View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  /** Server side implementation of smart push over HTTP. */
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 		@Override
134 		public void doFilter(ServletRequest request, ServletResponse response,
135 				FilterChain chain) throws IOException, ServletException {
136 			HttpServletRequest req = (HttpServletRequest) request;
137 			HttpServletResponse rsp = (HttpServletResponse) response;
138 			ReceivePack rp;
139 			try {
140 				rp = receivePackFactory.create(req, getRepository(req));
141 			} catch (ServiceNotAuthorizedException e) {
142 				rsp.sendError(SC_UNAUTHORIZED, e.getMessage());
143 				return;
144 			} catch (ServiceNotEnabledException e) {
145 				sendError(req, rsp, SC_FORBIDDEN, e.getMessage());
146 				return;
147 			}
148 
149 			try {
150 				req.setAttribute(ATTRIBUTE_HANDLER, rp);
151 				chain.doFilter(req, rsp);
152 			} finally {
153 				req.removeAttribute(ATTRIBUTE_HANDLER);
154 			}
155 		}
156 
157 		@Override
158 		public void init(FilterConfig filterConfig) throws ServletException {
159 			// Nothing.
160 		}
161 
162 		@Override
163 		public void destroy() {
164 			// Nothing.
165 		}
166 	}
167 
168 	/** {@inheritDoc} */
169 	@Override
170 	public void doPost(final HttpServletRequest req,
171 			final HttpServletResponse rsp) throws IOException {
172 		if (!RECEIVE_PACK_REQUEST_TYPE.equals(req.getContentType())) {
173 			rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE);
174 			return;
175 		}
176 
177 		int[] version = parseVersion(req.getHeader(HDR_USER_AGENT));
178 		if (hasChunkedEncodingRequestBug(version, req)) {
179 			GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n"
180 					+ HttpServerText.get().clientHas175ChunkedEncodingBug);
181 			return;
182 		}
183 
184 		SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
185 			@Override
186 			public void flush() throws IOException {
187 				doFlush();
188 			}
189 		};
190 
191 		ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER);
192 		try {
193 			rp.setBiDirectionalPipe(false);
194 			rp.setEchoCommandFailures(hasPushStatusBug(version));
195 			rsp.setContentType(RECEIVE_PACK_RESULT_TYPE);
196 
197 			rp.receive(getInputStream(req), out, null);
198 			out.close();
199 		} catch (CorruptObjectException e ) {
200 			// This should be already reported to the client.
201 			getServletContext().log(MessageFormat.format(
202 					HttpServerText.get().receivedCorruptObject,
203 					e.getMessage(),
204 					ServletUtils.identify(rp.getRepository())));
205 			consumeRequestBody(req);
206 			out.close();
207 
208 		} catch (UnpackException | PackProtocolException e) {
209 			// This should be already reported to the client.
210 			log(rp.getRepository(), e.getCause());
211 			consumeRequestBody(req);
212 			out.close();
213 
214 		} catch (Throwable e) {
215 			log(rp.getRepository(), e);
216 			if (!rsp.isCommitted()) {
217 				rsp.reset();
218 				sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
219 			}
220 			return;
221 		}
222 	}
223 
224 	private void log(Repository git, Throwable e) {
225 		getServletContext().log(MessageFormat.format(
226 				HttpServerText.get().internalErrorDuringReceivePack,
227 				ServletUtils.identify(git)), e);
228 	}
229 }