1 /** @file
2
3 A plugin that sets custom 204 response bodies.
4
5 @section license License
6
7 Licensed to the Apache Software Foundation (ASF) under one
8 or more contributor license agreements. See the NOTICE file
9 distributed with this work for additional information
10 regarding copyright ownership. The ASF licenses this file
11 to you under the Apache License, Version 2.0 (the
12 "License"); you may not use this file except in compliance
13 with the License. You may obtain a copy of the License at
14
15 http://www.apache.org/licenses/LICENSE-2.0
16
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
23
24 #include "ts/ts.h"
25 #include <cstring>
26
27 #define PLUGIN_NAME "custom204plugintest"
28
29 static int
local_handler(TSCont contp,TSEvent event,void * edata)30 local_handler(TSCont contp, TSEvent event, void *edata)
31 {
32 const char *msg = "<HTML>\n"
33 "<HEAD>\n"
34 "<TITLE>Spec-breaking 204!</TITLE>\n"
35 "</HEAD>\n"
36 "\n"
37 "<BODY>\n"
38 "<H1>This is body content for a 204.</H1>\n"
39 "<HR>\n"
40 "\n"
41 "Description: According to rfc7231 I should not have been sent to you!<BR/>\n"
42 "This response was sent via the custom204plugin via a call to TSHttpTxnErrorBodySet.\n"
43 "<HR>\n"
44 "</BODY>";
45 TSHttpTxn txnp = static_cast<TSHttpTxn>(edata);
46 TSMBuffer bufp = nullptr;
47 TSMLoc hdr_loc = nullptr;
48 TSMLoc url_loc = nullptr;
49 ;
50 const char *host = nullptr;
51 int host_length;
52 const char *test_host = "www.customplugin204.test";
53
54 switch (event) {
55 case TS_EVENT_HTTP_PRE_REMAP:
56 TSDebug(PLUGIN_NAME, "event TS_EVENT_HTTP_PRE_REMAP received");
57 TSDebug(PLUGIN_NAME, "running plugin logic.");
58 if (TSHttpTxnClientReqGet(txnp, &bufp, &hdr_loc) != TS_SUCCESS) {
59 TSDebug(PLUGIN_NAME, "Couldn't retrieve client request header");
60 TSError("[%s] Couldn't retrieve client request header", PLUGIN_NAME);
61 goto done;
62 }
63 TSDebug(PLUGIN_NAME, "got client request");
64
65 if (TSHttpHdrUrlGet(bufp, hdr_loc, &url_loc) != TS_SUCCESS) {
66 TSError("[%s] Couldn't retrieve request url", PLUGIN_NAME);
67 TSDebug(PLUGIN_NAME, "Couldn't retrieve request url");
68 TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
69 goto done;
70 }
71 TSDebug(PLUGIN_NAME, "got client request url");
72
73 host = TSUrlHostGet(bufp, url_loc, &host_length);
74 if (!host) {
75 TSError("[%s] Couldn't retrieve request hostname", PLUGIN_NAME);
76 TSDebug(PLUGIN_NAME, "Couldn't retrieve request hostname");
77 TSHandleMLocRelease(bufp, hdr_loc, url_loc);
78 TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
79 goto done;
80 }
81 TSDebug(PLUGIN_NAME, "request's host was retrieved");
82
83 if (strncmp(host, test_host, strlen(test_host)) == 0) {
84 TSDebug(PLUGIN_NAME, "host matches, hook TS_HTTP_SEND_RESPONSE_HDR_HOOK");
85 TSHttpTxnHookAdd(txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK, contp);
86 TSHandleMLocRelease(bufp, hdr_loc, url_loc);
87 TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
88 TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
89 return 0;
90 }
91 TSDebug(PLUGIN_NAME, "Host != expected host '%s'", test_host);
92 break;
93 case TS_EVENT_HTTP_SEND_RESPONSE_HDR:
94 TSDebug(PLUGIN_NAME, "Returning 204 with custom response body.");
95 TSHttpTxnStatusSet(txnp, TS_HTTP_STATUS_NO_CONTENT);
96 TSHttpTxnErrorBodySet(txnp, TSstrdup(msg), strlen(msg), TSstrdup("text/html"));
97 break;
98
99 case TS_EVENT_HTTP_TXN_CLOSE:
100 TSDebug(PLUGIN_NAME, "event TS_EVENT_HTTP_TXN_CLOSE received");
101 TSContDestroy(contp);
102 break;
103
104 default:
105 TSAssert(!"Unexpected event");
106 break;
107 }
108
109 done:
110 TSHandleMLocRelease(bufp, hdr_loc, url_loc);
111 TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
112 TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
113 return 1;
114 }
115
116 static int
global_handler(TSCont contp,TSEvent event,void * edata)117 global_handler(TSCont contp, TSEvent event, void *edata)
118 {
119 TSHttpTxn txnp = static_cast<TSHttpTxn>(edata);
120 TSCont txn_contp = nullptr;
121
122 switch (event) {
123 case TS_EVENT_HTTP_TXN_START:
124 txn_contp = TSContCreate(local_handler, TSMutexCreate());
125 TSHttpTxnHookAdd(txnp, TS_HTTP_PRE_REMAP_HOOK, txn_contp);
126 TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
127 TSDebug(PLUGIN_NAME, "hooked TS_HTTP_OS_DNS_HOOK and TS_EVENT_HTTP_TXN_CLOSE_HOOK");
128 break;
129 default:
130 TSAssert(!"Unexpected event");
131 break;
132 }
133 TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
134 return 1;
135 }
136
137 void
TSPluginInit(int argc,const char * argv[])138 TSPluginInit(int argc, const char *argv[])
139 {
140 TSPluginRegistrationInfo info;
141
142 info.plugin_name = PLUGIN_NAME;
143 info.vendor_name = "Apache Software Foundation";
144 info.support_email = "dev@trafficserver.apache.org";
145
146 if (TSPluginRegister(&info) != TS_SUCCESS) {
147 TSError("[%s] Plugin registration failed", PLUGIN_NAME);
148 }
149
150 TSCont contp = TSContCreate(global_handler, TSMutexCreate());
151 TSHttpHookAdd(TS_HTTP_TXN_START_HOOK, contp);
152 }
153