diff --git a/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java b/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java index d485955b72..b20f00715d 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java @@ -37,6 +37,7 @@ public class ByteProcessorTest { assertEquals(16, buf.forEachByte(14, length - 14, ByteProcessor.FIND_NON_LF)); assertEquals(19, buf.forEachByte(16, length - 16, ByteProcessor.FIND_NUL)); assertEquals(21, buf.forEachByte(19, length - 19, ByteProcessor.FIND_NON_NUL)); + assertEquals(24, buf.forEachByte(19, length - 19, ByteProcessor.FIND_ASCII_SPACE)); assertEquals(24, buf.forEachByte(21, length - 21, ByteProcessor.FIND_LINEAR_WHITESPACE)); assertEquals(28, buf.forEachByte(24, length - 24, ByteProcessor.FIND_NON_LINEAR_WHITESPACE)); assertEquals(-1, buf.forEachByte(28, length - 28, ByteProcessor.FIND_LINEAR_WHITESPACE)); @@ -51,6 +52,7 @@ public class ByteProcessorTest { final int length = buf.readableBytes(); assertEquals(27, buf.forEachByteDesc(0, length, ByteProcessor.FIND_LINEAR_WHITESPACE)); + assertEquals(25, buf.forEachByteDesc(0, length, ByteProcessor.FIND_ASCII_SPACE)); assertEquals(23, buf.forEachByteDesc(0, 28, ByteProcessor.FIND_NON_LINEAR_WHITESPACE)); assertEquals(20, buf.forEachByteDesc(0, 24, ByteProcessor.FIND_NUL)); assertEquals(18, buf.forEachByteDesc(0, 21, ByteProcessor.FIND_NON_NUL)); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java index c6351c47bf..21491971ef 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java @@ -116,7 +116,7 @@ public class HttpResponseDecoder extends HttpObjectDecoder { protected HttpMessage createMessage(String[] initialLine) { return new DefaultHttpResponse( HttpVersion.valueOf(initialLine[0]), - new HttpResponseStatus(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders); + HttpResponseStatus.valueOf(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders); } @Override diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java index 026866ebcc..b7e2c10d45 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java @@ -15,13 +15,15 @@ */ package io.netty.handler.codec.http; -import static io.netty.handler.codec.http.HttpConstants.SP; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.util.AsciiString; -import io.netty.util.ByteProcessor; import io.netty.util.CharsetUtil; +import static io.netty.handler.codec.http.HttpConstants.SP; +import static io.netty.util.ByteProcessor.FIND_ASCII_SPACE; +import static java.lang.Integer.parseInt; + /** * The response code and its description of HTTP or its derived protocols, such as * RTSP and @@ -324,10 +326,15 @@ public class HttpResponseStatus implements Comparable { /** * Returns the {@link HttpResponseStatus} represented by the specified code. - * If the specified code is a standard HTTP getStatus code, a cached instance + * If the specified code is a standard HTTP status code, a cached instance * will be returned. Otherwise, a new instance will be returned. */ public static HttpResponseStatus valueOf(int code) { + HttpResponseStatus status = valueOf0(code); + return status != null ? status : new HttpResponseStatus(code); + } + + private static HttpResponseStatus valueOf0(int code) { switch (code) { case 100: return CONTINUE; @@ -442,12 +449,25 @@ public class HttpResponseStatus implements Comparable { case 511: return NETWORK_AUTHENTICATION_REQUIRED; } - - return new HttpResponseStatus(code); + return null; } /** - * Parses the specified HTTP status line into a {@link HttpResponseStatus}. The expected formats of the line are: + * Returns the {@link HttpResponseStatus} represented by the specified {@code code} and {@code reasonPhrase}. + * If the specified code is a standard HTTP status {@code code} and {@code reasonPhrase}, a cached instance + * will be returned. Otherwise, a new instance will be returned. + * @param code The response code value. + * @param reasonPhrase The response code reason phrase. + * @return the {@link HttpResponseStatus} represented by the specified {@code code} and {@code reasonPhrase}. + */ + public static HttpResponseStatus valueOf(int code, String reasonPhrase) { + HttpResponseStatus responseStatus = valueOf0(code); + return responseStatus != null && responseStatus.reasonPhrase().contentEquals(reasonPhrase) ? responseStatus : + new HttpResponseStatus(code, reasonPhrase); + } + + /** + * Parses the specified HTTP status line into a {@link HttpResponseStatus}. The expected formats of the line are: *