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 		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 			// Nothing.
158 		}
159 
160 		public void destroy() {
161 			// Nothing.
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 			// This should be already reported to the client.
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 			// This should be already reported to the client.
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 }