Skip to content

Commit c586766

Browse files
committed
Optimized header parsing code in ProtocolSwitchStrategy
1 parent 558d851 commit c586766

File tree

2 files changed

+30
-50
lines changed

2 files changed

+30
-50
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/ProtocolSwitchStrategy.java

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@
2626
*/
2727
package org.apache.hc.client5.http.impl;
2828

29-
import java.util.Iterator;
3029
import java.util.concurrent.atomic.AtomicReference;
3130

3231
import org.apache.hc.core5.annotation.Internal;
33-
import org.apache.hc.core5.http.FormattedHeader;
34-
import org.apache.hc.core5.http.Header;
3532
import org.apache.hc.core5.http.HttpHeaders;
3633
import org.apache.hc.core5.http.HttpMessage;
3734
import org.apache.hc.core5.http.HttpVersion;
3835
import org.apache.hc.core5.http.ParseException;
3936
import org.apache.hc.core5.http.ProtocolException;
4037
import org.apache.hc.core5.http.ProtocolVersion;
4138
import org.apache.hc.core5.http.ProtocolVersionParser;
39+
import org.apache.hc.core5.http.message.MessageSupport;
4240
import org.apache.hc.core5.http.message.ParserCursor;
4341
import org.apache.hc.core5.http.ssl.TLS;
44-
import org.apache.hc.core5.util.Args;
45-
import org.apache.hc.core5.util.CharArrayBuffer;
4642
import 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

httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestProtocolSwitchStrategy.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,6 @@ void testSwitchInvalid() {
9696
Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response3));
9797
}
9898

99-
@Test
100-
void testNullToken() throws ProtocolException {
101-
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
102-
response.addHeader(HttpHeaders.UPGRADE, "TLS,");
103-
response.addHeader(HttpHeaders.UPGRADE, null);
104-
Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response));
105-
}
106-
10799
@Test
108100
void testWhitespaceOnlyToken() throws ProtocolException {
109101
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);

0 commit comments

Comments
 (0)