xref: /trafficserver/proxy/http/remap/UrlMapping.h (revision 128507ae)
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  */
24 
25 #pragma once
26 
27 #include <vector>
28 
29 #include "tscore/ink_config.h"
30 #include "AclFiltering.h"
31 #include "URL.h"
32 #include "RemapPluginInfo.h"
33 #include "PluginFactory.h"
34 #include "tscore/Regex.h"
35 #include "tscore/List.h"
36 
37 class NextHopSelectionStrategy;
38 
39 /**
40  * Used to store http referer strings (and/or regexp)
41  **/
42 class referer_info
43 {
44 public:
45   referer_info(char *_ref, bool *error_flag = nullptr, char *errmsgbuf = nullptr, int errmsgbuf_size = 0);
46   ~referer_info();
47   referer_info *next;
48   char *referer;
49   int referer_size;
50   bool any;      /* any flag '*' */
51   bool negative; /* negative referer '~' */
52   bool regx_valid;
53   pcre *regx;
54 };
55 
56 /**
57  *
58  **/
59 class redirect_tag_str
60 {
61 public:
redirect_tag_str()62   redirect_tag_str() {}
~redirect_tag_str()63   ~redirect_tag_str()
64   {
65     type = 0;
66     if (chunk_str) {
67       ats_free(chunk_str);
68       chunk_str = nullptr;
69     }
70   }
71 
72   redirect_tag_str *next = nullptr;
73   char *chunk_str        = nullptr;
74   char type              = 0; /* s - string, r - referer, t - url_to, f - url_from, o - origin url */
75   static redirect_tag_str *parse_format_redirect_url(char *url);
76 };
77 
78 /**
79  * Used to store the mapping for class UrlRewrite
80  **/
81 class url_mapping
82 {
83 public:
84   ~url_mapping();
85 
86   bool add_plugin_instance(RemapPluginInst *i);
87   RemapPluginInst *get_plugin_instance(std::size_t) const;
88 
89   std::size_t
plugin_instance_count() const90   plugin_instance_count() const
91   {
92     return _plugin_inst_list.size();
93   }
94 
95   void Print();
96 
97   int from_path_len = 0;
98   URL fromURL;
99   URL toURL; // Default TO-URL (from remap.config)
100   bool homePageRedirect              = false;
101   bool unique                        = false; // INKqa11970 - unique mapping
102   bool default_redirect_url          = false;
103   bool optional_referer              = false;
104   bool negative_referer              = false;
105   bool wildcard_from_scheme          = false;   // from url is '/foo', only http or https for now
106   char *tag                          = nullptr; // tag
107   char *filter_redirect_url          = nullptr; // redirect url when referer filtering enabled
108   unsigned int map_id                = 0;
109   referer_info *referer_list         = nullptr;
110   redirect_tag_str *redir_chunk_list = nullptr;
111   bool ip_allow_check_enabled_p      = false;
112   acl_filter_rule *filter            = nullptr; // acl filtering (list of rules)
113   LINK(url_mapping, link);                      // For use with the main Queue linked list holding all the mapping
114   std::shared_ptr<NextHopSelectionStrategy> strategy = nullptr;
115 
116   int
getRank() const117   getRank() const
118   {
119     return _rank;
120   };
121   void
setRank(int rank)122   setRank(int rank)
123   {
124     _rank = rank;
125   };
126 
127 private:
128   std::vector<RemapPluginInst *> _plugin_inst_list;
129   int _rank = 0;
130 };
131 
132 /**
133  * UrlMappingContainer wraps a url_mapping object and allows a caller to rewrite the target URL.
134  * This is used while evaluating remap rules.
135  **/
136 class UrlMappingContainer
137 {
138 public:
UrlMappingContainer()139   UrlMappingContainer() {}
UrlMappingContainer(HdrHeap * heap)140   explicit UrlMappingContainer(HdrHeap *heap) : _heap(heap) {}
~UrlMappingContainer()141   ~UrlMappingContainer() { deleteToURL(); }
142   URL *
getToURL() const143   getToURL() const
144   {
145     return _toURLPtr;
146   };
147   URL *
getFromURL() const148   getFromURL() const
149   {
150     return _mapping ? &(_mapping->fromURL) : nullptr;
151   };
152 
153   url_mapping *
getMapping() const154   getMapping() const
155   {
156     return _mapping;
157   };
158 
159   void
set(url_mapping * m)160   set(url_mapping *m)
161   {
162     deleteToURL();
163     _mapping  = m;
164     _toURLPtr = m ? &(m->toURL) : nullptr;
165   }
166 
167   void
set(HdrHeap * heap)168   set(HdrHeap *heap)
169   {
170     _heap = heap;
171   }
172 
173   URL *
createNewToURL()174   createNewToURL()
175   {
176     ink_assert(_heap != nullptr);
177     deleteToURL();
178     _toURL.create(_heap);
179     _toURLPtr = &_toURL;
180     return _toURLPtr;
181   }
182 
183   void
deleteToURL()184   deleteToURL()
185   {
186     if (_toURLPtr == &_toURL) {
187       _toURL.clear();
188     }
189   }
190 
191   void
clear()192   clear()
193   {
194     deleteToURL();
195     _mapping  = nullptr;
196     _toURLPtr = nullptr;
197     _heap     = nullptr;
198   }
199 
200   // noncopyable, non-assignable
201   UrlMappingContainer(const UrlMappingContainer &orig) = delete;
202   UrlMappingContainer &operator=(const UrlMappingContainer &rhs) = delete;
203 
204 private:
205   url_mapping *_mapping = nullptr;
206   URL *_toURLPtr        = nullptr;
207   URL _toURL;
208   HdrHeap *_heap = nullptr;
209 };
210