forked from YahooArchive/http-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts-impl.h
168 lines (129 loc) · 4.93 KB
/
ts-impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2015, Yahoo Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
#ifndef TS_IMPL_H
#define TS_IMPL_H
#include <string>
#include <cstring>
#include <ts/ts.h>
#include "base-impl.h"
#include "string-view.h"
#include "ts.h"
namespace http {
namespace filters {
struct TSImplementation : BaseImplementation {
std::string tag_;
TSMBuffer buffer_;
TSMLoc location_;
TSMLoc url_;
Headers headers_;
QueryParameters queryParameters_;
Cookies cookies_;
~TSImplementation() {
ASSERT(buffer_ != NULL);
ASSERT(location_ != NULL);
if (url_ != NULL) {
TSHandleMLocRelease(buffer_, location_, url_);
}
}
TSImplementation(const char * const t, const TSMBuffer & b, const TSMLoc & l) :
tag_(t), buffer_(b), location_(l), url_(NULL) { }
inline Headers & headers(void) {
if (headers_.empty()) {
headers_ = Headers(buffer_, location_);
}
return headers_;
}
inline Cookies & cookies(void) {
if (cookies_.empty()) {
Headers::Result r = headers()["Cookie"];
if (r.second && ! r.first->empty()) {
cookies_ = Cookies((*r.first)[0]);
}
}
return cookies_;
}
inline QueryParameters & queryParameters(void) {
if (queryParameters_.empty()) {
queryParameters_ = QueryParameters(queryParameter(buffer_));
}
return queryParameters_;
}
bool PrintError(const char * const c, const char * const l) const {
TSError("[%s] %s\n", strlen(l) > 0 ? l : tag_.c_str(), c);
return true;
}
bool PrintDebug(const char * const c, const char * const l) const {
TSDebug(strlen(l) > 0 ? l : tag_.c_str(), "%s", c);
return true;
}
bool IsMethod(const char * const, const uint32_t) const;
inline TSMLoc & url(void) {
if (url_ == NULL) {
TSHttpHdrUrlGet(buffer_, location_, &url_);
ASSERT(url_ != NULL);
}
return url_;
}
bool IsScheme(const char * const, const uint32_t);
bool ContainsDomain(const char * const, const uint32_t);
bool EqualDomain(const char * const, const uint32_t);
inline bool NotEqualDomain(const char * const a, const uint32_t b) {
return ! EqualDomain(a, b);
}
bool StartsWithDomain(const char * const, const uint32_t, const uint32_t);
bool ContainsPath(const char * const, const uint32_t);
bool EqualPath(const char * const, const uint32_t);
inline bool NotEqualPath(const char * const a, const uint32_t b) {
return ! EqualPath(a, b);
}
bool StartsWithPath(const char * const, const uint32_t, const uint32_t);
util::StringView queryParameter(const TSMBuffer & b) {
int length = 0;
const char * const pointer = TSUrlHttpQueryGet(buffer_, url(), &length);
return util::StringView(pointer, length);
}
bool ContainsQueryParameter(const char * const, const char * const);
bool EqualQueryParameter(const char * const, const char * const);
inline bool ExistsQueryParameter(const char * const a) {
return queryParameters()[a].second;
}
bool GreaterThanQueryParameter(const char * const, const int64_t);
bool GreaterThanAfterQueryParameter(const char * const, const char * const, const int64_t);
bool LessThanQueryParameter(const char * const, const int64_t);
bool LessThanAfterQueryParameter(const char * const, const char * const, const int64_t);
inline bool NotEqualQueryParameter(const char * const a, const char * const b) {
return ExistsQueryParameter(a) && ! EqualQueryParameter(a, b);
}
bool StartsWithQueryParameter(const char * const, const char * const, const uint32_t);
bool ContainsHeader(const char * const, const char * const);
bool EqualHeader(const char * const, const char * const);
inline bool ExistsHeader(const char * const a) {
return headers()[a].second;
}
bool GreaterThanHeader(const char * const, const int64_t);
bool GreaterThanAfterHeader(const char * const, const char * const, const int64_t);
bool LessThanHeader(const char * const, const int64_t);
bool LessThanAfterHeader(const char * const, const char * const, const int64_t);
inline bool NotEqualHeader(const char * const a, const char * const b) {
return ExistsHeader(a) && ! EqualHeader(a, b);
}
bool StartsWithHeader(const char * const, const char * const, const uint32_t);
bool ContainsCookie(const char * const, const char * const);
bool EqualCookie(const char * const, const char * const);
inline bool ExistsCookie(const char * const a) {
return cookies()[a].second;
}
bool GreaterThanCookie(const char * const, const int64_t);
bool GreaterThanAfterCookie(const char * const, const char * const, const int64_t);
bool LessThanCookie(const char * const, const int64_t);
bool LessThanAfterCookie(const char * const, const char * const, const int64_t);
inline bool NotEqualCookie(const char * const a, const char * const b) {
return ExistsCookie(a) && ! EqualCookie(a, b);
}
};
} //end of filters namespace
} //end of http namespace
#endif //TS_IMPL_H