View Javadoc

1   // ========================================================================
2   // Copyright (c) 2002-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server;
15  
16  import java.util.Enumeration;
17  import java.util.List;
18  import java.util.StringTokenizer;
19  
20  import org.eclipse.jetty.util.LazyList;
21  import org.eclipse.jetty.util.log.Log;
22  
23  /* ------------------------------------------------------------ */
24  /** Byte range inclusive of end points.
25   * <PRE>
26   * 
27   *   parses the following types of byte ranges:
28   * 
29   *       bytes=100-499
30   *       bytes=-300
31   *       bytes=100-
32   *       bytes=1-2,2-3,6-,-2
33   *
34   *   given an entity length, converts range to string
35   * 
36   *       bytes 100-499/500
37   * 
38   * </PRE>
39   * 
40   * Based on RFC2616 3.12, 14.16, 14.35.1, 14.35.2
41   * @version $version$
42   * 
43   */
44  public class InclusiveByteRange 
45  {
46      long first = 0;
47      long last  = 0;    
48  
49      public InclusiveByteRange(long first, long last)
50      {
51          this.first = first;
52          this.last = last;
53      }
54      
55      public long getFirst()
56      {
57          return first;
58      }
59  
60      public long getLast()
61      {
62          return last;
63      }    
64  
65  
66      
67      /* ------------------------------------------------------------ */
68      /** 
69       * @param headers Enumeration of Range header fields.
70       * @param size Size of the resource.
71       * @return LazyList of satisfiable ranges
72       */
73      public static List satisfiableRanges(Enumeration headers,long size)
74      {
75          Object satRanges=null;
76          
77          // walk through all Range headers
78      headers:
79          while (headers.hasMoreElements())
80          {
81              String header = (String) headers.nextElement();
82              StringTokenizer tok = new StringTokenizer(header,"=,",false);
83              String t=null;
84              try
85              {
86                  // read all byte ranges for this header 
87                  while (tok.hasMoreTokens())
88                  {
89                      t=tok.nextToken().trim();
90                      
91                      long first = -1;
92                      long last  = -1;
93                      int d=t.indexOf('-');
94                      if (d<0 || t.indexOf("-",d+1)>=0)
95                      {           
96                          if ("bytes".equals(t))
97                              continue;
98                          Log.warn("Bad range format: {}",t);
99                          continue headers;
100                     }
101                     else if (d==0)
102                     {
103                         if (d+1<t.length())
104                             last = Long.parseLong(t.substring(d+1).trim());
105                         else
106                         {
107                             Log.warn("Bad range format: {}",t);
108                             continue headers;
109                         }
110                     }
111                     else if (d+1<t.length())
112                     {
113                         first = Long.parseLong(t.substring(0,d).trim());
114                         last = Long.parseLong(t.substring(d+1).trim());
115                     }
116                     else
117                         first = Long.parseLong(t.substring(0,d).trim());
118 
119                     
120                     if (first == -1 && last == -1)
121                         continue headers;
122                     
123                     if (first != -1 && last != -1 && (first > last))
124                         continue headers;
125 
126                     if (first<size)
127                     {
128                         InclusiveByteRange range = new
129                             InclusiveByteRange(first, last);
130                         satRanges=LazyList.add(satRanges,range);
131                     }
132                 }
133             }
134             catch(Exception e)
135             {
136                 Log.warn("Bad range format: "+t);
137                 Log.ignore(e);
138             }    
139         }
140         return LazyList.getList(satRanges,true);
141     }
142 
143     /* ------------------------------------------------------------ */
144     public long getFirst(long size)
145     {
146         if (first<0)
147         {
148             long tf=size-last;
149             if (tf<0)
150                 tf=0;
151             return tf;
152         }
153         return first;
154     }
155     
156     /* ------------------------------------------------------------ */
157     public long getLast(long size)
158     {
159         if (first<0)
160             return size-1;
161         
162         if (last<0 ||last>=size)
163             return size-1;
164         return last;
165     }
166     
167     /* ------------------------------------------------------------ */
168     public long getSize(long size)
169     {
170         return getLast(size)-getFirst(size)+1;
171     }
172 
173 
174     /* ------------------------------------------------------------ */
175     public String toHeaderRangeString(long size)
176     {
177         StringBuilder sb = new StringBuilder(40);
178         sb.append("bytes ");
179         sb.append(getFirst(size));
180         sb.append('-');
181         sb.append(getLast(size));
182         sb.append("/");
183         sb.append(size);
184         return sb.toString();
185     }
186 
187     /* ------------------------------------------------------------ */
188     public static String to416HeaderRangeString(long size)
189     {
190         StringBuilder sb = new StringBuilder(40);
191         sb.append("bytes */");
192         sb.append(size);
193         return sb.toString();
194     }
195 
196 
197     /* ------------------------------------------------------------ */
198     public String toString()
199     {
200         StringBuilder sb = new StringBuilder(60);
201         sb.append(Long.toString(first));
202         sb.append(":");
203         sb.append(Long.toString(last));
204         return sb.toString();
205     }
206 
207 
208 }
209 
210 
211