View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.http2.parser;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.http.MetaData;
24  import org.eclipse.jetty.http2.hpack.HpackDecoder;
25  import org.eclipse.jetty.io.ByteBufferPool;
26  import org.eclipse.jetty.util.BufferUtil;
27  
28  public class HeaderBlockParser
29  {
30      private final ByteBufferPool byteBufferPool;
31      private final HpackDecoder hpackDecoder;
32      private ByteBuffer blockBuffer;
33  
34      public HeaderBlockParser(ByteBufferPool byteBufferPool, HpackDecoder hpackDecoder)
35      {
36          this.byteBufferPool = byteBufferPool;
37          this.hpackDecoder = hpackDecoder;
38      }
39  
40      public MetaData parse(ByteBuffer buffer, int blockLength)
41      {
42          // We must wait for the all the bytes of the header block to arrive.
43          // If they are not all available, accumulate them.
44          // When all are available, decode them.
45  
46          int accumulated = blockBuffer == null ? 0 : blockBuffer.position();
47          int remaining = blockLength - accumulated;
48  
49          if (buffer.remaining() < remaining)
50          {
51              if (blockBuffer == null)
52              {
53                  blockBuffer = byteBufferPool.acquire(blockLength, false);
54                  BufferUtil.clearToFill(blockBuffer);
55              }
56              blockBuffer.put(buffer);
57              return null;
58          }
59          else
60          {
61              int limit = buffer.limit();
62              buffer.limit(buffer.position() + remaining);
63              ByteBuffer toDecode;
64              if (blockBuffer != null)
65              {
66                  blockBuffer.put(buffer);
67                  BufferUtil.flipToFlush(blockBuffer, 0);
68                  toDecode = blockBuffer;
69              }
70              else
71              {
72                  toDecode = buffer;
73              }
74  
75              MetaData result = hpackDecoder.decode(toDecode);
76  
77              buffer.limit(limit);
78  
79              if (blockBuffer != null)
80              {
81                  byteBufferPool.release(blockBuffer);
82                  blockBuffer = null;
83              }
84  
85              return result;
86          }
87      }
88  }