3

I'm writing a Spring controller that needs to accept the Range header, use the values there to request some data using a 3rd-party library, then return the result.

The Spring HttpRange class looks promising, specifically the parseRanges function, which says:

Parse the given, comma-separated string into a list of HttpRange objects.

This method can be used to parse an [sic] Range header.

However, this class doesn't provide any interface to extract the parsed values without knowing the "the total length of a representation".

Given I am handling a request which I'll forward to another API, I don't know the total length.

Is there a standard Spring way to inspect the Range header and extract the values without knowing the length? I want to avoid writing a custom parser if possible.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
  • Are you able to pass the header as-is? It *sounds* like that may be enough. Otherwise: "inspect the Range header and extract the values" -- what are you looking for here in the case of open-ended ranges? How are you looking to transform them? – Joe Nov 30 '20 at 11:57

1 Answers1

0

You can try this:

HttpHeaders rangeHeaders = new ServletServerHttpRequest(httpRequest).getHeaders();
List<HttpRange> ranges = rangeHeaders.getRange();

Refer: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html#getRange()

mcacorner
  • 1,304
  • 3
  • 22
  • 45