Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 19 additions & 31 deletions httpcore5/src/main/java/org/apache/hc/core5/http/HttpVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,35 @@ public final class HttpVersion extends ProtocolVersion {
public static final HttpVersion[] ALL = {HTTP_0_9, HTTP_1_0, HTTP_1_1, HTTP_2_0};

/**
* Create an HTTP protocol version designator.
* Gets a specific instance or creates a new one.
*
* @param major the major version number of the HTTP protocol
* @param minor the minor version number of the HTTP protocol
* @param major the major version
* @param minor the minor version
*
* @return an instance of {@link HttpVersion} with the argument version, never null.
* @throws IllegalArgumentException if either major or minor version number is negative
* @since 5.0
*/
public HttpVersion(final int major, final int minor) {
super(HTTP, major, minor);
public static HttpVersion get(final int major, final int minor) {
for (int i = 0; i < ALL.length; i++) {
if (ALL[i].equals(major, minor)) {
return ALL[i];
}
}
// argument checking is done in the constructor
return new HttpVersion(major, minor);
}


/**
* Obtains a specific HTTP version.
* Creates an HTTP protocol version designator.
*
* @param major the major version
* @param minor the minor version
* @param major the major version number of the HTTP protocol
* @param minor the minor version number of the HTTP protocol
*
* @return an instance of {@link HttpVersion} with the argument version
* @throws IllegalArgumentException if either major or minor version number is negative
*/
@Override
public ProtocolVersion forVersion(final int major, final int minor) {

if ((major == getMajor()) && (minor == getMinor())) {
return this;
}

if (major == 1) {
if (minor == 0) {
return HTTP_1_0;
}
if (minor == 1) {
return HTTP_1_1;
}
}
if ((major == 0) && (minor == 9)) {
return HTTP_0_9;
}

// argument checking is done in the constructor
return new HttpVersion(major, minor);
public HttpVersion(final int major, final int minor) {
super(HTTP, major, minor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,6 @@ public final int getMinor() {
}


/**
* Obtains a specific version of this protocol.
* This can be used by derived classes to instantiate themselves instead
* of the base class, and to define constants for commonly used versions.
* <p>
* The default implementation in this class returns {@code this}
* if the version matches, and creates a new {@link ProtocolVersion}
* otherwise.
* </p>
*
* @param major the major version
* @param minor the minor version
*
* @return a protocol version with the same protocol name
* and the argument version
*/
public ProtocolVersion forVersion(final int major, final int minor) {

if ((major == this.major) && (minor == this.minor)) {
return this;
}

// argument checking is done in the constructor
return new ProtocolVersion(this.protocol, major, minor);
}


/**
* Obtains a hash code consistent with {@link #equals}.
*
Expand All @@ -138,10 +111,21 @@ public final int hashCode() {
return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor;
}

/**
* Checks whether this instance has the same major and minor version as the arguments.
*
* @param major the major version to check.
* @param minor the minor version to check.
* @return whether this instance has the same major and minor version as the arguments.
* @since 5.0
*/
public final boolean equals(final int major, final int minor) {
return this.major == major && this.minor == minor;
}

/**
* Checks equality of this protocol version with an object.
* The object is equal if it is a protocl version with the same
* The object is equal if it is a protocol version with the same
* protocol name, major version number, and minor version number.
* The specific class of the object is <i>not</i> relevant,
* instances of derived classes with identical attributes are
Expand All @@ -167,6 +151,21 @@ public final boolean equals(final Object obj) {
(this.minor == that.minor));
}

/**
* Formats this protocol version as a string.
*
* @return a protocol version string, like "HTTP/1.1"
* @since 5.0
*/
public String format() {
final StringBuilder buffer = new StringBuilder();
buffer.append(this.protocol);
buffer.append('/');
buffer.append(Integer.toString(this.major));
buffer.append('.');
buffer.append(Integer.toString(this.minor));
return buffer.toString();
}

/**
* Checks whether this protocol can be compared to another one.
Expand Down Expand Up @@ -248,13 +247,7 @@ public final boolean lessEquals(final ProtocolVersion version) {
*/
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(this.protocol);
buffer.append('/');
buffer.append(Integer.toString(this.major));
buffer.append('.');
buffer.append(Integer.toString(this.minor));
return buffer.toString();
return format();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public BasicLineFormatter() {
}

void formatProtocolVersion(final CharArrayBuffer buffer, final ProtocolVersion version) {
buffer.append(version.getProtocol());
buffer.append('/');
buffer.append(Integer.toString(version.getMajor()));
buffer.append('.');
buffer.append(Integer.toString(version.getMinor()));
buffer.append(version.format());
}

@Override
Expand All @@ -68,15 +64,15 @@ public void formatRequestLine(final CharArrayBuffer buffer, final RequestLine re
}

@Override
public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statline) {
public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statusLine) {
Args.notNull(buffer, "Char array buffer");
Args.notNull(statline, "Status line");
Args.notNull(statusLine, "Status line");

formatProtocolVersion(buffer, statline.getProtocolVersion());
formatProtocolVersion(buffer, statusLine.getProtocolVersion());
buffer.append(' ');
buffer.append(Integer.toString(statline.getStatusCode()));
buffer.append(Integer.toString(statusLine.getStatusCode()));
buffer.append(' '); // keep whitespace even if reason phrase is empty
final String reasonPhrase = statline.getReasonPhrase();
final String reasonPhrase = statusLine.getReasonPhrase();
if (reasonPhrase != null) {
buffer.append(reasonPhrase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ProtocolVersion parseProtocolVersion(
throw new ParseException("Invalid protocol minor version number",
buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
return new HttpVersion(major, minor);
return HttpVersion.get(major, minor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@
*/
public class TestHttpVersion {

@Test
public void testEqualsMajorMinor() {
Assert.assertTrue(HttpVersion.HTTP_0_9.equals(0, 9));
Assert.assertTrue(HttpVersion.HTTP_1_0.equals(1, 0));
Assert.assertTrue(HttpVersion.HTTP_1_1.equals(1, 1));
Assert.assertTrue(HttpVersion.HTTP_2.equals(2, 0));
Assert.assertTrue(HttpVersion.HTTP_2_0.equals(2, 0));
//
Assert.assertFalse(HttpVersion.HTTP_0_9.equals(2, 0));
}

@Test
public void testGet() {
Assert.assertEquals(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
Assert.assertEquals(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
Assert.assertEquals(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
Assert.assertEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
Assert.assertEquals(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
Assert.assertNotEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
//
Assert.assertSame(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
Assert.assertSame(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
Assert.assertSame(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
Assert.assertSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
Assert.assertSame(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
Assert.assertNotSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
}

@SuppressWarnings("unused")
@Test
public void testHttpVersionInvalidConstructorInput() throws Exception {
try {
Expand Down Expand Up @@ -108,14 +137,15 @@ public void testHttpVersionComparison() {
public void testSerialization() throws Exception {
final HttpVersion orig = HttpVersion.HTTP_1_1;
final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
outStream.writeObject(orig);
outStream.close();
final byte[] raw = outbuffer.toByteArray();
final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
final HttpVersion clone = (HttpVersion) inStream.readObject();
Assert.assertEquals(orig, clone);
try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) {
outStream.writeObject(orig);
outStream.close();
final byte[] raw = outbuffer.toByteArray();
final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
final HttpVersion clone = (HttpVersion) inStream.readObject();
Assert.assertEquals(orig, clone);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* ====================================================================
* 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.junit.Assert;
import org.junit.Test;

public class TestProtocolVersion {

private static final ProtocolVersion PROTOCOL_VERSION_0_0 = new ProtocolVersion("a", 0, 0);
private static final ProtocolVersion PROTOCOL_VERSION_1_0 = new ProtocolVersion("b", 1, 0);
private static final ProtocolVersion PROTOCOL_VERSION_1_2 = new ProtocolVersion("c", 1, 2);

@Test
public void testEqualsMajorMinor() {
Assert.assertTrue(PROTOCOL_VERSION_0_0.equals(0, 0));
Assert.assertTrue(PROTOCOL_VERSION_1_0.equals(1, 0));
Assert.assertTrue(PROTOCOL_VERSION_1_2.equals(1, 2));
//
Assert.assertFalse(PROTOCOL_VERSION_1_2.equals(2, 0));
}

}