|
| 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 | +#include <stdio.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | + |
| 28 | +#include "ts/ts.h" |
| 29 | +#include "ts/ink_assert.h" |
| 30 | +#include "ts/ink_defs.h" |
| 31 | + |
| 32 | +#define PLUGIN_NAME "request_buffer" |
| 33 | + |
| 34 | +#define TS_NULL_MUTEX NULL |
| 35 | + |
| 36 | +static int |
| 37 | +request_buffer_plugin(TSCont contp, TSEvent event, void *edata) |
| 38 | +{ |
| 39 | + TSDebug(PLUGIN_NAME, "request_buffer_plugin starting, event[%d]", event); |
| 40 | + TSHttpTxn txnp = (TSHttpTxn)(edata); |
| 41 | + if (event == TS_EVENT_HTTP_REQUEST_BUFFER_COMPLETE) { |
| 42 | + int len = 0; |
| 43 | + char *body = TSHttpTxnGetClientRequestBody(txnp, &len); |
| 44 | + TSDebug(PLUGIN_NAME, "request_buffer_plugin gets the request body with length[%d]", len); |
| 45 | + TSfree(body); |
| 46 | + TSContDestroy(contp); |
| 47 | + } else { |
| 48 | + ink_assert(0); |
| 49 | + } |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +bool |
| 54 | +is_post_request(TSHttpTxn txnp) |
| 55 | +{ |
| 56 | + TSMLoc req_loc; |
| 57 | + TSMBuffer req_bufp; |
| 58 | + if (TSHttpTxnClientReqGet(txnp, &req_bufp, &req_loc) == TS_ERROR) { |
| 59 | + TSError("Error while retrieving client request header\n"); |
| 60 | + return false; |
| 61 | + } |
| 62 | + int method_len = 0; |
| 63 | + const char *method = TSHttpHdrMethodGet(req_bufp, req_loc, &method_len); |
| 64 | + if (method_len != (int)strlen(TS_HTTP_METHOD_POST) || strncasecmp(method, TS_HTTP_METHOD_POST, method_len) != 0) { |
| 65 | + TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, req_loc); |
| 66 | + return false; |
| 67 | + } |
| 68 | + TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, req_loc); |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +static int |
| 73 | +global_plugin(TSCont contp ATS_UNUSED, TSEvent event, void *edata) |
| 74 | +{ |
| 75 | + TSDebug(PLUGIN_NAME, "transform_plugin starting"); |
| 76 | + TSHttpTxn txnp = (TSHttpTxn)edata; |
| 77 | + |
| 78 | + switch (event) { |
| 79 | + case TS_EVENT_HTTP_READ_REQUEST_HDR: |
| 80 | + if (is_post_request(txnp)) { |
| 81 | + TSHttpTxnConfigIntSet(txnp, TS_CONFIG_HTTP_REQUEST_BUFFER_ENABLED, 1); |
| 82 | + TSHttpTxnHookAdd(txnp, TS_HTTP_REQUEST_BUFFER_READ_COMPLETE_HOOK, TSContCreate(request_buffer_plugin, TSMutexCreate())); |
| 83 | + } |
| 84 | + TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); |
| 85 | + return 0; |
| 86 | + default: |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +void |
| 94 | +TSPluginInit(int argc ATS_UNUSED, const char *argv[] ATS_UNUSED) |
| 95 | +{ |
| 96 | + TSPluginRegistrationInfo info; |
| 97 | + |
| 98 | + info.plugin_name = PLUGIN_NAME; |
| 99 | + info.vendor_name = "Apache Software Foundation"; |
| 100 | + info.support_email = "dev@trafficserver.apache.org"; |
| 101 | + |
| 102 | + if (TSPluginRegister(&info) != TS_SUCCESS) { |
| 103 | + TSDebug(PLUGIN_NAME, "[%s] Plugin registration failed", PLUGIN_NAME); |
| 104 | + |
| 105 | + goto Lerror; |
| 106 | + } |
| 107 | + |
| 108 | + /* This is call we could use if we need to protect global data */ |
| 109 | + /* TSReleaseAssert ((mutex = TSMutexCreate()) != TS_NULL_MUTEX); */ |
| 110 | + |
| 111 | + TSMutex mutex = TS_NULL_MUTEX; |
| 112 | + TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate(global_plugin, mutex)); |
| 113 | + TSDebug(PLUGIN_NAME, "[%s] Plugin registration succeeded", PLUGIN_NAME); |
| 114 | + return; |
| 115 | + |
| 116 | +Lerror: |
| 117 | + TSDebug(PLUGIN_NAME, "[%s] Plugin disabled", PLUGIN_NAME); |
| 118 | +} |
0 commit comments