001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http;
014
015import org.apache.juneau.http.annotation.*;
016
017/**
018 * Represents a parsed <l>Range</l> HTTP request header.
019 *
020 * <p>
021 * Request only part of an entity. Bytes are numbered from 0.
022 *
023 * <h5 class='figure'>Example</h5>
024 * <p class='bcode w800'>
025 *    Range: bytes=500-999
026 * </p>
027 *
028 * <h5 class='topic'>RFC2616 Specification</h5>
029 *
030 * Since all HTTP entities are represented in HTTP messages as sequences of bytes, the concept of a byte range is
031 * meaningful for any HTTP entity.
032 * (However, not all clients and servers need to support byte- range operations.)
033 *
034 * <p>
035 * Byte range specifications in HTTP apply to the sequence of bytes in the entity-body (not necessarily the same as the
036 * message-body).
037 *
038 * <p>
039 * A byte range operation MAY specify a single range of bytes, or a set of ranges within a single entity.
040 * <p class='bcode w800'>
041 *    ranges-specifier = byte-ranges-specifier
042 *    byte-ranges-specifier = bytes-unit "=" byte-range-set
043 *    byte-range-set  = 1#( byte-range-spec | suffix-byte-range-spec )
044 *    byte-range-spec = first-byte-pos "-" [last-byte-pos]
045 *    first-byte-pos  = 1*DIGIT
046 *    last-byte-pos   = 1*DIGIT
047 * </p>
048 *
049 * <p>
050 * The first-byte-pos value in a byte-range-spec gives the byte-offset of the first byte in a range.
051 * The last-byte-pos value gives the byte-offset of the last byte in the range; that is, the byte positions specified
052 * are inclusive.
053 * Byte offsets start at zero.
054 *
055 * <p>
056 * If the last-byte-pos value is present, it MUST be greater than or equal to the first-byte-pos in that
057 * byte-range-spec, or the byte- range-spec is syntactically invalid.
058 * The recipient of a byte-range- set that includes one or more syntactically invalid byte-range-spec values MUST
059 * ignore the header field that includes that byte-range-set.
060 *
061 * <p>
062 * If the last-byte-pos value is absent, or if the value is greater than or equal to the current length of the
063 * entity-body, last-byte-pos is taken to be equal to one less than the current length of the entity-body in bytes.
064 *
065 * <p>
066 * By its choice of last-byte-pos, a client can limit the number of bytes retrieved without knowing the size of the
067 * entity.
068 * <p class='bcode w800'>
069 *    suffix-byte-range-spec = "-" suffix-length
070 *    suffix-length = 1*DIGIT
071 * </p>
072 *
073 * <p>
074 * A suffix-byte-range-spec is used to specify the suffix of the entity-body, of a length given by the suffix-length
075 * value.
076 * (That is, this form specifies the last N bytes of an entity-body.)
077 * If the entity is shorter than the specified suffix-length, the entire entity-body is used.
078 *
079 * <p>
080 * If a syntactically valid byte-range-set includes at least one byte- range-spec whose first-byte-pos is less than the
081 * current length of the entity-body, or at least one suffix-byte-range-spec with a non-zero suffix-length, then the
082 * byte-range-set is satisfiable.
083 * Otherwise, the byte-range-set is unsatisfiable.
084 * If the byte-range-set is unsatisfiable, the server SHOULD return a response with a status of 416 (Requested range
085 * not satisfiable).
086 * Otherwise, the server SHOULD return a response with a status of 206 (Partial Content) containing the satisfiable
087 * ranges of the entity-body.
088 *
089 * <p>
090 * Examples of byte-ranges-specifier values (assuming an entity-body of length 10000):
091 * <p class='bcode w800'>
092 *    - The first 500 bytes (byte offsets 0-499, inclusive):  bytes=0-499
093 *    - The second 500 bytes (byte offsets 500-999, inclusive):  bytes=500-999
094 *    - The final 500 bytes (byte offsets 9500-9999, inclusive):  bytes=-500
095 *    - Or bytes=9500-
096 *    - The first and last bytes only (bytes 0 and 9999):  bytes=0-0,-1
097 *    - Several legal but not canonical specifications of the second 500 bytes (byte offsets 500-999, inclusive):
098 *       bytes=500-600,601-999
099 *       bytes=500-700,601-999
100 * </p>
101 *
102 * <p>
103 * HTTP retrieval requests using conditional or unconditional GET methods MAY request one or more sub-ranges of the
104 * entity, instead of the entire entity, using the Range request header, which applies to the entity returned as the
105 * result of the request:
106 *
107 * <p class='bcode w800'>
108 *    Range = "Range" ":" ranges-specifier
109 * </p>
110 *
111 * <p>
112 * A server MAY ignore the Range header.
113 * However, HTTP/1.1 origin servers and intermediate caches ought to support byte ranges when possible, since Range
114 * supports efficient recovery from partially failed transfers, and supports efficient partial retrieval of large
115 * entities.
116 *
117 * <p>
118 * If the server supports the Range header and the specified range or ranges are appropriate for the entity:
119 * <ul>
120 *    <li>The presence of a Range header in an unconditional GET modifies what is returned if the GET is otherwise
121 *       successful.
122 *       In other words, the response carries a status code of 206 (Partial Content) instead of 200 (OK).
123 *    <li>The presence of a Range header in a conditional GET (a request using one or both of If-Modified-Since and
124 *       If-None-Match, or one or both of If-Unmodified-Since and If-Match) modifies what is returned if the GET is
125 *       otherwise successful and the condition is true. It does not affect the 304 (Not Modified) response returned if
126 *       the conditional is false.
127 * </ul>
128 *
129 * <p>
130 * In some cases, it might be more appropriate to use the If-Range header (see section 14.27) in addition to the Range
131 * header.
132 *
133 * <p>
134 * If a proxy that supports ranges receives a Range request, forwards the request to an inbound server, and receives an
135 * entire entity in reply, it SHOULD only return the requested range to its client.
136 * It SHOULD store the entire received response in its cache if that is consistent with its cache allocation policies.
137 *
138 * <ul class='seealso'>
139 *    <li class='extlink'>{@doc RFC2616}
140 * </ul>
141 */
142@Header("Range")
143public final class Range extends HeaderString {
144
145   /**
146    * Returns a parsed <c>Range</c> header.
147    *
148    * @param value The <c>Range</c> header string.
149    * @return The parsed <c>Range</c> header, or <jk>null</jk> if the string was null.
150    */
151   public static Range forString(String value) {
152      if (value == null)
153         return null;
154      return new Range(value);
155   }
156
157   private Range(String value) {
158      super(value);
159   }
160}