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.http2.ErrorCode;
24 import org.eclipse.jetty.http2.frames.WindowUpdateFrame;
25
26 public class WindowUpdateBodyParser extends BodyParser
27 {
28 private State state = State.PREPARE;
29 private int cursor;
30 private int windowDelta;
31
32 public WindowUpdateBodyParser(HeaderParser headerParser, Parser.Listener listener)
33 {
34 super(headerParser, listener);
35 }
36
37 private void reset()
38 {
39 state = State.PREPARE;
40 cursor = 0;
41 windowDelta = 0;
42 }
43
44 @Override
45 public boolean parse(ByteBuffer buffer)
46 {
47 while (buffer.hasRemaining())
48 {
49 switch (state)
50 {
51 case PREPARE:
52 {
53 int length = getBodyLength();
54 if (length != 4)
55 return connectionFailure(buffer, ErrorCode.FRAME_SIZE_ERROR.code, "invalid_window_update_frame");
56 state = State.WINDOW_DELTA;
57 break;
58 }
59 case WINDOW_DELTA:
60 {
61 if (buffer.remaining() >= 4)
62 {
63 windowDelta = buffer.getInt() & 0x7F_FF_FF_FF;
64 return onWindowUpdate(windowDelta);
65 }
66 else
67 {
68 state = State.WINDOW_DELTA_BYTES;
69 cursor = 4;
70 }
71 break;
72 }
73 case WINDOW_DELTA_BYTES:
74 {
75 byte currByte = buffer.get();
76 --cursor;
77 windowDelta += (currByte & 0xFF) << 8 * cursor;
78 if (cursor == 0)
79 {
80 windowDelta &= 0x7F_FF_FF_FF;
81 return onWindowUpdate(windowDelta);
82 }
83 break;
84 }
85 default:
86 {
87 throw new IllegalStateException();
88 }
89 }
90 }
91 return false;
92 }
93
94 private boolean onWindowUpdate(int windowDelta)
95 {
96 WindowUpdateFrame frame = new WindowUpdateFrame(getStreamId(), windowDelta);
97 reset();
98 notifyWindowUpdate(frame);
99 return true;
100 }
101
102 private enum State
103 {
104 PREPARE, WINDOW_DELTA, WINDOW_DELTA_BYTES
105 }
106 }