View Javadoc

1   //========================================================================
2   //Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //The Eclipse Public License is available at
8   //http://www.eclipse.org/legal/epl-v10.html
9   //The Apache License v2.0 is available at
10  //http://www.opensource.org/licenses/apache2.0.php
11  //You may elect to redistribute this code under either of these licenses.
12  //========================================================================
13  
14  package org.eclipse.jetty.spdy.generator;
15  
16  import java.nio.ByteBuffer;
17  
18  import org.eclipse.jetty.spdy.ByteBufferPool;
19  import org.eclipse.jetty.spdy.SessionException;
20  import org.eclipse.jetty.spdy.api.SPDY;
21  import org.eclipse.jetty.spdy.api.SessionStatus;
22  import org.eclipse.jetty.spdy.frames.ControlFrame;
23  import org.eclipse.jetty.spdy.frames.SynReplyFrame;
24  
25  public class SynReplyGenerator extends ControlFrameGenerator
26  {
27      private final HeadersBlockGenerator headersBlockGenerator;
28  
29      public SynReplyGenerator(ByteBufferPool bufferPool, HeadersBlockGenerator headersBlockGenerator)
30      {
31          super(bufferPool);
32          this.headersBlockGenerator = headersBlockGenerator;
33      }
34  
35      @Override
36      public ByteBuffer generate(ControlFrame frame)
37      {
38          SynReplyFrame synReply = (SynReplyFrame)frame;
39          short version = synReply.getVersion();
40  
41          ByteBuffer headersBuffer = headersBlockGenerator.generate(version, synReply.getHeaders());
42  
43          int frameBodyLength = getFrameDataLength(version);
44  
45          int frameLength = frameBodyLength + headersBuffer.remaining();
46          if (frameLength > 0xFF_FF_FF)
47          {
48              // Too many headers, but unfortunately we have already modified the compression
49              // context, so we have no other choice than tear down the connection.
50              throw new SessionException(SessionStatus.PROTOCOL_ERROR, "Too many headers");
51          }
52  
53          int totalLength = ControlFrame.HEADER_LENGTH + frameLength;
54  
55          ByteBuffer buffer = getByteBufferPool().acquire(totalLength, true);
56          generateControlFrameHeader(synReply, frameLength, buffer);
57  
58          buffer.putInt(synReply.getStreamId() & 0x7F_FF_FF_FF);
59          writeAdditional(version, buffer);
60  
61          buffer.put(headersBuffer);
62  
63          buffer.flip();
64          return buffer;
65      }
66  
67      private int getFrameDataLength(short version)
68      {
69          switch (version)
70          {
71              case SPDY.V2:
72                  return 6;
73              case SPDY.V3:
74                  return 4;
75              default:
76                  // Here the version is trusted to be correct; if it's not
77                  // then it's a bug rather than an application error
78                  throw new IllegalStateException();
79          }
80      }
81  
82      private void writeAdditional(short version, ByteBuffer buffer)
83      {
84          switch (version)
85          {
86              case SPDY.V2:
87                  buffer.putShort((short)0);
88                  break;
89              case SPDY.V3:
90                  break;
91              default:
92                  // Here the version is trusted to be correct; if it's not
93                  // then it's a bug rather than an application error
94                  throw new IllegalStateException();
95          }
96      }
97  }