2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.util.Enumeration;
22 import java.util.List;
23 import java.util.StringTokenizer;
25 import org.eclipse.jetty.util.LazyList;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
29 /* ------------------------------------------------------------ */
30 /** Byte range inclusive of end points.
33 * parses the following types of byte ranges:
40 * given an entity length, converts range to string
46 * Based on RFC2616 3.12, 14.16, 14.35.1, 14.35.2
48 * And yes the spec does strangely say that while 10-20, is bytes 10 to 20 and 10- is bytes 10 until the end that -20 IS NOT bytes 0-20, but the last 20 bytes of the content.
53 public class InclusiveByteRange
55 private static final Logger LOG = Log.getLogger(InclusiveByteRange.class);
60 public InclusiveByteRange(long first, long last)
66 public long getFirst()
78 /* ------------------------------------------------------------ */
80 * @param headers Enumeration of Range header fields.
81 * @param size Size of the resource.
82 * @return LazyList of satisfiable ranges
84 public static List<InclusiveByteRange> satisfiableRanges(Enumeration headers, long size)
86 Object satRanges=null;
88 // walk through all Range headers
90 while (headers.hasMoreElements())
92 String header = (String) headers.nextElement();
93 StringTokenizer tok = new StringTokenizer(header,"=,",false);
97 // read all byte ranges for this header
98 while (tok.hasMoreTokens())
102 t = tok.nextToken().trim();
106 int d = t.indexOf('-');
107 if (d < 0 || t.indexOf("-",d + 1) >= 0)
109 if ("bytes".equals(t))
111 LOG.warn("Bad range format: {}",t);
116 if (d + 1 < t.length())
117 last = Long.parseLong(t.substring(d + 1).trim());
120 LOG.warn("Bad range format: {}",t);
124 else if (d + 1 < t.length())
126 first = Long.parseLong(t.substring(0,d).trim());
127 last = Long.parseLong(t.substring(d + 1).trim());
130 first = Long.parseLong(t.substring(0,d).trim());
132 if (first == -1 && last == -1)
135 if (first != -1 && last != -1 && (first > last))
140 InclusiveByteRange range = new InclusiveByteRange(first,last);
141 satRanges = LazyList.add(satRanges,range);
144 catch (NumberFormatException e)
146 LOG.warn("Bad range format: {}",t);
154 LOG.warn("Bad range format: {}",t);
158 return LazyList.getList(satRanges,true);
161 /* ------------------------------------------------------------ */
162 public long getFirst(long size)
174 /* ------------------------------------------------------------ */
175 public long getLast(long size)
180 if (last<0 ||last>=size)
185 /* ------------------------------------------------------------ */
186 public long getSize(long size)
188 return getLast(size)-getFirst(size)+1;
192 /* ------------------------------------------------------------ */
193 public String toHeaderRangeString(long size)
195 StringBuilder sb = new StringBuilder(40);
197 sb.append(getFirst(size));
199 sb.append(getLast(size));
202 return sb.toString();
205 /* ------------------------------------------------------------ */
206 public static String to416HeaderRangeString(long size)
208 StringBuilder sb = new StringBuilder(40);
209 sb.append("bytes */");
211 return sb.toString();
215 /* ------------------------------------------------------------ */
217 public String toString()
219 StringBuilder sb = new StringBuilder(60);
220 sb.append(Long.toString(first));
222 sb.append(Long.toString(last));
223 return sb.toString();