Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/server/admin/utils.cc
Line
Count
Source (jump to first uncovered line)
1
#include "source/server/admin/utils.h"
2
3
#include "source/common/common/enum_to_int.h"
4
#include "source/common/http/headers.h"
5
6
namespace Envoy {
7
namespace Server {
8
namespace Utility {
9
10
2.64k
void populateFallbackResponseHeaders(Http::Code code, Http::ResponseHeaderMap& header_map) {
11
2.64k
  header_map.setStatus(std::to_string(enumToInt(code)));
12
2.64k
  if (header_map.ContentType() == nullptr) {
13
    // Default to text-plain if unset.
14
0
    header_map.setReferenceContentType(Http::Headers::get().ContentTypeValues.TextUtf8);
15
0
  }
16
  // Default to 'no-cache' if unset, but not 'no-store' which may break the back button.
17
2.64k
  if (header_map.get(Http::CustomHeaders::get().CacheControl).empty()) {
18
2.64k
    header_map.setReference(Http::CustomHeaders::get().CacheControl,
19
2.64k
                            Http::CustomHeaders::get().CacheControlValues.NoCacheMaxAge0);
20
2.64k
  }
21
22
  // Under no circumstance should browsers sniff content-type.
23
2.64k
  header_map.addReference(Http::Headers::get().XContentTypeOptions,
24
2.64k
                          Http::Headers::get().XContentTypeOptionValues.Nosniff);
25
2.64k
}
26
27
// Helper method to get the histogram_buckets parameter. Returns false if histogram_buckets query
28
// param is found and value is not "cumulative" or "disjoint", true otherwise.
29
absl::Status histogramBucketsParam(const Http::Utility::QueryParams& params,
30
0
                                   HistogramBucketsMode& histogram_buckets_mode) {
31
0
  absl::optional<std::string> histogram_buckets_query_param =
32
0
      queryParam(params, "histogram_buckets");
33
0
  histogram_buckets_mode = HistogramBucketsMode::NoBuckets;
34
0
  if (histogram_buckets_query_param.has_value()) {
35
0
    if (histogram_buckets_query_param.value() == "cumulative") {
36
0
      histogram_buckets_mode = HistogramBucketsMode::Cumulative;
37
0
    } else if (histogram_buckets_query_param.value() == "disjoint") {
38
0
      histogram_buckets_mode = HistogramBucketsMode::Disjoint;
39
0
    } else if (histogram_buckets_query_param.value() == "detailed") {
40
0
      histogram_buckets_mode = HistogramBucketsMode::Detailed;
41
0
    } else if (histogram_buckets_query_param.value() != "none") {
42
0
      return absl::InvalidArgumentError(
43
0
          "usage: /stats?histogram_buckets=(cumulative|disjoint|none)\n");
44
0
    }
45
0
  }
46
0
  return absl::OkStatus();
47
0
}
48
49
// Helper method to get the format parameter.
50
0
absl::optional<std::string> formatParam(const Http::Utility::QueryParams& params) {
51
0
  return queryParam(params, "format");
52
0
}
53
54
// Helper method to get a query parameter.
55
absl::optional<std::string> queryParam(const Http::Utility::QueryParams& params,
56
7.94k
                                       const std::string& key) {
57
7.94k
  const auto iter = params.find(key);
58
7.94k
  if (iter != params.end()) {
59
0
    const std::string& value = iter->second;
60
0
    if (!value.empty()) {
61
0
      return value;
62
0
    }
63
0
  }
64
7.94k
  return absl::nullopt;
65
7.94k
}
66
67
} // namespace Utility
68
} // namespace Server
69
} // namespace Envoy