1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.filter.codec.statemachine;
21
22 import org.apache.mina.core.buffer.IoBuffer;
23 import org.apache.mina.filter.codec.ProtocolDecoderException;
24 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
25
26 /**
27 * {@link DecodingState} which decodes a single <code>CRLF</code>.
28 * If it is found, the bytes are consumed and <tt>true</tt>
29 * is provided as the product. Otherwise, read bytes are pushed back
30 * to the stream, and <tt>false</tt> is provided as the
31 * product.
32 * Note that if we find a CR but do not find a following LF, we raise
33 * an error.
34 *
35 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36 */
37 public abstract class CrLfDecodingState implements DecodingState {
38 /**
39 * Carriage return character
40 */
41 private static final byte CR = 13;
42
43 /**
44 * Line feed character
45 */
46 private static final byte LF = 10;
47
48 private boolean hasCR;
49
50 /**
51 * {@inheritDoc}
52 */
53 @Override
54 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
55 boolean found = false;
56 boolean finished = false;
57 while (in.hasRemaining()) {
58 byte b = in.get();
59 if (!hasCR) {
60 if (b == CR) {
61 hasCR = true;
62 } else {
63 if (b == LF) {
64 found = true;
65 } else {
66 in.position(in.position() - 1);
67 found = false;
68 }
69 finished = true;
70 break;
71 }
72 } else {
73 if (b == LF) {
74 found = true;
75 finished = true;
76 break;
77 }
78
79 throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff));
80 }
81 }
82
83 if (finished) {
84 hasCR = false;
85 return finishDecode(found, out);
86 }
87
88 return this;
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 @Override
95 public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
96 return finishDecode(false, out);
97 }
98
99 /**
100 * Invoked when this state has found a <code>CRLF</code>.
101 *
102 * @param foundCRLF <tt>true</tt> if <code>CRLF</code> was found.
103 * @param out the current {@link ProtocolDecoderOutput} used to write
104 * decoded messages.
105 * @return the next state if a state transition was triggered (use
106 * <code>this</code> for loop transitions) or <code>null</code> if
107 * the state machine has reached its end.
108 * @throws Exception if the read data violated protocol specification.
109 */
110 protected abstract DecodingState finishDecode(boolean foundCRLF, ProtocolDecoderOutput out) throws Exception;
111 }