Skip to content

Commit bf345d3

Browse files
committed
Support byte ranges in ResourceHttpRequestHandler
This commit introduces support for HTTP byte ranges in the ResourceHttpRequestHandler. This support consists of a number of changes: - Parsing of HTTP Range headers in HttpHeaders, using a new HttpRange class and inner ByteRange/SuffixByteRange subclasses. - MIME boundary generation moved from FormHttpMessageConverter to MimeTypeUtils. - writePartialContent() method introduced in ResourceHttpRequestHandler, handling the byte range logic - Additional partial content tests added to ResourceHttpRequestHandlerTests. Issue: SPR-10805
1 parent 83ff0ad commit bf345d3

File tree

8 files changed

+640
-35
lines changed

8 files changed

+640
-35
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.util;
1818

19+
import java.nio.charset.Charset;
1920
import java.nio.charset.UnsupportedCharsetException;
2021
import java.util.ArrayList;
2122
import java.util.Collection;
@@ -25,6 +26,7 @@
2526
import java.util.LinkedHashMap;
2627
import java.util.List;
2728
import java.util.Map;
29+
import java.util.Random;
2830

2931
import org.springframework.util.MimeType.SpecificityComparator;
3032

@@ -37,6 +39,17 @@
3739
*/
3840
public abstract class MimeTypeUtils {
3941

42+
private static final byte[] BOUNDARY_CHARS =
43+
new byte[] {'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
44+
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
45+
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
46+
'V', 'W', 'X', 'Y', 'Z'};
47+
48+
private static final Random RND = new Random();
49+
50+
private static Charset US_ASCII = Charset.forName("US-ASCII");
51+
52+
4053
/**
4154
* Public constant mime type that includes all media ranges (i.e. "*/*").
4255
*/
@@ -319,6 +332,25 @@ public static void sortBySpecificity(List<MimeType> mimeTypes) {
319332
}
320333
}
321334

335+
/**
336+
* Generate a random MIME boundary as bytes, often used in multipart mime types.
337+
*/
338+
public static byte[] generateMultipartBoundary() {
339+
byte[] boundary = new byte[RND.nextInt(11) + 30];
340+
for (int i = 0; i < boundary.length; i++) {
341+
boundary[i] = BOUNDARY_CHARS[RND.nextInt(BOUNDARY_CHARS.length)];
342+
}
343+
return boundary;
344+
}
345+
346+
/**
347+
* Generate a random MIME boundary as String, often used in multipart mime types.
348+
*/
349+
public static String generateMultipartBoundaryString() {
350+
return new String(generateMultipartBoundary(), US_ASCII);
351+
}
352+
353+
322354

323355
/**
324356
* Comparator used by {@link #sortBySpecificity(List)}.

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -744,6 +744,23 @@ public String getPragma() {
744744
return getFirst(PRAGMA);
745745
}
746746

747+
/**
748+
* Sets the (new) value of the {@code Range} header.
749+
*/
750+
public void setRange(List<HttpRange> ranges) {
751+
String value = HttpRange.toString(ranges);
752+
set(RANGE, value);
753+
}
754+
755+
/**
756+
* Returns the value of the {@code Range} header.
757+
* <p>Returns an empty list when the range is unknown.
758+
*/
759+
public List<HttpRange> getRange() {
760+
String value = getFirst(RANGE);
761+
return HttpRange.parseRanges(value);
762+
}
763+
747764
/**
748765
* Set the (new) value of the {@code Upgrade} header.
749766
*/

0 commit comments

Comments
 (0)