@ Nullable public static Cookie parseCookie ( String cookieStr , String host , String defaultPath ) { String [ ] items = cookieStr . split ( ";" ) ; Parameter < String > param = parseCookieNameValue ( items [ 0 ] ) ; if ( param == null ) { return null ; } String domain = "" ; String path = "" ; long expiry = 0 ; boolean secure = false ; for ( String item : items ) { item = item . trim ( ) ; if ( item . isEmpty ( ) ) { continue ; } Parameter < String > attribute = parseCookieAttribute ( item ) ; switch ( attribute . name ( ) . toLowerCase ( ) ) { case "domain" : domain = normalizeDomain ( attribute . value ( ) ) ; break ; case "path" : path = normalizePath ( attribute . value ( ) ) ; break ; case "expires" : // If a cookie has both the Max-Age and the Expires attribute, the Max-Age attribute has precedence // and controls the expiration date of the cookie. if ( expiry == 0 ) { Date date = CookieDateUtil . parseDate ( attribute . value ( ) ) ; if ( date != null ) { expiry = date . getTime ( ) ; if ( expiry == 0 ) { expiry = 1 ; } } } break ; case "max-age" : try { int seconds = Integer . parseInt ( attribute . value ( ) ) ; expiry = System . currentTimeMillis ( ) + seconds * 1000 ; if ( expiry == 0 ) { expiry = 1 ; } } catch ( NumberFormatException ignore ) { } break ; case "secure" : secure = true ; break ; case "httponly" : // ignore http only now break ; default : } } if ( path . isEmpty ( ) ) { path = defaultPath ; } boolean hostOnly ; if ( domain . isEmpty ( ) ) { domain = host ; hostOnly = true ; } else { if ( isIP ( host ) ) { // should not set return null ; } if ( ! isDomainSuffix ( host , domain ) ) { return null ; } hostOnly = false ; } return new Cookie ( domain , path , param . name ( ) , param . value ( ) , expiry , secure , hostOnly ) ; }
Parse one cookie header value return the cookie .