Spring Invalid load throw exception If-Modified-Since value

Sorry if this is not the case.

As defined by http: http://tools.ietf.org/html/rfc7232#section-3.3

The receiver MUST ignore the If-Modified-Since header field if the received field value is not a valid HTTP date, or if the request method is neither GET nor HEAD.

Spring Boot does not do this. It throws an IllegalArgumentException, which is not handled by code that checks the value of the header.

Here is the conversion code in org.springframework.http.HttpHeaders.java

/** * Return the value of the {@code If-Modified-Since} header. * <p>The date is returned as the number of milliseconds since * January 1, 1970 GMT. Returns -1 when the date is unknown. */ public long getIfModifiedSince() { return getFirstDate(IF_MODIFIED_SINCE); } /** * Parse the first header value for the given header name as a date, * return -1 if there is no value, or raise {@link IllegalArgumentException} * if the value cannot be parsed as a date. */ public long getFirstDate(String headerName) { String headerValue = getFirst(headerName); if (headerValue == null) { return -1; } for (String dateFormat : DATE_FORMATS) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US); simpleDateFormat.setTimeZone(GMT); try { return simpleDateFormat.parse(headerValue).getTime(); } catch (ParseException ex) { // ignore } } throw new IllegalArgumentException("Cannot parse date value \"" + headerValue + "\" for \"" + headerName + "\" header"); } 

So, if you send the If-Modified-Since: 0 header, you will get an exception instead of returning a new GET response as defined in the http specification.

Does anyone else see this as a problem?

+5
source share
2 answers

I saw this and recently created a ticket and sent a PR to fix it. In the meantime, you can work around the problem by using a filter to remove the header, for example

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper((HttpServletRequest) request) { @Override public Enumeration<String> getHeaderNames() { List<String> hdrs = Collections.list(super.getHeaderNames()) .stream() .filter(h -> !h.equals(IF_MODIFIED_SINCE)) .collect(Collectors.toList()); return Collections.enumeration(hdrs); } }; chain.doFilter(wrapper, response); } 
+3
source

ran into the same problem using Spring - 4.2.5 . updated to 4.2.6 โ†’ the problem is resolved.

+3
source

Source: https://habr.com/ru/post/1236335/


All Articles