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_PARTIAL_CONTENT;
47  import static javax.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE;
48  import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT_RANGES;
49  import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_LENGTH;
50  import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_RANGE;
51  import static org.eclipse.jgit.util.HttpSupport.HDR_IF_RANGE;
52  import static org.eclipse.jgit.util.HttpSupport.HDR_RANGE;
53  
54  import java.io.EOFException;
55  import java.io.File;
56  import java.io.FileNotFoundException;
57  import java.io.IOException;
58  import java.io.OutputStream;
59  import java.io.RandomAccessFile;
60  import java.text.MessageFormat;
61  import java.util.Enumeration;
62  
63  import javax.servlet.http.HttpServletRequest;
64  import javax.servlet.http.HttpServletResponse;
65  
66  import org.eclipse.jgit.lib.ObjectId;
67  
68  /**
69   * Dumps a file over HTTP GET (or its information via HEAD).
70   * <p>
71   * Supports a single byte range requested via {@code Range} HTTP header. This
72   * feature supports a dumb client to resume download of a larger object file.
73   */
74  final class FileSender {
75  	private final File path;
76  
77  	private final RandomAccessFile source;
78  
79  	private final long lastModified;
80  
81  	private final long fileLen;
82  
83  	private long pos;
84  
85  	private long end;
86  
87  	FileSender(final File path) throws FileNotFoundException {
88  		this.path = path;
89  		this.source = new RandomAccessFile(path, "r");
90  
91  		try {
92  			this.lastModified = path.lastModified();
93  			this.fileLen = source.getChannel().size();
94  			this.end = fileLen;
95  		} catch (IOException e) {
96  			try {
97  				source.close();
98  			} catch (IOException closeError) {
99  				// Ignore any error closing the stream.
100 			}
101 
102 			final FileNotFoundException r;
103 			r = new FileNotFoundException(MessageFormat.format(HttpServerText.get().cannotGetLengthOf, path));
104 			r.initCause(e);
105 			throw r;
106 		}
107 	}
108 
109 	void close() {
110 		try {
111 			source.close();
112 		} catch (IOException e) {
113 			// Ignore close errors on a read-only stream.
114 		}
115 	}
116 
117 	long getLastModified() {
118 		return lastModified;
119 	}
120 
121 	String getTailChecksum() throws IOException {
122 		final int n = 20;
123 		final byte[] buf = new byte[n];
124 		source.seek(fileLen - n);
125 		source.readFully(buf, 0, n);
126 		return ObjectId.fromRaw(buf).getName();
127 	}
128 
129 	void serve(final HttpServletRequest req, final HttpServletResponse rsp,
130 			final boolean sendBody) throws IOException {
131 		if (!initRangeRequest(req, rsp)) {
132 			rsp.sendError(SC_REQUESTED_RANGE_NOT_SATISFIABLE);
133 			return;
134 		}
135 
136 		rsp.setHeader(HDR_ACCEPT_RANGES, "bytes");
137 		rsp.setHeader(HDR_CONTENT_LENGTH, Long.toString(end - pos));
138 
139 		if (sendBody) {
140 			final OutputStream out = rsp.getOutputStream();
141 			try {
142 				final byte[] buf = new byte[4096];
143 				source.seek(pos);
144 				while (pos < end) {
145 					final int r = (int) Math.min(buf.length, end - pos);
146 					final int n = source.read(buf, 0, r);
147 					if (n < 0) {
148 						throw new EOFException(MessageFormat.format(HttpServerText.get().unexpectedeOFOn, path));
149 					}
150 					out.write(buf, 0, n);
151 					pos += n;
152 				}
153 				out.flush();
154 			} finally {
155 				out.close();
156 			}
157 		}
158 	}
159 
160 	private boolean initRangeRequest(final HttpServletRequest req,
161 			final HttpServletResponse rsp) throws IOException {
162 		final Enumeration<String> rangeHeaders = getRange(req);
163 		if (!rangeHeaders.hasMoreElements()) {
164 			// No range headers, the request is fine.
165 			return true;
166 		}
167 
168 		final String range = rangeHeaders.nextElement();
169 		if (rangeHeaders.hasMoreElements()) {
170 			// To simplify the code we support only one range.
171 			return false;
172 		}
173 
174 		final int eq = range.indexOf('=');
175 		final int dash = range.indexOf('-');
176 		if (eq < 0 || dash < 0 || !range.startsWith("bytes=")) {
177 			return false;
178 		}
179 
180 		final String ifRange = req.getHeader(HDR_IF_RANGE);
181 		if (ifRange != null && !getTailChecksum().equals(ifRange)) {
182 			// If the client asked us to verify the ETag and its not
183 			// what they expected we need to send the entire content.
184 			return true;
185 		}
186 
187 		try {
188 			if (eq + 1 == dash) {
189 				// "bytes=-500" means last 500 bytes
190 				pos = Long.parseLong(range.substring(dash + 1));
191 				pos = fileLen - pos;
192 			} else {
193 				// "bytes=500-" (position 500 to end)
194 				// "bytes=500-1000" (position 500 to 1000)
195 				pos = Long.parseLong(range.substring(eq + 1, dash));
196 				if (dash < range.length() - 1) {
197 					end = Long.parseLong(range.substring(dash + 1));
198 					end++; // range was inclusive, want exclusive
199 				}
200 			}
201 		} catch (NumberFormatException e) {
202 			// We probably hit here because of a non-digit such as
203 			// "," appearing at the end of the first range telling
204 			// us there is a second range following. To simplify
205 			// the code we support only one range.
206 			return false;
207 		}
208 
209 		if (end > fileLen) {
210 			end = fileLen;
211 		}
212 		if (pos >= end) {
213 			return false;
214 		}
215 
216 		rsp.setStatus(SC_PARTIAL_CONTENT);
217 		rsp.setHeader(HDR_CONTENT_RANGE, "bytes " + pos + "-" + (end - 1) + "/"
218 				+ fileLen);
219 		source.seek(pos);
220 		return true;
221 	}
222 
223 	private static Enumeration<String> getRange(final HttpServletRequest req) {
224 		return req.getHeaders(HDR_RANGE);
225 	}
226 }