@@ -1726,8 +1726,46 @@ fn AsyncHandler(comptime H: type) type {
17261726 const handler = self .handler ;
17271727
17281728 if (self .connect_protocol ) | * connect_protocol | {
1729- _ = try connect_protocol .encrypted .conn .decrypt (data , data );
1730- // TODO needsmore
1729+ blk : {
1730+ const res = try connect_protocol .encrypted .conn .decrypt (data , data );
1731+
1732+ // Not sure about this code:
1733+
1734+ if (res .ciphertext_pos == 0 ) {
1735+ // no part of the encrypted data was consumed no cleartext data should have been generated
1736+ std .debug .assert (res .cleartext .len == 0 );
1737+
1738+ // our next read needs to append more data to the existing data
1739+ handler .read_pos = data .len ;
1740+ return if (res .closed ) break :blk else return .need_more ;
1741+ }
1742+
1743+ if (res .cleartext .len > 0 ) break :blk ;
1744+ if (res .closed ) break :blk ;
1745+
1746+ const unused = res .unused_ciphertext ;
1747+ if (unused .len == 0 ) {
1748+ // all of data was used up, our next read can use
1749+ // the whole read buffer.
1750+ handler .read_pos = 0 ;
1751+ return .need_more ;
1752+ }
1753+
1754+ // We used some of the data, but have some leftover
1755+ // (i.e. there was 1+ full records AND an incomplete
1756+ // record). We need to maintain the "leftover" data
1757+ // for subsequent reads.
1758+
1759+ // Remember that our read_buf is the MAX possible TLS
1760+ // record size. So as long as we make sure that the start
1761+ // of a record is at read_buf[0], we know that we'll
1762+ // always have enough space for 1 record.
1763+ std .mem .copyForwards (u8 , handler .read_buf , unused );
1764+ handler .read_pos = unused .len ;
1765+
1766+ // an incomplete record means there must be more data
1767+ return .need_more ;
1768+ }
17311769 }
17321770
17331771 switch (self .protocol ) {
@@ -2962,7 +3000,7 @@ const State = struct {
29623000
29633001 const write_buf = try allocator .alloc (u8 , buf_size );
29643002 errdefer allocator .free (write_buf );
2965- const write_connect_buf = try allocator .alloc (u8 , buf_size * 2 );
3003+ const write_connect_buf = try allocator .alloc (u8 , buf_size * 5 );
29663004 errdefer allocator .free (write_connect_buf );
29673005
29683006 const header_buf = try allocator .alloc (u8 , header_size );
0 commit comments