Issue 647: fix handling of malformed 'Expires' header

This commit is contained in:
Aled Sage 2012-05-17 23:37:06 +01:00
parent 70cf2bb6a9
commit fffa3eb8af
2 changed files with 53 additions and 3 deletions

View File

@ -17,8 +17,10 @@ import org.jclouds.date.DateCodec;
import org.jclouds.date.DateCodecFactory;
import org.jclouds.io.ContentMetadataCodec.DefaultContentMetadataCodec;
import org.jclouds.logging.Logger;
import org.jclouds.util.Throwables2;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.Multimap;
@ -115,9 +117,13 @@ public interface ContentMetadataCodec {
public Date parseExpires(String expires) {
try {
return (expires != null) ? getExpiresDateCodec().toDate(expires) : null;
} catch (ParseException e) {
logger.warn(e, "Invalid Expires header (%s); should be in RFC-1123 format; treating as already expired", expires);
} catch (Exception e) {
if (Throwables2.getFirstThrowableOfType(e, ParseException.class) != null) {
logger.debug("Invalid Expires header (%s); should be in RFC-1123 format; treating as already expired: %s", expires, e.getMessage());
return new Date(0);
} else {
throw Throwables.propagate(e);
}
}
}
}

View File

@ -0,0 +1,44 @@
package org.jclouds.date.internal;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import java.text.ParseException;
import java.util.Date;
import org.jclouds.date.DateCodec;
import org.jclouds.util.Throwables2;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class SimpleDateCodecFactoryTest {
private SimpleDateCodecFactory simpleDateCodecFactory;
private DateCodec rfc1123Codec;
@BeforeMethod
public void setUp() throws Exception {
simpleDateCodecFactory = new SimpleDateCodecFactory(new SimpleDateFormatDateService());
rfc1123Codec = simpleDateCodecFactory.rfc1123();
}
@Test
public void testCodecForRfc1123() throws Exception {
Date date = new Date(1000);
assertEquals(rfc1123Codec.toDate(rfc1123Codec.toString(date)), date);
assertEquals(rfc1123Codec.toDate("Thu, 01 Dec 1994 16:00:00 GMT"), new Date(786297600000L));
}
@Test
public void testCodecForRfc1123ThrowsParseExceptionWhenMalformed() throws Exception {
try {
rfc1123Codec.toDate("wrong");
fail();
} catch (Exception e) {
if (Throwables2.getFirstThrowableOfType(e, ParseException.class) == null) {
throw e;
}
}
}
}