View Javadoc

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