1 /** @file
2 
3   A brief file description
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 /*
25  *   replace_header.c:
26  *	an example program...
27  *
28  *   NOTE: If faced with duplicate headers, this will only detect the
29  *         first instance.  Operational plugins may need to do more!
30  *
31  *	Usage:
32  *
33  *
34  */
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 
39 #include "ts/ts.h"
40 #include "tscore/ink_defs.h"
41 
42 #define PLUGIN_NAME "replace_header"
43 
44 static void
replace_header(TSHttpTxn txnp)45 replace_header(TSHttpTxn txnp)
46 {
47   TSMBuffer resp_bufp;
48   TSMLoc resp_loc;
49   TSMLoc field_loc;
50 
51   if (TSHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc) != TS_SUCCESS) {
52     TSError("[%s] Couldn't retrieve server response header", PLUGIN_NAME);
53     goto done;
54   }
55 
56   field_loc = TSMimeHdrFieldFind(resp_bufp, resp_loc, TS_MIME_FIELD_ACCEPT_RANGES, TS_MIME_LEN_ACCEPT_RANGES);
57   if (field_loc == TS_NULL_MLOC) {
58     /* field was not found */
59 
60     /* create a new field in the header */
61     TSMimeHdrFieldCreate(resp_bufp, resp_loc, &field_loc); /* Probably should check for errors. */
62     /* set its name */
63     TSMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, TS_MIME_FIELD_ACCEPT_RANGES, TS_MIME_LEN_ACCEPT_RANGES);
64     /* set its value */
65     TSMimeHdrFieldValueAppend(resp_bufp, resp_loc, field_loc, -1, "none", 4);
66     /* insert it into the header */
67     TSMimeHdrFieldAppend(resp_bufp, resp_loc, field_loc);
68     TSHandleMLocRelease(resp_bufp, resp_loc, field_loc);
69     TSHandleMLocRelease(resp_bufp, TS_NULL_MLOC, resp_loc);
70   } else {
71     /* clear the field */
72     TSMimeHdrFieldValuesClear(resp_bufp, resp_loc, field_loc);
73     /* set the value to "none" */
74     TSMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, "none", 4);
75     TSHandleMLocRelease(resp_bufp, resp_loc, field_loc);
76     TSHandleMLocRelease(resp_bufp, TS_NULL_MLOC, resp_loc);
77   }
78 
79 done:
80   TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
81 }
82 
83 static int
replace_header_plugin(TSCont contp ATS_UNUSED,TSEvent event,void * edata)84 replace_header_plugin(TSCont contp ATS_UNUSED, TSEvent event, void *edata)
85 {
86   TSHttpTxn txnp = (TSHttpTxn)edata;
87 
88   switch (event) {
89   case TS_EVENT_HTTP_READ_RESPONSE_HDR:
90     replace_header(txnp);
91     return 0;
92   default:
93     break;
94   }
95   return 0;
96 }
97 
98 void
TSPluginInit(int argc ATS_UNUSED,const char * argv[]ATS_UNUSED)99 TSPluginInit(int argc ATS_UNUSED, const char *argv[] ATS_UNUSED)
100 {
101   TSPluginRegistrationInfo info;
102 
103   info.plugin_name   = PLUGIN_NAME;
104   info.vendor_name   = "Apache Software Foundation";
105   info.support_email = "dev@trafficserver.apache.org";
106 
107   if (TSPluginRegister(&info) != TS_SUCCESS) {
108     TSError("[%s] Plugin registration failed", PLUGIN_NAME);
109   }
110 
111   TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(replace_header_plugin, NULL));
112 }
113