Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/extensions/filters/network/thrift_proxy/twitter_protocol_impl.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <string>
4
5
#include "envoy/buffer/buffer.h"
6
#include "envoy/common/pure.h"
7
8
#include "source/common/common/macros.h"
9
#include "source/extensions/filters/network/thrift_proxy/binary_protocol_impl.h"
10
11
#include "absl/types/optional.h"
12
13
namespace Envoy {
14
namespace Extensions {
15
namespace NetworkFilters {
16
namespace ThriftProxy {
17
18
/**
19
 * TwitterProtocolImpl implements the Twitter-upgraded (AKA "TTwitter") Thrift protocol.
20
 * See https://twitter.github.io/finagle/docs/com/twitter/finagle/Thrift$ and
21
 * https://github.com/twitter/finagle/blob/master/finagle-thrift/src/main/thrift/tracing.thrift
22
 */
23
class TwitterProtocolImpl : public BinaryProtocolImpl {
24
public:
25
  // Protocol
26
0
  const std::string& name() const override { return ProtocolNames::get().TWITTER; }
27
0
  ProtocolType type() const override { return ProtocolType::Twitter; }
28
  bool readMessageBegin(Buffer::Instance& buffer, MessageMetadata& metadata) override;
29
  void writeMessageBegin(Buffer::Instance& buffer, const MessageMetadata& metadata) override;
30
0
  bool supportsUpgrade() override { return true; }
31
  DecoderEventHandlerSharedPtr upgradeRequestDecoder() override;
32
  DirectResponsePtr upgradeResponse(const DecoderEventHandler& decoder) override;
33
  ThriftObjectPtr attemptUpgrade(Transport& transport, ThriftConnectionState& state,
34
                                 Buffer::Instance& buffer) override;
35
  void completeUpgrade(ThriftConnectionState& state, ThriftObject& response) override;
36
37
  /**
38
   * @return true if the protocol upgrade was success, false if not, no value if the result is not
39
   *         yet known
40
   */
41
0
  absl::optional<bool> upgraded() { return upgraded_; }
42
43
  /**
44
   * @return std::string containing the "improbably-named method" used for Twitter protocol upgrade.
45
   */
46
0
  static const std::string& upgradeMethodName() {
47
    // https://github.com/twitter/finagle/blob/master/finagle-thrift/src/main/scala/com/twitter/finagle/thrift/ThriftTracing.scala
48
0
    CONSTRUCT_ON_FIRST_USE(std::string, "__can__finagle__trace__v3__");
49
0
  }
50
51
  /**
52
   * @return true if the buffer (minimum 12 bytes) appears to start with a Twitter protocol
53
   *         upgrade message, false otherwise
54
   */
55
  static bool isUpgradePrefix(Buffer::Instance& buffer);
56
57
protected:
58
  static void updateMetadataWithRequestHeader(const ThriftObject& header_object,
59
                                              MessageMetadata& metadata);
60
  static void updateMetadataWithResponseHeader(const ThriftObject& header_object,
61
                                               MessageMetadata& metadata);
62
  static void writeRequestHeader(Buffer::Instance& buffer, const MessageMetadata& metadata);
63
  static void writeResponseHeader(Buffer::Instance& buffer, const MessageMetadata& metadata);
64
  static ThriftObjectPtr newHeader();
65
66
private:
67
  ThriftObjectPtr header_;
68
  bool header_complete_{false};
69
  absl::optional<bool> upgraded_;
70
};
71
72
} // namespace ThriftProxy
73
} // namespace NetworkFilters
74
} // namespace Extensions
75
} // namespace Envoy