1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
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 }