4545import org .apache .hc .client5 .http .cookie .MalformedCookieException ;
4646import org .apache .hc .core5 .annotation .Contract ;
4747import org .apache .hc .core5 .annotation .ThreadingBehavior ;
48- import org .apache .hc .core5 .http .FormattedHeader ;
4948import org .apache .hc .core5 .http .Header ;
5049import org .apache .hc .core5 .http .ParseException ;
5150import org .apache .hc .core5 .http .message .BufferedHeader ;
51+ import org .apache .hc .core5 .http .message .MessageSupport ;
5252import org .apache .hc .core5 .util .Args ;
5353import org .apache .hc .core5 .util .CharArrayBuffer ;
5454import org .apache .hc .core5 .util .Tokenizer ;
@@ -109,43 +109,65 @@ static String getDefaultDomain(final CookieOrigin origin) {
109109 public final List <Cookie > parse (final Header header , final CookieOrigin origin ) throws MalformedCookieException {
110110 Args .notNull (header , "Header" );
111111 Args .notNull (origin , "Cookie origin" );
112- if (!header .getName ().equalsIgnoreCase ("Set-Cookie" )) {
113- throw new MalformedCookieException ("Unrecognized cookie header: '" + header + "'" );
112+ final RawCookie rawCookie = MessageSupport .parserHeaderValue (header , this ::parseCookie );
113+ if (rawCookie == null ) {
114+ throw new MalformedCookieException ("Cookie value is invalid" );
114115 }
115- final CharArrayBuffer buffer ;
116- final Tokenizer .Cursor cursor ;
117- if (header instanceof FormattedHeader ) {
118- buffer = ((FormattedHeader ) header ).getBuffer ();
119- cursor = new Tokenizer .Cursor (((FormattedHeader ) header ).getValuePos (), buffer .length ());
120- } else {
121- final String s = header .getValue ();
122- if (s == null ) {
123- throw new MalformedCookieException ("Header value is null" );
116+
117+ final BasicClientCookie cookie = new BasicClientCookie (rawCookie .name , rawCookie .value );
118+ cookie .setPath (getDefaultPath (origin ));
119+ cookie .setDomain (getDefaultDomain (origin ));
120+ cookie .setCreationDate (Instant .now ());
121+
122+ for (final Map .Entry <String , String > entry : rawCookie .attribMap .entrySet ()) {
123+ final String paramName = entry .getKey ();
124+ final String paramValue = entry .getValue ();
125+ cookie .setAttribute (paramName , paramValue );
126+ final CookieAttributeHandler handler = this .attribHandlerMap .get (paramName );
127+ if (handler != null ) {
128+ handler .parse (cookie , paramValue );
124129 }
125- buffer = new CharArrayBuffer (s .length ());
126- buffer .append (s );
127- cursor = new Tokenizer .Cursor (0 , buffer .length ());
128130 }
131+ return Collections .singletonList (cookie );
132+ }
133+
134+ static class RawCookie {
135+
136+ final String name ;
137+ final String value ;
138+ final Map <String , String > attribMap ;
139+
140+ RawCookie (final String name , final String value , final Map <String , String > attribMap ) {
141+ this .name = name ;
142+ this .value = value ;
143+ this .attribMap = attribMap ;
144+ }
145+
146+ @ Override
147+ public String toString () {
148+ return name + " = " + value + " " + attribMap ;
149+ }
150+
151+ }
152+
153+ private RawCookie parseCookie (final CharSequence buffer ,
154+ final Tokenizer .Cursor cursor ) {
129155 final String name = tokenParser .parseToken (buffer , cursor , TOKEN_DELIMS );
130156 if (name .isEmpty ()) {
131- return Collections . emptyList () ;
157+ return null ;
132158 }
133159 if (cursor .atEnd ()) {
134- return Collections . emptyList () ;
160+ return null ;
135161 }
136162 final int valueDelim = buffer .charAt (cursor .getPos ());
137163 cursor .updatePos (cursor .getPos () + 1 );
138164 if (valueDelim != '=' ) {
139- throw new MalformedCookieException ( "Cookie value is invalid: '" + header + "'" ) ;
165+ return null ;
140166 }
141167 final String value = tokenParser .parseValue (buffer , cursor , VALUE_DELIMS );
142168 if (!cursor .atEnd ()) {
143169 cursor .updatePos (cursor .getPos () + 1 );
144170 }
145- final BasicClientCookie cookie = new BasicClientCookie (name , value );
146- cookie .setPath (getDefaultPath (origin ));
147- cookie .setDomain (getDefaultDomain (origin ));
148- cookie .setCreationDate (Instant .now ());
149171
150172 final Map <String , String > attribMap = new LinkedHashMap <>();
151173 while (!cursor .atEnd ()) {
@@ -162,24 +184,14 @@ public final List<Cookie> parse(final Header header, final CookieOrigin origin)
162184 }
163185 }
164186 }
165- cookie .setAttribute (paramName , paramValue );
166187 attribMap .put (paramName , paramValue );
167188 }
168189 // Ignore 'Expires' if 'Max-Age' is present
169190 if (attribMap .containsKey (Cookie .MAX_AGE_ATTR )) {
170191 attribMap .remove (Cookie .EXPIRES_ATTR );
171192 }
172193
173- for (final Map .Entry <String , String > entry : attribMap .entrySet ()) {
174- final String paramName = entry .getKey ();
175- final String paramValue = entry .getValue ();
176- final CookieAttributeHandler handler = this .attribHandlerMap .get (paramName );
177- if (handler != null ) {
178- handler .parse (cookie , paramValue );
179- }
180- }
181-
182- return Collections .singletonList (cookie );
194+ return new RawCookie (name , value , attribMap );
183195 }
184196
185197 @ Override
0 commit comments