View Javadoc
1   /*
2    * Copyright 2013 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.spdy;
17  
18  import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_MAX_NV_LENGTH;
19  import io.netty.buffer.ByteBuf;
20  import io.netty.buffer.ByteBufAllocator;
21  import io.netty.buffer.Unpooled;
22  import io.netty.handler.codec.AsciiString;
23  import io.netty.util.CharsetUtil;
24  
25  import java.util.Set;
26  
27  public class SpdyHeaderBlockRawEncoder extends SpdyHeaderBlockEncoder {
28  
29      private final int version;
30  
31      public SpdyHeaderBlockRawEncoder(SpdyVersion version) {
32          if (version == null) {
33              throw new NullPointerException("version");
34          }
35          this.version = version.getVersion();
36      }
37  
38      private static void setLengthField(ByteBuf buffer, int writerIndex, int length) {
39          buffer.setInt(writerIndex, length);
40      }
41  
42      private static void writeLengthField(ByteBuf buffer, int length) {
43          buffer.writeInt(length);
44      }
45  
46      @Override
47      public ByteBuf encode(ByteBufAllocator alloc, SpdyHeadersFrame frame) throws Exception {
48          Set<CharSequence> names = frame.headers().names();
49          int numHeaders = names.size();
50          if (numHeaders == 0) {
51              return Unpooled.EMPTY_BUFFER;
52          }
53          if (numHeaders > SPDY_MAX_NV_LENGTH) {
54              throw new IllegalArgumentException(
55                      "header block contains too many headers");
56          }
57          ByteBuf headerBlock = alloc.heapBuffer();
58          writeLengthField(headerBlock, numHeaders);
59          for (CharSequence name: names) {
60              byte[] nameBytes = AsciiString.getBytes(name, CharsetUtil.UTF_8);
61              writeLengthField(headerBlock, nameBytes.length);
62              headerBlock.writeBytes(nameBytes);
63              int savedIndex = headerBlock.writerIndex();
64              int valueLength = 0;
65              writeLengthField(headerBlock, valueLength);
66              for (CharSequence value: frame.headers().getAll(name)) {
67                  byte[] valueBytes = AsciiString.getBytes(value, CharsetUtil.UTF_8);
68                  if (valueBytes.length > 0) {
69                      headerBlock.writeBytes(valueBytes);
70                      headerBlock.writeByte(0);
71                      valueLength += valueBytes.length + 1;
72                  }
73              }
74              if (valueLength != 0) {
75                  valueLength --;
76              }
77              if (valueLength > SPDY_MAX_NV_LENGTH) {
78                  throw new IllegalArgumentException(
79                          "header exceeds allowable length: " + name);
80              }
81              if (valueLength > 0) {
82                  setLengthField(headerBlock, savedIndex, valueLength);
83                  headerBlock.writerIndex(headerBlock.writerIndex() - 1);
84              }
85          }
86          return headerBlock;
87      }
88  
89      @Override
90      void end() {
91      }
92  }