forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteRange.java
More file actions
180 lines (166 loc) · 4.84 KB
/
ByteRange.java
File metadata and controls
180 lines (166 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import io.jooby.internal.NoByteRange;
import io.jooby.internal.NotSatisfiableByteRange;
import io.jooby.internal.SingleByteRange;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
/**
* Utility class to compute single byte range requests when response content length is known.
* Jooby support single byte range requests on file responses, like: assets, input stream, files,
* etc.
*
* Single byte range request looks like: <code>bytes=0-100</code>, <code>bytes=100-</code>,
* <code>bytes=-100</code>.
*
* Multiple byte range request are not supported.
*
* @since 2.0.0
* @author edgar
*/
public interface ByteRange {
/**
* Byte range prefix.
*/
String BYTES_RANGE = "bytes=";
/**
* Parse a byte range request value. Example of valid values:
*
* - bytes=0-100
* - bytes=-100
* - bytes=100-
*
* Any non-matching values produces a not satisfiable response.
*
* If value is null or content length less or equal to <code>0</code>, produces an empty/NOOP
* response.
*
* @param value Valid byte range request value.
* @param contentLength Content length.
* @return Byte range instance.
*/
static @Nonnull ByteRange parse(@Nullable String value, long contentLength) {
if (contentLength <= 0 || value == null) {
// NOOP
return new NoByteRange(contentLength);
}
if (!value.startsWith(SingleByteRange.BYTES_RANGE)) {
return new NotSatisfiableByteRange(value, contentLength);
}
try {
long[] range = {-1, -1};
int r = 0;
int len = value.length();
int i = SingleByteRange.BYTES_RANGE.length();
int offset = i;
char ch;
// Only Single Byte Range Requests:
while (i < len && (ch = value.charAt(i)) != ',') {
if (ch == '-') {
if (offset < i) {
range[r] = Long.parseLong(value.substring(offset, i).trim());
}
offset = i + 1;
r += 1;
}
i += 1;
}
if (offset < i) {
if (r == 0) {
return new NotSatisfiableByteRange(value, contentLength);
}
range[r++] = Long.parseLong(value.substring(offset, i).trim());
}
if (r == 0 || (range[0] == -1 && range[1] == -1)) {
return new NotSatisfiableByteRange(value, contentLength);
}
long start = range[0];
long end = range[1];
if (start == -1) {
start = contentLength - end;
end = contentLength - 1;
}
if (end == -1 || end > contentLength - 1) {
end = contentLength - 1;
}
if (start > end) {
return new NotSatisfiableByteRange(value, contentLength);
}
// offset
long limit = (end - start + 1);
return new SingleByteRange(value, start, limit, limit,
"bytes " + start + "-" + end + "/" + contentLength);
} catch (NumberFormatException expected) {
return new NotSatisfiableByteRange(value, contentLength);
}
}
/**
* Start range or <code>-1</code>.
*
* @return Start range or <code>-1</code>.
*/
long getStart();
/**
* End range or <code>-1</code>.
*
* @return End range or <code>-1</code>.
*/
long getEnd();
/**
* New content length.
*
* @return New content length.
*/
long getContentLength();
/**
* Value for <code>Content-Range</code> response header.
*
* @return Value for <code>Content-Range</code> response header.
*/
@Nonnull String getContentRange();
/**
* For partial requests this method returns {@link StatusCode#PARTIAL_CONTENT}.
*
* For not satisfiable requests this returns {@link StatusCode#REQUESTED_RANGE_NOT_SATISFIABLE}..
*
* Otherwise just returns {@link StatusCode#OK}.
*
* @return Status code.
*/
@Nonnull StatusCode getStatusCode();
/**
* For partial request this method set the following byte range response headers:
*
* - Accept-Ranges
* - Content-Range
* - Content-Length
*
* For not satisfiable requests:
*
* - Throws a {@link StatusCode#REQUESTED_RANGE_NOT_SATISFIABLE}
*
* Otherwise this method does nothing.
*
* @param ctx Web context.
* @return This byte range request.
*/
@Nonnull ByteRange apply(@Nonnull Context ctx);
/**
* For partial requests this method generates a new truncated input stream.
*
* For not satisfiable requests this method throws an exception.
*
* If there is no range to apply this method returns the given input stream.
*
* @param input Input stream.
* @return A truncated input stream for partial request or same input stream.
* @throws IOException When truncation fails.
*/
@Nonnull InputStream apply(@Nonnull InputStream input) throws IOException;
}