Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 3eb955d

Browse files
jasnelladdaleax
authored andcommitted
quic: minor refactor for QuicStream::Header
PR-URL: #207 Reviewed-By: #207
1 parent 081a37f commit 3eb955d

File tree

6 files changed

+66
-41
lines changed

6 files changed

+66
-41
lines changed

lib/internal/quic/core.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ function onStreamError(streamHandle, error) {
534534
// received for the stream. Not all QuicStreams
535535
// will support headers. The headers argument
536536
// here is an Array of name-value pairs.
537-
function onStreamHeaders(headers, kind) {
538-
this[owner_symbol][kHeaders](headers, kind);
537+
function onStreamHeaders(id, headers, kind) {
538+
this[owner_symbol][kHeaders](id, headers, kind);
539539
}
540540

541541
function onSessionSilentClose(statelessReset, code, family) {
@@ -1491,6 +1491,14 @@ class QuicSession extends EventEmitter {
14911491
stream.destroy();
14921492
}
14931493

1494+
[kHeaders](id, headers, kind) {
1495+
const stream = this.#streams.get(id);
1496+
if (stream === undefined)
1497+
return;
1498+
1499+
stream[kHeaders](headers, kind);
1500+
}
1501+
14941502
[kStreamReset](id, code, finalSize) {
14951503
const stream = this.#streams.get(id);
14961504
if (stream === undefined)

src/node_quic_http3_application.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ constexpr size_t DEFAULT_MAX_PUSHES = 65535;
4949

5050
using Http3ConnectionPointer = DeleteFnPtr<nghttp3_conn, nghttp3_conn_del>;
5151

52-
class Http3Header : public QuicStream::Header {
52+
class Http3Header : public QuicHeader {
5353
public:
5454
Http3Header(int32_t token, nghttp3_rcbuf* name, nghttp3_rcbuf* value);
5555
Http3Header(Http3Header&& other) noexcept :

src/node_quic_session.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,38 @@ void QuicCryptoContext::WriteHandshake(
699699
handshake_[level].Push(std::move(buffer));
700700
}
701701

702+
void QuicApplication::StreamHeaders(
703+
int64_t stream_id,
704+
int kind,
705+
std::vector<std::unique_ptr<QuicHeader>>* headers) {
706+
HandleScope scope(env()->isolate());
707+
Context::Scope context_scope(env()->context());
708+
std::vector<Local<Value>> head;
709+
for (const auto& header : *headers) {
710+
// name and value should never be empty here, and if
711+
// they are, there's an actual bug so go ahead and crash
712+
Local<Value> pair[] = {
713+
header->GetName(Session()->Application()).ToLocalChecked(),
714+
header->GetValue(Session()->Application()).ToLocalChecked()
715+
};
716+
head.push_back(Array::New(env()->isolate(), pair, arraysize(pair)));
717+
}
718+
Local<Value> argv[] = {
719+
Number::New(env()->isolate(), static_cast<double>(stream_id)),
720+
Array::New(
721+
env()->isolate(),
722+
head.data(),
723+
head.size()),
724+
Integer::New(
725+
env()->isolate(),
726+
kind)
727+
};
728+
BaseObjectPtr<QuicSession> ptr(Session());
729+
Session()->MakeCallback(
730+
env()->quic_on_stream_headers_function(),
731+
arraysize(argv), argv);
732+
}
733+
702734
void QuicApplication::StreamClose(
703735
int64_t stream_id,
704736
uint64_t app_error_code) {

src/node_quic_session.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using ConnectionPointer = DeleteFnPtr<ngtcp2_conn, ngtcp2_conn_del>;
3232

3333
class QuicSocket;
3434
class QuicStream;
35+
class QuicHeader;
3536

3637
enum class QlogMode {
3738
kDisabled,
@@ -361,6 +362,10 @@ class QuicApplication : public MemoryRetainer {
361362
virtual void ExtendMaxStreamData(int64_t stream_id, uint64_t max_data) {}
362363
virtual bool SendPendingData() = 0;
363364
virtual bool SendStreamData(QuicStream* stream) = 0;
365+
virtual void StreamHeaders(
366+
int64_t stream_id,
367+
int kind,
368+
std::vector<std::unique_ptr<QuicHeader>>* headers);
364369
virtual void StreamClose(
365370
int64_t stream_id,
366371
uint64_t app_error_code);

src/node_quic_stream.cc

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ void QuicStream::SetHeadersKind(QuicStreamHeadersKind kind) {
387387
headers_kind_ = kind;
388388
}
389389

390-
bool QuicStream::AddHeader(std::unique_ptr<Header> header) {
390+
bool QuicStream::AddHeader(std::unique_ptr<QuicHeader> header) {
391391
Debug(this, "Header Added");
392392
headers_.emplace_back(std::move(header));
393393
// TODO(@jasnell): We need to limit the maximum number of headers.
@@ -400,28 +400,8 @@ void QuicStream::EndHeaders() {
400400
// Upon completion of a block of headers, convert the
401401
// vector of Header objects into an array of name+value
402402
// pairs, then call the on_stream_headers function.
403-
std::vector<Local<Value>> headers;
404-
for (const auto& header : headers_) {
405-
// name and value should never be empty here, and if
406-
// they are, there's an actual bug so go ahead and crash
407-
Local<Value> pair[] = {
408-
header->GetName(Session()->Application()).ToLocalChecked(),
409-
header->GetValue(Session()->Application()).ToLocalChecked()
410-
};
411-
headers.push_back(Array::New(env()->isolate(), pair, arraysize(pair)));
412-
}
413-
Local<Value> argv[] = {
414-
Array::New(
415-
env()->isolate(),
416-
headers.data(),
417-
headers.size()),
418-
Integer::New(
419-
env()->isolate(),
420-
headers_kind_)
421-
};
403+
Session()->Application()->StreamHeaders(GetID(), headers_kind_, &headers_);
422404
headers_.clear();
423-
BaseObjectPtr<QuicStream> ptr(this);
424-
MakeCallback(env()->quic_on_stream_headers_function(), arraysize(argv), argv);
425405
}
426406

427407
void QuicStream::MemoryInfo(MemoryTracker* tracker) const {

src/node_quic_stream.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ enum QuicStreamHeadersKind : int {
3737
QUICSTREAM_HEADERS_KIND_TRAILING
3838
};
3939

40+
// QuicHeader is a base class for implementing QUIC application
41+
// specific headers. Each type of QUIC application may have
42+
// different internal representations for a header name+value
43+
// pair. QuicApplication implementations that support headers
44+
// per stream must create a specialization of the Header class.
45+
class QuicHeader {
46+
public:
47+
QuicHeader() {}
48+
49+
virtual ~QuicHeader() {}
50+
virtual v8::MaybeLocal<v8::String> GetName(QuicApplication* app) const = 0;
51+
virtual v8::MaybeLocal<v8::String> GetValue(QuicApplication* app) const = 0;
52+
};
53+
4054
// QuicStream's are simple data flows that, fortunately, do not
4155
// require much. They may be:
4256
//
@@ -105,20 +119,6 @@ enum QuicStreamHeadersKind : int {
105119
// ngtcp2 level.
106120
class QuicStream : public AsyncWrap, public StreamBase {
107121
public:
108-
// Header is a base class for implementing QUIC application
109-
// specific headers. Each type of QUIC application may have
110-
// different internal representations for a header name+value
111-
// pair. QuicApplication implementations that support headers
112-
// per stream must create a specialization of the Header class.
113-
class Header {
114-
public:
115-
Header() {}
116-
117-
virtual ~Header() {}
118-
virtual v8::MaybeLocal<v8::String> GetName(QuicApplication* app) const = 0;
119-
virtual v8::MaybeLocal<v8::String> GetValue(QuicApplication* app) const = 0;
120-
};
121-
122122
enum QuicStreamStates : uint32_t {
123123
// QuicStream is fully open. Readable and Writable
124124
QUICSTREAM_FLAG_INITIAL = 0x0,
@@ -361,7 +361,7 @@ class QuicStream : public AsyncWrap, public StreamBase {
361361
// Returns false if the header cannot be added. This will
362362
// typically only happen if a maximimum number of headers
363363
// has been reached.
364-
bool AddHeader(std::unique_ptr<Header> header);
364+
bool AddHeader(std::unique_ptr<QuicHeader> header);
365365
void EndHeaders();
366366

367367
// Sets the kind of headers currently being processed.
@@ -428,7 +428,7 @@ class QuicStream : public AsyncWrap, public StreamBase {
428428
size_t available_outbound_length_ = 0;
429429
size_t inbound_consumed_data_while_paused_ = 0;
430430

431-
std::vector<std::unique_ptr<Header>> headers_;
431+
std::vector<std::unique_ptr<QuicHeader>> headers_;
432432
QuicStreamHeadersKind headers_kind_;
433433

434434
struct stream_stats {

0 commit comments

Comments
 (0)