2015-11-19 14 views
5

Spiacente se questo è il posto sbagliato per questo.Eccezione lancio lancio a molla su valore If-Modified-Since non valido

Secondo le specifiche HTTP come definito: http://tools.ietf.org/html/rfc7232#section-3.3

Il destinatario deve ignorare il If-Modified-Since campo di intestazione se il ricevuta campo-valore non è un HTTP-data valida, o se la richiesta il metodo non è né GET né HEAD.

Spring Boot non lo fa. Sta lanciando un IllegalArgumentException che non viene gestito dal codice che controlla il valore dell'intestazione.

Ecco il codice di conversione 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"); 
} 

Quindi, se si invia l'intestazione If-Modified-Since: 0 si otterrà un'eccezione, invece di restituire il fresco OTTIENI la risposta come definito nella specifica http.

Qualcun altro lo vede come un problema?

+0

La vera domanda è cosa fa un chiamante di questo metodo. Il metodo stesso non può violare le specifiche. – zeroflagL

+0

Mentre hai ragione, perché qualcuno dovrebbe inviare un'intestazione If-Modified-Since non valida? Preferirei risolvere il bug nel client, temo che il springframework non risolvesse immediatamente questo problema. – burna

+0

@zeroflagL - il chiamante non lo gestisce. L'eccezione bolle in alto. – micko

risposta

3

Ho visto questo e recentemente created a ticket e submitted a PR per risolvere il problema Nel frattempo, potresti riuscire a risolvere il problema utilizzando un filtro per rimuovere l'intestazione, ad es.

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

incontrato lo stesso problema, utilizzando Spring - 4.2.5. aggiornato a 4.2.6 -> problema risolto.

Problemi correlati