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.parseVersion;
53  import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK;
54  import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_REQUEST_TYPE;
55  import static org.eclipse.jgit.http.server.GitSmartHttpTools.UPLOAD_PACK_RESULT_TYPE;
56  import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
57  import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER;
58  import static org.eclipse.jgit.http.server.ServletUtils.consumeRequestBody;
59  import static org.eclipse.jgit.http.server.ServletUtils.getInputStream;
60  import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
61  import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
62  
63  import java.io.IOException;
64  import java.text.MessageFormat;
65  import java.util.List;
66  
67  import javax.servlet.Filter;
68  import javax.servlet.FilterChain;
69  import javax.servlet.FilterConfig;
70  import javax.servlet.ServletException;
71  import javax.servlet.ServletRequest;
72  import javax.servlet.ServletResponse;
73  import javax.servlet.http.HttpServlet;
74  import javax.servlet.http.HttpServletRequest;
75  import javax.servlet.http.HttpServletResponse;
76  
77  import org.eclipse.jgit.lib.Repository;
78  import org.eclipse.jgit.transport.InternalHttpServerGlue;
79  import org.eclipse.jgit.transport.PacketLineOut;
80  import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
81  import org.eclipse.jgit.transport.ServiceMayNotContinueException;
82  import org.eclipse.jgit.transport.UploadPack;
83  import org.eclipse.jgit.transport.UploadPackInternalServerErrorException;
84  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
85  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
86  import org.eclipse.jgit.transport.resolver.UploadPackFactory;
87  
88  /** Server side implementation of smart fetch over HTTP. */
89  class UploadPackServlet extends HttpServlet {
90  	private static final long serialVersionUID = 1L;
91  
92  	static class InfoRefs extends SmartServiceInfoRefs {
93  		private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
94  
95  		InfoRefs(UploadPackFactory<HttpServletRequest> uploadPackFactory,
96  				List<Filter> filters) {
97  			super(UPLOAD_PACK, filters);
98  			this.uploadPackFactory = uploadPackFactory;
99  		}
100 
101 		@Override
102 		protected void begin(HttpServletRequest req, Repository db)
103 				throws IOException, ServiceNotEnabledException,
104 				ServiceNotAuthorizedException {
105 			UploadPack up = uploadPackFactory.create(req, db);
106 			InternalHttpServerGlue.setPeerUserAgent(
107 					up,
108 					req.getHeader(HDR_USER_AGENT));
109 			req.setAttribute(ATTRIBUTE_HANDLER, up);
110 		}
111 
112 		@Override
113 		protected void advertise(HttpServletRequest req,
114 				PacketLineOutRefAdvertiser pck) throws IOException,
115 				ServiceNotEnabledException, ServiceNotAuthorizedException {
116 			UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
117 			try {
118 				up.setBiDirectionalPipe(false);
119 				up.sendAdvertisedRefs(pck);
120 			} finally {
121 				// TODO(jonathantanmy): Move responsibility for closing the
122 				// RevWalk to UploadPack, either by making it AutoCloseable
123 				// or by making sendAdvertisedRefs clean up after itself.
124 				up.getRevWalk().close();
125 			}
126 		}
127 
128 		@Override
129 		protected void respond(HttpServletRequest req,
130 				PacketLineOut pckOut, String serviceName) throws IOException,
131 				ServiceNotEnabledException, ServiceNotAuthorizedException {
132 			UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
133 			try {
134 				up.setBiDirectionalPipe(false);
135 				up.sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut), serviceName);
136 			} finally {
137 				// TODO(jonathantanmy): Move responsibility for closing the
138 				// RevWalk to UploadPack, either by making it AutoCloseable
139 				// or by making sendAdvertisedRefs clean up after itself.
140 				up.getRevWalk().close();
141 			}
142 		}
143 	}
144 
145 	static class Factory implements Filter {
146 		private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
147 
148 		Factory(UploadPackFactory<HttpServletRequest> uploadPackFactory) {
149 			this.uploadPackFactory = uploadPackFactory;
150 		}
151 
152 		@Override
153 		public void doFilter(ServletRequest request, ServletResponse response,
154 				FilterChain chain) throws IOException, ServletException {
155 			HttpServletRequest req = (HttpServletRequest) request;
156 			HttpServletResponse rsp = (HttpServletResponse) response;
157 			UploadPack rp;
158 			try {
159 				rp = uploadPackFactory.create(req, getRepository(req));
160 			} catch (ServiceNotAuthorizedException e) {
161 				rsp.sendError(SC_UNAUTHORIZED, e.getMessage());
162 				return;
163 			} catch (ServiceNotEnabledException e) {
164 				sendError(req, rsp, SC_FORBIDDEN, e.getMessage());
165 				return;
166 			}
167 
168 			try {
169 				req.setAttribute(ATTRIBUTE_HANDLER, rp);
170 				chain.doFilter(req, rsp);
171 			} finally {
172 				req.removeAttribute(ATTRIBUTE_HANDLER);
173 			}
174 		}
175 
176 		@Override
177 		public void init(FilterConfig filterConfig) throws ServletException {
178 			// Nothing.
179 		}
180 
181 		@Override
182 		public void destroy() {
183 			// Nothing.
184 		}
185 	}
186 
187 	/** {@inheritDoc} */
188 	@Override
189 	public void doPost(final HttpServletRequest req,
190 			final HttpServletResponse rsp) throws IOException {
191 		if (!UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType())) {
192 			rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE);
193 			return;
194 		}
195 
196 		int[] version = parseVersion(req.getHeader(HDR_USER_AGENT));
197 		if (hasChunkedEncodingRequestBug(version, req)) {
198 			GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n"
199 					+ HttpServerText.get().clientHas175ChunkedEncodingBug);
200 			return;
201 		}
202 
203 		SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
204 			@Override
205 			public void flush() throws IOException {
206 				doFlush();
207 			}
208 		};
209 
210 		UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
211 		try {
212 			up.setBiDirectionalPipe(false);
213 			rsp.setContentType(UPLOAD_PACK_RESULT_TYPE);
214 
215 			up.upload(getInputStream(req), out, null);
216 			out.close();
217 
218 		} catch (ServiceMayNotContinueException e) {
219 			if (e.isOutput()) {
220 				consumeRequestBody(req);
221 				out.close();
222 			} else if (!rsp.isCommitted()) {
223 				rsp.reset();
224 				sendError(req, rsp, e.getStatusCode(), e.getMessage());
225 			}
226 			return;
227 
228 		} catch (UploadPackInternalServerErrorException e) {
229 			// Special case exception, error message was sent to client.
230 			log(up.getRepository(), e.getCause());
231 			consumeRequestBody(req);
232 			out.close();
233 
234 		} catch (Throwable e) {
235 			log(up.getRepository(), e);
236 			if (!rsp.isCommitted()) {
237 				rsp.reset();
238 				sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
239 			}
240 			return;
241 		}
242 	}
243 
244 	private void log(Repository git, Throwable e) {
245 		getServletContext().log(MessageFormat.format(
246 				HttpServerText.get().internalErrorDuringUploadPack,
247 				ServletUtils.identify(git)), e);
248 	}
249 }