@@ -2,7 +2,6 @@ use crate::poll::Pollable;
22use crate :: streams:: { InputStream , OutputStream , StreamError } ;
33use crate :: WasiHttp ;
44use anyhow:: { anyhow, bail} ;
5- use bytes:: BufMut ;
65use std:: vec:: Vec ;
76
87impl crate :: streams:: Host for WasiHttp {
@@ -11,10 +10,14 @@ impl crate::streams::Host for WasiHttp {
1110 stream : InputStream ,
1211 len : u64 ,
1312 ) -> wasmtime:: Result < Result < ( Vec < u8 > , bool ) , StreamError > > {
14- let s = self
13+ let st = self
1514 . streams
1615 . get_mut ( & stream)
1716 . ok_or_else ( || anyhow ! ( "stream not found: {stream}" ) ) ?;
17+ if st. closed {
18+ bail ! ( "stream is dropped!" ) ;
19+ }
20+ let s = & mut st. data ;
1821 if len == 0 {
1922 Ok ( Ok ( ( bytes:: Bytes :: new ( ) . to_vec ( ) , s. len ( ) > 0 ) ) )
2023 } else if s. len ( ) > len. try_into ( ) ? {
@@ -31,10 +34,14 @@ impl crate::streams::Host for WasiHttp {
3134 stream : InputStream ,
3235 len : u64 ,
3336 ) -> wasmtime:: Result < Result < ( u64 , bool ) , StreamError > > {
34- let s = self
37+ let st = self
3538 . streams
3639 . get_mut ( & stream)
3740 . ok_or_else ( || anyhow ! ( "stream not found: {stream}" ) ) ?;
41+ if st. closed {
42+ bail ! ( "stream is dropped!" ) ;
43+ }
44+ let s = & mut st. data ;
3845 if len == 0 {
3946 Ok ( Ok ( ( 0 , s. len ( ) > 0 ) ) )
4047 } else if s. len ( ) > len. try_into ( ) ? {
@@ -52,7 +59,11 @@ impl crate::streams::Host for WasiHttp {
5259 }
5360
5461 fn drop_input_stream ( & mut self , stream : InputStream ) -> wasmtime:: Result < ( ) > {
55- self . streams . remove ( & stream) ;
62+ let st = self
63+ . streams
64+ . get_mut ( & stream)
65+ . ok_or_else ( || anyhow ! ( "stream not found: {stream}" ) ) ?;
66+ st. closed = true ;
5667 Ok ( ( ) )
5768 }
5869
@@ -61,18 +72,13 @@ impl crate::streams::Host for WasiHttp {
6172 this : OutputStream ,
6273 buf : Vec < u8 > ,
6374 ) -> wasmtime:: Result < Result < u64 , StreamError > > {
64- match self . streams . get ( & this) {
65- Some ( data) => {
66- let mut new = bytes:: BytesMut :: with_capacity ( data. len ( ) + buf. len ( ) ) ;
67- new. put ( data. clone ( ) ) ;
68- new. put ( bytes:: Bytes :: from ( buf. clone ( ) ) ) ;
69- self . streams . insert ( this, new. freeze ( ) ) ;
70- }
71- None => {
72- self . streams . insert ( this, bytes:: Bytes :: from ( buf. clone ( ) ) ) ;
73- }
75+ let len = buf. len ( ) ;
76+ let st = self . streams . entry ( this) . or_default ( ) ;
77+ if st. closed {
78+ bail ! ( "cannot write to closed stream" ) ;
7479 }
75- Ok ( Ok ( buf. len ( ) . try_into ( ) ?) )
80+ st. data . extend_from_slice ( buf. as_slice ( ) ) ;
81+ Ok ( Ok ( len. try_into ( ) ?) )
7682 }
7783
7884 fn write_zeroes (
@@ -111,7 +117,11 @@ impl crate::streams::Host for WasiHttp {
111117 }
112118
113119 fn drop_output_stream ( & mut self , stream : OutputStream ) -> wasmtime:: Result < ( ) > {
114- self . streams . remove ( & stream) ;
120+ let st = self
121+ . streams
122+ . get_mut ( & stream)
123+ . ok_or_else ( || anyhow ! ( "stream not found: {stream}" ) ) ?;
124+ st. closed = true ;
115125 Ok ( ( ) )
116126 }
117127}
0 commit comments