Skip to content

Commit

Permalink
Improved protocol version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ok2c committed Jan 19, 2024
1 parent dcfb4a1 commit 5bb917d
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import java.io.Serializable;

import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.Tokenizer;

/**
* Represents a protocol version. The "major.minor" numbering
Expand Down Expand Up @@ -239,6 +241,29 @@ public final boolean lessEquals(final ProtocolVersion version) {
return isComparable(version) && (compareToVersion(version) <= 0);
}

@Internal
public static ProtocolVersion parse(
final CharSequence buffer,
final Tokenizer.Cursor cursor,
final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
return ProtocolVersionParser.INSTANCE.parse(buffer, cursor, delimiterPredicate);
}

/**
* @since 5.3
*/
public static ProtocolVersion parse(final String s) throws ParseException {
if (s == null) {
return null;
}
final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
final ProtocolVersion protocolVersion = ProtocolVersionParser.INSTANCE.parse(s, cursor, null);
Tokenizer.INSTANCE.skipWhiteSpace(s, cursor);
if (!cursor.atEnd()) {
throw new ParseException("Invalid protocol version; trailing content");
}
return protocolVersion;
}

/**
* Converts this protocol version to a string.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc.core5.http;

import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.util.TextUtils;
import org.apache.hc.core5.util.Tokenizer;

@Internal
public class ProtocolVersionParser {

public final static ProtocolVersionParser INSTANCE = new ProtocolVersionParser();

private static final char SLASH = '/';
private static final char FULL_STOP = '.';
private static final Tokenizer.Delimiter PROTO_DELIMITER = Tokenizer.delimiters(SLASH);
private static final Tokenizer.Delimiter FULL_STOP_OR_BLANK = Tokenizer.delimiters(FULL_STOP, ' ', '\t');
private static final Tokenizer.Delimiter BLANK = Tokenizer.delimiters(' ', '\t');
private final Tokenizer tokenizer;

public ProtocolVersionParser() {
this.tokenizer = Tokenizer.INSTANCE;
}

@Internal
@FunctionalInterface
public interface Factory {

ProtocolVersion create(int major, int minor);

}

public ProtocolVersion parse(
final String protocol,
final Factory factory,
final CharSequence buffer,
final Tokenizer.Cursor cursor,
final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
final int lowerBound = cursor.getLowerBound();
final int upperBound = cursor.getUpperBound();
final String token1 = tokenizer.parseToken(buffer, cursor,
delimiterPredicate != null ? (ch) -> delimiterPredicate.test(ch) || FULL_STOP_OR_BLANK.test(ch) : FULL_STOP_OR_BLANK);
final int major;
try {
major = Integer.parseInt(token1);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid " + protocol + " major version number",
buffer, lowerBound, upperBound, cursor.getPos());
}
if (cursor.atEnd()) {
return factory != null ? factory.create(major, major) : new ProtocolVersion(protocol, major, 0);
}
if (buffer.charAt(cursor.getPos()) != FULL_STOP) {
return factory != null ? factory.create(major, major) : new ProtocolVersion(protocol, major, 0);
} else {
cursor.updatePos(cursor.getPos() + 1);
final String token2 = tokenizer.parseToken(buffer, cursor,
delimiterPredicate != null ? (ch) -> delimiterPredicate.test(ch) || BLANK.test(ch) : BLANK);
final int minor;
try {
minor = Integer.parseInt(token2);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid " + protocol + " minor version number",
buffer, lowerBound, upperBound, cursor.getPos());
}
return factory != null ? factory.create(major, minor) : new ProtocolVersion(protocol, major, minor);
}
}

public ProtocolVersion parse(
final String protocol,
final CharSequence buffer,
final Tokenizer.Cursor cursor,
final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
return parse(protocol, null, buffer, cursor, delimiterPredicate);
}

public ProtocolVersion parse(
final String protocol,
final CharSequence buffer,
final Tokenizer.Cursor cursor) throws ParseException {
return parse(protocol, null, buffer, cursor, null);
}

public ProtocolVersion parse(
final CharSequence buffer,
final Tokenizer.Cursor cursor,
final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
tokenizer.skipWhiteSpace(buffer, cursor);
final String proto = tokenizer.parseToken(buffer, cursor, PROTO_DELIMITER);
if (TextUtils.isBlank(proto)) {
throw new ParseException("Invalid protocol name");
}
if (!cursor.atEnd() && buffer.charAt(cursor.getPos()) == SLASH) {
cursor.updatePos(cursor.getPos() + 1);
return parse(proto, null, buffer, cursor, delimiterPredicate);
} else {
throw new ParseException("Invalid protocol name");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.ProtocolVersionParser;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
import org.apache.hc.core5.util.TextUtils;
Expand All @@ -48,7 +49,6 @@ public class BasicLineParser implements LineParser {

public final static BasicLineParser INSTANCE = new BasicLineParser();

private static final Tokenizer.Delimiter FULL_STOP = Tokenizer.delimiters('.');
private static final Tokenizer.Delimiter BLANKS = Tokenizer.delimiters(' ', '\t');
private static final Tokenizer.Delimiter COLON = Tokenizer.delimiters(':');

Expand Down Expand Up @@ -108,29 +108,7 @@ ProtocolVersion parseProtocolVersion(
}

cursor.updatePos(pos + protolength + 1);

final String token1 = this.tokenizer.parseToken(buffer, cursor, FULL_STOP);
final int major;
try {
major = Integer.parseInt(token1);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid protocol major version number",
buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
if (cursor.atEnd()) {
throw new ParseException("Invalid protocol version",
buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
cursor.updatePos(cursor.getPos() + 1);
final String token2 = this.tokenizer.parseToken(buffer, cursor, BLANKS);
final int minor;
try {
minor = Integer.parseInt(token2);
} catch (final NumberFormatException e) {
throw new ParseException("Invalid protocol minor version number",
buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
return HttpVersion.get(major, minor);
return ProtocolVersionParser.INSTANCE.parse(protoname, HttpVersion::get, buffer, cursor, null);
}

/**
Expand Down
33 changes: 32 additions & 1 deletion httpcore5/src/main/java/org/apache/hc/core5/http/ssl/TLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.ProtocolVersionParser;
import org.apache.hc.core5.util.Tokenizer;

/**
Expand Down Expand Up @@ -90,12 +92,41 @@ public boolean lessEquals(final ProtocolVersion protocolVersion) {
return version.lessEquals(protocolVersion);
}

@Internal
public static ProtocolVersion parse(
final CharSequence buffer,
final Tokenizer.Cursor cursor,
final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
final int lowerBound = cursor.getLowerBound();
final int upperBound = cursor.getUpperBound();

int pos = cursor.getPos();
if (pos + 4 > cursor.getUpperBound()) {
throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
}
if (buffer.charAt(pos) != 'T' || buffer.charAt(pos + 1) != 'L' || buffer.charAt(pos + 2) != 'S'
|| buffer.charAt(pos + 3) != 'v') {
throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
}
pos = pos + 4;
cursor.updatePos(pos);
if (cursor.atEnd()) {
throw new ParseException("Invalid TLS version", buffer, lowerBound, upperBound, pos);
}
return ProtocolVersionParser.INSTANCE.parse("TLS", null, buffer, cursor, delimiterPredicate);
}

public static ProtocolVersion parse(final String s) throws ParseException {
if (s == null) {
return null;
}
final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
return TlsVersionParser.INSTANCE.parse(s, cursor, null);
final ProtocolVersion protocolVersion = parse(s, cursor, null);
Tokenizer.INSTANCE.skipWhiteSpace(s, cursor);
if (!cursor.atEnd()) {
throw new ParseException("Invalid TLS protocol version; trailing content");
}
return protocolVersion;
}

public static String[] excludeWeak(final String... protocols) {
Expand Down

This file was deleted.

Loading

0 comments on commit 5bb917d

Please sign in to comment.