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 io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.buffer.ByteBufUtil;
21  import io.netty.buffer.Unpooled;
22  
23  import java.util.Set;
24  
25  import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_MAX_NV_LENGTH;
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              writeLengthField(headerBlock, name.length());
61              ByteBufUtil.writeAscii(headerBlock, name);
62              int savedIndex = headerBlock.writerIndex();
63              int valueLength = 0;
64              writeLengthField(headerBlock, valueLength);
65              for (CharSequence value: frame.headers().getAll(name)) {
66                  int length = value.length();
67                  if (length > 0) {
68                      ByteBufUtil.writeAscii(headerBlock, value);
69                      headerBlock.writeByte(0);
70                      valueLength += length + 1;
71                  }
72              }
73              if (valueLength != 0) {
74                  valueLength --;
75              }
76              if (valueLength > SPDY_MAX_NV_LENGTH) {
77                  throw new IllegalArgumentException(
78                          "header exceeds allowable length: " + name);
79              }
80              if (valueLength > 0) {
81                  setLengthField(headerBlock, savedIndex, valueLength);
82                  headerBlock.writerIndex(headerBlock.writerIndex() - 1);
83              }
84          }
85          return headerBlock;
86      }
87  
88      @Override
89      void end() {
90      }
91  }