View Javadoc
1   /*
2    * Copyright 2014 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  
17  package io.netty.handler.codec;
18  
19  
20  import java.util.Map.Entry;
21  
22  import io.netty.buffer.ByteBuf;
23  import io.netty.buffer.ByteBufUtil;
24  import io.netty.util.AsciiString;
25  import io.netty.util.CharsetUtil;
26  import io.netty.util.internal.ObjectUtil;
27  
28  public final class AsciiHeadersEncoder {
29  
30      /**
31       * The separator characters to insert between a header name and a header value.
32       */
33      public enum SeparatorType {
34          /**
35           * {@code ':'}
36           */
37          COLON,
38          /**
39           * {@code ': '}
40           */
41          COLON_SPACE,
42      }
43  
44      /**
45       * The newline characters to insert between header entries.
46       */
47      public enum NewlineType {
48          /**
49           * {@code '\n'}
50           */
51          LF,
52          /**
53           * {@code '\r\n'}
54           */
55          CRLF
56      }
57  
58      private final ByteBuf buf;
59      private final SeparatorType separatorType;
60      private final NewlineType newlineType;
61  
62      public AsciiHeadersEncoder(ByteBuf buf) {
63          this(buf, SeparatorType.COLON_SPACE, NewlineType.CRLF);
64      }
65  
66      public AsciiHeadersEncoder(ByteBuf buf, SeparatorType separatorType, NewlineType newlineType) {
67          this.buf = ObjectUtil.checkNotNull(buf, "buf");
68          this.separatorType = ObjectUtil.checkNotNull(separatorType, "separatorType");
69          this.newlineType = ObjectUtil.checkNotNull(newlineType, "newlineType");
70      }
71  
72      public void encode(Entry<CharSequence, CharSequence> entry) {
73          final CharSequence name = entry.getKey();
74          final CharSequence value = entry.getValue();
75          final ByteBuf buf = this.buf;
76          final int nameLen = name.length();
77          final int valueLen = value.length();
78          final int entryLen = nameLen + valueLen + 4;
79          int offset = buf.writerIndex();
80          buf.ensureWritable(entryLen);
81          writeAscii(buf, offset, name);
82          offset += nameLen;
83  
84          switch (separatorType) {
85              case COLON:
86                  buf.setByte(offset ++, ':');
87                  break;
88              case COLON_SPACE:
89                  buf.setByte(offset ++, ':');
90                  buf.setByte(offset ++, ' ');
91                  break;
92              default:
93                  throw new Error();
94          }
95  
96          writeAscii(buf, offset, value);
97          offset += valueLen;
98  
99          switch (newlineType) {
100             case LF:
101                 buf.setByte(offset ++, '\n');
102                 break;
103             case CRLF:
104                 buf.setByte(offset ++, '\r');
105                 buf.setByte(offset ++, '\n');
106                 break;
107             default:
108                 throw new Error();
109         }
110 
111         buf.writerIndex(offset);
112     }
113 
114     private static void writeAscii(ByteBuf buf, int offset, CharSequence value) {
115         if (value instanceof AsciiString) {
116             ByteBufUtil.copy((AsciiString) value, 0, buf, offset, value.length());
117         } else {
118             buf.setCharSequence(offset, value, CharsetUtil.US_ASCII);
119         }
120     }
121 }