@@ -1714,8 +1714,46 @@ fn AsyncHandler(comptime H: type) type {
17141714 const handler = self .handler ;
17151715
17161716 if (self .connect_protocol ) | * connect_protocol | {
1717- _ = try connect_protocol .encrypted .conn .decrypt (data , data );
1718- // TODO needsmore
1717+ blk : {
1718+ const res = try connect_protocol .encrypted .conn .decrypt (data , data );
1719+
1720+ // Not sure about this code:
1721+
1722+ if (res .ciphertext_pos == 0 ) {
1723+ // no part of the encrypted data was consumed no cleartext data should have been generated
1724+ std .debug .assert (res .cleartext .len == 0 );
1725+
1726+ // our next read needs to append more data to the existing data
1727+ handler .read_pos = data .len ;
1728+ return if (res .closed ) break :blk else return .need_more ;
1729+ }
1730+
1731+ if (res .cleartext .len > 0 ) break :blk ;
1732+ if (res .closed ) break :blk ;
1733+
1734+ const unused = res .unused_ciphertext ;
1735+ if (unused .len == 0 ) {
1736+ // all of data was used up, our next read can use
1737+ // the whole read buffer.
1738+ handler .read_pos = 0 ;
1739+ return .need_more ;
1740+ }
1741+
1742+ // We used some of the data, but have some leftover
1743+ // (i.e. there was 1+ full records AND an incomplete
1744+ // record). We need to maintain the "leftover" data
1745+ // for subsequent reads.
1746+
1747+ // Remember that our read_buf is the MAX possible TLS
1748+ // record size. So as long as we make sure that the start
1749+ // of a record is at read_buf[0], we know that we'll
1750+ // always have enough space for 1 record.
1751+ std .mem .copyForwards (u8 , handler .read_buf , unused );
1752+ handler .read_pos = unused .len ;
1753+
1754+ // an incomplete record means there must be more data
1755+ return .need_more ;
1756+ }
17191757 }
17201758
17211759 switch (self .protocol ) {
@@ -2950,7 +2988,7 @@ const State = struct {
29502988
29512989 const write_buf = try allocator .alloc (u8 , buf_size );
29522990 errdefer allocator .free (write_buf );
2953- const write_connect_buf = try allocator .alloc (u8 , buf_size * 2 );
2991+ const write_connect_buf = try allocator .alloc (u8 , buf_size * 5 );
29542992 errdefer allocator .free (write_connect_buf );
29552993
29562994 const header_buf = try allocator .alloc (u8 , header_size );
0 commit comments