| /** @file | |
| A brief file description | |
| @par License | |
| Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you under the Apache License, Version 2.0 (the | |
| "License"); you may not use this file except in compliance | |
| with the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| */ | |
| #pragma once | |
| #include "tscore/ink_platform.h" | |
| #include <map> | |
| #include <string> | |
| #include <string_view> | |
| #include "proxy/hdrs/URL.h" | |
| #include "proxy/http/remap/UrlMapping.h" | |
| #include "tscore/Trie.h" | |
| class UrlMappingPathIndex | |
| { | |
| public: | |
| explicit UrlMappingPathIndex(std::string_view host) : m_host(host) {} | |
| virtual ~UrlMappingPathIndex(); | |
| std::string_view | |
| host() const | |
| { | |
| return m_host; | |
| } | |
| bool Insert(url_mapping *mapping); | |
| url_mapping *Search(URL *request_url, int request_port, bool normal_search = true) const; | |
| void Print() const; | |
| // Apply a function to each url_mapping in this index. | |
| // Note: Trie only exposes const_iterator, so const_cast is required. | |
| template <typename Func> | |
| void | |
| foreach_mapping(Func &&f) | |
| { | |
| for (auto &trie_pair : m_tries) { | |
| for (auto const &mapping : *trie_pair.second) { | |
| f(const_cast<url_mapping &>(mapping)); | |
| } | |
| } | |
| } | |
| private: | |
| using UrlMappingTrie = Trie<url_mapping>; | |
| struct UrlMappingTrieKey { | |
| int scheme_wks_idx; | |
| int port; | |
| UrlMappingTrieKey(int idx, int p) : scheme_wks_idx(idx), port(p) {} | |
| bool | |
| operator<(const UrlMappingTrieKey &rhs) const | |
| { | |
| if (scheme_wks_idx == rhs.scheme_wks_idx) { | |
| return (port < rhs.port); | |
| } | |
| return (scheme_wks_idx < rhs.scheme_wks_idx); | |
| } | |
| }; | |
| using UrlMappingGroup = std::map<UrlMappingTrieKey, UrlMappingTrie *>; | |
| UrlMappingGroup m_tries; | |
| std::string m_host; | |
| // make copy-constructor and assignment operator private | |
| // till we properly implement them | |
| UrlMappingPathIndex(const UrlMappingPathIndex & /* rhs ATS_UNUSED */){}; | |
| UrlMappingPathIndex & | |
| operator=(const UrlMappingPathIndex & /* rhs ATS_UNUSED */) | |
| { | |
| return *this; | |
| } | |
| inline UrlMappingTrie * | |
| _GetTrie(URL *url, int &idx, int port, bool search = true) const | |
| { | |
| idx = url->scheme_get_wksidx(); | |
| // If the scheme is empty (e.g. because of a CONNECT method), guess it | |
| // based on port | |
| if (idx == -1 && url->scheme_get().empty()) { | |
| if (port == 80) { | |
| idx = URL_WKSIDX_HTTP; | |
| } else { | |
| idx = URL_WKSIDX_HTTPS; | |
| } | |
| } | |
| UrlMappingGroup::const_iterator group_iter; | |
| if (search) { // normal search | |
| group_iter = m_tries.find(UrlMappingTrieKey(idx, port)); | |
| } else { // return the first trie arbitrarily | |
| static DbgCtl dc{"UrlMappingPathIndex::_GetTrie"}; | |
| Dbg(dc, "Not performing search; will return first available trie"); | |
| group_iter = m_tries.begin(); | |
| } | |
| if (group_iter != m_tries.end()) { | |
| return group_iter->second; | |
| } | |
| return nullptr; | |
| } | |
| }; |