From e121c68e0fb633b1fceca24ad0f5b04d14879990 Mon Sep 17 00:00:00 2001 From: Sverker Abrahamsson Date: Sat, 23 May 2015 01:34:21 +0200 Subject: [PATCH] Created RTSPEncoder and RTSPDecoder which are now common for both requests and responses to be able to handle both types of messages on the same channel. Keep RTSPRequestEncoder, RTSPRequestDecoder, RTSPResponseEncoder and RTSPResponseDecoder for backwards compatibility but they now just extends the generic encoder/decoder and are markes as deprecated. Renamed the decoder test, because the decoder is now generic. Added testcase for when ANNOUNCE request is received from server. Created testcases for encoder. Mark abstract base classes RTSPObjectEncoder and RTSPObjectDecoder as deprecated, that functionality is now in RTSPEncoder and RTSPDecoder. Added annotation in RtspHeaders to suppress warnings about deprecation, no need when whole class is deprecated. --- .../netty/handler/codec/rtsp/RtspDecoder.java | 173 ++++++++++++++++++ .../netty/handler/codec/rtsp/RtspEncoder.java | 75 ++++++++ .../netty/handler/codec/rtsp/RtspHeaders.java | 1 + .../handler/codec/rtsp/RtspObjectDecoder.java | 3 + .../handler/codec/rtsp/RtspObjectEncoder.java | 3 + .../codec/rtsp/RtspRequestDecoder.java | 72 +------- .../codec/rtsp/RtspRequestEncoder.java | 32 +--- .../codec/rtsp/RtspResponseDecoder.java | 77 +------- .../codec/rtsp/RtspResponseEncoder.java | 31 +--- .../handler/codec/rtsp/RtspDecoderTest.java | 73 ++++++++ .../handler/codec/rtsp/RtspEncoderTest.java | 173 ++++++++++++++++++ 11 files changed, 513 insertions(+), 200 deletions(-) create mode 100644 codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspDecoder.java create mode 100644 codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspEncoder.java create mode 100644 codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspDecoderTest.java create mode 100644 codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspEncoderTest.java diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspDecoder.java new file mode 100644 index 0000000000..b98c8115a6 --- /dev/null +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspDecoder.java @@ -0,0 +1,173 @@ +/* + * Copyright 2015 The Netty Project + * + * The Netty Project 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. + */ +package io.netty.handler.codec.rtsp; + +import java.util.regex.Pattern; + +import io.netty.handler.codec.http.DefaultFullHttpRequest; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.DefaultHttpRequest; +import io.netty.handler.codec.http.DefaultHttpResponse; +import io.netty.handler.codec.http.HttpMessage; +import io.netty.handler.codec.http.HttpObjectDecoder; +import io.netty.handler.codec.http.HttpResponseStatus; + +/** + * Decodes {@link ByteBuf}s into RTSP messages represented in + * {@link HttpMessage}s. + *

+ *

Parameters that prevents excessive memory consumption

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
NameMeaning
{@code maxInitialLineLength}The maximum length of the initial line + * (e.g. {@code "SETUP / RTSP/1.0"} or {@code "RTSP/1.0 200 OK"}) + * If the length of the initial line exceeds this value, a + * {@link TooLongFrameException} will be raised.
{@code maxHeaderSize}The maximum length of all headers. If the sum of the length of each + * header exceeds this value, a {@link TooLongFrameException} will be + * raised.
{@code maxContentLength}The maximum length of the content. If the content length exceeds this + * value, a {@link TooLongFrameException} will be raised.
+ */ +public class RtspDecoder extends HttpObjectDecoder { + /** + * Status code for unknown responses. + */ + private static final HttpResponseStatus UNKNOWN_STATUS = + new HttpResponseStatus(999, "Unknown"); + /** + * True if the message to decode is a request. + * False if the message to decode is a response. + */ + private boolean isDecodingRequest; + + /** + * Regex used on first line in message to detect if it is a response. + */ + private static final Pattern versionPattern = Pattern.compile("RTSP/\\d\\.\\d"); + + /** + * Constant for default max initial line length. + */ + public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096; + + /** + * Constant for default max header size. + */ + public static final int DEFAULT_MAX_HEADER_SIZE = 8192; + + /** + * Constant for default max content length. + */ + public static final int DEFAULT_MAX_CONTENT_LENGTH = 8192; + + /** + * Creates a new instance with the default + * {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and + * {@code maxContentLength (8192)}. + */ + public RtspDecoder() { + this(DEFAULT_MAX_INITIAL_LINE_LENGTH, + DEFAULT_MAX_HEADER_SIZE, + DEFAULT_MAX_CONTENT_LENGTH); + } + + /** + * Creates a new instance with the specified parameters. + * @param maxInitialLineLength The max allowed length of initial line + * @param maxHeaderSize The max allowed size of header + * @param maxContentLength The max allowed content length + */ + public RtspDecoder(final int maxInitialLineLength, + final int maxHeaderSize, + final int maxContentLength) { + super(maxInitialLineLength, maxHeaderSize, maxContentLength * 2, false); + } + + /** + * Creates a new instance with the specified parameters. + * @param maxInitialLineLength The max allowed length of initial line + * @param maxHeaderSize The max allowed size of header + * @param maxContentLength The max allowed content length + * @param validateHeaders Set to true if headers should be validated + */ + public RtspDecoder(final int maxInitialLineLength, + final int maxHeaderSize, + final int maxContentLength, + final boolean validateHeaders) { + super(maxInitialLineLength, + maxHeaderSize, + maxContentLength * 2, + false, + validateHeaders); + } + + @Override + protected HttpMessage createMessage(final String[] initialLine) + throws Exception { + // If the first element of the initial line is a version string then + // this is a response + if (versionPattern.matcher(initialLine[0]).matches()) { + isDecodingRequest = false; + return new DefaultHttpResponse(RtspVersions.valueOf(initialLine[0]), + new HttpResponseStatus(Integer.parseInt(initialLine[1]), + initialLine[2]), + validateHeaders); + } else { + isDecodingRequest = true; + return new DefaultHttpRequest(RtspVersions.valueOf(initialLine[2]), + RtspMethods.valueOf(initialLine[0]), + initialLine[1], + validateHeaders); + } + } + + @Override + protected boolean isContentAlwaysEmpty(final HttpMessage msg) { + // Unlike HTTP, RTSP always assumes zero-length body if Content-Length + // header is absent. + return super.isContentAlwaysEmpty(msg) || !msg.headers().contains(RtspHeaderNames.CONTENT_LENGTH); + } + + @Override + protected HttpMessage createInvalidMessage() { + if (isDecodingRequest) { + return new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, + RtspMethods.OPTIONS, "/bad-request", validateHeaders); + } else { + return new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, + UNKNOWN_STATUS, + validateHeaders); + } + } + + @Override + protected boolean isDecodingRequest() { + return isDecodingRequest; + } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspEncoder.java new file mode 100644 index 0000000000..f8abe88fec --- /dev/null +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspEncoder.java @@ -0,0 +1,75 @@ +/* + * Copyright 2015 The Netty Project + * + * The Netty Project 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. + */ +package io.netty.handler.codec.rtsp; + +import static io.netty.handler.codec.http.HttpConstants.CR; +import static io.netty.handler.codec.http.HttpConstants.LF; +import static io.netty.handler.codec.http.HttpConstants.SP; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.UnsupportedMessageTypeException; +import io.netty.handler.codec.http.HttpContent; +import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpMessage; +import io.netty.handler.codec.http.HttpObjectEncoder; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.util.CharsetUtil; +import io.netty.util.internal.StringUtil; + +/** + * Encodes an RTSP message represented in {@link HttpMessage} or an {@link HttpContent} into + * a {@link ByteBuf}. + */ +public class RtspEncoder extends HttpObjectEncoder { + /** + * Constant for CRLF. + */ + private static final byte[] CRLF = {CR, LF}; + + @Override + public boolean acceptOutboundMessage(final Object msg) + throws Exception { + return super.acceptOutboundMessage(msg) && ((msg instanceof HttpRequest) || (msg instanceof HttpResponse)); + } + + @Override + protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message) + throws Exception { + if (message instanceof HttpRequest) { + HttpRequest request = (HttpRequest) message; + HttpHeaders.encodeAscii(request.method().toString(), buf); + buf.writeByte(SP); + buf.writeBytes(request.uri().getBytes(CharsetUtil.UTF_8)); + buf.writeByte(SP); + HttpHeaders.encodeAscii(request.protocolVersion().toString(), buf); + buf.writeBytes(CRLF); + } else if (message instanceof HttpResponse) { + HttpResponse response = (HttpResponse) message; + HttpHeaders.encodeAscii(response.protocolVersion().toString(), + buf); + buf.writeByte(SP); + buf.writeBytes(String.valueOf(response.status().code()) + .getBytes(CharsetUtil.US_ASCII)); + buf.writeByte(SP); + HttpHeaders.encodeAscii(String.valueOf(response.status().reasonPhrase()), + buf); + buf.writeBytes(CRLF); + } else { + throw new UnsupportedMessageTypeException("Unsupported type " + + StringUtil.simpleClassName(message)); + } + } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java index 6383705c29..780b376388 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspHeaders.java @@ -24,6 +24,7 @@ import io.netty.handler.codec.http.HttpHeaders; * Standard RTSP header names and values. */ @Deprecated +@SuppressWarnings("deprecation") public final class RtspHeaders { /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java index e73320d297..e52c0ce51e 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectDecoder.java @@ -47,7 +47,10 @@ import io.netty.handler.codec.http.HttpObjectDecoder; * value, a {@link TooLongFrameException} will be raised. * * + * + * @deprecated Use {@link RtspDecoder} instead. */ +@Deprecated public abstract class RtspObjectDecoder extends HttpObjectDecoder { /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectEncoder.java index 0dba3f0b71..030bde66b8 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspObjectEncoder.java @@ -24,8 +24,11 @@ import io.netty.handler.codec.http.HttpObjectEncoder; /** * Encodes an RTSP message represented in {@link FullHttpMessage} into * a {@link ByteBuf}. + * + * @deprecated Use {@link RtspEncoder} instead. */ @Sharable +@Deprecated public abstract class RtspObjectEncoder extends HttpObjectEncoder { /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestDecoder.java index 6be4d403a6..8d22e6d9aa 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestDecoder.java @@ -15,75 +15,9 @@ */ package io.netty.handler.codec.rtsp; -import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.TooLongFrameException; -import io.netty.handler.codec.http.DefaultFullHttpRequest; -import io.netty.handler.codec.http.DefaultHttpRequest; -import io.netty.handler.codec.http.HttpMessage; -import io.netty.handler.codec.http.HttpRequest; - /** - * Decodes {@link ByteBuf}s into RTSP requests represented in - * {@link HttpRequest}s. - *

- *

Parameters that prevents excessive memory consumption

- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
NameMeaning
{@code maxInitialLineLength}The maximum length of the initial line (e.g. {@code "SETUP / RTSP/1.0"}) - * If the length of the initial line exceeds this value, a - * {@link TooLongFrameException} will be raised.
{@code maxHeaderSize}The maximum length of all headers. If the sum of the length of each - * header exceeds this value, a {@link TooLongFrameException} will be raised.
{@code maxContentLength}The maximum length of the content. If the content length exceeds this - * value, a {@link TooLongFrameException} will be raised.
+ * @deprecated Use {@link RtspDecoder} directly instead */ -public class RtspRequestDecoder extends RtspObjectDecoder { - - /** - * Creates a new instance with the default - * {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and - * {@code maxContentLength (8192)}. - */ - public RtspRequestDecoder() { - } - - /** - * Creates a new instance with the specified parameters. - */ - public RtspRequestDecoder(int maxInitialLineLength, int maxHeaderSize, int maxContentLength) { - super(maxInitialLineLength, maxHeaderSize, maxContentLength); - } - - public RtspRequestDecoder( - int maxInitialLineLength, int maxHeaderSize, int maxContentLength, boolean validateHeaders) { - super(maxInitialLineLength, maxHeaderSize, maxContentLength, validateHeaders); - } - - @Override - protected HttpMessage createMessage(String[] initialLine) throws Exception { - return new DefaultHttpRequest(RtspVersions.valueOf(initialLine[2]), - RtspMethods.valueOf(initialLine[0]), initialLine[1], validateHeaders); - } - - @Override - protected HttpMessage createInvalidMessage() { - return new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.OPTIONS, "/bad-request", validateHeaders); - } - - @Override - protected boolean isDecodingRequest() { - return true; - } +@Deprecated +public class RtspRequestDecoder extends RtspDecoder { } diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestEncoder.java index da13aa89d9..d37336d2ac 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspRequestEncoder.java @@ -15,35 +15,9 @@ */ package io.netty.handler.codec.rtsp; -import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpRequest; -import io.netty.util.CharsetUtil; - -import static io.netty.handler.codec.http.HttpConstants.*; - /** - * Encodes an RTSP request represented in {@link FullHttpRequest} into - * a {@link ByteBuf}. - + * @deprecated Use {@link RtspEncoder} directly instead */ -public class RtspRequestEncoder extends RtspObjectEncoder { - private static final byte[] CRLF = { CR, LF }; - - @Override - public boolean acceptOutboundMessage(Object msg) throws Exception { - return msg instanceof FullHttpRequest; - } - - @Override - @SuppressWarnings("deprecation") - protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception { - HttpHeaders.encodeAscii(request.method().toString(), buf); - buf.writeByte(SP); - buf.writeBytes(request.uri().getBytes(CharsetUtil.UTF_8)); - buf.writeByte(SP); - HttpHeaders.encodeAscii(request.protocolVersion().toString(), buf); - buf.writeBytes(CRLF); - } +@Deprecated +public class RtspRequestEncoder extends RtspEncoder { } diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseDecoder.java index 0234c6773b..9073d86d2c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseDecoder.java @@ -15,80 +15,9 @@ */ package io.netty.handler.codec.rtsp; -import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.TooLongFrameException; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.DefaultHttpResponse; -import io.netty.handler.codec.http.HttpMessage; -import io.netty.handler.codec.http.HttpResponse; -import io.netty.handler.codec.http.HttpResponseStatus; - /** - * Decodes {@link ByteBuf}s into RTSP responses represented in - * {@link HttpResponse}s. - *

- *

Parameters that prevents excessive memory consumption

- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
NameMeaning
{@code maxInitialLineLength}The maximum length of the initial line (e.g. {@code "RTSP/1.0 200 OK"}) - * If the length of the initial line exceeds this value, a - * {@link TooLongFrameException} will be raised.
{@code maxHeaderSize}The maximum length of all headers. If the sum of the length of each - * header exceeds this value, a {@link TooLongFrameException} will be raised.
{@code maxContentLength}The maximum length of the content. If the content length exceeds this - * value, a {@link TooLongFrameException} will be raised.
+ * @deprecated Use {@link RtspDecoder} directly instead */ -public class RtspResponseDecoder extends RtspObjectDecoder { - - private static final HttpResponseStatus UNKNOWN_STATUS = new HttpResponseStatus(999, "Unknown"); - - /** - * Creates a new instance with the default - * {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and - * {@code maxContentLength (8192)}. - */ - public RtspResponseDecoder() { - } - - /** - * Creates a new instance with the specified parameters. - */ - public RtspResponseDecoder(int maxInitialLineLength, int maxHeaderSize, - int maxContentLength) { - super(maxInitialLineLength, maxHeaderSize, maxContentLength); - } - - public RtspResponseDecoder(int maxInitialLineLength, int maxHeaderSize, - int maxContentLength, boolean validateHeaders) { - super(maxInitialLineLength, maxHeaderSize, maxContentLength, validateHeaders); - } - - @Override - protected HttpMessage createMessage(String[] initialLine) throws Exception { - return new DefaultHttpResponse( - RtspVersions.valueOf(initialLine[0]), - new HttpResponseStatus(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders); - } - - @Override - protected HttpMessage createInvalidMessage() { - return new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, UNKNOWN_STATUS, validateHeaders); - } - - @Override - protected boolean isDecodingRequest() { - return false; - } +@Deprecated +public class RtspResponseDecoder extends RtspDecoder { } diff --git a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseEncoder.java index 53968e405c..1ffa74567f 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspResponseEncoder.java @@ -15,34 +15,9 @@ */ package io.netty.handler.codec.rtsp; -import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.HttpResponse; -import io.netty.util.CharsetUtil; - -import static io.netty.handler.codec.http.HttpConstants.*; - /** - * Encodes an RTSP response represented in {@link FullHttpResponse} into - * a {@link ByteBuf}. + * @deprecated Use {@link RtspEncoder} directly instead */ -public class RtspResponseEncoder extends RtspObjectEncoder { - private static final byte[] CRLF = { CR, LF }; - - @Override - public boolean acceptOutboundMessage(Object msg) throws Exception { - return msg instanceof FullHttpResponse; - } - - @Override - @SuppressWarnings("deprecation") - protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception { - HttpHeaders.encodeAscii(response.protocolVersion().toString(), buf); - buf.writeByte(SP); - buf.writeBytes(String.valueOf(response.status().code()).getBytes(CharsetUtil.US_ASCII)); - buf.writeByte(SP); - HttpHeaders.encodeAscii(String.valueOf(response.status().reasonPhrase()), buf); - buf.writeBytes(CRLF); - } +@Deprecated +public class RtspResponseEncoder extends RtspEncoder { } diff --git a/codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspDecoderTest.java new file mode 100644 index 0000000000..d416720a25 --- /dev/null +++ b/codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspDecoderTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2015 The Netty Project + * + * The Netty Project 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. + */ +package io.netty.handler.codec.rtsp; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import io.netty.buffer.Unpooled; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpObject; +import io.netty.handler.codec.http.HttpObjectAggregator; + +import org.junit.Test; + +/** + * Test cases for RTSP decoder. + */ +public class RtspDecoderTest { + + /** + * There was a problem when an ANNOUNCE request was issued by the server, + * i.e. entered through the response decoder. First the decoder failed to + * parse the ANNOUNCE request, then it stopped receiving any more + * responses. This test verifies that the issue is solved. + */ + @Test + public void testReceiveAnnounce() { + byte[] data1 = ("ANNOUNCE rtsp://172.20.184.218:554/d3abaaa7-65f2-" + + "42b4-8d6b-379f492fcf0f RTSP/1.0\r\n" + + "CSeq: 2\r\n" + + "Session: 2777476816092819869\r\n" + + "x-notice: 5402 \"Session Terminated by Server\" " + + "event-date=20150514T075303Z\r\n" + + "Range: npt=0\r\n\r\n").getBytes(); + + byte[] data2 = ("RTSP/1.0 200 OK\r\n" + + "Server: Orbit2x\r\n" + + "CSeq: 172\r\n" + + "Session: 2547019973447939919\r\n" + + "\r\n").getBytes(); + + EmbeddedChannel ch = new EmbeddedChannel(new RtspDecoder(), + new HttpObjectAggregator(1048576)); + ch.writeInbound(Unpooled.wrappedBuffer(data1), + Unpooled.wrappedBuffer(data2)); + + HttpObject res1 = ch.readInbound(); + System.out.println(res1); + assertNotNull(res1); + assertTrue(res1 instanceof FullHttpRequest); + ((FullHttpRequest) res1).release(); + + HttpObject res2 = ch.readInbound(); + System.out.println(res2); + assertNotNull(res2); + assertTrue(res2 instanceof FullHttpResponse); + ((FullHttpResponse) res2).release(); + } +} diff --git a/codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspEncoderTest.java new file mode 100644 index 0000000000..00bf6aad1b --- /dev/null +++ b/codec-http/src/test/java/io/netty/handler/codec/rtsp/RtspEncoderTest.java @@ -0,0 +1,173 @@ +/* + * Copyright 2015 The Netty Project + * + * The Netty Project 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. + */ +package io.netty.handler.codec.rtsp; + +import static org.junit.Assert.assertEquals; +import io.netty.buffer.ByteBuf; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.http.DefaultFullHttpRequest; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.DefaultHttpRequest; +import io.netty.handler.codec.http.DefaultHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.util.CharsetUtil; + +import org.junit.Test; + +/** + * Test cases for RTSP encoder. + */ +public class RtspEncoderTest { + + /** + * Test of a SETUP request, with no body. + */ + @Test + public void testSendSetupRequest() { + String expected = "SETUP rtsp://172.10.20.30:554/d3abaaa7-65f2-42b4-" + + "8d6b-379f492fcf0f RTSP/1.0\r\n" + + "transport: MP2T/DVBC/UDP;unicast;client=01234567;" + + "source=172.10.20.30;" + + "destination=1.1.1.1;client_port=6922\r\n" + + "cseq: 1\r\n" + + "\r\n"; + + HttpRequest request = new DefaultHttpRequest(RtspVersions.RTSP_1_0, + RtspMethods.SETUP, + "rtsp://172.10.20.30:554/d3abaaa7-65f2-42b4-8d6b-379f492fcf0f"); + request.headers().add(RtspHeaderNames.TRANSPORT, + "MP2T/DVBC/UDP;unicast;client=01234567;source=172.10.20.30;" + + "destination=1.1.1.1;client_port=6922"); + request.headers().add(RtspHeaderNames.CSEQ, "1"); + + EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder()); + ch.writeOutbound(request); + + ByteBuf buf = ch.readOutbound(); + String actual = buf.toString(CharsetUtil.UTF_8); + buf.release(); + assertEquals(expected, actual); + } + + /** + * Test of a GET_PARAMETER request, with body. + */ + @Test + public void testSendGetParameterRequest() { + String expected = "GET_PARAMETER rtsp://172.10.20.30:554 RTSP/1.0\r\n" + + "session: 2547019973447939919\r\n" + + "cseq: 3\r\n" + + "content-length: 31\r\n" + + "content-type: text/parameters\r\n" + + "\r\n" + + "stream_state\r\n" + + "position\r\n" + + "scale\r\n"; + + byte[] content = ("stream_state\r\n" + + "position\r\n" + + "scale\r\n").getBytes(CharsetUtil.UTF_8); + + FullHttpRequest request = new DefaultFullHttpRequest( + RtspVersions.RTSP_1_0, + RtspMethods.GET_PARAMETER, + "rtsp://172.10.20.30:554"); + request.headers().add(RtspHeaderNames.SESSION, "2547019973447939919"); + request.headers().add(RtspHeaderNames.CSEQ, "3"); + request.headers().add(RtspHeaderNames.CONTENT_LENGTH, + "" + content.length); + request.headers().add(RtspHeaderNames.CONTENT_TYPE, "text/parameters"); + request.content().writeBytes(content); + + EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder()); + ch.writeOutbound(request); + + ByteBuf buf = ch.readOutbound(); + String actual = buf.toString(CharsetUtil.UTF_8); + buf.release(); + assertEquals(expected, actual); + } + + /** + * Test of a 200 OK response, without body. + */ + @Test + public void testSend200OkResponseWithoutBody() { + String expected = "RTSP/1.0 200 OK\r\n" + + "server: Testserver\r\n" + + "cseq: 1\r\n" + + "session: 2547019973447939919\r\n" + + "\r\n"; + + HttpResponse response = new DefaultHttpResponse(RtspVersions.RTSP_1_0, + RtspResponseStatuses.OK); + response.headers().add(RtspHeaderNames.SERVER, "Testserver"); + response.headers().add(RtspHeaderNames.CSEQ, "1"); + response.headers().add(RtspHeaderNames.SESSION, "2547019973447939919"); + + EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder()); + ch.writeOutbound(response); + + ByteBuf buf = ch.readOutbound(); + String actual = buf.toString(CharsetUtil.UTF_8); + buf.release(); + assertEquals(expected, actual); + } + + /** + * Test of a 200 OK response, with body. + */ + @Test + public void testSend200OkResponseWithBody() { + String expected = "RTSP/1.0 200 OK\r\n" + + "server: Testserver\r\n" + + "session: 2547019973447939919\r\n" + + "content-type: text/parameters\r\n" + + "content-length: 50\r\n" + + "cseq: 3\r\n" + + "\r\n" + + "position: 24\r\n" + + "stream_state: playing\r\n" + + "scale: 1.00\r\n"; + + byte[] content = ("position: 24\r\n" + + "stream_state: playing\r\n" + + "scale: 1.00\r\n").getBytes(CharsetUtil.UTF_8); + + FullHttpResponse response = + new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, + RtspResponseStatuses.OK); + response.headers().add(RtspHeaderNames.SERVER, "Testserver"); + response.headers().add(RtspHeaderNames.SESSION, "2547019973447939919"); + response.headers().add(RtspHeaderNames.CONTENT_TYPE, + "text/parameters"); + response.headers().add(RtspHeaderNames.CONTENT_LENGTH, + "" + content.length); + response.headers().add(RtspHeaderNames.CSEQ, "3"); + response.content().writeBytes(content); + + EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder()); + ch.writeOutbound(response); + + ByteBuf buf = ch.readOutbound(); + String actual = buf.toString(CharsetUtil.UTF_8); + buf.release(); + assertEquals(expected, actual); + } +}