Skip to content

Support byte ranges in ResourceHttpRequestHandler #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.util;

import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -25,6 +26,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.util.MimeType.SpecificityComparator;

Expand All @@ -37,6 +39,17 @@
*/
public abstract class MimeTypeUtils {

private static final byte[] BOUNDARY_CHARS =
new byte[] {'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'};

private static final Random RND = new Random();

private static Charset US_ASCII = Charset.forName("US-ASCII");


/**
* Public constant mime type that includes all media ranges (i.e. "*/*").
*/
Expand Down Expand Up @@ -319,6 +332,25 @@ public static void sortBySpecificity(List<MimeType> mimeTypes) {
}
}

/**
* Generate a random MIME boundary as bytes, often used in multipart mime types.
*/
public static byte[] generateMultipartBoundary() {
byte[] boundary = new byte[RND.nextInt(11) + 30];
for (int i = 0; i < boundary.length; i++) {
boundary[i] = BOUNDARY_CHARS[RND.nextInt(BOUNDARY_CHARS.length)];
}
return boundary;
}

/**
* Generate a random MIME boundary as String, often used in multipart mime types.
*/
public static String generateMultipartBoundaryString() {
return new String(generateMultipartBoundary(), US_ASCII);
}



/**
* Comparator used by {@link #sortBySpecificity(List)}.
Expand Down
21 changes: 19 additions & 2 deletions spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -744,6 +744,23 @@ public String getPragma() {
return getFirst(PRAGMA);
}

/**
* Sets the (new) value of the {@code Range} header.
*/
public void setRange(List<HttpRange> ranges) {
String value = HttpRange.toString(ranges);
set(RANGE, value);
}

/**
* Returns the value of the {@code Range} header.
* <p>Returns an empty list when the range is unknown.
*/
public List<HttpRange> getRange() {
String value = getFirst(RANGE);
return HttpRange.parseRanges(value);
}

/**
* Set the (new) value of the {@code Upgrade} header.
*/
Expand Down
Loading