Skip to content

Commit 24fbe8c

Browse files
Define bitwise flags; group by frame type
1 parent 538db34 commit 24fbe8c

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

src/Node/Http2/Flags.purs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
-- | Defines a Bitwise flag and the two operations used in this context.
2+
-- | Also groups all flags that can be used in a given frame type into
3+
-- | a record with a corresponding name. For example,
4+
-- | the DATA frame can use flags found in the `dataFlags` record.
5+
module Node.Http2.Flags
6+
( BitwiseFlag -- constructor intentionally not exported
7+
, printFlags
8+
, enable
9+
, isDisabled
10+
, isEnabled
11+
, endStream
12+
, ack
13+
, endHeaders
14+
, padded
15+
, priority
16+
, unFlag
17+
, dataFlags
18+
, headersFlags
19+
, settingsFlags
20+
, pushPromiseFlags
21+
, pingFlags
22+
, continuationFlags
23+
) where
24+
25+
import Prelude
26+
27+
import Data.Array as Array
28+
import Data.Int.Bits as B
29+
30+
-- | An integer whose bits store bitwise flags
31+
newtype BitwiseFlag = BitwiseFlag Int
32+
33+
derive instance Eq BitwiseFlag
34+
derive newtype instance Show BitwiseFlag
35+
36+
printFlags :: BitwiseFlag -> String
37+
printFlags input = Array.intercalate "; "
38+
[ "END_STREAM/ACK: " <> show (isEnabled input endStream)
39+
, "END_STREAM: " <> show (isEnabled input endStream)
40+
, "END_HEADERS: " <> show (isEnabled input endHeaders)
41+
, "PADDED: " <> show (isEnabled input padded)
42+
, "PRIORITY: " <> show (isEnabled input priority)
43+
]
44+
45+
-- | Checks whether the first arg, the `input`, has disabled the flag represented by the second arg, `endStream`.
46+
-- | ```
47+
-- | isDisabled input endStream
48+
-- | ```
49+
isDisabled :: BitwiseFlag -> BitwiseFlag -> Boolean
50+
isDisabled (BitwiseFlag original) (BitwiseFlag flag) = original `B.and` flag == 0
51+
52+
-- | Checks whether the first arg, the `input`, has enabled the flag represented by the second arg, `endStream`.
53+
-- | ```
54+
-- | isEnabled input endStream
55+
-- | ```
56+
isEnabled :: BitwiseFlag -> BitwiseFlag -> Boolean
57+
isEnabled (BitwiseFlag original) (BitwiseFlag flag) = original `B.and` flag == flag
58+
59+
-- | Enable two flags.
60+
-- | ```
61+
-- | enable endStream padded
62+
-- | ```
63+
enable :: BitwiseFlag -> BitwiseFlag -> BitwiseFlag
64+
enable (BitwiseFlag original) (BitwiseFlag flag) = BitwiseFlag (original `B.and` flag)
65+
66+
-- | - on DATA frames, indicates that this frame is the last that the endpoint will send for the identified stream. Setting this flag causes the stream to enter one of the "half-closed" states or the "closed" state
67+
-- | - on HEADERS frames, indicates that the header block (Section 4.3) is the last that the endpoint will send for the identified stream.
68+
endStream :: BitwiseFlag
69+
endStream = BitwiseFlag 0x1
70+
71+
-- | - on SETTINGS frames, indicates that this frame acknowledges receipt and application of the peer's `SETTINGS` frame. When this bit is set, the payload of the `SETTINGS` frame MUST be empty. Receipt of a SETTINGS frame with the ACK flag set and a length field value other than 0 MUST be treated as a connection error (Section 5.4.1) of type FRAME_SIZE_ERROR. For more information, see Section 6.5.3 ("Settings Synchronization")
72+
-- | - on PING frames, indicates that this PING frame is a PING response. An endpoint MUST set this flag in PING responses. An endpoint MUST NOT respond to PING frames containing this flag.
73+
ack :: BitwiseFlag
74+
ack = BitwiseFlag 0x1
75+
76+
-- | - on HEADERS frames, indicates that this frame contains an entire header block (Section 4.3) and is not followed by any `CONTINUATION` frames.
77+
-- | - on PUSH_PROMISE frames, indicates that this frame contains an entire header block (Section 4.3) and is not followed by any `CONTINUATION` frames.
78+
-- | - on CONTINUATION frames, indicates that this frame ends a header block (Section 4.3).
79+
endHeaders :: BitwiseFlag
80+
endHeaders = BitwiseFlag 0x4
81+
82+
-- | - on DATA frames, indicates that the Pad Length field and any padding that it describes are present.
83+
-- | - on HEADERS frames, indicates that the Pad Length field and any padding that it describes are present.
84+
-- | - on PUSH_PROMISE frames, indicates that the Pad Length field and any padding that it describes are present.
85+
padded :: BitwiseFlag
86+
padded = BitwiseFlag 0x8
87+
88+
-- | - on HEADERS frames, indicates that the Exclusive Flag (E), Stream Dependency, and Weight fields are present; see Section 5.3.
89+
priority :: BitwiseFlag
90+
priority = BitwiseFlag 0x20
91+
92+
unFlag :: BitwiseFlag -> Int
93+
unFlag (BitwiseFlag i) = i
94+
95+
dataFlags :: { endStream :: BitwiseFlag, padded :: BitwiseFlag }
96+
dataFlags =
97+
{ endStream
98+
, padded
99+
}
100+
101+
headersFlags :: { endHeaders :: BitwiseFlag, endStream :: BitwiseFlag, padded :: BitwiseFlag, priority :: BitwiseFlag }
102+
headersFlags =
103+
{ endStream
104+
, endHeaders
105+
, padded
106+
, priority
107+
}
108+
109+
settingsFlags :: { ack :: BitwiseFlag }
110+
settingsFlags =
111+
{ ack
112+
}
113+
114+
pushPromiseFlags :: { endHeaders :: BitwiseFlag, padded :: BitwiseFlag }
115+
pushPromiseFlags =
116+
{ endHeaders
117+
, padded
118+
}
119+
120+
pingFlags :: { ack :: BitwiseFlag }
121+
pingFlags =
122+
{ ack
123+
}
124+
125+
continuationFlags :: { endHeaders :: BitwiseFlag }
126+
continuationFlags =
127+
{ endHeaders
128+
}

0 commit comments

Comments
 (0)