Coverage Report

Created: 2026-10-31 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/trafficserver/include/proxy/http/remap/UrlMappingPathIndex.h
Line
Count
Source
1
/** @file
2
3
    A brief file description
4
5
    @section license License
6
7
    Licensed to the Apache Software Foundation (ASF) under one
8
    or more contributor license agreements.  See the NOTICE file
9
    distributed with this work for additional information
10
    regarding copyright ownership.  The ASF licenses this file
11
    to you under the Apache License, Version 2.0 (the
12
    "License"); you may not use this file except in compliance
13
    with the License.  You may obtain a copy of the License at
14
15
    http://www.apache.org/licenses/LICENSE-2.0
16
17
    Unless required by applicable law or agreed to in writing, software
18
    distributed under the License is distributed on an "AS IS" BASIS,
19
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
    See the License for the specific language governing permissions and
21
    limitations under the License.
22
*/
23
#pragma once
24
25
#include "tscore/ink_platform.h"
26
27
#include <map>
28
29
#include "proxy/hdrs/URL.h"
30
#include "proxy/http/remap/UrlMapping.h"
31
#include "tscore/Trie.h"
32
33
class UrlMappingPathIndex
34
{
35
public:
36
0
  UrlMappingPathIndex() {}
37
  virtual ~UrlMappingPathIndex();
38
  bool         Insert(url_mapping *mapping);
39
  url_mapping *Search(URL *request_url, int request_port, bool normal_search = true) const;
40
  void         Print() const;
41
  std::string  PrintUrlMappingPathIndex() const;
42
43
  // Apply a function to each url_mapping in this index.
44
  // Note: Trie only exposes const_iterator, so const_cast is required.
45
  template <typename Func>
46
  void
47
  foreach_mapping(Func &&f)
48
0
  {
49
0
    for (auto &trie_pair : m_tries) {
50
0
      for (auto const &mapping : *trie_pair.second) {
51
0
        f(const_cast<url_mapping &>(mapping));
52
0
      }
53
0
    }
54
0
  }
55
56
private:
57
  using UrlMappingTrie = Trie<url_mapping>;
58
59
  struct UrlMappingTrieKey {
60
    int scheme_wks_idx;
61
    int port;
62
63
0
    UrlMappingTrieKey(int idx, int p) : scheme_wks_idx(idx), port(p) {}
64
    bool
65
    operator<(const UrlMappingTrieKey &rhs) const
66
0
    {
67
0
      if (scheme_wks_idx == rhs.scheme_wks_idx) {
68
0
        return (port < rhs.port);
69
0
      }
70
0
      return (scheme_wks_idx < rhs.scheme_wks_idx);
71
0
    }
72
  };
73
74
  using UrlMappingGroup = std::map<UrlMappingTrieKey, UrlMappingTrie *>;
75
  UrlMappingGroup m_tries;
76
77
  // make copy-constructor and assignment operator private
78
  // till we properly implement them
79
0
  UrlMappingPathIndex(const UrlMappingPathIndex & /* rhs ATS_UNUSED */){};
80
  UrlMappingPathIndex &
81
  operator=(const UrlMappingPathIndex & /* rhs ATS_UNUSED */)
82
0
  {
83
0
    return *this;
84
0
  }
85
86
  inline UrlMappingTrie *
87
  _GetTrie(URL *url, int &idx, int port, bool search = true) const
88
0
  {
89
0
    idx = url->scheme_get_wksidx();
90
    // If the scheme is empty (e.g. because of a CONNECT method), guess it
91
    // based on port
92
0
    if (idx == -1 && url->scheme_get().empty()) {
93
0
      if (port == 80) {
94
0
        idx = URL_WKSIDX_HTTP;
95
0
      } else {
96
0
        idx = URL_WKSIDX_HTTPS;
97
0
      }
98
0
    }
99
0
    UrlMappingGroup::const_iterator group_iter;
100
0
    if (search) { // normal search
101
0
      group_iter = m_tries.find(UrlMappingTrieKey(idx, port));
102
0
    } else { // return the first trie arbitrarily
103
0
      static DbgCtl dc{"UrlMappingPathIndex::_GetTrie"};
104
0
      Dbg(dc, "Not performing search; will return first available trie");
105
0
      group_iter = m_tries.begin();
106
0
    }
107
0
    if (group_iter != m_tries.end()) {
108
0
      return group_iter->second;
109
0
    }
110
0
    return nullptr;
111
0
  }
112
};