public static Cookie parseSetCookieHeader(final String headerValue) {

        String key = null;
        CookieImpl cookie = null;
        int state = 0;
        int current = 0;
        for (int i = 0; i < headerValue.length(); ++i) {
            char c = headerValue.charAt(i);
            switch (state) {
                case 0: {
                    //reading key
                    if (c == '=') {
                        key = headerValue.substring(current, i);
                        current = i + 1;
                        state = 1;
                    } else if ((c == ';' || c == ' ') && current == i) {
                        current++;
                    } else if (c == ';') {
                        if (cookie == null) {
                            throw UndertowMessages.MESSAGES.couldNotParseCookie(headerValue);
                        } else {
                            handleValue(cookie, headerValue.substring(current, i), null);
                        }
                        current = i + 1;
                    }
                    break;
                }
                case 1: {
                    if (c == ';') {
                        if (cookie == null) {
                            cookie = new CookieImpl(key, headerValue.substring(current, i));
                        } else {
                            handleValue(cookie, key, headerValue.substring(current, i));
                        }
                        state = 0;
                        current = i + 1;
                        key = null;
                    } else if (c == '"' && current == i) {
                        current++;
                        state = 2;
                    }
                    break;
                }
                case 2: {
                    if (c == '"') {
                        if (cookie == null) {
                            cookie = new CookieImpl(key, headerValue.substring(current, i));
                        } else {
                            handleValue(cookie, key, headerValue.substring(current, i));
                        }
                        state = 0;
                        current = i + 1;
                        key = null;
                    }
                    break;
                }
            }
        }
        if (key == null) {
            if (current != headerValue.length()) {
                handleValue(cookie, headerValue.substring(current, headerValue.length()), null);
            }
        } else {
            if (current != headerValue.length()) {
                if(cookie == null) {
                    cookie = new CookieImpl(key, headerValue.substring(current, headerValue.length()));
                } else {
                    handleValue(cookie, key, headerValue.substring(current, headerValue.length()));
                }
            } else {
                handleValue(cookie, key, null);
            }
        }

        return cookie;
    }
public static Cookie parseSetCookieHeader ( final String headerValue ) { String key = null ; CookieImpl cookie = null ; int state = 0 ; int current = 0 ; for ( int i = 0 ; i < headerValue . length ( ) ; ++ i ) { char c = headerValue . charAt ( i ) ; switch ( state ) { case 0 : { if ( c == '=' ) { key = headerValue . substring ( current , i ) ; current = i + 1 ; state = 1 ; } else if ( ( c == ';' || c == ' ' ) && current == i ) { current ++ ; } else if ( c == ';' ) { if ( cookie == null ) { throw UndertowMessages . MESSAGES . couldNotParseCookie ( headerValue ) ; } else { handleValue ( cookie , headerValue . substring ( current , i ) , null ) ; } current = i + 1 ; } break ; } case 1 : { if ( c == ';' ) { if ( cookie == null ) { cookie = new CookieImpl ( key , headerValue . substring ( current , i ) ) ; } else { handleValue ( cookie , key , headerValue . substring ( current , i ) ) ; } state = 0 ; current = i + 1 ; key = null ; } else if ( c == '"' && current == i ) { current ++ ; state = 2 ; } break ; } case 2 : { if ( c == '"' ) { if ( cookie == null ) { cookie = new CookieImpl ( key , headerValue . substring ( current , i ) ) ; } else { handleValue ( cookie , key , headerValue . substring ( current , i ) ) ; } state = 0 ; current = i + 1 ; key = null ; } break ; } } } if ( key == null ) { if ( current != headerValue . length ( ) ) { handleValue ( cookie , headerValue . substring ( current , headerValue . length ( ) ) , null ) ; } } else { if ( current != headerValue . length ( ) ) { if ( cookie == null ) { cookie = new CookieImpl ( key , headerValue . substring ( current , headerValue . length ( ) ) ) ; } else { handleValue ( cookie , key , headerValue . substring ( current , headerValue . length ( ) ) ) ; } } else { handleValue ( cookie , key , null ) ; } } return cookie ; }
Parses a "Set-Cookie:" response header value into its cookie representation. The header value is parsed according to the
syntax that's defined in RFC2109:

<pre>
<code>
set-cookie      =       "Set-Cookie:" cookies
cookies         =       1#cookie
cookie          =       NAME "=" VALUE *(";" cookie-av)
NAME            =       attr
VALUE           =       value
cookie-av       =       "Comment" "=" value
|       "Domain" "=" value
|       "Max-Age" "=" value
|       "Path" "=" value
|       "Secure"
|       "Version" "=" 1*DIGIT

</code>
</pre>

@param headerValue The header value
@return The cookie

@see Cookie
@see <a href="http://tools.ietf.org/search/rfc2109">rfc2109</a>