Repository: trafficserver Updated Branches: refs/heads/master 868efaf93 -> 60bcf3e26
TS-4144: Add TSHttpTxnSetHttpRetStatus wrapper in CPP API This closes #435 Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/60bcf3e2 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/60bcf3e2 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/60bcf3e2 Branch: refs/heads/master Commit: 60bcf3e26f2ce4fed3cf04788425e18f64654a5b Parents: 868efaf Author: Yukihisa Ishimura <[email protected]> Authored: Fri Jan 22 11:00:33 2016 +0900 Committer: Masaori Koshiba <[email protected]> Committed: Wed Feb 3 15:46:35 2016 +0900 ---------------------------------------------------------------------- configure.ac | 1 + lib/atscppapi/examples/Makefile.am | 3 +- .../CustomErrorRemapPlugin.cc | 57 ++++++++++++++++++++ .../custom_error_remap_plugin/Makefile.am | 25 +++++++++ lib/atscppapi/src/Transaction.cc | 22 +++++++- .../src/include/atscppapi/Transaction.h | 20 +++++++ 6 files changed, 125 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/60bcf3e2/configure.ac ---------------------------------------------------------------------- diff --git a/configure.ac b/configure.ac index 56793a1..58adee7 100644 --- a/configure.ac +++ b/configure.ac @@ -1958,6 +1958,7 @@ AS_IF([test "x$enable_cppapi" = "xyes"], [ lib/atscppapi/examples/null_transformation_plugin/Makefile lib/atscppapi/examples/post_buffer/Makefile lib/atscppapi/examples/remap_plugin/Makefile + lib/atscppapi/examples/custom_error_remap_plugin/Makefile lib/atscppapi/examples/serverresponse/Makefile lib/atscppapi/examples/stat_example/Makefile lib/atscppapi/examples/timeout_example/Makefile http://git-wip-us.apache.org/repos/asf/trafficserver/blob/60bcf3e2/lib/atscppapi/examples/Makefile.am ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/Makefile.am b/lib/atscppapi/examples/Makefile.am index 9f56838..bcc3ba0 100644 --- a/lib/atscppapi/examples/Makefile.am +++ b/lib/atscppapi/examples/Makefile.am @@ -30,10 +30,11 @@ SUBDIRS = \ boom \ stat_example \ remap_plugin \ + custom_error_remap_plugin \ async_http_fetch \ gzip_transformation \ timeout_example \ internal_transaction_handling \ async_timer \ intercept \ - async_http_fetch_streaming + async_http_fetch_streaming http://git-wip-us.apache.org/repos/asf/trafficserver/blob/60bcf3e2/lib/atscppapi/examples/custom_error_remap_plugin/CustomErrorRemapPlugin.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/custom_error_remap_plugin/CustomErrorRemapPlugin.cc b/lib/atscppapi/examples/custom_error_remap_plugin/CustomErrorRemapPlugin.cc new file mode 100644 index 0000000..7d29b60 --- /dev/null +++ b/lib/atscppapi/examples/custom_error_remap_plugin/CustomErrorRemapPlugin.cc @@ -0,0 +1,57 @@ +/** + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#include <atscppapi/RemapPlugin.h> +#include <atscppapi/PluginInit.h> +#include <string> + +using namespace std; +using namespace atscppapi; + +class MyRemapPlugin : public RemapPlugin +{ +public: + MyRemapPlugin(void **instance_handle) : RemapPlugin(instance_handle) {} + + Result + doRemap(const Url &map_from_url, const Url &map_to_url, Transaction &transaction, bool &redirect) + { + if (transaction.getClientRequest().getUrl().getQuery().find("custom=1") != string::npos) { + transaction.setStatusCode(HTTP_STATUS_FORBIDDEN); + if (transaction.getClientRequest().getUrl().getQuery().find("output=xml") != string::npos) { + transaction.setErrorBody( + "<Error>Hello! This is a custom response without making an origin request and no server intercept.</Error>", + "application/xml"); + } else { + transaction.setErrorBody("Hello! This is a custom response without making an origin request and no server intercept."); + } + + return RESULT_DID_REMAP; + } + + return RESULT_NO_REMAP; + } +}; + +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/60bcf3e2/lib/atscppapi/examples/custom_error_remap_plugin/Makefile.am ---------------------------------------------------------------------- diff --git a/lib/atscppapi/examples/custom_error_remap_plugin/Makefile.am b/lib/atscppapi/examples/custom_error_remap_plugin/Makefile.am new file mode 100644 index 0000000..1a0d420 --- /dev/null +++ b/lib/atscppapi/examples/custom_error_remap_plugin/Makefile.am @@ -0,0 +1,25 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include $(top_srcdir)/build/plugins.mk + +AM_CPPFLAGS += -I$(top_srcdir)/lib/atscppapi/src/include -Wno-unused-variable + +target=CustomErrorRemapPlugin.so +pkglib_LTLIBRARIES = CustomErrorRemapPlugin.la +CustomErrorRemapPlugin_la_SOURCES = CustomErrorRemapPlugin.cc +CustomErrorRemapPlugin_la_LDFLAGS = -module -avoid-version -shared -L$(top_builddir)/lib/atscppapi/src/ -latscppapi $(TS_PLUGIN_LDFLAGS) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/60bcf3e2/lib/atscppapi/src/Transaction.cc ---------------------------------------------------------------------- diff --git a/lib/atscppapi/src/Transaction.cc b/lib/atscppapi/src/Transaction.cc index 332ba58..a1694d5 100644 --- a/lib/atscppapi/src/Transaction.cc +++ b/lib/atscppapi/src/Transaction.cc @@ -160,8 +160,26 @@ Transaction::error(const std::string &page) void Transaction::setErrorBody(const std::string &page) { - LOG_DEBUG("Transaction tshttptxn=%p setting error body page: %s", state_->txn_, page.c_str()); - TSHttpTxnErrorBodySet(state_->txn_, TSstrdup(page.c_str()), page.length(), NULL); // Default to text/html + LOG_DEBUG("Transaction tshttptxn=%p setting error body page length: %lu", state_->txn_, page.length()); + char *body = (char *)TSmalloc(page.length()); + memcpy(body, page.data(), page.length()); + TSHttpTxnErrorBodySet(state_->txn_, body, page.length(), NULL); // Default to text/html +} + +void +Transaction::setErrorBody(const std::string &page, const std::string &mimetype) +{ + LOG_DEBUG("Transaction tshttptxn=%p setting error body page length: %lu", state_->txn_, page.length()); + char *body = (char *)TSmalloc(page.length()); + memcpy(body, page.data(), page.length()); + TSHttpTxnErrorBodySet(state_->txn_, body, page.length(), TSstrdup(mimetype.c_str())); +} + +void +Transaction::setStatusCode(HttpStatus code) +{ + LOG_DEBUG("Transaction tshttptxn=%p setting status code: %d", state_->txn_, code); + TSHttpTxnSetHttpRetStatus(state_->txn_, static_cast<TSHttpStatus>(code)); } bool http://git-wip-us.apache.org/repos/asf/trafficserver/blob/60bcf3e2/lib/atscppapi/src/include/atscppapi/Transaction.h ---------------------------------------------------------------------- diff --git a/lib/atscppapi/src/include/atscppapi/Transaction.h b/lib/atscppapi/src/include/atscppapi/Transaction.h index 42b980d..b96f9ae 100644 --- a/lib/atscppapi/src/include/atscppapi/Transaction.h +++ b/lib/atscppapi/src/include/atscppapi/Transaction.h @@ -30,6 +30,7 @@ #include "atscppapi/shared_ptr.h" #include "atscppapi/ClientRequest.h" #include "atscppapi/Response.h" +#include "atscppapi/HttpStatus.h" #include <ts/apidefs.h> namespace atscppapi { @@ -135,6 +136,25 @@ public: void setErrorBody(const std::string &content); /** + * Sets the error body page with mimetype. + * This method does not advance the state machine to the error state. + * To do that you must explicitally call error(). + * + * @param content the error page content. + * @param mimetype the error page's content-type. + */ + void setErrorBody(const std::string &content, const std::string &mimetype); + + /** + * Sets the status code. + * This is usable before transaction has the response of client like a remap state. + * A remap logic may advance the state machine to the error state depending on status code. + * + * @param code the status code. + */ + void setStatusCode(HttpStatus code); + + /** * Get the clients address * @return The sockaddr structure representing the client's address * @see atscppapi::utils::getIpString() in atscppapi/utils.h
