diff options
author | Momtchil Momtchev <momtchil@momtchev.com> | 2020-10-16 13:41:09 +0200 |
---|---|---|
committer | Node.js GitHub Bot <github-bot@iojs.org> | 2020-10-25 10:29:45 +0000 |
commit | 75202d971d3a18824f357bbb31671291a1dc4e84 (patch) | |
tree | 011cb4585690540a284cd23a9d70686ea512fc3b /src/node_http2.cc | |
parent | 629e1ab5aa84b75cd26ee9208c909eff500a3a88 (diff) | |
download | node-new-75202d971d3a18824f357bbb31671291a1dc4e84.tar.gz |
http2: reinject data received before http2 is attached
Reinject the data already received from the TLS
socket when the HTTP2 client is attached with a
delay
Fixes: https://github.com/nodejs/node/issues/35475
PR-URL: https://github.com/nodejs/node/pull/35678
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Alba Mendez <me@alba.sh>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Diffstat (limited to 'src/node_http2.cc')
-rw-r--r-- | src/node_http2.cc | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/node_http2.cc b/src/node_http2.cc index b72e03e857..b8e462419e 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1829,6 +1829,33 @@ void Http2Session::Consume(Local<Object> stream_obj) { Debug(this, "i/o stream consumed"); } +// Allow injecting of data from JS +// This is used when the socket has already some data received +// before our listener was attached +// https://github.com/nodejs/node/issues/35475 +void Http2Session::Receive(const FunctionCallbackInfo<Value>& args) { + Http2Session* session; + ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); + CHECK(args[0]->IsObject()); + + ArrayBufferViewContents<char> buffer(args[0]); + const char* data = buffer.data(); + size_t len = buffer.length(); + Debug(session, "Receiving %zu bytes injected from JS", len); + + // Copy given buffer + while (len > 0) { + uv_buf_t buf = session->OnStreamAlloc(len); + size_t copy = buf.len > len ? len : buf.len; + memcpy(buf.base, data, copy); + buf.len = copy; + session->OnStreamRead(copy, buf); + + data += copy; + len -= copy; + } +} + Http2Stream* Http2Stream::New(Http2Session* session, int32_t id, nghttp2_headers_category category, @@ -3054,6 +3081,7 @@ void Initialize(Local<Object> target, env->SetProtoMethod(session, "altsvc", Http2Session::AltSvc); env->SetProtoMethod(session, "ping", Http2Session::Ping); env->SetProtoMethod(session, "consume", Http2Session::Consume); + env->SetProtoMethod(session, "receive", Http2Session::Receive); env->SetProtoMethod(session, "destroy", Http2Session::Destroy); env->SetProtoMethod(session, "goaway", Http2Session::Goaway); env->SetProtoMethod(session, "settings", Http2Session::Settings); |