2626 */
2727package org .apache .hc .client5 .http .impl ;
2828
29- import java .util .Iterator ;
3029import java .util .concurrent .atomic .AtomicReference ;
3130
3231import org .apache .hc .core5 .annotation .Internal ;
33- import org .apache .hc .core5 .http .FormattedHeader ;
34- import org .apache .hc .core5 .http .Header ;
3532import org .apache .hc .core5 .http .HttpHeaders ;
3633import org .apache .hc .core5 .http .HttpMessage ;
3734import org .apache .hc .core5 .http .HttpVersion ;
3835import org .apache .hc .core5 .http .ParseException ;
3936import org .apache .hc .core5 .http .ProtocolException ;
4037import org .apache .hc .core5 .http .ProtocolVersion ;
4138import org .apache .hc .core5 .http .ProtocolVersionParser ;
39+ import org .apache .hc .core5 .http .message .MessageSupport ;
4240import org .apache .hc .core5 .http .message .ParserCursor ;
4341import org .apache .hc .core5 .http .ssl .TLS ;
44- import org .apache .hc .core5 .util .Args ;
45- import org .apache .hc .core5 .util .CharArrayBuffer ;
4642import org .apache .hc .core5 .util .Tokenizer ;
4743
4844/**
@@ -65,6 +61,14 @@ private interface HeaderConsumer {
6561
6662 }
6763
64+ private static class InternalProtocolException extends RuntimeException {
65+
66+ public InternalProtocolException (final ProtocolException cause ) {
67+ super (cause );
68+ }
69+
70+ }
71+
6872 public ProtocolVersion switchProtocol (final HttpMessage response ) throws ProtocolException {
6973 final AtomicReference <ProtocolVersion > tlsUpgrade = new AtomicReference <>();
7074
@@ -109,43 +113,27 @@ private ProtocolVersion parseProtocolVersion(final CharSequence buffer, final Pa
109113 }
110114 }
111115
112-
113- private void parseHeaders (final HttpMessage message , final String name , final HeaderConsumer consumer )
114- throws ProtocolException {
115- final Iterator <Header > it = message .headerIterator (name );
116- while (it .hasNext ()) {
117- parseHeader (it .next (), consumer );
118- }
119- }
120-
121- private void parseHeader (final Header header , final HeaderConsumer consumer ) throws ProtocolException {
122- Args .notNull (header , "Header" );
123- if (header instanceof FormattedHeader ) {
124- final CharArrayBuffer buf = ((FormattedHeader ) header ).getBuffer ();
125- final ParserCursor cursor = new ParserCursor (0 , buf .length ());
126- cursor .updatePos (((FormattedHeader ) header ).getValuePos ());
127- parseHeaderElements (buf , cursor , consumer );
128- } else {
129- final String value = header .getValue ();
130- if (value == null ) {
131- return ;
132- }
133- final ParserCursor cursor = new ParserCursor (0 , value .length ());
134- parseHeaderElements (value , cursor , consumer );
135- }
136- }
137-
138- private void parseHeaderElements (final CharSequence buffer ,
139- final ParserCursor cursor ,
140- final HeaderConsumer consumer ) throws ProtocolException {
141- while (!cursor .atEnd ()) {
142- consumer .accept (buffer , cursor );
143- if (!cursor .atEnd ()) {
144- final char ch = buffer .charAt (cursor .getPos ());
145- if (ch == ',' ) {
146- cursor .updatePos (cursor .getPos () + 1 );
147- }
148- }
116+ //TODO To be replaced by MessageSupport method that can propagate ProtocolException
117+ private void parseHeaders (final HttpMessage message ,
118+ final String name ,
119+ final HeaderConsumer consumer ) throws ProtocolException {
120+ try {
121+ MessageSupport .parseHeaders (
122+ message ,
123+ name ,
124+ (cs1 , c1 ) ->
125+ MessageSupport .parseElementList (
126+ cs1 ,
127+ c1 ,
128+ (cs2 , c2 ) -> {
129+ try {
130+ consumer .accept (cs2 , c2 );
131+ } catch (final ProtocolException ex ) {
132+ throw new InternalProtocolException (ex );
133+ }
134+ }));
135+ } catch (final InternalProtocolException ex ) {
136+ throw (ProtocolException ) ex .getCause ();
149137 }
150138 }
151139
0 commit comments