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