HeaderUtils { public static void copyResponseTransportHeaders(Series<Header> headers, Response response) { if (headers != null) { for (Header header : headers) { if (HEADER_LOCATION.equalsIgnoreCase(header.getName())) { response.setLocationRef(header.getValue()); } else if (HEADER_AGE.equalsIgnoreCase(header.getName())) { try { response.setAge(Integer.parseInt(header.getValue())); } catch (NumberFormatException nfe) { Context.getCurrentLogger().log( Level.WARNING, "Error during Age header parsing. Header: " + header.getValue(), nfe); } } else if (HEADER_DATE.equalsIgnoreCase(header.getName())) { Date date = DateUtils.parse(header.getValue()); if (date == null) { date = new Date(); } response.setDate(date); } else if (HEADER_RETRY_AFTER.equalsIgnoreCase(header.getName())) { Date retryAfter = DateUtils.parse(header.getValue()); if (retryAfter == null) { try { int retryAfterSecs = (int) Double.parseDouble(header.getValue()); java.util.Calendar calendar = java.util.Calendar.getInstance(); calendar.add(java.util.Calendar.SECOND, retryAfterSecs); retryAfter = calendar.getTime(); } catch (NumberFormatException nfe) { Context.getCurrentLogger().log( Level.WARNING, "Error during Retry-After header parsing. Header: " + header.getValue(), nfe); } } response.setRetryAfter(retryAfter); } else if (HEADER_SET_COOKIE.equalsIgnoreCase(header.getName()) || HEADER_SET_COOKIE2.equalsIgnoreCase(header.getName())) { try { CookieSettingReader cr = new CookieSettingReader(header.getValue()); response.getCookieSettings().add(cr.readValue()); } catch (Exception e) { Context.getCurrentLogger().log( Level.WARNING, "Error during cookie setting parsing. Header: " + header.getValue(), e); } } else if (HEADER_WWW_AUTHENTICATE.equalsIgnoreCase(header.getName())) { List<ChallengeRequest> crs = org.restlet.engine.security.AuthenticatorUtils .parseRequest(response, header.getValue(), headers); response.getChallengeRequests().addAll(crs); } else if (HEADER_PROXY_AUTHENTICATE.equalsIgnoreCase(header.getName())) { List<ChallengeRequest> crs = org.restlet.engine.security.AuthenticatorUtils .parseRequest(response, header.getValue(), headers); response.getProxyChallengeRequests().addAll(crs); } else if (HEADER_AUTHENTICATION_INFO.equalsIgnoreCase(header.getName())) { AuthenticationInfo authenticationInfo = org.restlet.engine.security.AuthenticatorUtils .parseAuthenticationInfo(header.getValue()); response.setAuthenticationInfo(authenticationInfo); } else if (HEADER_SERVER.equalsIgnoreCase(header.getName())) { response.getServerInfo().setAgent(header.getValue()); } else if (HEADER_ALLOW.equalsIgnoreCase(header.getName())) { MethodReader.addValues(header, response.getAllowedMethods()); } else if (HEADER_VARY.equalsIgnoreCase(header.getName())) { DimensionReader.addValues(header, response.getDimensions()); } else if (HEADER_VIA.equalsIgnoreCase(header.getName())) { RecipientInfoReader.addValues(header, response.getRecipientsInfo()); } else if (HEADER_WARNING.equalsIgnoreCase(header.getName())) { WarningReader.addValues(header, response.getWarnings()); } else if (HEADER_CACHE_CONTROL.equalsIgnoreCase(header.getName())) { CacheDirectiveReader.addValues(header, response.getCacheDirectives()); } else if (HEADER_ACCEPT_RANGES.equalsIgnoreCase(header.getName())) { TokenReader tr = new TokenReader(header.getValue()); response.getServerInfo().setAcceptingRanges(tr.readValues().contains(RANGE_BYTES_UNIT)); } else if (HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS.equalsIgnoreCase(header.getName())) { response.setAccessControlAllowCredentials(parseBoolean(header.getValue())); StringReader.addValues(header, response.getAccessControlAllowHeaders()); } else if (HEADER_ACCESS_CONTROL_ALLOW_ORIGIN.equalsIgnoreCase(header.getName())) { response.setAccessControlAllowOrigin(header.getValue()); } else if (HEADER_ACCESS_CONTROL_ALLOW_METHODS.equalsIgnoreCase(header.getName())) { MethodReader.addValues(header, response.getAccessControlAllowMethods()); } else if (HEADER_ACCESS_CONTROL_MAX_AGE.equalsIgnoreCase(header.getName())) { response.setAccessControlMaxAge(Integer.parseInt(header.getValue())); } } } } private  HeaderUtils(); static void addEntityHeaders(Representation entity,
            Series<Header> headers); static void addExtensionHeaders(Series<Header> existingHeaders,
            Series<Header> additionalHeaders); static void addGeneralHeaders(Message message, Series<Header> headers); static void addHeader(String headerName, String headerValue,
            Series<Header> headers); static void addNotModifiedEntityHeaders(Representation entity,
            Series<Header> headers); static void addRequestHeaders(Request request, Series<Header> headers); static void addResponseHeaders(Response response,
            Series<Header> headers); static void keepExtensionHeadersOnly(Message message); static void copyExtensionHeaders(Series<Header> headers, Message message); static void copyResponseTransportHeaders(Series<Header> headers, Response response); static Representation extractEntityHeaders(Iterable<Header> headers,
            Representation representation); static long getContentLength(Series<Header> headers); static boolean isAlpha(int character); static boolean isAsciiChar(int character); static boolean isCarriageReturn(int character); static boolean isChunkedEncoding(Series<Header> headers); static boolean isComma(int character); static boolean isCommentText(int character); static boolean isConnectionClose(Series<Header> headers); static boolean isControlChar(int character); static boolean isDigit(int character); static boolean isDoubleQuote(int character); static boolean isHorizontalTab(int character); static boolean isLatin1Char(int character); static boolean isLinearWhiteSpace(int character); static boolean isLineFeed(int character); static boolean isLowerCase(int character); static boolean isQuoteCharacter(int character); static boolean isQuotedText(int character); static boolean isSemiColon(int character); static boolean isSeparator(int character); static boolean isSpace(int character); static boolean isText(int character); static boolean isToken(CharSequence token); static boolean isTokenChar(int character); static boolean isUpperCase(int character); static void writeCRLF(OutputStream os); static void writeHeaderLine(Header header, OutputStream os);  }
@Test public void whenHeaderRetryAfterIsDecimalThenParsingIsStillFine() { Header header = new Header(HeaderConstants.HEADER_RETRY_AFTER, "2.1"); Response response = new Response(new Request()); HeaderUtils.copyResponseTransportHeaders(new Series<>(Header.class, Collections.singletonList(header)), response); assertNotNull(response.getRetryAfter()); }
@Test public void whenHeaderRetryAfterIsAlphabeticalThenParsingFailsSilently() { Header header = new Header(HeaderConstants.HEADER_RETRY_AFTER, "2.1a"); Response response = new Response(new Request()); HeaderUtils.copyResponseTransportHeaders(new Series<>(Header.class, Collections.singletonList(header)), response); assertNull(response.getRetryAfter()); }