Coverage Report

Created: 2024-09-19 09:45

/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
0
void populateFallbackResponseHeaders(Http::Code code, Http::ResponseHeaderMap& header_map) {
11
0
  header_map.setStatus(std::to_string(enumToInt(code)));
12
0
  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
0
  if (header_map.get(Http::CustomHeaders::get().CacheControl).empty()) {
18
0
    header_map.setReference(Http::CustomHeaders::get().CacheControl,
19
0
                            Http::CustomHeaders::get().CacheControlValues.NoCacheMaxAge0);
20
0
  }
21
22
  // Under no circumstance should browsers sniff content-type.
23
0
  header_map.addReference(Http::Headers::get().XContentTypeOptions,
24
0
                          Http::Headers::get().XContentTypeOptionValues.Nosniff);
25
0
}
26
27
// Helper method to get the histogram_buckets parameter. Returns an InvalidArgumentError
28
// if histogram_buckets query param is found and value is not "cumulative" or "disjoint",
29
// Ok otherwise.
30
absl::Status histogramBucketsParam(const Http::Utility::QueryParamsMulti& params,
31
0
                                   HistogramBucketsMode& histogram_buckets_mode) {
32
0
  absl::optional<std::string> histogram_buckets_query_param =
33
0
      nonEmptyQueryParam(params, "histogram_buckets");
34
0
  histogram_buckets_mode = HistogramBucketsMode::Unset;
35
0
  if (histogram_buckets_query_param.has_value()) {
36
0
    if (histogram_buckets_query_param.value() == "cumulative") {
37
0
      histogram_buckets_mode = HistogramBucketsMode::Cumulative;
38
0
    } else if (histogram_buckets_query_param.value() == "disjoint") {
39
0
      histogram_buckets_mode = HistogramBucketsMode::Disjoint;
40
0
    } else if (histogram_buckets_query_param.value() == "detailed") {
41
0
      histogram_buckets_mode = HistogramBucketsMode::Detailed;
42
      // "none" is a synonym for "summary", and exists to maintain backwards compatibility
43
0
    } else if (histogram_buckets_query_param.value() == "summary" ||
44
0
               histogram_buckets_query_param.value() == "none") {
45
0
      histogram_buckets_mode = HistogramBucketsMode::Summary;
46
0
    } else {
47
0
      return absl::InvalidArgumentError(
48
0
          "usage: /stats?histogram_buckets=(cumulative|disjoint|detailed|summary)\n");
49
0
    }
50
0
  }
51
0
  return absl::OkStatus();
52
0
}
53
54
// Helper method to get a query parameter.
55
// Returns the first value for that query parameter, unless that value is empty.
56
// In that case, it returns nullopt.
57
absl::optional<std::string> nonEmptyQueryParam(const Http::Utility::QueryParamsMulti& params,
58
0
                                               const std::string& key) {
59
0
  const auto data = params.getFirstValue(key);
60
0
  if (data.has_value() && data.value().empty()) {
61
0
    return absl::nullopt;
62
0
  }
63
0
  return data;
64
0
}
65
66
// Helper method to get the format parameter.
67
0
absl::optional<std::string> formatParam(const Http::Utility::QueryParamsMulti& params) {
68
0
  return nonEmptyQueryParam(params, "format");
69
0
}
70
71
} // namespace Utility
72
} // namespace Server
73
} // namespace Envoy