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_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.RECEIVE_PACK;
51  import static org.eclipse.jgit.http.server.GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE;
52  import static org.eclipse.jgit.http.server.GitSmartHttpTools.RECEIVE_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.errors.CorruptObjectException;
75  import org.eclipse.jgit.errors.PackProtocolException;
76  import org.eclipse.jgit.errors.UnpackException;
77  import org.eclipse.jgit.lib.Repository;
78  import org.eclipse.jgit.transport.InternalHttpServerGlue;
79  import org.eclipse.jgit.transport.ReceivePack;
80  import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
81  import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
82  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
83  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
84  
85  /** Server side implementation of smart push over HTTP. */
86  class ReceivePackServlet extends HttpServlet {
87  	private static final long serialVersionUID = 1L;
88  
89  	static class InfoRefs extends SmartServiceInfoRefs {
90  		private final ReceivePackFactory<HttpServletRequest> receivePackFactory;
91  
92  		InfoRefs(ReceivePackFactory<HttpServletRequest> receivePackFactory,
93  				List<Filter> filters) {
94  			super(RECEIVE_PACK, filters);
95  			this.receivePackFactory = receivePackFactory;
96  		}
97  
98  		@Override
99  		protected void begin(HttpServletRequest req, Repository db)
100 				throws IOException, ServiceNotEnabledException,
101 				ServiceNotAuthorizedException {
102 			ReceivePack rp = receivePackFactory.create(req, db);
103 			InternalHttpServerGlue.setPeerUserAgent(
104 					rp,
105 					req.getHeader(HDR_USER_AGENT));
106 			req.setAttribute(ATTRIBUTE_HANDLER, rp);
107 		}
108 
109 		@Override
110 		protected void advertise(HttpServletRequest req,
111 				PacketLineOutRefAdvertiser pck) throws IOException,
112 				ServiceNotEnabledException, ServiceNotAuthorizedException {
113 			ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER);
114 			try {
115 				rp.sendAdvertisedRefs(pck);
116 			} finally {
117 				rp.getRevWalk().close();
118 			}
119 		}
120 	}
121 
122 	static class Factory implements Filter {
123 		private final ReceivePackFactory<HttpServletRequest> receivePackFactory;
124 
125 		Factory(ReceivePackFactory<HttpServletRequest> receivePackFactory) {
126 			this.receivePackFactory = receivePackFactory;
127 		}
128 
129 		@Override
130 		public void doFilter(ServletRequest request, ServletResponse response,
131 				FilterChain chain) throws IOException, ServletException {
132 			HttpServletRequest req = (HttpServletRequest) request;
133 			HttpServletResponse rsp = (HttpServletResponse) response;
134 			ReceivePack rp;
135 			try {
136 				rp = receivePackFactory.create(req, getRepository(req));
137 			} catch (ServiceNotAuthorizedException e) {
138 				rsp.sendError(SC_UNAUTHORIZED, e.getMessage());
139 				return;
140 			} catch (ServiceNotEnabledException e) {
141 				sendError(req, rsp, SC_FORBIDDEN, e.getMessage());
142 				return;
143 			}
144 
145 			try {
146 				req.setAttribute(ATTRIBUTE_HANDLER, rp);
147 				chain.doFilter(req, rsp);
148 			} finally {
149 				req.removeAttribute(ATTRIBUTE_HANDLER);
150 			}
151 		}
152 
153 		@Override
154 		public void init(FilterConfig filterConfig) throws ServletException {
155 			// Nothing.
156 		}
157 
158 		@Override
159 		public void destroy() {
160 			// Nothing.
161 		}
162 	}
163 
164 	/** {@inheritDoc} */
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 		SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
174 			@Override
175 			public void flush() throws IOException {
176 				doFlush();
177 			}
178 		};
179 
180 		ReceivePack rp = (ReceivePack) req.getAttribute(ATTRIBUTE_HANDLER);
181 		try {
182 			rp.setBiDirectionalPipe(false);
183 			rsp.setContentType(RECEIVE_PACK_RESULT_TYPE);
184 
185 			rp.receive(getInputStream(req), out, null);
186 			out.close();
187 		} catch (CorruptObjectException e ) {
188 			// This should be already reported to the client.
189 			getServletContext().log(MessageFormat.format(
190 					HttpServerText.get().receivedCorruptObject,
191 					e.getMessage(),
192 					ServletUtils.identify(rp.getRepository())));
193 			consumeRequestBody(req);
194 			out.close();
195 
196 		} catch (UnpackException | PackProtocolException e) {
197 			// This should be already reported to the client.
198 			log(rp.getRepository(), e.getCause());
199 			consumeRequestBody(req);
200 			out.close();
201 
202 		} catch (Throwable e) {
203 			log(rp.getRepository(), e);
204 			if (!rsp.isCommitted()) {
205 				rsp.reset();
206 				sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
207 			}
208 			return;
209 		}
210 	}
211 
212 	private void log(Repository git, Throwable e) {
213 		getServletContext().log(MessageFormat.format(
214 				HttpServerText.get().internalErrorDuringReceivePack,
215 				ServletUtils.identify(git)), e);
216 	}
217 }