@@ -18,7 +18,7 @@ import Data.Maybe (isNothing)
1818import Data.Word (Word8, Word32)
1919import Foreign.Ptr (Ptr, plusPtr)
2020import Foreign.Storable (poke)
21−import Network.HPACK (setLimitForEncoding)
21+import Network.HPACK (setLimitForEncoding, toHeaderTable)
2222import Network.HTTP2
2323import Network.HTTP2.Priority (isEmptySTM, dequeueSTM, Precedence)
2424import Network.Wai
@@ -122,16 +122,16 @@ frameSender ctx@Context{outputQ,controlQ,connectionWindow,encodeDynamicTable}
122122 Nothing -> return ()
123123 Just siz -> setLimitForEncoding siz encodeDynamicTable
124124
125− output out@(Output strm _ _ tell (ONext curr)) off0 lim = do
125+ output out@(Output strm _ _ tell getH2D (ONext curr)) off0 lim = do
126126 -- Data frame payload
127127 let !buf = connWriteBuffer `plusPtr` off0
128128 !siz = connBufferSize - off0
129129 Next datPayloadLen mnext <- curr buf siz lim
130− off <- fillDataHeader strm off0 datPayloadLen mnext tell
130+ off <- fillDataHeader strm off0 datPayloadLen mnext tell getH2D
131131 maybeEnqueueNext out mnext
132132 return off
133133
134− output out@(Output strm rspn ii tell ORspn) off0 lim = do
134+ output out@(Output strm rspn ii tell getH2D ORspn) off0 lim = do
135135 -- Header frame and Continuation frame
136136 let !sid = streamNumber strm
137137 !endOfStream = case rspn of
@@ -149,7 +149,7 @@ frameSender ctx@Context{outputQ,controlQ,connectionWindow,encodeDynamicTable}
149149 let payloadOff = off + frameHeaderLength
150150 Next datPayloadLen mnext <-
151151 fillFileBodyGetNext conn ii payloadOff lim path mpart
152− off' <- fillDataHeader strm off datPayloadLen mnext tell
152+ off' <- fillDataHeader strm off datPayloadLen mnext tell getH2D
153153 maybeEnqueueNext out mnext
154154 return off'
155155 RspnBuilder _ _ builder -> do
@@ -157,24 +157,24 @@ frameSender ctx@Context{outputQ,controlQ,connectionWindow,encodeDynamicTable}
157157 let payloadOff = off + frameHeaderLength
158158 Next datPayloadLen mnext <-
159159 fillBuilderBodyGetNext conn ii payloadOff lim builder
160− off' <- fillDataHeader strm off datPayloadLen mnext tell
160+ off' <- fillDataHeader strm off datPayloadLen mnext tell getH2D
161161 maybeEnqueueNext out mnext
162162 return off'
163163 RspnStreaming _ _ tbq -> do
164164 let payloadOff = off + frameHeaderLength
165165 Next datPayloadLen mnext <-
166166 fillStreamBodyGetNext conn payloadOff lim tbq strm
167− off' <- fillDataHeader strm off datPayloadLen mnext tell
167+ off' <- fillDataHeader strm off datPayloadLen mnext tell getH2D
168168 maybeEnqueueNext out mnext
169169 return off'
170170
171− output (Output strm rspn ii tell (OPush ths pid)) off0 lim = do
171+ output out@(Output strm _ _ _ _ (OPush ths pid)) off0 lim = do
172172 -- Creating a push promise header
173173 -- Frame id should be associated stream id from the client.
174174 let !sid = streamNumber strm
175175 len <- pushPromise pid sid ths off0
176176 off <- sendHeadersIfNecessary $ off0 + frameHeaderLength + len
177− output (Output strm rspn ii tell ORspn) off lim
177+ output out{ outputType = ORspn } off lim
178178
179179 output _ _ _ = undefined -- never reach
180180
@@ -183,9 +183,12 @@ frameSender ctx@Context{outputQ,controlQ,connectionWindow,encodeDynamicTable}
183183 if isClosed state then
184184 return off
185185 else case out of
186− Output strm' rsp ii wait OWait -> do
186+ Output _ _ _ wait _ OWait -> do
187187 -- Checking if all push are done.
188− let out' = Output strm' rsp ii (return ()) ORspn
188+ let out' = out {
189+ outputHook = return ()
190+ , outputType = ORspn
191+ }
189192 forkAndEnqueueWhenReady wait outputQ out' mgr
190193 return off
191194 _ -> case mtbq of
@@ -268,21 +271,36 @@ frameSender ctx@Context{outputQ,controlQ,connectionWindow,encodeDynamicTable}
268271 flushN off
269272 return 0
270273
271− fillDataHeader strm off datPayloadLen mnext tell = do
274+ fillDataHeader strm@Stream{streamWindow,streamNumber}
275+ off datPayloadLen mnext tell getH2D = do
272276 -- Data frame header
273− let !sid = streamNumber strm
277+ mh2d <- getH2D
278+ let (!trailers,!noTrailers) = case http2dataTrailers <$> mh2d of
279+ Nothing -> ([], True)
280+ Just ts -> (ts, null ts)
274281 !buf = connWriteBuffer `plusPtr` off
275282 !off' = off + frameHeaderLength + datPayloadLen
276− !done = isNothing mnext
277− flag | done = setEndStream defaultFlags
278− | otherwise = defaultFlags
279− fillFrameHeader FrameData datPayloadLen sid flag buf
280− when done $ do
283+ !noMoreBody = isNothing mnext
284+ flag | noMoreBody && noTrailers = setEndStream defaultFlags
285+ | otherwise = defaultFlags
286+ fillFrameHeader FrameData datPayloadLen streamNumber flag buf
287+ off'' <- handleEndOfBody noMoreBody off' noTrailers trailers
288+ atomically $ modifyTVar' connectionWindow (subtract datPayloadLen)
289+ atomically $ modifyTVar' streamWindow (subtract datPayloadLen)
290+ return off''
291+ where
292+ handleTrailers True off0 _ = return off0
293+ handleTrailers _ off0 trailers = do
294+ (ths,_) <- toHeaderTable trailers
295+ kvlen <- headerContinue streamNumber ths True off0
296+ sendHeadersIfNecessary $ off0 + frameHeaderLength + kvlen
297+ handleEndOfBody True off0 noTrailers trailers = do
298+ off1 <- handleTrailers noTrailers off0 trailers
281299 void $ tell
282300 closed ctx strm Finished
283− atomically $ modifyTVar' connectionWindow (subtract datPayloadLen)
284− atomically $ modifyTVar' (streamWindow strm) (subtract datPayloadLen)
285− return off'
301+ return off1
302+ handleEndOfBody False off0 _ _ = return off0
303+
286304
287305 pushPromise pid sid ths off = do
288306 let !offsid = off + frameHeaderLength