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.io.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  import org.eclipse.jetty.util.BufferUtil;
30  
31  public class SynReplyGenerator extends ControlFrameGenerator
32  {
33      private final HeadersBlockGenerator headersBlockGenerator;
34  
35      public SynReplyGenerator(ByteBufferPool bufferPool, HeadersBlockGenerator headersBlockGenerator)
36      {
37          super(bufferPool);
38          this.headersBlockGenerator = headersBlockGenerator;
39      }
40  
41      @Override
42      public ByteBuffer generate(ControlFrame frame)
43      {
44          SynReplyFrame synReply = (SynReplyFrame)frame;
45          short version = synReply.getVersion();
46  
47          ByteBuffer headersBuffer = headersBlockGenerator.generate(version, synReply.getHeaders());
48  
49          int frameBodyLength = getFrameDataLength(version);
50  
51          int frameLength = frameBodyLength + headersBuffer.remaining();
52          if (frameLength > 0xFF_FF_FF)
53          {
54              // Too many headers, but unfortunately we have already modified the compression
55              // context, so we have no other choice than tear down the connection.
56              throw new SessionException(SessionStatus.PROTOCOL_ERROR, "Too many headers");
57          }
58  
59          int totalLength = ControlFrame.HEADER_LENGTH + frameLength;
60  
61          ByteBuffer buffer = getByteBufferPool().acquire(totalLength, Generator.useDirectBuffers);
62          BufferUtil.clearToFill(buffer);
63          generateControlFrameHeader(synReply, frameLength, buffer);
64  
65          buffer.putInt(synReply.getStreamId() & 0x7F_FF_FF_FF);
66          writeAdditional(version, buffer);
67  
68          buffer.put(headersBuffer);
69  
70          buffer.flip();
71          return buffer;
72      }
73  
74      private int getFrameDataLength(short version)
75      {
76          switch (version)
77          {
78              case SPDY.V2:
79                  return 6;
80              case SPDY.V3:
81                  return 4;
82              default:
83                  // Here the version is trusted to be correct; if it's not
84                  // then it's a bug rather than an application error
85                  throw new IllegalStateException();
86          }
87      }
88  
89      private void writeAdditional(short version, ByteBuffer buffer)
90      {
91          switch (version)
92          {
93              case SPDY.V2:
94                  buffer.putShort((short)0);
95                  break;
96              case SPDY.V3:
97                  break;
98              default:
99                  // Here the version is trusted to be correct; if it's not
100                 // then it's a bug rather than an application error
101                 throw new IllegalStateException();
102         }
103     }
104 }