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    *   https://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;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.util.ByteProcessor;
21  
22  import java.util.List;
23  
24  /**
25   * A decoder that splits the received {@link ByteBuf}s on line endings.
26   * <p>
27   * Both {@code "\n"} and {@code "\r\n"} are handled.
28   * <p>
29   * The byte stream is expected to be in UTF-8 character encoding or ASCII. The current implementation
30   * uses direct {@code byte} to {@code char} cast and then compares that {@code char} to a few low range
31   * ASCII characters like {@code '\n'} or {@code '\r'}. UTF-8 is not using low range [0..0x7F]
32   * byte values for multibyte codepoint representations therefore fully supported by this implementation.
33   * <p>
34   * For a more general delimiter-based decoder, see {@link DelimiterBasedFrameDecoder}.
35   */
36  public class LineBasedFrameDecoder extends ByteToMessageDecoder {
37  
38      /** Maximum length of a frame we're willing to decode.  */
39      private final int maxLength;
40      /** Whether or not to throw an exception as soon as we exceed maxLength. */
41      private final boolean failFast;
42      private final boolean stripDelimiter;
43  
44      /** True if we're discarding input because we're already over maxLength.  */
45      private boolean discarding;
46      private int discardedBytes;
47  
48      /** Last scan position. */
49      private int offset;
50  
51      /**
52       * Creates a new decoder.
53       * @param maxLength  the maximum length of the decoded frame.
54       *                   A {@link TooLongFrameException} is thrown if
55       *                   the length of the frame exceeds this value.
56       */
57      public LineBasedFrameDecoder(final int maxLength) {
58          this(maxLength, true, false);
59      }
60  
61      /**
62       * Creates a new decoder.
63       * @param maxLength  the maximum length of the decoded frame.
64       *                   A {@link TooLongFrameException} is thrown if
65       *                   the length of the frame exceeds this value.
66       * @param stripDelimiter  whether the decoded frame should strip out the
67       *                        delimiter or not
68       * @param failFast  If <tt>true</tt>, a {@link TooLongFrameException} is
69       *                  thrown as soon as the decoder notices the length of the
70       *                  frame will exceed <tt>maxFrameLength</tt> regardless of
71       *                  whether the entire frame has been read.
72       *                  If <tt>false</tt>, a {@link TooLongFrameException} is
73       *                  thrown after the entire frame that exceeds
74       *                  <tt>maxFrameLength</tt> has been read.
75       */
76      public LineBasedFrameDecoder(final int maxLength, final boolean stripDelimiter, final boolean failFast) {
77          this.maxLength = maxLength;
78          this.failFast = failFast;
79          this.stripDelimiter = stripDelimiter;
80      }
81  
82      @Override
83      protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
84          Object decoded = decode(ctx, in);
85          if (decoded != null) {
86              out.add(decoded);
87          }
88      }
89  
90      /**
91       * Create a frame out of the {@link ByteBuf} and return it.
92       *
93       * @param   ctx             the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
94       * @param   buffer          the {@link ByteBuf} from which to read data
95       * @return  frame           the {@link ByteBuf} which represent the frame or {@code null} if no frame could
96       *                          be created.
97       */
98      protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
99          final int eol = findEndOfLine(buffer);
100         if (!discarding) {
101             if (eol >= 0) {
102                 final ByteBuf frame;
103                 final int length = eol - buffer.readerIndex();
104                 final int delimLength = buffer.getByte(eol) == '\r'? 2 : 1;
105 
106                 if (length > maxLength) {
107                     buffer.readerIndex(eol + delimLength);
108                     fail(ctx, length);
109                     return null;
110                 }
111 
112                 if (stripDelimiter) {
113                     frame = buffer.readRetainedSlice(length);
114                     buffer.skipBytes(delimLength);
115                 } else {
116                     frame = buffer.readRetainedSlice(length + delimLength);
117                 }
118 
119                 return frame;
120             } else {
121                 final int length = buffer.readableBytes();
122                 if (length > maxLength) {
123                     discardedBytes = length;
124                     buffer.readerIndex(buffer.writerIndex());
125                     discarding = true;
126                     offset = 0;
127                     if (failFast) {
128                         fail(ctx, "over " + discardedBytes);
129                     }
130                 }
131                 return null;
132             }
133         } else {
134             if (eol >= 0) {
135                 final int length = discardedBytes + eol - buffer.readerIndex();
136                 final int delimLength = buffer.getByte(eol) == '\r'? 2 : 1;
137                 buffer.readerIndex(eol + delimLength);
138                 discardedBytes = 0;
139                 discarding = false;
140                 if (!failFast) {
141                     fail(ctx, length);
142                 }
143             } else {
144                 discardedBytes += buffer.readableBytes();
145                 buffer.readerIndex(buffer.writerIndex());
146                 // We skip everything in the buffer, we need to set the offset to 0 again.
147                 offset = 0;
148             }
149             return null;
150         }
151     }
152 
153     private void fail(final ChannelHandlerContext ctx, int length) {
154         fail(ctx, String.valueOf(length));
155     }
156 
157     private void fail(final ChannelHandlerContext ctx, String length) {
158         ctx.fireExceptionCaught(
159                 new TooLongFrameException(
160                         "frame length (" + length + ") exceeds the allowed maximum (" + maxLength + ')'));
161     }
162 
163     /**
164      * Returns the index in the buffer of the end of line found.
165      * Returns -1 if no end of line was found in the buffer.
166      */
167     private int findEndOfLine(final ByteBuf buffer) {
168         int totalLength = buffer.readableBytes();
169         int i = buffer.forEachByte(buffer.readerIndex() + offset, totalLength - offset, ByteProcessor.FIND_LF);
170         if (i >= 0) {
171             offset = 0;
172             if (i > 0 && buffer.getByte(i - 1) == '\r') {
173                 i--;
174             }
175         } else {
176             offset = totalLength;
177         }
178         return i;
179     }
180 }