View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.rtsp;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.handler.codec.TooLongFrameException;
20  import io.netty.handler.codec.http.DefaultFullHttpResponse;
21  import io.netty.handler.codec.http.DefaultHttpResponse;
22  import io.netty.handler.codec.http.HttpMessage;
23  import io.netty.handler.codec.http.HttpResponse;
24  import io.netty.handler.codec.http.HttpResponseStatus;
25  
26  /**
27   * Decodes {@link ByteBuf}s into RTSP responses represented in
28   * {@link HttpResponse}s.
29   * <p>
30   * <h3>Parameters that prevents excessive memory consumption</h3>
31   * <table border="1">
32   * <tr>
33   * <th>Name</th><th>Meaning</th>
34   * </tr>
35   * <tr>
36   * <td>{@code maxInitialLineLength}</td>
37   * <td>The maximum length of the initial line (e.g. {@code "RTSP/1.0 200 OK"})
38   *     If the length of the initial line exceeds this value, a
39   *     {@link TooLongFrameException} will be raised.</td>
40   * </tr>
41   * <tr>
42   * <td>{@code maxHeaderSize}</td>
43   * <td>The maximum length of all headers.  If the sum of the length of each
44   *     header exceeds this value, a {@link TooLongFrameException} will be raised.</td>
45   * </tr>
46   * <tr>
47   * <td>{@code maxContentLength}</td>
48   * <td>The maximum length of the content.  If the content length exceeds this
49   *     value, a {@link TooLongFrameException} will be raised.</td>
50   * </tr>
51   * </table>
52   */
53  public class RtspResponseDecoder extends RtspObjectDecoder {
54  
55      private static final HttpResponseStatus UNKNOWN_STATUS = new HttpResponseStatus(999, "Unknown");
56  
57      /**
58       * Creates a new instance with the default
59       * {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
60       * {@code maxContentLength (8192)}.
61       */
62      public RtspResponseDecoder() {
63      }
64  
65      /**
66       * Creates a new instance with the specified parameters.
67       */
68      public RtspResponseDecoder(int maxInitialLineLength, int maxHeaderSize,
69              int maxContentLength) {
70          super(maxInitialLineLength, maxHeaderSize, maxContentLength);
71      }
72  
73      public RtspResponseDecoder(int maxInitialLineLength, int maxHeaderSize,
74                                 int maxContentLength, boolean validateHeaders) {
75          super(maxInitialLineLength, maxHeaderSize, maxContentLength, validateHeaders);
76      }
77  
78      @Override
79      protected HttpMessage createMessage(String[] initialLine) throws Exception {
80          return new DefaultHttpResponse(
81                  RtspVersions.valueOf(initialLine[0]),
82                  new HttpResponseStatus(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders);
83      }
84  
85      @Override
86      protected HttpMessage createInvalidMessage() {
87          return new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, UNKNOWN_STATUS, validateHeaders);
88      }
89  
90      @Override
91      protected boolean isDecodingRequest() {
92          return false;
93      }
94  }