http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc b/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc index 62db54a..6e7974c 100644 --- a/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc +++ b/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc @@ -34,57 +34,72 @@ using std::string; // To view the debug messages ./traffic_server -T "async_http_fetch_example.*" #define TAG "async_http_fetch_example" -class AsyncHttpFetch2 : public AsyncHttpFetch { +class AsyncHttpFetch2 : public AsyncHttpFetch +{ public: - AsyncHttpFetch2(string request) : AsyncHttpFetch(request) { }; + AsyncHttpFetch2(string request) : AsyncHttpFetch(request){}; }; -class AsyncHttpFetch3 : public AsyncHttpFetch { +class AsyncHttpFetch3 : public AsyncHttpFetch +{ public: - AsyncHttpFetch3(string request, HttpMethod method) : AsyncHttpFetch(request, method) { }; + AsyncHttpFetch3(string request, HttpMethod method) : AsyncHttpFetch(request, method){}; }; -class DelayedAsyncHttpFetch : public AsyncHttpFetch, public AsyncReceiver<AsyncTimer> { +class DelayedAsyncHttpFetch : public AsyncHttpFetch, public AsyncReceiver<AsyncTimer> +{ public: DelayedAsyncHttpFetch(string request, HttpMethod method, shared_ptr<Mutex> mutex) - : AsyncHttpFetch(request, method), mutex_(mutex), timer_(NULL) { }; - void run() { + : AsyncHttpFetch(request, method), mutex_(mutex), timer_(NULL){}; + void + run() + { timer_ = new AsyncTimer(AsyncTimer::TYPE_ONE_OFF, 1000 /* 1s */); Async::execute(this, timer_, mutex_); } - void handleAsyncComplete(AsyncTimer &/*timer ATS_UNUSED */) { + void + handleAsyncComplete(AsyncTimer & /*timer ATS_UNUSED */) + { TS_DEBUG(TAG, "Receiver should not be reachable"); assert(!getDispatchController()->dispatch()); delete this; } - bool isAlive() { + bool + isAlive() + { return getDispatchController()->isEnabled(); } ~DelayedAsyncHttpFetch() { delete timer_; } + private: shared_ptr<Mutex> mutex_; AsyncTimer *timer_; }; -class TransactionHookPlugin : public TransactionPlugin, public AsyncReceiver<AsyncHttpFetch>, - public AsyncReceiver<AsyncHttpFetch2>, public AsyncReceiver<AsyncHttpFetch3>, - public AsyncReceiver<DelayedAsyncHttpFetch> { +class TransactionHookPlugin : public TransactionPlugin, + public AsyncReceiver<AsyncHttpFetch>, + public AsyncReceiver<AsyncHttpFetch2>, + public AsyncReceiver<AsyncHttpFetch3>, + public AsyncReceiver<DelayedAsyncHttpFetch> +{ public: - TransactionHookPlugin(Transaction &transaction) : - TransactionPlugin(transaction), transaction_(transaction), num_fetches_pending_(0) { + TransactionHookPlugin(Transaction &transaction) + : TransactionPlugin(transaction), transaction_(transaction), num_fetches_pending_(0) + { TS_DEBUG(TAG, "Constructed TransactionHookPlugin, saved a reference to this transaction."); registerHook(HOOK_SEND_REQUEST_HEADERS); } - void handleSendRequestHeaders(Transaction & /*transaction ATS_UNUSED */) { + void + handleSendRequestHeaders(Transaction & /*transaction ATS_UNUSED */) + { Async::execute<AsyncHttpFetch>(this, new AsyncHttpFetch("http://127.0.0.1/"), getMutex()); ++num_fetches_pending_; AsyncHttpFetch *post_request = new AsyncHttpFetch("http://127.0.0.1/post", "data"); (void)post_request; - Async::execute<AsyncHttpFetch>(this, new AsyncHttpFetch("http://127.0.0.1/post", "data"), - getMutex()); + Async::execute<AsyncHttpFetch>(this, new AsyncHttpFetch("http://127.0.0.1/post", "data"), getMutex()); ++num_fetches_pending_; // we'll add some custom headers for this request @@ -105,29 +120,38 @@ public: assert(!delayed_provider->isAlive()); } - void handleAsyncComplete(AsyncHttpFetch &async_http_fetch) { + void + handleAsyncComplete(AsyncHttpFetch &async_http_fetch) + { // This will be called when our async event is complete. TS_DEBUG(TAG, "AsyncHttpFetch completed"); handleAnyAsyncComplete(async_http_fetch); } - void handleAsyncComplete(AsyncHttpFetch2 &async_http_fetch) { + void + handleAsyncComplete(AsyncHttpFetch2 &async_http_fetch) + { // This will be called when our async event is complete. TS_DEBUG(TAG, "AsyncHttpFetch2 completed"); handleAnyAsyncComplete(async_http_fetch); } - virtual ~TransactionHookPlugin() { + virtual ~TransactionHookPlugin() + { TS_DEBUG(TAG, "Destroyed TransactionHookPlugin!"); // since we die right away, we should not receive the callback for this (using POST request this time) Async::execute<AsyncHttpFetch3>(this, new AsyncHttpFetch3("http://127.0.0.1/", HTTP_METHOD_POST), getMutex()); } - void handleAsyncComplete(AsyncHttpFetch3 & /* async_http_fetch ATS_UNUSED */) { + void + handleAsyncComplete(AsyncHttpFetch3 & /* async_http_fetch ATS_UNUSED */) + { assert(!"AsyncHttpFetch3 shouldn't have completed!"); } - void handleAsyncComplete(DelayedAsyncHttpFetch &/*async_http_fetch ATS_UNUSED */) { + void + handleAsyncComplete(DelayedAsyncHttpFetch & /*async_http_fetch ATS_UNUSED */) + { assert(!"Should've been canceled!"); } @@ -135,14 +159,15 @@ private: Transaction &transaction_; int num_fetches_pending_; - void handleAnyAsyncComplete(AsyncHttpFetch &async_http_fetch) { + void + handleAnyAsyncComplete(AsyncHttpFetch &async_http_fetch) + { // This will be called when our async event is complete. TS_DEBUG(TAG, "Fetch completed for URL [%s]", async_http_fetch.getRequestUrl().getUrlString().c_str()); const Response &response = async_http_fetch.getResponse(); if (async_http_fetch.getResult() == AsyncHttpFetch::RESULT_SUCCESS) { TS_DEBUG(TAG, "Response version is [%s], status code %d, reason phrase [%s]", - HTTP_VERSION_STRINGS[response.getVersion()].c_str(), response.getStatusCode(), - response.getReasonPhrase().c_str()); + HTTP_VERSION_STRINGS[response.getVersion()].c_str(), response.getStatusCode(), response.getReasonPhrase().c_str()); TS_DEBUG(TAG, "Reponse Headers: \n%s\n", response.getHeaders().str().c_str()); @@ -150,44 +175,46 @@ private: size_t body_size; async_http_fetch.getResponseBody(body, body_size); TS_DEBUG(TAG, "Response body is %zu bytes long and is [%.*s]", body_size, static_cast<int>(body_size), - static_cast<const char*>(body)); + static_cast<const char *>(body)); } else { - TS_ERROR(TAG, "Fetch did not complete successfully; Result %d", - static_cast<int>(async_http_fetch.getResult())); + TS_ERROR(TAG, "Fetch did not complete successfully; Result %d", static_cast<int>(async_http_fetch.getResult())); } if (--num_fetches_pending_ == 0) { TS_DEBUG(TAG, "Reenabling transaction"); transaction_.resume(); } } - }; -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { + GlobalHookPlugin() + { TS_DEBUG(TAG, "Registering a global hook HOOK_READ_REQUEST_HEADERS_POST_REMAP"); registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { TS_DEBUG(TAG, "Received a request in handleReadRequestHeadersPostRemap."); // If we don't make sure to check if it's an internal request we can get ourselves into an infinite loop! if (!transaction.isInternalRequest()) { transaction.addPlugin(new TransactionHookPlugin(transaction)); - } - else { + } else { TS_DEBUG(TAG, "Ignoring internal transaction"); } transaction.resume(); } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ TS_DEBUG(TAG, "Loaded async_http_fetch_example plugin"); GlobalPlugin *instance = new GlobalHookPlugin(); (void)instance; } -
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/async_http_fetch_streaming/AsyncHttpFetchStreaming.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/async_http_fetch_streaming/AsyncHttpFetchStreaming.cc b/lib/atscppapi/examples/async_http_fetch_streaming/AsyncHttpFetchStreaming.cc index f570f6f..33902c3 100644 --- a/lib/atscppapi/examples/async_http_fetch_streaming/AsyncHttpFetchStreaming.cc +++ b/lib/atscppapi/examples/async_http_fetch_streaming/AsyncHttpFetchStreaming.cc @@ -35,16 +35,19 @@ using std::string; // To view the debug messages ./traffic_server -T "async_http_fetch_example.*" #define TAG "async_http_fetch_example" -class Intercept : public InterceptPlugin, public AsyncReceiver<AsyncHttpFetch> { +class Intercept : public InterceptPlugin, public AsyncReceiver<AsyncHttpFetch> +{ public: - Intercept(Transaction &transaction) : InterceptPlugin(transaction, InterceptPlugin::SERVER_INTERCEPT), - transaction_(transaction), num_fetches_(0) { + Intercept(Transaction &transaction) + : InterceptPlugin(transaction, InterceptPlugin::SERVER_INTERCEPT), transaction_(transaction), num_fetches_(0) + { main_url_ = transaction.getClientRequest().getUrl().getUrlString(); } void consume(const string &data, InterceptPlugin::RequestDataType type); void handleInputComplete(); void handleAsyncComplete(AsyncHttpFetch &async_http_fetch); ~Intercept(); + private: Transaction &transaction_; string request_body_; @@ -53,55 +56,66 @@ private: int num_fetches_; }; -class InterceptInstaller : public GlobalPlugin { +class InterceptInstaller : public GlobalPlugin +{ public: - InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) { + InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) + { GlobalPlugin::registerHook(Plugin::HOOK_READ_REQUEST_HEADERS_PRE_REMAP); } - void handleReadRequestHeadersPreRemap(Transaction &transaction) { + void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { transaction.addPlugin(new Intercept(transaction)); TS_DEBUG(TAG, "Added intercept"); transaction.resume(); } }; -void TSPluginInit(int /* argc ATS_UNUSED */, const char * /* argv ATS_UNUSED */ []) { +void +TSPluginInit(int /* argc ATS_UNUSED */, const char * /* argv ATS_UNUSED */ []) +{ new InterceptInstaller(); } -void Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) { +void +Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) +{ if (type == InterceptPlugin::REQUEST_BODY) { request_body_ += data; } } -void Intercept::handleInputComplete() { +void +Intercept::handleInputComplete() +{ TS_DEBUG(TAG, "Request data complete"); - AsyncHttpFetch *async_http_fetch = request_body_.empty() ? - new AsyncHttpFetch(main_url_, AsyncHttpFetch::STREAMING_ENABLED, transaction_.getClientRequest().getMethod()) : - new AsyncHttpFetch(main_url_, AsyncHttpFetch::STREAMING_ENABLED, request_body_); + AsyncHttpFetch *async_http_fetch = + request_body_.empty() ? + new AsyncHttpFetch(main_url_, AsyncHttpFetch::STREAMING_ENABLED, transaction_.getClientRequest().getMethod()) : + new AsyncHttpFetch(main_url_, AsyncHttpFetch::STREAMING_ENABLED, request_body_); Async::execute<AsyncHttpFetch>(this, async_http_fetch, getMutex()); ++num_fetches_; size_t dependent_url_param_pos = main_url_.find("dependent_url="); if (dependent_url_param_pos != string::npos) { dependent_url_ = main_url_.substr(dependent_url_param_pos + 14); - Async::execute<AsyncHttpFetch>(this, new AsyncHttpFetch(dependent_url_, - AsyncHttpFetch::STREAMING_ENABLED), - getMutex()); + Async::execute<AsyncHttpFetch>(this, new AsyncHttpFetch(dependent_url_, AsyncHttpFetch::STREAMING_ENABLED), getMutex()); ++num_fetches_; TS_DEBUG(TAG, "Started fetch for dependent URL [%s]", dependent_url_.c_str()); } } -void Intercept::handleAsyncComplete(AsyncHttpFetch &async_http_fetch) { +void +Intercept::handleAsyncComplete(AsyncHttpFetch &async_http_fetch) +{ AsyncHttpFetch::Result result = async_http_fetch.getResult(); string url = async_http_fetch.getRequestUrl().getUrlString(); if (result == AsyncHttpFetch::RESULT_HEADER_COMPLETE) { TS_DEBUG(TAG, "Header completed for URL [%s]", url.c_str()); const Response &response = async_http_fetch.getResponse(); std::ostringstream oss; - oss << HTTP_VERSION_STRINGS[response.getVersion()] << ' ' << response.getStatusCode() << ' ' - << response.getReasonPhrase() << "\r\n"; + oss << HTTP_VERSION_STRINGS[response.getVersion()] << ' ' << response.getStatusCode() << ' ' << response.getReasonPhrase() + << "\r\n"; Headers &response_headers = response.getHeaders(); for (Headers::iterator iter = response_headers.begin(), end = response_headers.end(); iter != end; ++iter) { HeaderFieldName header_name = (*iter).name(); @@ -112,27 +126,23 @@ void Intercept::handleAsyncComplete(AsyncHttpFetch &async_http_fetch) { oss << "\r\n"; if (url == main_url_) { Intercept::produce(oss.str()); - } - else { + } else { TS_DEBUG(TAG, "Response header for dependent URL\n%s", oss.str().c_str()); } - } - else if (result == AsyncHttpFetch::RESULT_PARTIAL_BODY || result == AsyncHttpFetch::RESULT_BODY_COMPLETE) { + } else if (result == AsyncHttpFetch::RESULT_PARTIAL_BODY || result == AsyncHttpFetch::RESULT_BODY_COMPLETE) { const void *body; size_t body_size; async_http_fetch.getResponseBody(body, body_size); if (url == main_url_) { Intercept::produce(string(static_cast<const char *>(body), body_size)); - } - else { + } else { TS_DEBUG(TAG, "Got dependent body bit; has %zu bytes and is [%.*s]", body_size, static_cast<int>(body_size), static_cast<const char *>(body)); } if (result == AsyncHttpFetch::RESULT_BODY_COMPLETE) { TS_DEBUG(TAG, "response body complete"); } - } - else { + } else { TS_ERROR(TAG, "Fetch did not complete successfully; Result %d", static_cast<int>(result)); if (url == main_url_) { InterceptPlugin::produce("HTTP/1.1 500 Internal Server Error\r\n\r\n"); @@ -147,7 +157,8 @@ void Intercept::handleAsyncComplete(AsyncHttpFetch &async_http_fetch) { } } -Intercept::~Intercept() { +Intercept::~Intercept() +{ if (num_fetches_) { TS_DEBUG(TAG, "Fetch still pending, but transaction closing"); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/async_timer/AsyncTimer.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/async_timer/AsyncTimer.cc b/lib/atscppapi/examples/async_timer/AsyncTimer.cc index c802462..6faa081 100644 --- a/lib/atscppapi/examples/async_timer/AsyncTimer.cc +++ b/lib/atscppapi/examples/async_timer/AsyncTimer.cc @@ -26,16 +26,20 @@ using std::string; #define TAG "async_timer" -class TimerEventReceiver : public AsyncReceiver<AsyncTimer> { +class TimerEventReceiver : public AsyncReceiver<AsyncTimer> +{ public: TimerEventReceiver(AsyncTimer::Type type, int period_in_ms, int initial_period_in_ms = 0, int max_instances = 0, bool cancel = false) - : max_instances_(max_instances), instance_count_(0), type_(type), cancel_(cancel) { + : max_instances_(max_instances), instance_count_(0), type_(type), cancel_(cancel) + { timer_ = new AsyncTimer(type, period_in_ms, initial_period_in_ms); Async::execute<AsyncTimer>(this, timer_, shared_ptr<Mutex>()); // letting the system create the mutex } - void handleAsyncComplete(AsyncTimer &timer ATSCPPAPI_UNUSED) { + void + handleAsyncComplete(AsyncTimer &timer ATSCPPAPI_UNUSED) + { TS_DEBUG(TAG, "Got timer event in object %p!", this); if ((type_ == AsyncTimer::TYPE_ONE_OFF) || (max_instances_ && (++instance_count_ == max_instances_))) { TS_DEBUG(TAG, "Stopping timer in object %p!", this); @@ -43,9 +47,7 @@ public: } } - ~TimerEventReceiver() { - delete timer_; - } + ~TimerEventReceiver() { delete timer_; } private: int max_instances_; @@ -55,22 +57,21 @@ private: bool cancel_; }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ int period_in_ms = 1000; TimerEventReceiver *timer1 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms); - TS_DEBUG(TAG, "Created periodic timer %p with initial period 0, regular period %d and max instances 0", timer1, - period_in_ms); + TS_DEBUG(TAG, "Created periodic timer %p with initial period 0, regular period %d and max instances 0", timer1, period_in_ms); int initial_period_in_ms = 100; - TimerEventReceiver *timer2 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, - initial_period_in_ms); + TimerEventReceiver *timer2 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms); TS_DEBUG(TAG, "Created periodic timer %p with initial period %d, regular period %d and max instances 0", timer2, initial_period_in_ms, period_in_ms); initial_period_in_ms = 200; int max_instances = 10; - TimerEventReceiver *timer3 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms, - max_instances); + TimerEventReceiver *timer3 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms, max_instances); TS_DEBUG(TAG, "Created periodic timer %p with initial period %d, regular period %d and max instances %d", timer3, initial_period_in_ms, period_in_ms, max_instances); @@ -79,8 +80,8 @@ void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED initial_period_in_ms = 0; max_instances = 5; - TimerEventReceiver *timer5 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms, - max_instances, true /* cancel */); + TimerEventReceiver *timer5 = + new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms, max_instances, true /* cancel */); TS_DEBUG(TAG, "Created canceling timer %p with initial period %d, regular period %d and max instances %d", timer5, initial_period_in_ms, period_in_ms, max_instances); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/boom/boom.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/boom/boom.cc b/lib/atscppapi/examples/boom/boom.cc index 1d99bb2..4d8b9d4 100644 --- a/lib/atscppapi/examples/boom/boom.cc +++ b/lib/atscppapi/examples/boom/boom.cc @@ -73,8 +73,8 @@ using namespace atscppapi; #define TAG "boom" -namespace { - +namespace +{ /// Name for the Boom invocation counter const std::string BOOM_COUNTER = "BOOM_COUNTER"; @@ -82,8 +82,7 @@ const std::string BOOM_COUNTER = "BOOM_COUNTER"; const std::string DEFAULT_ERROR_FILE = "default"; // default.html will be searched for // Default error response TBD when the default response will be used -const std::string DEFAULT_ERROR_RESPONSE = - "<html><body><h1>This page will be back soon</h1></body></html>"; +const std::string DEFAULT_ERROR_RESPONSE = "<html><body><h1>This page will be back soon</h1></body></html>"; // Default HTTP status code to use after booming const int DEFAULT_BOOM_HTTP_STATUS_CODE = 200; @@ -97,19 +96,22 @@ Stat boom_counter; // Functor that decides whether the HTTP error can be rewritten or not. // Rewritable codes are: 2xx, 3xx, 4xx, 5xx and 6xx. // 1xx is NOT rewritable! -class IsRewritableCode: public std::unary_function<std::string, bool> { // could probably be replaced with mem_ptr_fun().. +class IsRewritableCode : public std::unary_function<std::string, bool> +{ // could probably be replaced with mem_ptr_fun().. private: int current_code_; std::string current_code_string_; + public: - IsRewritableCode(int current_code) : - current_code_(current_code) { + IsRewritableCode(int current_code) : current_code_(current_code) + { std::ostringstream oss; oss << current_code_; current_code_string_ = oss.str(); } - bool operator()(const std::string &code) const { + bool operator()(const std::string &code) const + { TS_DEBUG(TAG, "Checking if %s matches code %s", current_code_string_.c_str(), code.c_str()); if (code == current_code_string_) return true; @@ -128,7 +130,8 @@ public: } }; -class BoomResponseRegistry { +class BoomResponseRegistry +{ // Boom error codes std::set<std::string> error_codes_; @@ -148,13 +151,12 @@ class BoomResponseRegistry { std::string generic_code_from_status(int http_status); public: - // Set a "catchall" global default response - void set_global_default_response(const std::string& global_default_response); + void set_global_default_response(const std::string &global_default_response); // Populate the registry lookup table with contents of files in // the base directory - void populate_error_responses(const std::string& base_directory); + void populate_error_responses(const std::string &base_directory); // Return custom response string for the custom code // Lookup logic (using 404 as example) @@ -163,7 +165,7 @@ public: // 3. Check for default response (i.e. contents of "default.html") // 4. Check for global default response (settable through "set_global_default_response" method) // 5. If all else fails, return compiled in response code - const std::string& get_response_for_error_code(int http_status_code); + const std::string &get_response_for_error_code(int http_status_code); // Returns true iff either of the three conditions are true: // 1. Exact match for the error is registered (e.g. "404.html" for HTTP 404) @@ -173,11 +175,12 @@ public: bool has_code_registered(int http_status_code); // Register error codes - void register_error_codes(const std::vector<std::string>& error_codes); + void register_error_codes(const std::vector<std::string> &error_codes); }; -void BoomResponseRegistry::register_error_codes(const std::vector<std::string>& error_codes) +void +BoomResponseRegistry::register_error_codes(const std::vector<std::string> &error_codes) { std::vector<std::string>::const_iterator i = error_codes.begin(), e = error_codes.end(); for (; i != e; ++i) { @@ -190,12 +193,13 @@ bool get_file_contents(std::string fileName, std::string &contents); // Examine the error file directory and populate the error_response // map with the file contents. -void BoomResponseRegistry::populate_error_responses(const std::string& base_directory) { +void +BoomResponseRegistry::populate_error_responses(const std::string &base_directory) +{ base_error_directory_ = base_directory; // Make sure we have a trailing / after the base directory - if (!base_error_directory_.empty() - && base_error_directory_[base_error_directory_.length() - 1] != '/') + if (!base_error_directory_.empty() && base_error_directory_[base_error_directory_.length() - 1] != '/') base_error_directory_.append("/"); // make sure we have a trailing / // Iterate over files in the base directory. @@ -228,11 +232,15 @@ void BoomResponseRegistry::populate_error_responses(const std::string& base_dire } } -void BoomResponseRegistry::set_global_default_response(const std::string& global_default_response) { +void +BoomResponseRegistry::set_global_default_response(const std::string &global_default_response) +{ global_response_string_ = global_default_response; } -const std::string& BoomResponseRegistry::get_response_for_error_code(int http_status_code) { +const std::string & +BoomResponseRegistry::get_response_for_error_code(int http_status_code) +{ std::string code_str = code_from_status(http_status_code); if (error_responses_.count(code_str)) @@ -249,7 +257,9 @@ const std::string& BoomResponseRegistry::get_response_for_error_code(int http_st return DEFAULT_ERROR_RESPONSE; } -bool BoomResponseRegistry::has_code_registered(int http_status_code) { +bool +BoomResponseRegistry::has_code_registered(int http_status_code) +{ // Only rewritable codes are allowed. std::set<std::string>::iterator ii = std::find_if(error_codes_.begin(), error_codes_.end(), IsRewritableCode(http_status_code)); if (ii == error_codes_.end()) @@ -258,7 +268,9 @@ bool BoomResponseRegistry::has_code_registered(int http_status_code) { return true; } -std::string BoomResponseRegistry::generic_code_from_status(int code) { +std::string +BoomResponseRegistry::generic_code_from_status(int code) +{ if (code >= 200 && code <= 299) return "2xx"; else if (code >= 300 && code <= 399) @@ -271,7 +283,9 @@ std::string BoomResponseRegistry::generic_code_from_status(int code) { return "default"; } -std::string BoomResponseRegistry::code_from_status(int code) { +std::string +BoomResponseRegistry::code_from_status(int code) +{ std::ostringstream oss; oss << code; std::string code_str = oss.str(); @@ -280,18 +294,21 @@ std::string BoomResponseRegistry::code_from_status(int code) { // Transaction plugin that intercepts error and displays // a error page as configured -class BoomTransactionPlugin: public TransactionPlugin { +class BoomTransactionPlugin : public TransactionPlugin +{ public: - BoomTransactionPlugin(Transaction &transaction, HttpStatus status, const std::string &reason, - const std::string &body) : - TransactionPlugin(transaction), status_(status), reason_(reason), body_(body) { + BoomTransactionPlugin(Transaction &transaction, HttpStatus status, const std::string &reason, const std::string &body) + : TransactionPlugin(transaction), status_(status), reason_(reason), body_(body) + { TransactionPlugin::registerHook(HOOK_SEND_RESPONSE_HEADERS); - TS_DEBUG(TAG, "Created BoomTransaction plugin for txn=%p, status=%d, reason=%s, body length=%d", - transaction.getAtsHandle(), status, reason.c_str(), static_cast<int>(body.length())); + TS_DEBUG(TAG, "Created BoomTransaction plugin for txn=%p, status=%d, reason=%s, body length=%d", transaction.getAtsHandle(), + status, reason.c_str(), static_cast<int>(body.length())); transaction.error(body_); // Set the error body now, and change the status and reason later. } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { transaction.getClientResponse().setStatusCode(status_); transaction.getClientResponse().setReasonPhrase(reason_); transaction.resume(); @@ -304,7 +321,9 @@ private: }; // Utility routine to split string by delimiter. -void stringSplit(const std::string &in, char delim, std::vector<std::string> &res) { +void +stringSplit(const std::string &in, char delim, std::vector<std::string> &res) +{ std::istringstream ss(in); std::string item; while (std::getline(ss, item, delim)) { @@ -314,7 +333,9 @@ void stringSplit(const std::string &in, char delim, std::vector<std::string> &re // Utility routine to read file contents into a string // @returns true if the file exists and has been successfully read -bool get_file_contents(std::string fileName, std::string &contents) { +bool +get_file_contents(std::string fileName, std::string &contents) +{ if (fileName.empty()) { return false; } @@ -337,14 +358,14 @@ bool get_file_contents(std::string fileName, std::string &contents) { return true; } -class BoomGlobalPlugin: public atscppapi::GlobalPlugin { - +class BoomGlobalPlugin : public atscppapi::GlobalPlugin +{ private: BoomResponseRegistry *response_registry_; public: - BoomGlobalPlugin(BoomResponseRegistry* response_registry) : - response_registry_(response_registry) { + BoomGlobalPlugin(BoomResponseRegistry *response_registry) : response_registry_(response_registry) + { TS_DEBUG(TAG, "Creating BoomGlobalHook %p", this); registerHook(HOOK_READ_RESPONSE_HEADERS); } @@ -356,44 +377,44 @@ private: BoomGlobalPlugin(); }; -void BoomGlobalPlugin::handleReadResponseHeaders(Transaction &transaction) { - // Get response status code from the transaction - HttpStatus http_status_code = transaction.getServerResponse().getStatusCode(); - - TS_DEBUG(TAG, "Checking if response with code %d is in the registry.", http_status_code); - - // If the custom response for the error code is registered, - // attach the BoomTransactionPlugin to the transaction - if (response_registry_->has_code_registered(http_status_code)) { - // Get the original reason phrase string from the transaction - std::string http_reason_phrase = transaction.getServerResponse().getReasonPhrase(); - - TS_DEBUG(TAG, "Response has code %d which matches a registered code, TransactionPlugin will be created.", http_status_code); - // Increment the statistics counter - boom_counter.increment(); - - // Get custom response code from the registry - const std::string& custom_response = response_registry_->get_response_for_error_code( - http_status_code); - - // Add the transaction plugin to the transaction - transaction.addPlugin( - new BoomTransactionPlugin(transaction, http_status_code, http_reason_phrase, - custom_response)); - // No need to resume/error the transaction, - // as BoomTransactionPlugin will take care of terminating the transaction - return; - } else { - TS_DEBUG(TAG, "Code %d was not in the registry, transaction will be resumed", http_status_code); - transaction.resume(); - } +void +BoomGlobalPlugin::handleReadResponseHeaders(Transaction &transaction) +{ + // Get response status code from the transaction + HttpStatus http_status_code = transaction.getServerResponse().getStatusCode(); + + TS_DEBUG(TAG, "Checking if response with code %d is in the registry.", http_status_code); + + // If the custom response for the error code is registered, + // attach the BoomTransactionPlugin to the transaction + if (response_registry_->has_code_registered(http_status_code)) { + // Get the original reason phrase string from the transaction + std::string http_reason_phrase = transaction.getServerResponse().getReasonPhrase(); + + TS_DEBUG(TAG, "Response has code %d which matches a registered code, TransactionPlugin will be created.", http_status_code); + // Increment the statistics counter + boom_counter.increment(); + + // Get custom response code from the registry + const std::string &custom_response = response_registry_->get_response_for_error_code(http_status_code); + + // Add the transaction plugin to the transaction + transaction.addPlugin(new BoomTransactionPlugin(transaction, http_status_code, http_reason_phrase, custom_response)); + // No need to resume/error the transaction, + // as BoomTransactionPlugin will take care of terminating the transaction + return; + } else { + TS_DEBUG(TAG, "Code %d was not in the registry, transaction will be resumed", http_status_code); + transaction.resume(); } +} /* * This is the plugin registration point */ -void TSPluginInit(int argc, const char *argv[]) { - +void +TSPluginInit(int argc, const char *argv[]) +{ boom_counter.init(BOOM_COUNTER); BoomResponseRegistry *pregistry = new BoomResponseRegistry(); @@ -405,7 +426,7 @@ void TSPluginInit(int argc, const char *argv[]) { pregistry->populate_error_responses(base_directory); std::string error_codes_argument(argv[2], strlen(argv[2])); - std::vector < std::string > error_codes; + std::vector<std::string> error_codes; stringSplit(error_codes_argument, ',', error_codes); pregistry->register_error_codes(error_codes); } else { @@ -414,4 +435,3 @@ void TSPluginInit(int argc, const char *argv[]) { new BoomGlobalPlugin(pregistry); } - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/clientredirect/ClientRedirect.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/clientredirect/ClientRedirect.cc b/lib/atscppapi/examples/clientredirect/ClientRedirect.cc index 7e6afab..b4fcbd1 100644 --- a/lib/atscppapi/examples/clientredirect/ClientRedirect.cc +++ b/lib/atscppapi/examples/clientredirect/ClientRedirect.cc @@ -29,11 +29,12 @@ using std::list; using std::string; -class ClientRedirectTransactionPlugin : public atscppapi::TransactionPlugin { +class ClientRedirectTransactionPlugin : public atscppapi::TransactionPlugin +{ public: ClientRedirectTransactionPlugin(Transaction &transaction, const string &location) - : TransactionPlugin(transaction), location_(location) { - + : TransactionPlugin(transaction), location_(location) + { // // We will set this transaction to jump to error state and then we will setup // the redirect on SEND_RESPONSE_HEADERS @@ -42,35 +43,40 @@ public: transaction.error(); } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { transaction.getClientResponse().setStatusCode(HTTP_STATUS_MOVED_TEMPORARILY); transaction.getClientResponse().setReasonPhrase("Moved Temporarily"); transaction.getClientResponse().getHeaders()["Location"] = location_; transaction.resume(); } - virtual ~ClientRedirectTransactionPlugin() { } + virtual ~ClientRedirectTransactionPlugin() {} + private: string location_; }; -class ClientRedirectGlobalPlugin : public GlobalPlugin { +class ClientRedirectGlobalPlugin : public GlobalPlugin +{ public: - ClientRedirectGlobalPlugin() { - registerHook(HOOK_SEND_REQUEST_HEADERS); - } + ClientRedirectGlobalPlugin() { registerHook(HOOK_SEND_REQUEST_HEADERS); } - void handleSendRequestHeaders(Transaction &transaction) { - if(transaction.getClientRequest().getUrl().getQuery().find("redirect=1") != string::npos) { + void + handleSendRequestHeaders(Transaction &transaction) + { + if (transaction.getClientRequest().getUrl().getQuery().find("redirect=1") != string::npos) { transaction.addPlugin(new ClientRedirectTransactionPlugin(transaction, "http://www.linkedin.com/")); return; } transaction.resume(); } - }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new ClientRedirectGlobalPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/clientrequest/ClientRequest.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/clientrequest/ClientRequest.cc b/lib/atscppapi/examples/clientrequest/ClientRequest.cc index 31cd789..323870f 100644 --- a/lib/atscppapi/examples/clientrequest/ClientRequest.cc +++ b/lib/atscppapi/examples/clientrequest/ClientRequest.cc @@ -28,16 +28,20 @@ using std::endl; using std::list; using std::string; -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { + GlobalHookPlugin() + { registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); registerHook(HOOK_SEND_REQUEST_HEADERS); } - void handleReadRequestHeadersPreRemap(Transaction &transaction) { + void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { cout << "Hello from handleReadRequesHeadersPreRemap!" << endl; ClientRequest &client_request = transaction.getClientRequest(); @@ -61,7 +65,9 @@ public: } - void handleReadRequestHeadersPostRemap(Transaction &transaction) { + void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { cout << "Hello from handleReadRequesHeadersPostRemap!" << endl; ClientRequest &client_request = transaction.getClientRequest(); @@ -79,7 +85,7 @@ public: Headers &client_request_headers = client_request.getHeaders(); Headers::iterator ii = client_request_headers.find("AccepT-EncodinG"); - if(ii != client_request_headers.end()) { + if (ii != client_request_headers.end()) { cout << "Deleting accept-encoding header" << endl; client_request_headers.erase("AccepT-EnCoDing"); // Case Insensitive } @@ -92,8 +98,8 @@ public: cout << "Adding a new accept type accept header" << endl; client_request_headers.append("accept", "text/blah"); - for (Headers::iterator header_iter = client_request_headers.begin(), - header_end = client_request_headers.end(); header_iter != header_end; ++header_iter) { + for (Headers::iterator header_iter = client_request_headers.begin(), header_end = client_request_headers.end(); + header_iter != header_end; ++header_iter) { cout << (*header_iter).str() << endl; } @@ -105,13 +111,17 @@ public: */ cout << "Joining on a non-existant header gives: " << client_request_headers.values("i_dont_exist") << endl; cout << "Joining the accept encoding header gives: " << client_request_headers.values("accept-encoding") << endl; - cout << "Joining the accept encoding header with space gives: " << client_request_headers.values("accept-encoding", ' ') << endl; - cout << "Joining the accept encoding header with long join string gives: " << client_request_headers.values("accept-encoding", "--join-string--") << endl; + cout << "Joining the accept encoding header with space gives: " << client_request_headers.values("accept-encoding", ' ') + << endl; + cout << "Joining the accept encoding header with long join string gives: " + << client_request_headers.values("accept-encoding", "--join-string--") << endl; transaction.resume(); } - void handleSendRequestHeaders(Transaction &transaction) { + void + handleSendRequestHeaders(Transaction &transaction) + { cout << "Hello from handleSendRequestHeaders!" << endl; cout << "---------------------IP INFORMATION-----------------" << endl; cout << "Server Address: " << utils::getIpPortString(transaction.getServerAddress()) << endl; @@ -120,9 +130,10 @@ public: cout << "Next Hop Address: " << utils::getIpPortString(transaction.getNextHopAddress()) << endl; transaction.resume(); } - }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new GlobalHookPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/customresponse/CustomResponse.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/customresponse/CustomResponse.cc b/lib/atscppapi/examples/customresponse/CustomResponse.cc index fef1263..b6d3057 100644 --- a/lib/atscppapi/examples/customresponse/CustomResponse.cc +++ b/lib/atscppapi/examples/customresponse/CustomResponse.cc @@ -37,21 +37,26 @@ using std::string; * */ -class CustomResponseTransactionPlugin : public atscppapi::TransactionPlugin { +class CustomResponseTransactionPlugin : public atscppapi::TransactionPlugin +{ public: CustomResponseTransactionPlugin(Transaction &transaction, HttpStatus status, const string &reason, const string &body) - : TransactionPlugin(transaction), status_(status), reason_(reason), body_(body) { + : TransactionPlugin(transaction), status_(status), reason_(reason), body_(body) + { TransactionPlugin::registerHook(HOOK_SEND_RESPONSE_HEADERS); transaction.error(body_); // Set the error body now, and change the status and reason later. } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { transaction.getClientResponse().setStatusCode(status_); transaction.getClientResponse().setReasonPhrase(reason_); transaction.resume(); } - virtual ~CustomResponseTransactionPlugin() { } + virtual ~CustomResponseTransactionPlugin() {} + private: HttpStatus status_; string reason_; @@ -59,14 +64,15 @@ private: }; -class ClientRedirectGlobalPlugin : public GlobalPlugin { +class ClientRedirectGlobalPlugin : public GlobalPlugin +{ public: - ClientRedirectGlobalPlugin() { - registerHook(HOOK_SEND_REQUEST_HEADERS); - } + ClientRedirectGlobalPlugin() { registerHook(HOOK_SEND_REQUEST_HEADERS); } - void handleSendRequestHeaders(Transaction &transaction) { - if(transaction.getClientRequest().getUrl().getQuery().find("custom=1") != string::npos) { + void + handleSendRequestHeaders(Transaction &transaction) + { + if (transaction.getClientRequest().getUrl().getQuery().find("custom=1") != string::npos) { transaction.addPlugin(new CustomResponseTransactionPlugin(transaction, HTTP_STATUS_OK, "Ok", "Hello! This is a custom response without making " "an origin request and no server intercept.")); @@ -74,9 +80,10 @@ public: } transaction.resume(); } - }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new ClientRedirectGlobalPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/globalhook/GlobalHookPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/globalhook/GlobalHookPlugin.cc b/lib/atscppapi/examples/globalhook/GlobalHookPlugin.cc index 7e47933..1602a05 100644 --- a/lib/atscppapi/examples/globalhook/GlobalHookPlugin.cc +++ b/lib/atscppapi/examples/globalhook/GlobalHookPlugin.cc @@ -22,20 +22,21 @@ using namespace atscppapi; -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { - registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); - } + GlobalHookPlugin() { registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); } - virtual void handleReadRequestHeadersPreRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { std::cout << "Hello from handleReadRequesHeadersPreRemap!" << std::endl; transaction.resume(); } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new GlobalHookPlugin(); } - - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/gzip_transformation/GzipTransformationPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/gzip_transformation/GzipTransformationPlugin.cc b/lib/atscppapi/examples/gzip_transformation/GzipTransformationPlugin.cc index caae81a..9f3a8d4 100644 --- a/lib/atscppapi/examples/gzip_transformation/GzipTransformationPlugin.cc +++ b/lib/atscppapi/examples/gzip_transformation/GzipTransformationPlugin.cc @@ -46,19 +46,30 @@ using std::string; * header is correctly set on the way out. */ -class Helpers { +class Helpers +{ public: - static bool clientAcceptsGzip(Transaction &transaction) { + static bool + clientAcceptsGzip(Transaction &transaction) + { return transaction.getClientRequest().getHeaders().values("Accept-Encoding").find("gzip") != string::npos; } - static bool serverReturnedGzip(Transaction &transaction) { + static bool + serverReturnedGzip(Transaction &transaction) + { return transaction.getServerResponse().getHeaders().values("Content-Encoding").find("gzip") != string::npos; } - enum ContentType { UNKNOWN = 0, TEXT_HTML = 1, TEXT_PLAIN = 2 }; + enum ContentType { + UNKNOWN = 0, + TEXT_HTML = 1, + TEXT_PLAIN = 2, + }; - static ContentType getContentType(Transaction &transaction) { + static ContentType + getContentType(Transaction &transaction) + { if (transaction.getServerResponse().getHeaders().values("Content-Type").find("text/html") != string::npos) { return TEXT_HTML; } else if (transaction.getServerResponse().getHeaders().values("Content-Type").find("text/plain") != string::npos) { @@ -69,24 +80,32 @@ public: } }; -class SomeTransformationPlugin : public TransformationPlugin { +class SomeTransformationPlugin : public TransformationPlugin +{ public: SomeTransformationPlugin(Transaction &transaction) - : TransformationPlugin(transaction, RESPONSE_TRANSFORMATION), transaction_(transaction) { + : TransformationPlugin(transaction, RESPONSE_TRANSFORMATION), transaction_(transaction) + { registerHook(HOOK_SEND_RESPONSE_HEADERS); } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { TS_DEBUG(TAG, "Added X-Content-Transformed header"); transaction.getClientResponse().getHeaders()["X-Content-Transformed"] = "1"; transaction.resume(); } - void consume(const string &data) { + void + consume(const string &data) + { produce(data); } - void handleInputComplete() { + void + handleInputComplete() + { Helpers::ContentType content_type = Helpers::getContentType(transaction_); if (content_type == Helpers::TEXT_HTML) { TS_DEBUG(TAG, "Adding an HTML comment at the end of the page"); @@ -100,20 +119,25 @@ public: setOutputComplete(); } - virtual ~SomeTransformationPlugin() { } + virtual ~SomeTransformationPlugin() {} + private: Transaction &transaction_; }; -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { + GlobalHookPlugin() + { registerHook(HOOK_SEND_REQUEST_HEADERS); registerHook(HOOK_READ_RESPONSE_HEADERS); registerHook(HOOK_SEND_RESPONSE_HEADERS); } - virtual void handleSendRequestHeaders(Transaction &transaction) { + virtual void + handleSendRequestHeaders(Transaction &transaction) + { // Since we can only decompress gzip we will change the accept encoding header // to gzip, even if the user cannot accept gziped content we will return to them // uncompressed content in that case since we have to be able to transform the content. @@ -126,45 +150,48 @@ public: transaction.resume(); } - virtual void handleReadResponseHeaders(Transaction &transaction) { + virtual void + handleReadResponseHeaders(Transaction &transaction) + { TS_DEBUG(TAG, "Determining if we need to add an inflate transformation or a deflate transformation.."); // We're guaranteed to have been returned either gzipped content or Identity. if (Helpers::serverReturnedGzip(transaction)) { // If the returned content was gziped we will inflate it so we can transform it. - TS_DEBUG(TAG,"Creating Inflate Transformation because the server returned gziped content"); - transaction.addPlugin(new GzipInflateTransformation(transaction, - TransformationPlugin::RESPONSE_TRANSFORMATION)); + TS_DEBUG(TAG, "Creating Inflate Transformation because the server returned gziped content"); + transaction.addPlugin(new GzipInflateTransformation(transaction, TransformationPlugin::RESPONSE_TRANSFORMATION)); } transaction.addPlugin(new SomeTransformationPlugin(transaction)); // Even if the server didn't return gziped content, if the user supports it we will gzip it. if (Helpers::clientAcceptsGzip(transaction)) { - TS_DEBUG(TAG,"The client supports gzip so we will deflate the content on the way out."); - transaction.addPlugin(new GzipDeflateTransformation(transaction, - TransformationPlugin::RESPONSE_TRANSFORMATION)); + TS_DEBUG(TAG, "The client supports gzip so we will deflate the content on the way out."); + transaction.addPlugin(new GzipDeflateTransformation(transaction, TransformationPlugin::RESPONSE_TRANSFORMATION)); } transaction.resume(); } - virtual void handleSendResponseHeaders(Transaction &transaction) { + virtual void + handleSendResponseHeaders(Transaction &transaction) + { // If the client supported gzip then we can guarantee they are receiving gzip since regardless of the // origins content-encoding we returned gzip, so let's make sure the content-encoding header is correctly // set to gzip or identity. if (Helpers::clientAcceptsGzip(transaction)) { - TS_DEBUG(TAG,"Setting the client response content-encoding to gzip since the user supported it, that's what they got."); + TS_DEBUG(TAG, "Setting the client response content-encoding to gzip since the user supported it, that's what they got."); transaction.getClientResponse().getHeaders()["Content-Encoding"] = "gzip"; } else { - TS_DEBUG(TAG,"Setting the client response content-encoding to identity since the user didn't support gzip"); + TS_DEBUG(TAG, "Setting the client response content-encoding to identity since the user didn't support gzip"); transaction.getClientResponse().getHeaders()["Content-Encoding"] = "identity"; } transaction.resume(); } - }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ TS_DEBUG(TAG, "TSPluginInit"); new GlobalHookPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/helloworld/HelloWorldPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/helloworld/HelloWorldPlugin.cc b/lib/atscppapi/examples/helloworld/HelloWorldPlugin.cc index 8d32502..e7a4399 100644 --- a/lib/atscppapi/examples/helloworld/HelloWorldPlugin.cc +++ b/lib/atscppapi/examples/helloworld/HelloWorldPlugin.cc @@ -21,16 +21,15 @@ #include <atscppapi/GlobalPlugin.h> #include <atscppapi/PluginInit.h> -class HelloWorldPlugin : public atscppapi::GlobalPlugin { +class HelloWorldPlugin : public atscppapi::GlobalPlugin +{ public: - HelloWorldPlugin() { - std::cout << "Hello World!" << std::endl; - } + HelloWorldPlugin() { std::cout << "Hello World!" << std::endl; } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ std::cout << "Hello from " << argv[0] << std::endl; new HelloWorldPlugin(); } - - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/intercept/intercept.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/intercept/intercept.cc b/lib/atscppapi/examples/intercept/intercept.cc index 4cac4ae..9cb0005 100644 --- a/lib/atscppapi/examples/intercept/intercept.cc +++ b/lib/atscppapi/examples/intercept/intercept.cc @@ -27,46 +27,56 @@ using std::string; using std::cout; using std::endl; -class Intercept : public InterceptPlugin { +class Intercept : public InterceptPlugin +{ public: - Intercept(Transaction &transaction) : InterceptPlugin(transaction, InterceptPlugin::SERVER_INTERCEPT) { } + Intercept(Transaction &transaction) : InterceptPlugin(transaction, InterceptPlugin::SERVER_INTERCEPT) {} void consume(const string &data, InterceptPlugin::RequestDataType type); void handleInputComplete(); ~Intercept() { cout << "Shutting down" << endl; } }; -class InterceptInstaller : public GlobalPlugin { +class InterceptInstaller : public GlobalPlugin +{ public: - InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) { + InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) + { GlobalPlugin::registerHook(Plugin::HOOK_READ_REQUEST_HEADERS_PRE_REMAP); } - void handleReadRequestHeadersPreRemap(Transaction &transaction) { + void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { transaction.addPlugin(new Intercept(transaction)); cout << "Added intercept" << endl; transaction.resume(); } }; -void TSPluginInit(int /* argc ATS_UNUSED */, const char * /* argv ATS_UNUSED */ []) { +void +TSPluginInit(int /* argc ATS_UNUSED */, const char * /* argv ATS_UNUSED */ []) +{ new InterceptInstaller(); } -void Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) { +void +Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) +{ if (type == InterceptPlugin::REQUEST_HEADER) { cout << "Read request header data" << endl << data; - } - else { + } else { cout << "Read request body data" << endl << data << endl; } } -void Intercept::handleInputComplete() { +void +Intercept::handleInputComplete() +{ cout << "Request data complete" << endl; string response("HTTP/1.1 200 OK\r\n" "Content-Length: 7\r\n" "\r\n"); InterceptPlugin::produce(response); -// sleep(5); TODO: this is a test for streaming; currently doesn't work + // sleep(5); TODO: this is a test for streaming; currently doesn't work response = "hello\r\n"; InterceptPlugin::produce(response); InterceptPlugin::setOutputComplete(); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/internal_transaction_handling/InternalTransactionHandling.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/internal_transaction_handling/InternalTransactionHandling.cc b/lib/atscppapi/examples/internal_transaction_handling/InternalTransactionHandling.cc index 28c7770..ee1ba07 100644 --- a/lib/atscppapi/examples/internal_transaction_handling/InternalTransactionHandling.cc +++ b/lib/atscppapi/examples/internal_transaction_handling/InternalTransactionHandling.cc @@ -27,38 +27,50 @@ using std::string; #define TAG "internal_transaction_handling" -class AllTransactionsGlobalPlugin : public GlobalPlugin { +class AllTransactionsGlobalPlugin : public GlobalPlugin +{ public: - AllTransactionsGlobalPlugin() : GlobalPlugin() { + AllTransactionsGlobalPlugin() : GlobalPlugin() + { TS_DEBUG(TAG, "Registering a global hook HOOK_READ_REQUEST_HEADERS_POST_REMAP"); registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { TS_DEBUG(TAG, "Received a request in handleReadRequestHeadersPostRemap."); transaction.resume(); } }; -class NoInternalTransactionsGlobalPlugin : public GlobalPlugin, public AsyncReceiver<AsyncHttpFetch> { +class NoInternalTransactionsGlobalPlugin : public GlobalPlugin, public AsyncReceiver<AsyncHttpFetch> +{ public: - NoInternalTransactionsGlobalPlugin() : GlobalPlugin(true) { + NoInternalTransactionsGlobalPlugin() : GlobalPlugin(true) + { TS_DEBUG(TAG, "Registering a global hook HOOK_READ_REQUEST_HEADERS_POST_REMAP"); registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { TS_DEBUG(TAG, "Received a request in handleReadRequestHeadersPostRemap."); - shared_ptr<Mutex> mutex(new Mutex()); // required for async operation + shared_ptr<Mutex> mutex(new Mutex()); // required for async operation Async::execute<AsyncHttpFetch>(this, new AsyncHttpFetch("http://127.0.0.1/"), mutex); // internal transaction transaction.resume(); } - void handleAsyncComplete(AsyncHttpFetch &provider ATSCPPAPI_UNUSED) { + void + handleAsyncComplete(AsyncHttpFetch &provider ATSCPPAPI_UNUSED) + { } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ TS_DEBUG(TAG, "Loaded async_http_fetch_example plugin"); new AllTransactionsGlobalPlugin(); new NoInternalTransactionsGlobalPlugin(); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/logger_example/LoggerExample.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/logger_example/LoggerExample.cc b/lib/atscppapi/examples/logger_example/LoggerExample.cc index c6cf32c..02f4732 100644 --- a/lib/atscppapi/examples/logger_example/LoggerExample.cc +++ b/lib/atscppapi/examples/logger_example/LoggerExample.cc @@ -32,7 +32,8 @@ using namespace atscppapi; using std::string; -namespace { +namespace +{ Logger log; } @@ -43,28 +44,31 @@ Logger log; * filename, function name, and line number of the message */ -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { - memset(big_buffer_6kb_,'a', sizeof(big_buffer_6kb_)); + GlobalHookPlugin() + { + memset(big_buffer_6kb_, 'a', sizeof(big_buffer_6kb_)); big_buffer_6kb_[sizeof(big_buffer_6kb_) - 1] = '\0'; - memset(big_buffer_14kb_,'a', sizeof(big_buffer_14kb_)); + memset(big_buffer_14kb_, 'a', sizeof(big_buffer_14kb_)); big_buffer_14kb_[sizeof(big_buffer_14kb_) - 1] = '\0'; registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { LOG_DEBUG(log, "handleReadRequestHeadersPostRemap.\n" - "\tRequest URL: %s\n" - "\tRequest Path: %s\n" - "\tRequest Query: %s\n" - "\tRequest Method: %s", transaction.getClientRequest().getUrl().getUrlString().c_str(), - transaction.getClientRequest().getUrl().getPath().c_str(), - transaction.getClientRequest().getUrl().getQuery().c_str(), - HTTP_METHOD_STRINGS[transaction.getClientRequest().getMethod()].c_str() - ); + "\tRequest URL: %s\n" + "\tRequest Path: %s\n" + "\tRequest Query: %s\n" + "\tRequest Method: %s", + transaction.getClientRequest().getUrl().getUrlString().c_str(), + transaction.getClientRequest().getUrl().getPath().c_str(), transaction.getClientRequest().getUrl().getQuery().c_str(), + HTTP_METHOD_STRINGS[transaction.getClientRequest().getMethod()].c_str()); // Next, to demonstrate how you can change logging levels: if (transaction.getClientRequest().getUrl().getPath() == "change_log_level") { @@ -89,12 +93,15 @@ public: transaction.resume(); } + private: - char big_buffer_6kb_[6*1024]; - char big_buffer_14kb_[14*1024]; + char big_buffer_6kb_[6 * 1024]; + char big_buffer_14kb_[14 * 1024]; }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ // Create a new logger // This will create a log file with the name logger_example.log (since we left off // the extension it will automatically add .log) @@ -111,7 +118,7 @@ void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED log.init("logger_example", true, true, Logger::LOG_LEVEL_DEBUG, true, 300); // Now that we've initialized a logger we can do all kinds of fun things on it: - log.setRollingEnabled(true); // already done via log.init, just an example. + log.setRollingEnabled(true); // already done via log.init, just an example. log.setRollingIntervalSeconds(300); // already done via log.init // You have two ways to log to a logger, you can log directly on the object itself: @@ -129,4 +136,3 @@ void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED new GlobalHookPlugin(); } - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/multiple_transaction_hooks/MultipleTransactionHookPlugins.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/multiple_transaction_hooks/MultipleTransactionHookPlugins.cc b/lib/atscppapi/examples/multiple_transaction_hooks/MultipleTransactionHookPlugins.cc index ca1dbe7..d1c2e40 100644 --- a/lib/atscppapi/examples/multiple_transaction_hooks/MultipleTransactionHookPlugins.cc +++ b/lib/atscppapi/examples/multiple_transaction_hooks/MultipleTransactionHookPlugins.cc @@ -25,63 +25,70 @@ using namespace atscppapi; -class MultipleTransactionHookPluginsOne : public atscppapi::TransactionPlugin { +class MultipleTransactionHookPluginsOne : public atscppapi::TransactionPlugin +{ public: - MultipleTransactionHookPluginsOne(Transaction &transaction) : TransactionPlugin(transaction) { + MultipleTransactionHookPluginsOne(Transaction &transaction) : TransactionPlugin(transaction) + { TransactionPlugin::registerHook(HOOK_SEND_RESPONSE_HEADERS); std::cout << "Constructed MultipleTransactionHookPluginsOne!" << std::endl; } - virtual ~MultipleTransactionHookPluginsOne() { - std::cout << "Destroyed MultipleTransactionHookPluginsOne!" << std::endl; - } + virtual ~MultipleTransactionHookPluginsOne() { std::cout << "Destroyed MultipleTransactionHookPluginsOne!" << std::endl; } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { std::cerr << "MultipleTransactionHookPluginsOne -- Send response headers!" << std::endl; transaction.resume(); } }; -class MultipleTransactionHookPluginsTwo : public atscppapi::TransactionPlugin { +class MultipleTransactionHookPluginsTwo : public atscppapi::TransactionPlugin +{ public: - MultipleTransactionHookPluginsTwo(Transaction &transaction) : TransactionPlugin(transaction) { + MultipleTransactionHookPluginsTwo(Transaction &transaction) : TransactionPlugin(transaction) + { TransactionPlugin::registerHook(HOOK_SEND_REQUEST_HEADERS); TransactionPlugin::registerHook(HOOK_SEND_RESPONSE_HEADERS); std::cout << "Constructed MultipleTransactionHookPluginsTwo!" << std::endl; } - virtual ~MultipleTransactionHookPluginsTwo() { - std::cout << "Destroyed MultipleTransactionHookPluginsTwo!" << std::endl; - } + virtual ~MultipleTransactionHookPluginsTwo() { std::cout << "Destroyed MultipleTransactionHookPluginsTwo!" << std::endl; } - void handleSendRequestHeaders(Transaction &transaction) { + void + handleSendRequestHeaders(Transaction &transaction) + { std::cout << "MultipleTransactionHookPluginsTwo -- Send request headers!" << std::endl; some_container_.push_back("We have transaction scoped storage in Transaction Hooks!"); transaction.resume(); } - void handleSendResponseHeaders(Transaction &transaction) { - std::cout << "MultipleTransactionHookPluginsTwo -- Send response headers!" << std::endl; + void + handleSendResponseHeaders(Transaction &transaction) + { + std::cout << "MultipleTransactionHookPluginsTwo -- Send response headers!" << std::endl; - // Demonstrate the concept of transaction scoped storage. - if(some_container_.size()) { - std::cout << some_container_.back() << std::endl; - } + // Demonstrate the concept of transaction scoped storage. + if (some_container_.size()) { + std::cout << some_container_.back() << std::endl; + } - transaction.resume(); - } + transaction.resume(); + } private: std::vector<std::string> some_container_; }; -class GlobalHookPlugin : public atscppapi::GlobalPlugin { +class GlobalHookPlugin : public atscppapi::GlobalPlugin +{ public: - GlobalHookPlugin() { - GlobalPlugin::registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); - } + GlobalHookPlugin() { GlobalPlugin::registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); } - virtual void handleReadRequestHeadersPreRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { std::cout << "Hello from handleReadRequesHeadersPreRemap!" << std::endl; // We need not store the addresses of the transaction plugins @@ -95,6 +102,8 @@ public: } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new GlobalHookPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/null_transformation_plugin/NullTransformationPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/null_transformation_plugin/NullTransformationPlugin.cc b/lib/atscppapi/examples/null_transformation_plugin/NullTransformationPlugin.cc index 445188e..fa6e710 100644 --- a/lib/atscppapi/examples/null_transformation_plugin/NullTransformationPlugin.cc +++ b/lib/atscppapi/examples/null_transformation_plugin/NullTransformationPlugin.cc @@ -26,62 +26,79 @@ using namespace atscppapi; using std::string; -namespace { +namespace +{ #define TAG "null_transformation" } -class NullTransformationPlugin : public TransformationPlugin { +class NullTransformationPlugin : public TransformationPlugin +{ public: NullTransformationPlugin(Transaction &transaction, TransformationPlugin::Type xformType) - : TransformationPlugin(transaction, xformType) { - registerHook((xformType == TransformationPlugin::REQUEST_TRANSFORMATION) ? - HOOK_SEND_REQUEST_HEADERS : HOOK_SEND_RESPONSE_HEADERS); + : TransformationPlugin(transaction, xformType) + { + registerHook((xformType == TransformationPlugin::REQUEST_TRANSFORMATION) ? HOOK_SEND_REQUEST_HEADERS : + HOOK_SEND_RESPONSE_HEADERS); } - void handleSendRequestHeaders(Transaction &transaction) { + void + handleSendRequestHeaders(Transaction &transaction) + { transaction.getServerRequest().getHeaders()["X-Content-Transformed"] = "1"; transaction.resume(); } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { transaction.getClientResponse().getHeaders()["X-Content-Transformed"] = "1"; transaction.resume(); } - void consume(const string &data) { + void + consume(const string &data) + { produce(data); } - void handleInputComplete() { + void + handleInputComplete() + { setOutputComplete(); } - virtual ~NullTransformationPlugin() { - - } + virtual ~NullTransformationPlugin() {} private: }; -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { + GlobalHookPlugin() + { registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); registerHook(HOOK_READ_RESPONSE_HEADERS); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { transaction.addPlugin(new NullTransformationPlugin(transaction, TransformationPlugin::REQUEST_TRANSFORMATION)); transaction.resume(); } - virtual void handleReadResponseHeaders(Transaction &transaction) { + virtual void + handleReadResponseHeaders(Transaction &transaction) + { transaction.addPlugin(new NullTransformationPlugin(transaction, TransformationPlugin::RESPONSE_TRANSFORMATION)); transaction.resume(); } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ TS_DEBUG(TAG, "TSPluginInit"); new GlobalHookPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/post_buffer/PostBuffer.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/post_buffer/PostBuffer.cc b/lib/atscppapi/examples/post_buffer/PostBuffer.cc index a360788..85131de 100644 --- a/lib/atscppapi/examples/post_buffer/PostBuffer.cc +++ b/lib/atscppapi/examples/post_buffer/PostBuffer.cc @@ -28,37 +28,44 @@ using std::cerr; using std::endl; using std::string; -class PostBufferTransformationPlugin : public TransformationPlugin { +class PostBufferTransformationPlugin : public TransformationPlugin +{ public: PostBufferTransformationPlugin(Transaction &transaction) - : TransformationPlugin(transaction, REQUEST_TRANSFORMATION), transaction_(transaction) { + : TransformationPlugin(transaction, REQUEST_TRANSFORMATION), transaction_(transaction) + { buffer_.reserve(1024); // not required, this is an optimization to start the buffer at a slightly higher value. (void)transaction_; } - void consume(const string &data) { + void + consume(const string &data) + { buffer_.append(data); } - void handleInputComplete() { + void + handleInputComplete() + { produce(buffer_); setOutputComplete(); } - virtual ~PostBufferTransformationPlugin() { } + virtual ~PostBufferTransformationPlugin() {} private: Transaction &transaction_; string buffer_; }; -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { - registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); - } + GlobalHookPlugin() { registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { cerr << "Read Request Headers Post Remap" << endl; cerr << "Path: " << transaction.getClientRequest().getUrl().getPath() << endl; cerr << "Method: " << HTTP_METHOD_STRINGS[transaction.getClientRequest().getMethod()] << endl; @@ -70,6 +77,8 @@ public: } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new GlobalHookPlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/remap_plugin/RemapPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/remap_plugin/RemapPlugin.cc b/lib/atscppapi/examples/remap_plugin/RemapPlugin.cc index bb0a246..cd83117 100644 --- a/lib/atscppapi/examples/remap_plugin/RemapPlugin.cc +++ b/lib/atscppapi/examples/remap_plugin/RemapPlugin.cc @@ -29,11 +29,14 @@ using namespace atscppapi; #define LOG_TAG "remapplugin" -class MyRemapPlugin : public RemapPlugin { +class MyRemapPlugin : public RemapPlugin +{ public: - MyRemapPlugin(void **instance_handle) : RemapPlugin(instance_handle) { } + MyRemapPlugin(void **instance_handle) : RemapPlugin(instance_handle) {} - Result doRemap(const Url &map_from_url, const Url &map_to_url, Transaction &transaction, bool &redirect) { + Result + doRemap(const Url &map_from_url, const Url &map_to_url, Transaction &transaction, bool &redirect) + { Url &request_url = transaction.getClientRequest().getUrl(); TS_DEBUG(LOG_TAG, "from URL is [%s], to URL is [%s], request URL is [%s]", map_from_url.getUrlString().c_str(), map_to_url.getUrlString().c_str(), request_url.getUrlString().c_str()); @@ -81,7 +84,10 @@ public: } }; -TsReturnCode TSRemapNewInstance(int argc ATSCPPAPI_UNUSED, char *argv[] ATSCPPAPI_UNUSED, void **instance_handle, char *errbuf ATSCPPAPI_UNUSED, int errbuf_size ATSCPPAPI_UNUSED) { +TsReturnCode +TSRemapNewInstance(int argc ATSCPPAPI_UNUSED, char *argv[] ATSCPPAPI_UNUSED, void **instance_handle, char *errbuf ATSCPPAPI_UNUSED, + int errbuf_size ATSCPPAPI_UNUSED) +{ new MyRemapPlugin(instance_handle); return TS_SUCCESS; } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/serverresponse/ServerResponse.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/serverresponse/ServerResponse.cc b/lib/atscppapi/examples/serverresponse/ServerResponse.cc index 01fb995..b151210 100644 --- a/lib/atscppapi/examples/serverresponse/ServerResponse.cc +++ b/lib/atscppapi/examples/serverresponse/ServerResponse.cc @@ -28,18 +28,22 @@ using std::endl; using std::list; using std::string; -class ServerResponsePlugin : public GlobalPlugin { +class ServerResponsePlugin : public GlobalPlugin +{ public: - ServerResponsePlugin() { + ServerResponsePlugin() + { registerHook(HOOK_SEND_REQUEST_HEADERS); registerHook(HOOK_READ_RESPONSE_HEADERS); registerHook(HOOK_SEND_RESPONSE_HEADERS); } - void handleSendRequestHeaders(Transaction &transaction) { + void + handleSendRequestHeaders(Transaction &transaction) + { // Here we can decide to abort the request to the origin (we can do this earlier too) // and just send the user an error page. - if(transaction.getClientRequest().getUrl().getQuery().find("error=1") != string::npos) { + if (transaction.getClientRequest().getUrl().getQuery().find("error=1") != string::npos) { // Give this user an error page and don't make a request to an origin. cout << "Sending this request an error page" << endl; transaction.error("This is the error response, but the response code is 500." @@ -52,7 +56,9 @@ public: cout << transaction.getServerRequest().getHeaders() << endl; } - void handleReadResponseHeaders(Transaction &transaction) { + void + handleReadResponseHeaders(Transaction &transaction) + { cout << "Hello from handleReadResponseHeaders!" << endl; cout << "Server response headers are" << endl; Response &server_response = transaction.getServerResponse(); @@ -61,7 +67,9 @@ public: transaction.resume(); } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { cout << "Hello from handleSendResponseHeaders!" << endl; cout << "Client response headers are" << endl; transaction.getClientResponse().getHeaders()["X-Foo-Header"] = "1"; @@ -75,7 +83,7 @@ public: // request and prevent the origin request in the first place. // - if(transaction.getClientRequest().getUrl().getQuery().find("redirect=1") != string::npos) { + if (transaction.getClientRequest().getUrl().getQuery().find("redirect=1") != string::npos) { cout << "Sending this guy to google." << endl; transaction.getClientResponse().getHeaders().append("Location", "http://www.google.com"); transaction.getClientResponse().setStatusCode(HTTP_STATUS_MOVED_TEMPORARILY); @@ -87,24 +95,24 @@ public: } private: - void printHeadersManual(Headers &headers) { - - for (Headers::iterator header_iter = headers.begin(), header_end = headers.end(); - header_iter != header_end; ++header_iter) { - - cout << "Header " << (*header_iter).name() << ": " << endl; - - for (HeaderField::iterator value_iter = (*header_iter).begin(), values_end = (*header_iter).end(); - value_iter != values_end; ++value_iter) { + void + printHeadersManual(Headers &headers) + { + for (Headers::iterator header_iter = headers.begin(), header_end = headers.end(); header_iter != header_end; ++header_iter) { + cout << "Header " << (*header_iter).name() << ": " << endl; + + for (HeaderField::iterator value_iter = (*header_iter).begin(), values_end = (*header_iter).end(); value_iter != values_end; + ++value_iter) { cout << "\t" << *value_iter << endl; } - } cout << endl; } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new ServerResponsePlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/stat_example/StatExample.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/stat_example/StatExample.cc b/lib/atscppapi/examples/stat_example/StatExample.cc index 7208526..12b10cb 100644 --- a/lib/atscppapi/examples/stat_example/StatExample.cc +++ b/lib/atscppapi/examples/stat_example/StatExample.cc @@ -26,7 +26,8 @@ using namespace atscppapi; using std::string; -namespace { +namespace +{ // This is for the -T tag debugging // To view the debug messages ./traffic_server -T "stat_example.*" #define TAG "stat_example" @@ -44,14 +45,18 @@ Stat stat; * This is a simple plugin that will increment a counter * everytime a request comes in. */ -class GlobalHookPlugin : public GlobalPlugin { +class GlobalHookPlugin : public GlobalPlugin +{ public: - GlobalHookPlugin() { + GlobalHookPlugin() + { TS_DEBUG(TAG, "Registering a global hook HOOK_READ_REQUEST_HEADERS_POST_REMAP"); registerHook(HOOK_READ_REQUEST_HEADERS_POST_REMAP); } - virtual void handleReadRequestHeadersPostRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPostRemap(Transaction &transaction) + { TS_DEBUG(TAG, "Received a request, incrementing the counter."); stat.increment(); TS_DEBUG(TAG, "Stat '%s' value = %lld", STAT_NAME.c_str(), static_cast<long long>(stat.get())); @@ -59,7 +64,9 @@ public: } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ TS_DEBUG(TAG, "Loaded stat_example plugin"); // Since this stat is not persistent it will be initialized to 0. @@ -68,4 +75,3 @@ void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED new GlobalHookPlugin(); } - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/timeout_example/TimeoutExamplePlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/timeout_example/TimeoutExamplePlugin.cc b/lib/atscppapi/examples/timeout_example/TimeoutExamplePlugin.cc index b50bf5a..63c4b78 100644 --- a/lib/atscppapi/examples/timeout_example/TimeoutExamplePlugin.cc +++ b/lib/atscppapi/examples/timeout_example/TimeoutExamplePlugin.cc @@ -26,19 +26,25 @@ using namespace atscppapi; #define TAG "timeout_example_plugin" -class TimeoutExamplePlugin : public GlobalPlugin { +class TimeoutExamplePlugin : public GlobalPlugin +{ public: - TimeoutExamplePlugin() { + TimeoutExamplePlugin() + { registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); registerHook(HOOK_SEND_RESPONSE_HEADERS); } - virtual void handleSendResponseHeaders(Transaction &transaction) { + virtual void + handleSendResponseHeaders(Transaction &transaction) + { TS_DEBUG(TAG, "Sending response headers to the client, status=%d", transaction.getClientResponse().getStatusCode()); transaction.resume(); } - virtual void handleReadRequestHeadersPreRemap(Transaction &transaction) { + virtual void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { TS_DEBUG(TAG, "Setting all timeouts to 1ms, this will likely cause the transaction to receive a 504."); transaction.setTimeout(Transaction::TIMEOUT_CONNECT, 1); transaction.setTimeout(Transaction::TIMEOUT_ACTIVE, 1); @@ -48,7 +54,9 @@ public: } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ TS_DEBUG(TAG, "TSPluginInit"); new TimeoutExamplePlugin(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/atscppapi/examples/transactionhook/TransactionHookPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/transactionhook/TransactionHookPlugin.cc b/lib/atscppapi/examples/transactionhook/TransactionHookPlugin.cc index 41d6eff..22adadf 100644 --- a/lib/atscppapi/examples/transactionhook/TransactionHookPlugin.cc +++ b/lib/atscppapi/examples/transactionhook/TransactionHookPlugin.cc @@ -24,37 +24,46 @@ using namespace atscppapi; -class TransactionHookPlugin : public atscppapi::TransactionPlugin { +class TransactionHookPlugin : public atscppapi::TransactionPlugin +{ public: - TransactionHookPlugin(Transaction &transaction) : TransactionPlugin(transaction) { + TransactionHookPlugin(Transaction &transaction) : TransactionPlugin(transaction) + { char_ptr_ = new char[100]; TransactionPlugin::registerHook(HOOK_SEND_RESPONSE_HEADERS); std::cout << "Constructed!" << std::endl; } - virtual ~TransactionHookPlugin() { + virtual ~TransactionHookPlugin() + { delete[] char_ptr_; // cleanup std::cout << "Destroyed!" << std::endl; } - void handleSendResponseHeaders(Transaction &transaction) { + void + handleSendResponseHeaders(Transaction &transaction) + { std::cout << "Send response headers!" << std::endl; transaction.resume(); } + private: char *char_ptr_; }; -class GlobalHookPlugin : public atscppapi::GlobalPlugin { +class GlobalHookPlugin : public atscppapi::GlobalPlugin +{ public: - GlobalHookPlugin() { - GlobalPlugin::registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); - } - virtual void handleReadRequestHeadersPreRemap(Transaction &transaction) { + GlobalHookPlugin() { GlobalPlugin::registerHook(HOOK_READ_REQUEST_HEADERS_PRE_REMAP); } + virtual void + handleReadRequestHeadersPreRemap(Transaction &transaction) + { std::cout << "Hello from handleReadRequesHeadersPreRemap!" << std::endl; transaction.addPlugin(new TransactionHookPlugin(transaction)); transaction.resume(); } }; -void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) { +void +TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) +{ new GlobalHookPlugin(); }
