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  package io.netty.handler.codec.spdy;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.util.internal.ObjectUtil;
21  
22  import java.nio.ByteOrder;
23  import java.util.Set;
24  
25  import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
26  
27  /**
28   * Encodes a SPDY Frame into a {@link ByteBuf}.
29   */
30  public class SpdyFrameEncoder {
31  
32      private final int version;
33  
34      /**
35       * Creates a new instance with the specified {@code spdyVersion}.
36       */
37      public SpdyFrameEncoder(SpdyVersion spdyVersion) {
38          version = ObjectUtil.checkNotNull(spdyVersion, "spdyVersion").version();
39      }
40  
41      // The frame length is a 24-bit field: a longer frame would be written with a wrong length.
42      private static int checkLength(int length) {
43          if (length > SPDY_MAX_LENGTH) {
44              throw new IllegalArgumentException(
45                      "frame length: " + length + " (expected: <= " + SPDY_MAX_LENGTH + ')');
46          }
47          return length;
48      }
49  
50      protected void writeControlFrameHeader(ByteBuf buffer, int type, byte flags, int length) {
51          buffer.writeShort(version | 0x8000);
52          buffer.writeShort(type);
53          buffer.writeByte(flags);
54          buffer.writeMedium(length);
55      }
56  
57      public ByteBuf encodeDataFrame(ByteBufAllocator allocator, int streamId, boolean last, ByteBuf data) {
58          byte flags = last ? SPDY_DATA_FLAG_FIN : 0;
59          int length = checkLength(data.readableBytes());
60          ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
61          frame.writeInt(streamId & 0x7FFFFFFF);
62          frame.writeByte(flags);
63          frame.writeMedium(length);
64          frame.writeBytes(data, data.readerIndex(), length);
65          return frame;
66      }
67  
68      public ByteBuf encodeSynStreamFrame(ByteBufAllocator allocator,  int streamId, int associatedToStreamId,
69              byte priority, boolean last, boolean unidirectional, ByteBuf headerBlock) {
70          int headerBlockLength = headerBlock.readableBytes();
71          byte flags = last ? SPDY_FLAG_FIN : 0;
72          if (unidirectional) {
73              flags |= SPDY_FLAG_UNIDIRECTIONAL;
74          }
75          int length = checkLength(10 + headerBlockLength);
76          ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
77          writeControlFrameHeader(frame, SPDY_SYN_STREAM_FRAME, flags, length);
78          frame.writeInt(streamId);
79          frame.writeInt(associatedToStreamId);
80          frame.writeShort((priority & 0xFF) << 13);
81          frame.writeBytes(headerBlock, headerBlock.readerIndex(), headerBlockLength);
82          return frame;
83      }
84  
85      public ByteBuf encodeSynReplyFrame(ByteBufAllocator allocator, int streamId, boolean last, ByteBuf headerBlock) {
86          int headerBlockLength = headerBlock.readableBytes();
87          byte flags = last ? SPDY_FLAG_FIN : 0;
88          int length = checkLength(4 + headerBlockLength);
89          ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
90          writeControlFrameHeader(frame, SPDY_SYN_REPLY_FRAME, flags, length);
91          frame.writeInt(streamId);
92          frame.writeBytes(headerBlock, headerBlock.readerIndex(), headerBlockLength);
93          return frame;
94      }
95  
96      public ByteBuf encodeRstStreamFrame(ByteBufAllocator allocator, int streamId, int statusCode) {
97          byte flags = 0;
98          int length = 8;
99          ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
100         writeControlFrameHeader(frame, SPDY_RST_STREAM_FRAME, flags, length);
101         frame.writeInt(streamId);
102         frame.writeInt(statusCode);
103         return frame;
104     }
105 
106     public ByteBuf encodeSettingsFrame(ByteBufAllocator allocator, SpdySettingsFrame spdySettingsFrame) {
107         Set<Integer> ids = spdySettingsFrame.ids();
108         int numSettings = ids.size();
109 
110         byte flags = spdySettingsFrame.clearPreviouslyPersistedSettings() ?
111                 SPDY_SETTINGS_CLEAR : 0;
112         int length = checkLength(4 + 8 * numSettings);
113         ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
114         writeControlFrameHeader(frame, SPDY_SETTINGS_FRAME, flags, length);
115         frame.writeInt(numSettings);
116         for (Integer id : ids) {
117             flags = 0;
118             if (spdySettingsFrame.isPersistValue(id)) {
119                 flags |= SPDY_SETTINGS_PERSIST_VALUE;
120             }
121             if (spdySettingsFrame.isPersisted(id)) {
122                 flags |= SPDY_SETTINGS_PERSISTED;
123             }
124             frame.writeByte(flags);
125             frame.writeMedium(id);
126             frame.writeInt(spdySettingsFrame.getValue(id));
127         }
128         return frame;
129     }
130 
131     public ByteBuf encodePingFrame(ByteBufAllocator allocator, int id) {
132         byte flags = 0;
133         int length = 4;
134         ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
135         writeControlFrameHeader(frame, SPDY_PING_FRAME, flags, length);
136         frame.writeInt(id);
137         return frame;
138     }
139 
140     public ByteBuf encodeGoAwayFrame(ByteBufAllocator allocator, int lastGoodStreamId, int statusCode) {
141         byte flags = 0;
142         int length = 8;
143         ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
144         writeControlFrameHeader(frame, SPDY_GOAWAY_FRAME, flags, length);
145         frame.writeInt(lastGoodStreamId);
146         frame.writeInt(statusCode);
147         return frame;
148     }
149 
150     public ByteBuf encodeHeadersFrame(ByteBufAllocator allocator, int streamId, boolean last, ByteBuf headerBlock) {
151         int headerBlockLength = headerBlock.readableBytes();
152         byte flags = last ? SPDY_FLAG_FIN : 0;
153         int length = checkLength(4 + headerBlockLength);
154         ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
155         writeControlFrameHeader(frame, SPDY_HEADERS_FRAME, flags, length);
156         frame.writeInt(streamId);
157         frame.writeBytes(headerBlock, headerBlock.readerIndex(), headerBlockLength);
158         return frame;
159     }
160 
161     public ByteBuf encodeWindowUpdateFrame(ByteBufAllocator allocator, int streamId, int deltaWindowSize) {
162         byte flags = 0;
163         int length = 8;
164         ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
165         writeControlFrameHeader(frame, SPDY_WINDOW_UPDATE_FRAME, flags, length);
166         frame.writeInt(streamId);
167         frame.writeInt(deltaWindowSize);
168         return frame;
169     }
170 
171     public ByteBuf encodeUnknownFrame(ByteBufAllocator allocator, int frameType, byte flags, ByteBuf data) {
172         int length = checkLength(data.readableBytes());
173         ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
174         writeControlFrameHeader(frame, frameType, flags, length);
175         if (length > 0) {
176             frame.writeBytes(data, data.readerIndex(), length);
177         }
178         return frame;
179     }
180 }