-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclack-pratchett.c
163 lines (132 loc) · 5.14 KB
/
clack-pratchett.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
/** @file
* CLACK Plugin to honor Terry Pratchett
* http://www.nerdcore.de/2015/03/14/gnu-terry-pratchett-http-header/
*
* @section license License
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <ts/ts.h>
#define PLUGIN_NAME "clack-pratchett"
static TSMBuffer hdr_bufp;
static TSMLoc hdr_loc;
static void
clack_pratchett(TSHttpTxn txnp, TSCont contp)
{
TSMBuffer resp_bufp;
TSMLoc resp_loc;
TSMLoc field_loc;
TSMLoc next_field_loc;
TSMLoc new_field_loc;
int retval;
TSDebug(PLUGIN_NAME,"getting server response");
if (TSHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc) != TS_SUCCESS) {
TSError("[clack_pratchett] Error while retrieving server response header");
goto done;
}
TSDebug(PLUGIN_NAME,"getting header field location");
field_loc = TSMimeHdrFieldGet(hdr_bufp, hdr_loc, 0);
if (field_loc == TS_NULL_MLOC) {
TSError("[clack_pratchett] Error while getting field");
goto error;
}
TSDebug(PLUGIN_NAME,"creating new header field");
if (TSMimeHdrFieldCreate(resp_bufp, resp_loc, &new_field_loc) != TS_SUCCESS) {
TSError("[clack_pratchett] Error while creating new field");
TSHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
goto error;
}
TSDebug(PLUGIN_NAME,"copy prepared header fieled");
retval = TSMimeHdrFieldCopy(resp_bufp, resp_loc, new_field_loc, hdr_bufp, hdr_loc, field_loc);
if (retval == TS_ERROR) {
TSError("[clack_pratchett] Error while copying new field");
TSHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
goto error;
}
TSDebug(PLUGIN_NAME,"add header field copy to the response");
retval = TSMimeHdrFieldAppend(resp_bufp, resp_loc, new_field_loc);
if (retval != TS_SUCCESS) {
TSError("[clack_pratchett] Error while appending new field");
TSHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
goto error;
}
TSDebug(PLUGIN_NAME,"releasing field_loc");
TSHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
error:
TSDebug(PLUGIN_NAME,"releasing resp_loc");
TSHandleMLocRelease(resp_bufp, TS_NULL_MLOC, resp_loc);
done:
TSDebug(PLUGIN_NAME,"reenable");
TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
}
static int
clack_partchett_plugin(TSCont contp, TSEvent event, void *edata)
{
TSHttpTxn txnp = (TSHttpTxn) edata;
TSDebug(PLUGIN_NAME,"Hook called");
clack_pratchett(txnp, contp);
return 0;
}
void TSPluginInit(int argc, const char *argv[]) {
TSMLoc field_loc;
const char *p;
int i, retval;
TSPluginRegistrationInfo info;
info.plugin_name = PLUGIN_NAME;
info.vendor_name = "Piratenpartei Deutschland";
info.support_email = "[email protected]";
if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) {
TSError("[PluginInit] Plugin registration failed.\n");
goto error;
}
TSDebug(PLUGIN_NAME,"[PluginInit] Registered plugin clack-pratchett");
hdr_bufp = TSMBufferCreate();
if (TSMimeHdrCreate(hdr_bufp, &hdr_loc) != TS_SUCCESS) {
TSError("[PluginInit] Can not create MIME header");
goto error;
}
TSDebug(PLUGIN_NAME,"[PluginInit] Created new MIME header");
if (TSMimeHdrFieldCreate(hdr_bufp, hdr_loc, &field_loc) != TS_SUCCESS) {
TSError("[PluginInit] Error while creating field");
goto error;
}
TSDebug(PLUGIN_NAME,"[PluginInit] Created new field for the header");
retval = TSMimeHdrFieldAppend(hdr_bufp, hdr_loc, field_loc);
if (retval != TS_SUCCESS) {
TSError("[PluginInit] Error while adding field");
goto error;
}
TSDebug(PLUGIN_NAME,"[PluginInit] Added new field for the MIME header");
retval = TSMimeHdrFieldNameSet(hdr_bufp, hdr_loc, field_loc, "X-Clacks-Overhead", 17);
if (retval == TS_ERROR) {
TSError("[PluginInit] Error while naming field");
goto error;
}
TSDebug(PLUGIN_NAME,"[PluginInit] Named MIME header field");
retval = TSMimeHdrFieldValueStringInsert(hdr_bufp, hdr_loc, field_loc, -1, "GNU Terry Pratchett", 19);
if (retval == TS_ERROR) {
TSError("[PluginInit] Error while inserting field value");
goto error;
}
TSDebug(PLUGIN_NAME,"[PluginInit] set MIME header field value");
TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(clack_partchett_plugin, TSMutexCreate()));
TSDebug(PLUGIN_NAME,"[PluginInit] registered global hook");
goto done;
error:
TSError("[PluginInit] Plugin not initialized");
done:
return;
}