@ -62,6 +62,7 @@ import java.io.IOException;
import java.net.MalformedURLException ;
import java.net.URL ;
import java.text.Collator ;
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.HashMap ;
@ -69,6 +70,7 @@ import java.util.Iterator;
import java.util.Locale ;
import java.util.Map ;
import java.util.Properties ;
import java.util.TimeZone ;
import java.util.TreeMap ;
import java.util.Vector ;
@ -94,35 +96,52 @@ public final class httpHeader extends TreeMap implements Map {
public static final String ACCEPT = "Accept" ;
public static final String ACCEPT_CHARSET = "Accept-Charset" ;
public static final String ACCEPT_LANGUAGE = "Accept-Language" ;
public static final String KEEP_ALIVE = "Keep-Alive" ;
public static final String USER_AGENT = "User-Agent" ;
public static final String HOST = "Host" ;
public static final String CONNECTION = "Connection" ;
public static final String PROXY_CONNECTION = "Proxy-Connection" ;
public static final String KEEP_ALIVE = "Keep-Alive" ;
public static final String REFERER = "Referer" ;
public static final String ACCEPT_ENCODING = "Accept-Encoding" ;
public static final String CONTENT_LENGTH = "Content-Length" ;
public static final String CONTENT_TYPE = "Content-Type" ;
public static final String USER_AGENT = "User-Agent" ;
public static final String AUTHORIZATION = "Authorization" ;
public static final String WWW_AUTHENTICATE = "WWW-Authenticate" ;
public static final String PROXY_AUTHORIZATION = "Proxy-Authorization" ;
public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate" ;
public static final String PROXY_CONNECTION = "Proxy-Connection" ;
public static final String DATE = "Date" ;
public static final String SERVER = "Server" ;
public static final String LAST_MODIFIED = "Last-modified" ;
public static final String PRAGMA = "Pragma" ;
public static final String CONTENT_LENGTH = "Content-Length" ;
public static final String CONTENT_TYPE = "Content-Type" ;
public static final String CONTENT_MD5 = "Content-MD5" ;
public static final String SET_COOKIE = "Set-Cookie" ;
public static final String SET_COOKIE2 = "Set-Cookie2" ;
public static final String IF_MODIFIED_SINCE = "If-Modified-Since" ;
public static final String COOKIE = "Cookie" ;
public static final String EXPIRES = "Expires" ;
public static final String ACCEPT_ENCODING = "Accept-Encoding" ;
public static final String CONTENT_ENCODING = "Content-Encoding" ;
public static final String TRANSFER_ENCODING = "Transfer-Encoding" ;
public static final String ACCEPT_RANGES = "Accept-Ranges" ;
public static final String CONTENT_RANGE = "Content-Range" ;
public static final String RANGE = "Range" ;
public static final String IF_RANGE = "If-Range" ;
public static final String PRAGMA = "Pragma" ;
public static final String CACHE_CONTROL = "Cache-Control" ;
public static final String TRANSFER_ENCODING = "Transfer-Encoding" ;
public static final String IF_MODIFIED_SINCE = "If-Modified-Since" ;
public static final String LAST_MODIFIED = "Last-modified" ;
public static final String LOCATION = "Location" ;
public static final String ETAG = "ETag" ;
public static final String VIA = "Via" ;
public static final String X_CACHE = "X-Cache" ;
public static final String X_CACHE_LOOKUP = "X-Cache-Lookup" ;
@ -360,29 +379,47 @@ public final class httpHeader extends TreeMap implements Map {
private static SimpleDateFormat EMLFormatter = new SimpleDateFormat ( "dd MMM yyyy HH:mm:ss" , Locale . US ) ;
public static Date parseHTTPDate ( String s ) {
if ( ( s = = null ) | | ( s . length ( ) < 9 ) ) return new Date ( ) ;
s = s . trim ( ) ;
if ( s . charAt ( 3 ) = = ',' ) s = s . substring ( 5 ) . trim ( ) ; // we skip the name of the day
if ( s . charAt ( 9 ) = = ' ' ) s = s . substring ( 0 , 7 ) + "20" + s . substring ( 7 ) ; // short year version
if ( s . charAt ( 2 ) = = ',' ) s = s . substring ( 0 , 2 ) + s . substring ( 3 ) ; // ommit comma after day of week
if ( ( s . charAt ( 0 ) > '9' ) & & ( s . length ( ) > 20 ) & & ( s . charAt ( 2 ) = = ' ' ) ) s = s . substring ( 3 ) ;
if ( s . length ( ) > 20 ) s = s . substring ( 0 , 20 ) . trim ( ) ; // truncate remaining, since that must be wrong
try {
return parseHTTPDate ( s , true ) ;
} catch ( ParseException e ) {
//System.out.println("ERROR long version parse: " + e.getMessage() + " at position " + e.getErrorOffset());
serverLog . logSevere ( "HTTPC-header" , "DATE ERROR (Parse): " + s ) ;
return null ;
} catch ( java . lang . NumberFormatException e ) {
//System.out.println("ERROR long version parse: " + e.getMessage() + " at position " + e.getErrorOffset());
serverLog . logSevere ( "HTTPC-header" , "DATE ERROR (NumberFormat): " + s ) ;
return null ;
}
}
public static Date parseHTTPDate ( String s , boolean ignoreTimezone ) throws ParseException , NumberFormatException {
SimpleDateFormat formatter = EMLFormatter ;
if ( ( s = = null ) | | ( s . length ( ) < 9 ) ) return null ;
s = s . trim ( ) ;
if ( s . charAt ( 3 ) = = ',' ) s = s . substring ( 5 ) . trim ( ) ; // we skip the name of the day
if ( s . charAt ( 9 ) = = ' ' ) s = s . substring ( 0 , 7 ) + "20" + s . substring ( 7 ) ; // short year version
if ( s . charAt ( 2 ) = = ',' ) s = s . substring ( 0 , 2 ) + s . substring ( 3 ) ; // ommit comma after day of week
if ( ( s . charAt ( 0 ) > '9' ) & & ( s . length ( ) > 20 ) & & ( s . charAt ( 2 ) = = ' ' ) ) s = s . substring ( 3 ) ;
if ( s . length ( ) > 20 ) {
if ( ! ignoreTimezone ) {
formatter = ( SimpleDateFormat ) formatter . clone ( ) ;
formatter . setTimeZone ( TimeZone . getTimeZone ( s . substring ( 20 ) ) ) ;
}
s = s . substring ( 0 , 20 ) . trim ( ) ; // truncate remaining, since that must be wrong
}
if ( s . indexOf ( "Mrz" ) > 0 ) s = s . replaceAll ( "Mrz" , "March" ) ;
try {
return EMLFormatter . parse ( s ) ;
} catch ( java . text . ParseException e ) {
//System.out.println("ERROR long version parse: " + e.getMessage() + " at position " + e.getErrorOffset());
serverLog . logSevere ( "HTTPC-header" , "DATE ERROR (Parse): " + s ) ;
return new Date ( ) ;
} catch ( java . lang . NumberFormatException e ) {
//System.out.println("ERROR long version parse: " + e.getMessage() + " at position " + e.getErrorOffset());
serverLog . logSevere ( "HTTPC-header" , "DATE ERROR (NumberFormat): " + s ) ;
return new Date ( ) ;
}
// parsing the date string
return formatter . parse ( s ) ;
}
private Date headerDate ( String kind ) {
if ( containsKey ( kind ) ) return new Date ( parseHTTPDate ( ( String ) get ( kind ) ) . getTime ( ) ) ;
if ( containsKey ( kind ) ) {
Date parsedDate = parseHTTPDate ( ( String ) get ( kind ) ) ;
if ( parsedDate = = null ) parsedDate = new Date ( ) ;
return new Date ( parsedDate . getTime ( ) ) ;
}
return null ;
}
@ -406,6 +443,17 @@ public final class httpHeader extends TreeMap implements Map {
return headerDate ( httpHeader . IF_MODIFIED_SINCE ) ;
}
public Object ifRange ( ) {
if ( containsKey ( httpHeader . IF_RANGE ) ) {
try {
Date rangeDate = parseHTTPDate ( ( String ) get ( httpHeader . IF_RANGE ) , false ) ;
if ( rangeDate ! = null ) return new Date ( rangeDate . getTime ( ) ) ;
} catch ( Exception e ) { }
return get ( httpHeader . IF_RANGE ) ;
}
return null ;
}
public long age ( ) {
Date lm = lastModified ( ) ;
Date sd = date ( ) ;