/
test-hns-plugin.c
253 lines (195 loc) · 7.38 KB
/
test-hns-plugin.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
prefetch-plugin-eg1.c : an example plugin which interacts with
Traffic Server's prefetch feature
*/
/*
This plugin tests the Parent Traffic Server handling of parse/prefetch
rules. It prints information to 'stdout' at various stages to verify the
correctness of the parse/prefetch module. It has the following options:
-p. If 0, the plugin returns INK_PREFETCH_DISCONTINUE when called at the
INK_PREFETCH_PRE_PARSE_HOOK. If 1, the plugin returns
INK_PREFETCH_CONTINUE.
-u. If 0, the plugin returns INK_PREFETCH_DISCONTINUE when called at the
INK_PREFETCH_EMBEDDED_URL_HOOK. If 1, the plugin returns
INK_PREFETCH_CONTINUE.
-o. If 1, the plugin sets 'object_buf_status' field in the INKPrefetchInfo to
INK_PREFETCH_OBJ_BUF_NEEDED and expects to be called back with the object.
If 2, this field is set to INK_PREFETCH_OBJ_BUF_NEEDED_N_TRANSMITTED
which implies the object is transmitted to the child as well.
-i. If 0, the plugin sets the 'url_response_proto' field in the
INKPrefetchInfo to INK_PREFETCH_PROTO_UDP. If 1, it sets the
'url_response_proto' field to INK_PREFETCH_PROTO_TCP.
-d. Specifies the directory where the plugin will store all the prefetched
objects. All prefetched objects are stored in the PkgPreload format in
the 'prefetched.objects' file in this directory.
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ts/ts.h>
#include <ts/ts_private.h>
/* We will register the following two hooks */
#define TAG "test-hns-plugin"
INKFile filep1 = NULL;
static int pre_parse_cont = 0;
static int embedded_url_cont = 0;
static int url_proto = 0;
static int embedded_object = 0;
static INKMutex file_write_mutex;
int pre_parse_hook(int hook, INKPrefetchInfo * info);
int embedded_url_hook(int hook, INKPrefetchInfo * info);
int embedded_object_hook(int hook, INKPrefetchInfo * info);
int
embedded_object_hook(int hook, INKPrefetchInfo * info)
{
int total_avail;
INKIOBufferBlock block;
const char *block_start;
int block_avail;
INKMutexLock(file_write_mutex);
printf("(%s) >>> INK_PREFETCH_EMBEDDED_OBJECT_HOOK (%d)\n", TAG, hook);
printf("(%s) \tobject size for: %s is %d\n",
TAG, info->embedded_url, INKIOBufferReaderAvail(info->object_buf_reader));
/* Get the embedded objects here */
total_avail = INKIOBufferReaderAvail(info->object_buf_reader);
block = INKIOBufferReaderStart(info->object_buf_reader);
while (block) {
block_start = INKIOBufferBlockReadStart(block, info->object_buf_reader, &block_avail);
if (block_avail == 0) {
break;
}
/* Put the object fragment into the file */
INKfwrite(filep1, block_start, block_avail);
INKfflush(filep1);
INKIOBufferReaderConsume(info->object_buf_reader, block_avail);
block = INKIOBufferReaderStart(info->object_buf_reader);
}
INKIOBufferDestroy(info->object_buf);
INKMutexUnlock(file_write_mutex);
return 0;
}
int
embedded_url_hook(int hook, INKPrefetchInfo * info)
{
unsigned char *ip = (unsigned char *) &info->client_ip;
printf("(%s) >>> INK_PREFETCH_EMBEDDED_URL_HOOK (%d)\n", TAG, hook);
printf("(%s) \tURL: %s %s Child IP: %u.%u.%u.%u\n",
TAG, info->embedded_url, (info->present_in_cache) ? "(present in cache)" : "", ip[0], ip[1], ip[2], ip[3]);
/* We will select UDP for sending url and TCP for sending object */
if (embedded_object)
info->object_buf_status = (embedded_object == 1)
? INK_PREFETCH_OBJ_BUF_NEEDED : INK_PREFETCH_OBJ_BUF_NEEDED_N_TRANSMITTED;
if (url_proto)
info->url_response_proto = INK_PREFETCH_PROTO_TCP;
else
info->url_response_proto = INK_PREFETCH_PROTO_UDP;
if (!embedded_url_cont) {
/* This will cause parent TS not to parse the HTML page */
printf("(%s) \tPlugin returns - INK_PREFETCH_DISCONTINUE\n", TAG);
return INK_PREFETCH_DISCONTINUE;
} else {
/* This will cause TS (MDE plugin) to prefetch the URL */
printf("(%s) \tURL Response Protocol: %s\n", TAG,
(info->url_response_proto == INK_PREFETCH_PROTO_TCP) ? "INK_PREFETCH_PROTO_TCP" : "INK_PREFETCH_PROTO_UDP");
printf("(%s) \tPlugin returns - INK_PREFETCH_CONTINUE\n", TAG);
return INK_PREFETCH_CONTINUE;
}
}
int
pre_parse_hook(int hook, INKPrefetchInfo * info)
{
unsigned char *ip = (unsigned char *) &info->client_ip;
printf("(%s) >>> INK_PREFETCH_PRE_PARSE_HOOK (%d)\n", TAG, hook);
printf("(%s) \tChild IP : %u.%u.%u.%u\n", TAG, ip[0], ip[1], ip[2], ip[3]);
if (!pre_parse_cont) {
/* This will cause parent TS not to parse the HTML page */
printf("(%s) \tPlugin returns - INK_PREFETCH_DISCONTINUE\n", TAG);
return INK_PREFETCH_DISCONTINUE;
} else {
/* we will let TS parse the page */
printf("(%s) \tPlugin returns - INK_PREFETCH_CONTINUE\n", TAG);
return INK_PREFETCH_CONTINUE;
}
}
void
INKPluginInit(int argc, const char *argv[])
{
int c, arg;
extern char *optarg;
extern int optind, opterr, optopt;
INKPluginRegistrationInfo plugin_info;
char file_name[512] = { 0 };
plugin_info.plugin_name = "test-prefetch";
plugin_info.vendor_name = "MyCompany";
plugin_info.support_email = "ts-api-support@MyCompany.com";
if (!INKPluginRegister(INK_SDK_VERSION_2_0, &plugin_info)) {
INKError("Plugin registration failed.\n");
return;
}
while ((c = getopt(argc, (char *const *) argv, "p:u:i:o:d:")) != EOF) {
switch (c) {
case 'p':
case 'u':
case 'i':
case 'o':
if (!strcmp(optarg, "0"))
arg = 0;
else if (!strcmp(optarg, "1"))
arg = 1;
else if (!strcmp(optarg, "2"))
arg = 2;
else {
INKError("Invalid argument specified for option: %c\n", c);
return;
}
if (c == 'p')
pre_parse_cont = arg;
else if (c == 'u')
embedded_url_cont = arg;
else if (c == 'i')
url_proto = arg;
else
embedded_object = arg;
break;
case 'd':
sprintf(file_name, "%s/prefetched.objects", optarg);
break;
case '?':
INKError("Invalid argument specified\n");
return;
}
}
if (embedded_object) {
filep1 = INKfopen(file_name, "w");
if (!filep1) {
INKError("Cannot open file %d for writing\n", file_name);
return;
}
INKfwrite(filep1, "", 1);
INKfflush(filep1);
file_write_mutex = INKMutexCreate();
}
/* register our hooks */
INKPrefetchHookSet(INK_PREFETCH_PRE_PARSE_HOOK, &pre_parse_hook);
INKPrefetchHookSet(INK_PREFETCH_EMBEDDED_URL_HOOK, &embedded_url_hook);
INKPrefetchHookSet(INK_PREFETCH_EMBEDDED_OBJECT_HOOK, &embedded_object_hook);
}