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.RefAdvertiser.PacketLineOutRefAdvertiser;
80  import org.eclipse.jgit.transport.ServiceMayNotContinueException;
81  import org.eclipse.jgit.transport.UploadPack;
82  import org.eclipse.jgit.transport.UploadPackInternalServerErrorException;
83  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
84  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
85  import org.eclipse.jgit.transport.resolver.UploadPackFactory;
86  
87  /** Server side implementation of smart fetch over HTTP. */
88  class UploadPackServlet extends HttpServlet {
89  	private static final long serialVersionUID = 1L;
90  
91  	static class InfoRefs extends SmartServiceInfoRefs {
92  		private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
93  
94  		InfoRefs(UploadPackFactory<HttpServletRequest> uploadPackFactory,
95  				List<Filter> filters) {
96  			super(UPLOAD_PACK, filters);
97  			this.uploadPackFactory = uploadPackFactory;
98  		}
99  
100 		@Override
101 		protected void begin(HttpServletRequest req, Repository db)
102 				throws IOException, ServiceNotEnabledException,
103 				ServiceNotAuthorizedException {
104 			UploadPack up = uploadPackFactory.create(req, db);
105 			InternalHttpServerGlue.setPeerUserAgent(
106 					up,
107 					req.getHeader(HDR_USER_AGENT));
108 			req.setAttribute(ATTRIBUTE_HANDLER, up);
109 		}
110 
111 		@Override
112 		protected void advertise(HttpServletRequest req,
113 				PacketLineOutRefAdvertiser pck) throws IOException,
114 				ServiceNotEnabledException, ServiceNotAuthorizedException {
115 			UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
116 			try {
117 				up.setBiDirectionalPipe(false);
118 				up.sendAdvertisedRefs(pck);
119 			} finally {
120 				up.getRevWalk().close();
121 			}
122 		}
123 	}
124 
125 	static class Factory implements Filter {
126 		private final UploadPackFactory<HttpServletRequest> uploadPackFactory;
127 
128 		Factory(UploadPackFactory<HttpServletRequest> uploadPackFactory) {
129 			this.uploadPackFactory = uploadPackFactory;
130 		}
131 
132 		@Override
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 			UploadPack rp;
138 			try {
139 				rp = uploadPackFactory.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 		@Override
157 		public void init(FilterConfig filterConfig) throws ServletException {
158 			// Nothing.
159 		}
160 
161 		@Override
162 		public void destroy() {
163 			// Nothing.
164 		}
165 	}
166 
167 	@Override
168 	public void doPost(final HttpServletRequest req,
169 			final HttpServletResponse rsp) throws IOException {
170 		if (!UPLOAD_PACK_REQUEST_TYPE.equals(req.getContentType())) {
171 			rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE);
172 			return;
173 		}
174 
175 		int[] version = parseVersion(req.getHeader(HDR_USER_AGENT));
176 		if (hasChunkedEncodingRequestBug(version, req)) {
177 			GitSmartHttpTools.sendError(req, rsp, SC_BAD_REQUEST, "\n\n"
178 					+ HttpServerText.get().clientHas175ChunkedEncodingBug);
179 			return;
180 		}
181 
182 		SmartOutputStream out = new SmartOutputStream(req, rsp, false) {
183 			@Override
184 			public void flush() throws IOException {
185 				doFlush();
186 			}
187 		};
188 
189 		UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER);
190 		try {
191 			up.setBiDirectionalPipe(false);
192 			rsp.setContentType(UPLOAD_PACK_RESULT_TYPE);
193 
194 			up.upload(getInputStream(req), out, null);
195 			out.close();
196 
197 		} catch (ServiceMayNotContinueException e) {
198 			if (e.isOutput()) {
199 				consumeRequestBody(req);
200 				out.close();
201 			} else if (!rsp.isCommitted()) {
202 				rsp.reset();
203 				sendError(req, rsp, e.getStatusCode(), e.getMessage());
204 			}
205 			return;
206 
207 		} catch (UploadPackInternalServerErrorException e) {
208 			// Special case exception, error message was sent to client.
209 			log(up.getRepository(), e.getCause());
210 			consumeRequestBody(req);
211 			out.close();
212 
213 		} catch (Throwable e) {
214 			log(up.getRepository(), e);
215 			if (!rsp.isCommitted()) {
216 				rsp.reset();
217 				sendError(req, rsp, SC_INTERNAL_SERVER_ERROR);
218 			}
219 			return;
220 		}
221 	}
222 
223 	private void log(Repository git, Throwable e) {
224 		getServletContext().log(MessageFormat.format(
225 				HttpServerText.get().internalErrorDuringUploadPack,
226 				ServletUtils.identify(git)), e);
227 	}
228 }