1 /*
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10   http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 */
18 
19 /**
20  * @file aws_auth_v4_ts.h
21  * @brief TS API adapter and header iterator using the TS API which are swapped with mocks during testing.
22  * @see aws_auth_v4.h
23  */
24 
25 #pragma once
26 
27 /* Define a header iterator to be used in the plugin using ATS API */
28 class HeaderIterator
29 {
30 public:
HeaderIterator()31   HeaderIterator() : _hdrs(TS_NULL_MLOC), _field(TS_NULL_MLOC) {}
HeaderIterator(TSMBuffer bufp,TSMLoc hdrs,TSMLoc field)32   HeaderIterator(TSMBuffer bufp, TSMLoc hdrs, TSMLoc field) : _bufp(bufp), _hdrs(hdrs), _field(field) {}
HeaderIterator(const HeaderIterator & it)33   HeaderIterator(const HeaderIterator &it)
34   {
35     _bufp  = it._bufp;
36     _hdrs  = it._hdrs;
37     _field = it._field;
38   }
~HeaderIterator()39   ~HeaderIterator() {}
40   HeaderIterator &
41   operator=(HeaderIterator &it)
42   {
43     _bufp  = it._bufp;
44     _hdrs  = it._hdrs;
45     _field = it._field;
46     return *this;
47   }
48   HeaderIterator &
49   operator++()
50   {
51     /* @todo this is said to be slow in the API call comments, do something better here */
52     TSMLoc next = TSMimeHdrFieldNext(_bufp, _hdrs, _field);
53     TSHandleMLocRelease(_bufp, _hdrs, _field);
54     _field = next;
55     return *this;
56   }
57   HeaderIterator
58   operator++(int)
59   {
60     HeaderIterator tmp(*this);
61     operator++();
62     return tmp;
63   }
64   bool
65   operator!=(const HeaderIterator &it)
66   {
67     return _bufp != it._bufp || _hdrs != it._hdrs || _field != it._field;
68   }
69   bool
70   operator==(const HeaderIterator &it)
71   {
72     return _bufp == it._bufp && _hdrs == it._hdrs && _field == it._field;
73   }
74   const char *
getName(int * len)75   getName(int *len)
76   {
77     return TSMimeHdrFieldNameGet(_bufp, _hdrs, _field, len);
78   }
79   const char *
getValue(int * len)80   getValue(int *len)
81   {
82     return TSMimeHdrFieldValueStringGet(_bufp, _hdrs, _field, -1, len);
83   }
84   TSMBuffer _bufp = nullptr;
85   TSMLoc _hdrs;
86   TSMLoc _field;
87 };
88 
89 /* Define a API to be used in the plugin using ATS API */
90 class TsApi : public TsInterface
91 {
92 public:
TsApi(TSMBuffer bufp,TSMLoc hdrs,TSMLoc url)93   TsApi(TSMBuffer bufp, TSMLoc hdrs, TSMLoc url) : _bufp(bufp), _hdrs(hdrs), _url(url) {}
~TsApi()94   ~TsApi() override {}
95   const char *
getMethod(int * len)96   getMethod(int *len) override
97   {
98     return TSHttpHdrMethodGet(_bufp, _hdrs, len);
99   }
100   const char *
getHost(int * len)101   getHost(int *len) override
102   {
103     return TSHttpHdrHostGet(_bufp, _hdrs, len);
104   }
105   const char *
getPath(int * len)106   getPath(int *len) override
107   {
108     return TSUrlPathGet(_bufp, _url, len);
109   }
110   const char *
getQuery(int * len)111   getQuery(int *len) override
112   {
113     return TSUrlHttpQueryGet(_bufp, _url, len);
114   }
115   HeaderIterator
headerBegin()116   headerBegin() override
117   {
118     return HeaderIterator(_bufp, _hdrs, TSMimeHdrFieldGet(_bufp, _hdrs, 0));
119   }
120   HeaderIterator
headerEnd()121   headerEnd() override
122   {
123     return HeaderIterator(_bufp, _hdrs, TS_NULL_MLOC);
124   }
125   TSMBuffer _bufp;
126   TSMLoc _hdrs;
127   TSMLoc _url;
128 };
129