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