@@ -458,6 +458,18 @@ ReadOrParseState Filter::readProxyHeader(Network::ListenerFilterBuffer& buffer)
458
458
for (; search_index_ < raw_slice.len_ ; search_index_++) {
459
459
if (buf[search_index_] == ' \n ' && buf[search_index_ - 1 ] == ' \r ' ) {
460
460
if (search_index_ == 1 ) {
461
+ if (config_.get ()->allowRequestsWithoutProxyProtocol ()) {
462
+ // we need to check if what we have already could be v2 proxy protocol;
463
+ // if it cannot be, then we might as well forward now
464
+ auto matchv2 = !memcmp (buf, PROXY_PROTO_V2_SIGNATURE,
465
+ std::min<size_t >(PROXY_PROTO_V2_SIGNATURE_LEN, raw_slice.len_ ));
466
+ if (!matchv2) {
467
+ // the bytes we have seen so far do not match v1 or v2 proxy protocol, so we can
468
+ // safely short-circuit
469
+ ENVOY_LOG (debug, " request does not use v1 or v2 proxy protocol, forwarding as is" );
470
+ return ReadOrParseState::SkipFilter;
471
+ }
472
+ }
461
473
// There is not enough data to determine if it contains the v2 protocol signature, so wait
462
474
// for more data.
463
475
break ;
@@ -469,13 +481,6 @@ ReadOrParseState Filter::readProxyHeader(Network::ListenerFilterBuffer& buffer)
469
481
} else if (config_.get ()->allowRequestsWithoutProxyProtocol ()) {
470
482
if (search_index_ < PROXY_PROTO_V1_SIGNATURE_LEN &&
471
483
buf[search_index_] != PROXY_PROTO_V1_SIGNATURE[search_index_]) {
472
- possibly_v1_ = false ;
473
- }
474
- if (search_index_ < PROXY_PROTO_V2_SIGNATURE_LEN &&
475
- buf[search_index_] != PROXY_PROTO_V2_SIGNATURE[search_index_]) {
476
- possibly_v2_ = false ;
477
- }
478
- if (!possibly_v1_ && !possibly_v2_) {
479
484
// the bytes we have seen so far do not match v1 or v2 proxy protocol, so we can safely
480
485
// short-circuit
481
486
ENVOY_LOG (debug, " request does not use v1 or v2 proxy protocol, forwarding as is" );
0 commit comments