@@ -1774,17 +1774,8 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
1774
1774
Http2Scope h2scope (this );
1775
1775
CHECK_NOT_NULL (stream_);
1776
1776
Debug (this , " receiving %d bytes" , nread);
1777
- IncrementCurrentSessionMemory (buf.len );
1778
1777
CHECK (stream_buf_ab_.IsEmpty ());
1779
1778
1780
- OnScopeLeave on_scope_leave ([&]() {
1781
- // Once finished handling this write, reset the stream buffer.
1782
- // The memory has either been free()d or was handed over to V8.
1783
- DecrementCurrentSessionMemory (buf.len );
1784
- stream_buf_ab_ = Local<ArrayBuffer>();
1785
- stream_buf_ = uv_buf_init (nullptr , 0 );
1786
- });
1787
-
1788
1779
// Only pass data on if nread > 0
1789
1780
if (nread <= 0 ) {
1790
1781
free (buf.base );
@@ -1794,29 +1785,35 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
1794
1785
return ;
1795
1786
}
1796
1787
1788
+ // Shrink to the actual amount of used data.
1789
+ char * base = Realloc (buf.base , nread);
1790
+
1791
+ IncrementCurrentSessionMemory (nread);
1792
+ OnScopeLeave on_scope_leave ([&]() {
1793
+ // Once finished handling this write, reset the stream buffer.
1794
+ // The memory has either been free()d or was handed over to V8.
1795
+ DecrementCurrentSessionMemory (nread);
1796
+ stream_buf_ab_ = Local<ArrayBuffer>();
1797
+ stream_buf_ = uv_buf_init (nullptr , 0 );
1798
+ });
1799
+
1797
1800
// Make sure that there was no read previously active.
1798
1801
CHECK_NULL (stream_buf_.base );
1799
1802
CHECK_EQ (stream_buf_.len , 0 );
1800
1803
1801
1804
// Remember the current buffer, so that OnDataChunkReceived knows the
1802
1805
// offset of a DATA frame's data into the socket read buffer.
1803
- stream_buf_ = uv_buf_init (buf.base , nread);
1804
-
1805
- // Verify that currently: There is memory allocated into which
1806
- // the data has been read, and that memory buffer is at least as large
1807
- // as the amount of data we have read, but we have not yet made an
1808
- // ArrayBuffer out of it.
1809
- CHECK_LE (static_cast <size_t >(nread), stream_buf_.len );
1806
+ stream_buf_ = uv_buf_init (base, nread);
1810
1807
1811
1808
Isolate* isolate = env ()->isolate ();
1812
1809
1813
1810
// Create an array buffer for the read data. DATA frames will be emitted
1814
1811
// as slices of this array buffer to avoid having to copy memory.
1815
1812
stream_buf_ab_ =
1816
1813
ArrayBuffer::New (isolate,
1817
- buf. base ,
1818
- nread,
1819
- ArrayBufferCreationMode::kInternalized );
1814
+ base,
1815
+ nread,
1816
+ ArrayBufferCreationMode::kInternalized );
1820
1817
1821
1818
statistics_.data_received += nread;
1822
1819
ssize_t ret = Write (&stream_buf_, 1 );
0 commit comments