This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathats-filters.cc
168 lines (136 loc) · 3.84 KB
/
ats-filters.cc
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 ATS_FILTERS
#define ATS_FILTERS
#include <sstream>
#include <ts/ts.h>
#include "compiler.h"
#include "representation.h"
#include "ts-impl.h"
#include "vm-impl.h"
#include "vm-printer.h"
#ifndef PLUGIN_TAG
#error Please define a PLUGIN_TAG before including this file.
#endif
struct Data {
const http::filters::Code code;
const http::filters::Memory memory;
const http::filters::Offsets offsets;
~Data() {
delete code.t;
delete memory.t;
}
Data(const http::filters::Compiler & c,
const http::filters::Offsets & o) :
code(http::filters::Code::Copy(c.assembler_.code())),
memory(http::filters::Memory::Copy(c.assembler_.memory())),
offsets(o) { }
};
static int handler(TSCont continuation, TSEvent event, void * data) {
TSHttpTxn transaction = static_cast< TSHttpTxn >(data);
TSMBuffer buffer;
TSMLoc header;
if (TSHttpTxnClientReqGet(transaction, &buffer, &header) == TS_SUCCESS) {
{
using namespace http::filters;
Data * data = static_cast< Data * >(TSContDataGet(continuation));
ASSERT(data != NULL);
typedef VM< TSImplementation > MyVM;
MyVM vm(TSImplementation(PLUGIN_TAG, buffer, header),
data->code, data->memory);
const char * names[] = {
"http get", "firefox", "yahoo domain", "slash-search",
"california", "city starts with san",
};
for (int i = 0; i < ARRAY_SIZE(names); ++i) {
if (vm.run(data->offsets[i])) {
/*
* replace here with your own logic.
*/
TSDebug(PLUGIN_TAG, "vm result says it is: %s", names[i]);
}
}
}
TSHandleMLocRelease(buffer, TS_NULL_MLOC, header);
}
TSHttpTxnReenable(transaction, TS_EVENT_HTTP_CONTINUE);
return 0;
}
void TSPluginInit(int argc, const char * * argv) {
TSPluginRegistrationInfo info;
info.plugin_name = const_cast< char * >(PLUGIN_TAG);
info.support_email = const_cast< char * >("[email protected]");
info.vendor_name = const_cast< char * >("My Company");
TSCont continuation = TSContCreate(handler, NULL);
ASSERT(continuation != NULL);
{
using namespace http::filters;
Forest f;
{
Tree t;
t.addAnd();
CHILD_OP(t, "isMethod", "GET");
OP(t, "isScheme", "http");
t.parent();
f.push_back(t);
}
{
Tree t;
t.addOr();
t.addChildAnd();
CHILD_OP(t, "existsHeader", "User-Agent");
OP(t, "containsHeader", "User-Agent", "Firefox");
t.parent();
t.addAnd();
CHILD_OP(t, "existsHeader", "user-agent");
OP(t, "containsHeader", "user-agent", "Firefox");
t.parent();
t.parent();
f.push_back(t);
}
{
Tree t;
OP(t, "containsDomain", ".yahoo.com");
f.push_back(t);
}
{
Tree t;
OP(t, "equalPath", "search");
f.push_back(t);
}
{
Tree t;
OP(t, "containsQueryParameter", "state", "california");
f.push_back(t);
}
{
Tree t;
OP(t, "startsWithQueryParameter", "city", "san");
f.push_back(t);
}
{
http::filters::Offsets o;
o.reserve(f.size());
Compiler c;
c.compile(f, o);
cleanAll(f);
{
vm::Printer printer;
std::stringstream ss;
ss << "printing vm code" "\n";
printer.print(c.assembler_.code(),
c.assembler_.memory(), ss);
ss << "\n";
std::cout << ss.str() << std::endl;
}
const Offsets::const_iterator end = o.end();
Offsets::const_iterator it = o.begin();
TSContDataSet(continuation, new Data(c, o));
}
}
TSHttpHookAdd(TS_HTTP_SEND_REQUEST_HDR_HOOK, continuation);
}
#endif //ATS_FILTERS