Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/common/http/http1/balsa_parser.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <memory>
4
5
#include "source/common/http/http1/parser.h"
6
7
#include "absl/base/attributes.h"
8
#include "quiche/balsa/balsa_enums.h"
9
#include "quiche/balsa/balsa_frame.h"
10
#include "quiche/balsa/balsa_headers.h"
11
#include "quiche/balsa/balsa_visitor_interface.h"
12
13
namespace Envoy {
14
namespace Http {
15
namespace Http1 {
16
17
// This class wraps BalsaFrame and BalsaHeaders into a Parser implementation
18
// to be used by ConnectionImpl.
19
class BalsaParser : public Parser, public quiche::BalsaVisitorInterface {
20
public:
21
  BalsaParser(MessageType type, ParserCallbacks* connection, size_t max_header_length,
22
              bool enable_trailers, bool allow_custom_methods);
23
19.3k
  ~BalsaParser() override = default;
24
25
  // Http1::Parser implementation
26
  size_t execute(const char* slice, int len) override;
27
  void resume() override;
28
  CallbackResult pause() override;
29
  ParserStatus getStatus() const override;
30
  Http::Code statusCode() const override;
31
  bool isHttp11() const override;
32
  absl::optional<uint64_t> contentLength() const override;
33
  bool isChunked() const override;
34
  absl::string_view methodName() const override;
35
  absl::string_view errorMessage() const override;
36
  int hasTransferEncoding() const override;
37
38
private:
39
  // quiche::BalsaVisitorInterface implementation
40
  // TODO(bnc): Encapsulate in a private object.
41
  void OnRawBodyInput(absl::string_view input) override;
42
  void OnBodyChunkInput(absl::string_view input) override;
43
  void OnHeaderInput(absl::string_view input) override;
44
  void OnHeader(absl::string_view key, absl::string_view value) override;
45
  void OnTrailerInput(absl::string_view input) override;
46
  void OnTrailers(std::unique_ptr<quiche::BalsaHeaders> trailers) override;
47
  void ProcessHeaders(const quiche::BalsaHeaders& headers) override;
48
0
  void ProcessTrailers(const quiche::BalsaHeaders& /*trailer*/) override{};
49
  void OnRequestFirstLineInput(absl::string_view line_input, absl::string_view method_input,
50
                               absl::string_view request_uri,
51
                               absl::string_view version_input) override;
52
  void OnResponseFirstLineInput(absl::string_view line_input, absl::string_view version_input,
53
                                absl::string_view status_input,
54
                                absl::string_view reason_input) override;
55
  void OnChunkLength(size_t chunk_length) override;
56
  void OnChunkExtensionInput(absl::string_view input) override;
57
  void OnInterimHeaders(std::unique_ptr<quiche::BalsaHeaders> headers) override;
58
  void HeaderDone() override;
59
  void ContinueHeaderDone() override;
60
  void MessageDone() override;
61
  void HandleError(quiche::BalsaFrameEnums::ErrorCode error_code) override;
62
  void HandleWarning(quiche::BalsaFrameEnums::ErrorCode error_code) override;
63
64
  // Shared implementation for ProcessHeaders() and OnTrailers().
65
  void validateAndProcessHeadersOrTrailersImpl(const quiche::BalsaHeaders& headers, bool trailers);
66
67
  // Return ParserStatus::Error if `result` is CallbackResult::Error.
68
  // Return current value of `status_` otherwise.
69
  // Typical use would be `status_ = convertResult(result);`
70
  ABSL_MUST_USE_RESULT ParserStatus convertResult(CallbackResult result) const;
71
72
  quiche::BalsaFrame framer_;
73
  quiche::BalsaHeaders headers_;
74
75
  const MessageType message_type_ = MessageType::Request;
76
  ParserCallbacks* connection_ = nullptr;
77
  const bool enable_trailers_ = false;
78
  const bool allow_custom_methods_ = false;
79
  bool first_byte_processed_ = false;
80
  bool headers_done_ = false;
81
  ParserStatus status_ = ParserStatus::Ok;
82
  // An error message, often seemingly arbitrary to match http-parser behavior.
83
  absl::string_view error_message_;
84
};
85
86
} // namespace Http1
87
} // namespace Http
88
} // namespace Envoy