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  *
26  *  ControlBase.h - Base class to process generic modifiers to
27  *                         ControlMatcher Directives
28  *
29  *
30  ****************************************************************************/
31 
32 #pragma once
33 
34 #include <vector>
35 
36 #include "tscore/ink_platform.h"
37 
38 class HttpRequestData;
39 class Tokenizer;
40 struct matcher_line;
41 
42 class ControlBase
43 {
44 public:
45   struct Modifier {
46     enum Type {
47       MOD_INVALID,
48       MOD_PORT,
49       MOD_SCHEME,
50       MOD_PREFIX,
51       MOD_SUFFIX,
52       MOD_METHOD,
53       MOD_TIME,
54       MOD_SRC_IP,
55       MOD_IPORT,
56       MOD_TAG,
57       MOD_INTERNAL,
58     };
59     /// Destructor - force virtual.
60     virtual ~Modifier();
61     /// Return the modifier type.
62     virtual Type type() const;
63     /// Return the name for the modifier type.
64     virtual const char *name() const = 0;
65     /** Test if the modifier matches the request.
66         @return @c true if the request is matched, @c false if not.
67     */
68     virtual bool check(HttpRequestData *req ///< Request to check.
69     ) const = 0;
70     /// Print the mod information.
71     virtual void print(FILE *f ///< Output stream.
72     ) const = 0;
73   };
74 
75   ControlBase();
76   ~ControlBase();
77 
78   const char *ProcessModifiers(matcher_line *line_info);
79   bool CheckModifiers(HttpRequestData *request_data);
80   bool CheckForMatch(HttpRequestData *request_data, int last_number);
81 
82   void Print() const;
83 
84   int line_num = 0;
85   Modifier *findModOfType(Modifier::Type t) const;
86 
87 protected:
88   /// Get the text for the Scheme modifier, if any.
89   /// @return The text if present, 0 otherwise.
90   /// @internal Ugly but it's the only place external access is needed.
91   const char *getSchemeModText() const;
92 
93 private:
94   typedef std::vector<Modifier *> Array;
95   Array _mods;
96   const char *ProcessSrcIp(char *val, void **opaque_ptr);
97   const char *ProcessTimeOfDay(char *val, void **opaque_ptr);
98   const char *ProcessPort(char *val, void **opaque_ptr);
99 
100   // Reset to default constructed state, free all allocations.
101   void clear();
102 };
103 
ControlBase()104 inline ControlBase::ControlBase() {}
105 
106 inline bool
CheckForMatch(HttpRequestData * request_data,int last_number)107 ControlBase::CheckForMatch(HttpRequestData *request_data, int last_number)
108 {
109   return (last_number < 0 || last_number > this->line_num) && this->CheckModifiers(request_data);
110 }
111