You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by br...@apache.org on 2014/03/06 21:47:01 UTC
[1/2] git commit: Fixed async http fetch to support headers and post
body
Repository: trafficserver
Updated Branches:
refs/heads/master f5c07af5d -> b4c340033
Fixed async http fetch to support headers and post body
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/79ae4a1f
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/79ae4a1f
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/79ae4a1f
Branch: refs/heads/master
Commit: 79ae4a1fc37662ae79167beca0533785be63498a
Parents: f5c07af
Author: Manjesh Nilange <ma...@yahoo.com>
Authored: Thu Mar 6 12:29:23 2014 -0800
Committer: Manjesh Nilange <ma...@yahoo.com>
Committed: Thu Mar 6 12:29:23 2014 -0800
----------------------------------------------------------------------
.../examples/async_http_fetch/AsyncHttpFetch.cc | 14 +++++--
lib/atscppapi/src/AsyncHttpFetch.cc | 41 +++++++++++++-------
lib/atscppapi/src/Headers.cc | 15 ++++++-
.../src/include/atscppapi/AsyncHttpFetch.h | 3 ++
lib/atscppapi/src/include/atscppapi/Headers.h | 3 +-
5 files changed, 56 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/79ae4a1f/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 c0b8079..c296dfe 100644
--- a/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc
+++ b/lib/atscppapi/examples/async_http_fetch/AsyncHttpFetch.cc
@@ -55,12 +55,17 @@ public:
void handleSendRequestHeaders(Transaction &transaction) {
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");
+
+ 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
AsyncHttpFetch2 *provider2 = new AsyncHttpFetch2("http://127.0.0.1/");
Headers &request_headers = provider2->getRequestHeaders();
- request_headers["Header1"] = "Value1";
- request_headers["Header2"] = "Value2";
+ request_headers.set("Header1", "Value1");
+ request_headers.set("Header2", "Value2");
Async::execute<AsyncHttpFetch2>(this, provider2, getMutex());
++num_fetches_pending_;
}
@@ -93,6 +98,7 @@ private:
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]",
@@ -104,7 +110,7 @@ private:
const void *body;
size_t body_size;
async_http_fetch.getResponseBody(body, body_size);
- TS_DEBUG(TAG, "Response body is [%.*s]", static_cast<int>(body_size), static_cast<const char*>(body));
+ TS_DEBUG(TAG, "Response body is [%.*s]", body_size, body);
} else {
TS_ERROR(TAG, "Fetch did not complete successfully; Result %d",
static_cast<int>(async_http_fetch.getResult()));
@@ -137,6 +143,6 @@ public:
void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) {
TS_DEBUG(TAG, "Loaded async_http_fetch_example plugin");
- new GlobalHookPlugin();
+ GlobalPlugin *instance = new GlobalHookPlugin();
}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/79ae4a1f/lib/atscppapi/src/AsyncHttpFetch.cc
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/AsyncHttpFetch.cc b/lib/atscppapi/src/AsyncHttpFetch.cc
index 7f40dd2..1ea8b5f 100644
--- a/lib/atscppapi/src/AsyncHttpFetch.cc
+++ b/lib/atscppapi/src/AsyncHttpFetch.cc
@@ -26,6 +26,8 @@
#include "logging_internal.h"
#include "utils_internal.h"
+#include <cstdio>
+
using namespace atscppapi;
using std::string;
@@ -35,6 +37,7 @@ using std::string;
struct atscppapi::AsyncHttpFetchState : noncopyable {
Request request_;
Response response_;
+ string request_body_;
AsyncHttpFetch::Result result_;
const void *body_;
size_t body_size_;
@@ -42,9 +45,9 @@ struct atscppapi::AsyncHttpFetchState : noncopyable {
TSMLoc hdr_loc_;
shared_ptr<AsyncDispatchControllerBase> dispatch_controller_;
- AsyncHttpFetchState(const string &url_str, HttpMethod http_method)
- : request_(url_str, http_method, HTTP_VERSION_1_0), result_(AsyncHttpFetch::RESULT_FAILURE), body_(NULL),
- body_size_(0), hdr_buf_(NULL), hdr_loc_(NULL) { }
+ AsyncHttpFetchState(const string &url_str, HttpMethod http_method, string request_body)
+ : request_(url_str, http_method, HTTP_VERSION_1_0), request_body_(request_body),
+ result_(AsyncHttpFetch::RESULT_FAILURE), body_(NULL), body_size_(0), hdr_buf_(NULL), hdr_loc_(NULL) { }
~AsyncHttpFetchState() {
if (hdr_loc_) {
@@ -102,9 +105,17 @@ static int handleFetchEvents(TSCont cont, TSEvent event, void *edata) {
}
-AsyncHttpFetch::AsyncHttpFetch(const std::string &url_str, HttpMethod http_method) {
+AsyncHttpFetch::AsyncHttpFetch(const string &url_str, const string &request_body) {
+ init(url_str, HTTP_METHOD_POST, request_body);
+}
+
+AsyncHttpFetch::AsyncHttpFetch(const string &url_str, HttpMethod http_method) {
+ init(url_str, http_method, "");
+}
+
+void AsyncHttpFetch::init(const string &url_str, HttpMethod http_method, const string &request_body) {
LOG_DEBUG("Created new AsyncHttpFetch object %p", this);
- state_ = new AsyncHttpFetchState(url_str, http_method);
+ state_ = new AsyncHttpFetchState(url_str, http_method, request_body);
}
void AsyncHttpFetch::run(shared_ptr<AsyncDispatchControllerBase> sender) {
@@ -129,16 +140,20 @@ void AsyncHttpFetch::run(shared_ptr<AsyncDispatchControllerBase> sender) {
request_str += ' ';
request_str += HTTP_VERSION_STRINGS[state_->request_.getVersion()];
request_str += "\r\n";
-
- /* for (Headers::const_iterator iter = state_->request_.getHeaders().begin(),
- end = state_->request_.getHeaders().end(); iter != end; ++iter) {
- request_str += iter->first;
- request_str += ": ";
- request_str += Headers::getJoinedValues(iter->second);
- request_str += "\r\n";
+ Headers &headers = state_->request_.getHeaders();
+ if (headers.size()) {
+ // remove the possibility of keep-alive
+ headers.erase("Connection");
+ headers.erase("Proxy-Connection");
+ }
+ if (!state_->request_body_.empty()) {
+ char size_buf[128];
+ snprintf(size_buf, sizeof(size_buf), "%zu", state_->request_body_.size());
+ state_->request_.getHeaders().set("Content-Length", size_buf);
}
-*/
+ request_str += headers.str();
request_str += "\r\n";
+ request_str += state_->request_body_;
LOG_DEBUG("Issing TSFetchUrl with request\n[%s]", request_str.c_str());
TSFetchUrl(request_str.c_str(), request_str.size(), reinterpret_cast<struct sockaddr const *>(&addr), fetchCont,
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/79ae4a1f/lib/atscppapi/src/Headers.cc
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/Headers.cc b/lib/atscppapi/src/Headers.cc
index 871fecc..f65ae1e 100644
--- a/lib/atscppapi/src/Headers.cc
+++ b/lib/atscppapi/src/Headers.cc
@@ -372,11 +372,24 @@ HeaderField header_field_iterator::operator*() {
struct HeadersState: noncopyable {
TSMBuffer hdr_buf_;
TSMLoc hdr_loc_;
- HeadersState() : hdr_buf_(NULL), hdr_loc_(NULL) { }
+ bool self_created_structures_;
+ HeadersState() {
+ hdr_buf_ = TSMBufferCreate();
+ hdr_loc_ = TSHttpHdrCreate(hdr_buf_);
+ self_created_structures_ = true;
+ }
void reset(TSMBuffer bufp, TSMLoc hdr_loc) {
+ if (self_created_structures_) {
+ TSHandleMLocRelease(hdr_buf_, TS_NULL_MLOC /* no parent */, hdr_loc_);
+ TSMBufferDestroy(hdr_buf_);
+ self_created_structures_ = false;
+ }
hdr_buf_ = bufp;
hdr_loc_ = hdr_loc;
}
+ ~HeadersState() {
+ reset(NULL, NULL);
+ }
};
Headers::Headers() {
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/79ae4a1f/lib/atscppapi/src/include/atscppapi/AsyncHttpFetch.h
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/include/atscppapi/AsyncHttpFetch.h b/lib/atscppapi/src/include/atscppapi/AsyncHttpFetch.h
index 5355e41..a98e1a6 100644
--- a/lib/atscppapi/src/include/atscppapi/AsyncHttpFetch.h
+++ b/lib/atscppapi/src/include/atscppapi/AsyncHttpFetch.h
@@ -47,6 +47,8 @@ class AsyncHttpFetch : public AsyncProvider {
public:
AsyncHttpFetch(const std::string &url_str, HttpMethod http_method = HTTP_METHOD_GET);
+ AsyncHttpFetch(const std::string &url_str, const std::string &request_body);
+
/**
* Used to manipulate the headers of the request to be made.
*
@@ -94,6 +96,7 @@ public:
private:
AsyncHttpFetchState *state_;
+ void init(const std::string &url_str, HttpMethod http_method, const std::string &request_body);
friend class utils::internal;
};
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/79ae4a1f/lib/atscppapi/src/include/atscppapi/Headers.h
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/include/atscppapi/Headers.h b/lib/atscppapi/src/include/atscppapi/Headers.h
index 19d46f8..27c1f0a 100644
--- a/lib/atscppapi/src/include/atscppapi/Headers.h
+++ b/lib/atscppapi/src/include/atscppapi/Headers.h
@@ -396,8 +396,7 @@ public:
class Headers: noncopyable {
public:
/**
- * Constructor for Headers, this shouldn't be used directly unless you're trying to mix the C++ and C apis.
- * @warning This should only be used if you're mixing the C++ and C apis, it will be constructed automatically if using only the C++ api.
+ * Constructor for Headers. This creates a "detached" headers, i.e., not tied to any transaction.
*/
Headers();
Re: [2/2] git commit: intercept support in atscppapi
Posted by James Peach <jp...@apache.org>.
We should have a JIRA ticket for this so that we can publish accurate change logs and release notes
On Mar 6, 2014, at 12:47 PM, briang@apache.org wrote:
> intercept support in atscppapi
>
>
> Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
> Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/b4c34003
> Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/b4c34003
> Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/b4c34003
>
> Branch: refs/heads/master
> Commit: b4c34003301694dc5a26498382c4446364d4a2eb
> Parents: 79ae4a1
> Author: Manjesh Nilange <ma...@yahoo.com>
> Authored: Thu Mar 6 12:36:54 2014 -0800
> Committer: Manjesh Nilange <ma...@yahoo.com>
> Committed: Thu Mar 6 12:36:54 2014 -0800
>
> ----------------------------------------------------------------------
> configure.ac | 1 +
> lib/atscppapi/examples/Makefile.am | 3 +-
> lib/atscppapi/examples/intercept/Makefile.am | 24 ++++++++++
> lib/atscppapi/examples/intercept/intercept.cc | 55 ++++++++++++++++++++++
> lib/atscppapi/src/Makefile.am | 6 ++-
> lib/atscppapi/src/include/utils_internal.h | 6 +++
> 6 files changed, 92 insertions(+), 3 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/configure.ac
> ----------------------------------------------------------------------
> diff --git a/configure.ac b/configure.ac
> index 875f2c4..7460f7c 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -1937,6 +1937,7 @@ AC_CONFIG_FILES([
> lib/atscppapi/examples/timeout_example/Makefile
> lib/atscppapi/examples/internal_transaction_handling/Makefile
> lib/atscppapi/examples/async_timer/Makefile
> + lib/atscppapi/examples/intercept/Makefile
> lib/wccp/Makefile
> lib/perl/Makefile
> lib/perl/lib/Apache/TS.pm
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/examples/Makefile.am b/lib/atscppapi/examples/Makefile.am
> index 23c9c70..9cbf7d6 100644
> --- a/lib/atscppapi/examples/Makefile.am
> +++ b/lib/atscppapi/examples/Makefile.am
> @@ -33,4 +33,5 @@ SUBDIRS = helloworld \
> gzip_transformation \
> timeout_example \
> internal_transaction_handling \
> - async_timer
> \ No newline at end of file
> + async_timer \
> + intercept
> \ No newline at end of file
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/intercept/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/examples/intercept/Makefile.am b/lib/atscppapi/examples/intercept/Makefile.am
> new file mode 100644
> index 0000000..2400a10
> --- /dev/null
> +++ b/lib/atscppapi/examples/intercept/Makefile.am
> @@ -0,0 +1,24 @@
> +#
> +# Copyright (c) 2013 LinkedIn Corp. All rights reserved.
> +# Licensed 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.
> +#
> +
> +AM_CPPFLAGS = -I$(top_srcdir)/src/include
> +
> +target=intercept.so
> +pkglibdir = ${pkglibexecdir}
> +pkglib_LTLIBRARIES = intercept.la
> +intercept_la_SOURCES = intercept.cc
> +intercept_la_LDFLAGS = -module -avoid-version -shared -L$(top_srcdir) -latscppapi
> +
> +all:
> + ln -sf .libs/$(target)
> +
> +clean-local:
> + rm -f $(target)
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/intercept/intercept.cc
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/examples/intercept/intercept.cc b/lib/atscppapi/examples/intercept/intercept.cc
> new file mode 100644
> index 0000000..4c98782
> --- /dev/null
> +++ b/lib/atscppapi/examples/intercept/intercept.cc
> @@ -0,0 +1,55 @@
> +#include <atscppapi/GlobalPlugin.h>
> +#include <atscppapi/InterceptPlugin.h>
> +#include <atscppapi/PluginInit.h>
> +
> +#include <iostream>
> +
> +using namespace atscppapi;
> +using std::string;
> +using std::cout;
> +using std::endl;
> +
> +class Intercept : public InterceptPlugin {
> +public:
> + 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 {
> +public:
> + InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) {
> + GlobalPlugin::registerHook(Plugin::HOOK_READ_REQUEST_HEADERS_PRE_REMAP);
> + }
> + void handleReadRequestHeadersPreRemap(Transaction &transaction) {
> + transaction.addPlugin(new Intercept(transaction));
> + cout << "Added intercept" << endl;
> + transaction.resume();
> + }
> +};
> +
> +void TSPluginInit(int argc, const char *argv[]) {
> + new InterceptInstaller();
> +}
> +
> +void Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) {
> + if (type == InterceptPlugin::REQUEST_HEADER) {
> + cout << "Read request header data" << endl << data;
> + }
> + else {
> + cout << "Read request body data" << endl << data << endl;
> + }
> +}
> +
> +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
> + response = "hello\r\n";
> + InterceptPlugin::produce(response);
> + InterceptPlugin::setOutputComplete();
> +}
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/src/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/src/Makefile.am b/lib/atscppapi/src/Makefile.am
> index 4e7d204..346bdee 100644
> --- a/lib/atscppapi/src/Makefile.am
> +++ b/lib/atscppapi/src/Makefile.am
> @@ -51,7 +51,8 @@ libatscppapi_la_SOURCES = GlobalPlugin.cc \
> RemapPlugin.cc \
> GzipDeflateTransformation.cc \
> GzipInflateTransformation.cc \
> - AsyncTimer.cc
> + AsyncTimer.cc \
> + InterceptPlugin.cc
>
> library_includedir=$(includedir)/atscppapi
> base_include_folder = include/atscppapi/
> @@ -82,4 +83,5 @@ library_include_HEADERS = $(base_include_folder)/GlobalPlugin.h \
> $(base_include_folder)/AsyncHttpFetch.h \
> $(base_include_folder)/GzipDeflateTransformation.h \
> $(base_include_folder)/GzipInflateTransformation.h \
> - $(base_include_folder)/AsyncTimer.h
> + $(base_include_folder)/AsyncTimer.h \
> + $(base_include_folder)/InterceptPlugin.h
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/src/include/utils_internal.h
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/src/include/utils_internal.h b/lib/atscppapi/src/include/utils_internal.h
> index 0eb499e..c6ead71 100644
> --- a/lib/atscppapi/src/include/utils_internal.h
> +++ b/lib/atscppapi/src/include/utils_internal.h
> @@ -37,6 +37,7 @@
> #include "atscppapi/utils.h"
> #include "atscppapi/AsyncHttpFetch.h"
> #include "atscppapi/Transaction.h"
> +#include "atscppapi/InterceptPlugin.h"
>
> namespace atscppapi {
>
> @@ -80,6 +81,11 @@ public:
> static const std::list<TransactionPlugin *> &getTransactionPlugins(const Transaction &transaction) {
> return transaction.getPlugins();
> }
> +
> + static void dispatchInterceptEvent(InterceptPlugin *plugin, TSEvent event, void *edata) {
> + plugin->handleEvent(static_cast<int>(event), edata);
> + }
> +
> }; /* internal */
>
> } /* utils */
>
Re: [2/2] git commit: intercept support in atscppapi
Posted by James Peach <jp...@apache.org>.
We should have a JIRA ticket for this so that we can publish accurate change logs and release notes
On Mar 6, 2014, at 12:47 PM, briang@apache.org wrote:
> intercept support in atscppapi
>
>
> Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
> Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/b4c34003
> Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/b4c34003
> Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/b4c34003
>
> Branch: refs/heads/master
> Commit: b4c34003301694dc5a26498382c4446364d4a2eb
> Parents: 79ae4a1
> Author: Manjesh Nilange <ma...@yahoo.com>
> Authored: Thu Mar 6 12:36:54 2014 -0800
> Committer: Manjesh Nilange <ma...@yahoo.com>
> Committed: Thu Mar 6 12:36:54 2014 -0800
>
> ----------------------------------------------------------------------
> configure.ac | 1 +
> lib/atscppapi/examples/Makefile.am | 3 +-
> lib/atscppapi/examples/intercept/Makefile.am | 24 ++++++++++
> lib/atscppapi/examples/intercept/intercept.cc | 55 ++++++++++++++++++++++
> lib/atscppapi/src/Makefile.am | 6 ++-
> lib/atscppapi/src/include/utils_internal.h | 6 +++
> 6 files changed, 92 insertions(+), 3 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/configure.ac
> ----------------------------------------------------------------------
> diff --git a/configure.ac b/configure.ac
> index 875f2c4..7460f7c 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -1937,6 +1937,7 @@ AC_CONFIG_FILES([
> lib/atscppapi/examples/timeout_example/Makefile
> lib/atscppapi/examples/internal_transaction_handling/Makefile
> lib/atscppapi/examples/async_timer/Makefile
> + lib/atscppapi/examples/intercept/Makefile
> lib/wccp/Makefile
> lib/perl/Makefile
> lib/perl/lib/Apache/TS.pm
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/examples/Makefile.am b/lib/atscppapi/examples/Makefile.am
> index 23c9c70..9cbf7d6 100644
> --- a/lib/atscppapi/examples/Makefile.am
> +++ b/lib/atscppapi/examples/Makefile.am
> @@ -33,4 +33,5 @@ SUBDIRS = helloworld \
> gzip_transformation \
> timeout_example \
> internal_transaction_handling \
> - async_timer
> \ No newline at end of file
> + async_timer \
> + intercept
> \ No newline at end of file
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/intercept/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/examples/intercept/Makefile.am b/lib/atscppapi/examples/intercept/Makefile.am
> new file mode 100644
> index 0000000..2400a10
> --- /dev/null
> +++ b/lib/atscppapi/examples/intercept/Makefile.am
> @@ -0,0 +1,24 @@
> +#
> +# Copyright (c) 2013 LinkedIn Corp. All rights reserved.
> +# Licensed 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.
> +#
> +
> +AM_CPPFLAGS = -I$(top_srcdir)/src/include
> +
> +target=intercept.so
> +pkglibdir = ${pkglibexecdir}
> +pkglib_LTLIBRARIES = intercept.la
> +intercept_la_SOURCES = intercept.cc
> +intercept_la_LDFLAGS = -module -avoid-version -shared -L$(top_srcdir) -latscppapi
> +
> +all:
> + ln -sf .libs/$(target)
> +
> +clean-local:
> + rm -f $(target)
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/intercept/intercept.cc
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/examples/intercept/intercept.cc b/lib/atscppapi/examples/intercept/intercept.cc
> new file mode 100644
> index 0000000..4c98782
> --- /dev/null
> +++ b/lib/atscppapi/examples/intercept/intercept.cc
> @@ -0,0 +1,55 @@
> +#include <atscppapi/GlobalPlugin.h>
> +#include <atscppapi/InterceptPlugin.h>
> +#include <atscppapi/PluginInit.h>
> +
> +#include <iostream>
> +
> +using namespace atscppapi;
> +using std::string;
> +using std::cout;
> +using std::endl;
> +
> +class Intercept : public InterceptPlugin {
> +public:
> + 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 {
> +public:
> + InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) {
> + GlobalPlugin::registerHook(Plugin::HOOK_READ_REQUEST_HEADERS_PRE_REMAP);
> + }
> + void handleReadRequestHeadersPreRemap(Transaction &transaction) {
> + transaction.addPlugin(new Intercept(transaction));
> + cout << "Added intercept" << endl;
> + transaction.resume();
> + }
> +};
> +
> +void TSPluginInit(int argc, const char *argv[]) {
> + new InterceptInstaller();
> +}
> +
> +void Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) {
> + if (type == InterceptPlugin::REQUEST_HEADER) {
> + cout << "Read request header data" << endl << data;
> + }
> + else {
> + cout << "Read request body data" << endl << data << endl;
> + }
> +}
> +
> +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
> + response = "hello\r\n";
> + InterceptPlugin::produce(response);
> + InterceptPlugin::setOutputComplete();
> +}
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/src/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/src/Makefile.am b/lib/atscppapi/src/Makefile.am
> index 4e7d204..346bdee 100644
> --- a/lib/atscppapi/src/Makefile.am
> +++ b/lib/atscppapi/src/Makefile.am
> @@ -51,7 +51,8 @@ libatscppapi_la_SOURCES = GlobalPlugin.cc \
> RemapPlugin.cc \
> GzipDeflateTransformation.cc \
> GzipInflateTransformation.cc \
> - AsyncTimer.cc
> + AsyncTimer.cc \
> + InterceptPlugin.cc
>
> library_includedir=$(includedir)/atscppapi
> base_include_folder = include/atscppapi/
> @@ -82,4 +83,5 @@ library_include_HEADERS = $(base_include_folder)/GlobalPlugin.h \
> $(base_include_folder)/AsyncHttpFetch.h \
> $(base_include_folder)/GzipDeflateTransformation.h \
> $(base_include_folder)/GzipInflateTransformation.h \
> - $(base_include_folder)/AsyncTimer.h
> + $(base_include_folder)/AsyncTimer.h \
> + $(base_include_folder)/InterceptPlugin.h
>
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/src/include/utils_internal.h
> ----------------------------------------------------------------------
> diff --git a/lib/atscppapi/src/include/utils_internal.h b/lib/atscppapi/src/include/utils_internal.h
> index 0eb499e..c6ead71 100644
> --- a/lib/atscppapi/src/include/utils_internal.h
> +++ b/lib/atscppapi/src/include/utils_internal.h
> @@ -37,6 +37,7 @@
> #include "atscppapi/utils.h"
> #include "atscppapi/AsyncHttpFetch.h"
> #include "atscppapi/Transaction.h"
> +#include "atscppapi/InterceptPlugin.h"
>
> namespace atscppapi {
>
> @@ -80,6 +81,11 @@ public:
> static const std::list<TransactionPlugin *> &getTransactionPlugins(const Transaction &transaction) {
> return transaction.getPlugins();
> }
> +
> + static void dispatchInterceptEvent(InterceptPlugin *plugin, TSEvent event, void *edata) {
> + plugin->handleEvent(static_cast<int>(event), edata);
> + }
> +
> }; /* internal */
>
> } /* utils */
>
[2/2] git commit: intercept support in atscppapi
Posted by br...@apache.org.
intercept support in atscppapi
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/b4c34003
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/b4c34003
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/b4c34003
Branch: refs/heads/master
Commit: b4c34003301694dc5a26498382c4446364d4a2eb
Parents: 79ae4a1
Author: Manjesh Nilange <ma...@yahoo.com>
Authored: Thu Mar 6 12:36:54 2014 -0800
Committer: Manjesh Nilange <ma...@yahoo.com>
Committed: Thu Mar 6 12:36:54 2014 -0800
----------------------------------------------------------------------
configure.ac | 1 +
lib/atscppapi/examples/Makefile.am | 3 +-
lib/atscppapi/examples/intercept/Makefile.am | 24 ++++++++++
lib/atscppapi/examples/intercept/intercept.cc | 55 ++++++++++++++++++++++
lib/atscppapi/src/Makefile.am | 6 ++-
lib/atscppapi/src/include/utils_internal.h | 6 +++
6 files changed, 92 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index 875f2c4..7460f7c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1937,6 +1937,7 @@ AC_CONFIG_FILES([
lib/atscppapi/examples/timeout_example/Makefile
lib/atscppapi/examples/internal_transaction_handling/Makefile
lib/atscppapi/examples/async_timer/Makefile
+ lib/atscppapi/examples/intercept/Makefile
lib/wccp/Makefile
lib/perl/Makefile
lib/perl/lib/Apache/TS.pm
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/Makefile.am
----------------------------------------------------------------------
diff --git a/lib/atscppapi/examples/Makefile.am b/lib/atscppapi/examples/Makefile.am
index 23c9c70..9cbf7d6 100644
--- a/lib/atscppapi/examples/Makefile.am
+++ b/lib/atscppapi/examples/Makefile.am
@@ -33,4 +33,5 @@ SUBDIRS = helloworld \
gzip_transformation \
timeout_example \
internal_transaction_handling \
- async_timer
\ No newline at end of file
+ async_timer \
+ intercept
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/intercept/Makefile.am
----------------------------------------------------------------------
diff --git a/lib/atscppapi/examples/intercept/Makefile.am b/lib/atscppapi/examples/intercept/Makefile.am
new file mode 100644
index 0000000..2400a10
--- /dev/null
+++ b/lib/atscppapi/examples/intercept/Makefile.am
@@ -0,0 +1,24 @@
+#
+# Copyright (c) 2013 LinkedIn Corp. All rights reserved.
+# Licensed 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.
+#
+
+AM_CPPFLAGS = -I$(top_srcdir)/src/include
+
+target=intercept.so
+pkglibdir = ${pkglibexecdir}
+pkglib_LTLIBRARIES = intercept.la
+intercept_la_SOURCES = intercept.cc
+intercept_la_LDFLAGS = -module -avoid-version -shared -L$(top_srcdir) -latscppapi
+
+all:
+ ln -sf .libs/$(target)
+
+clean-local:
+ rm -f $(target)
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/examples/intercept/intercept.cc
----------------------------------------------------------------------
diff --git a/lib/atscppapi/examples/intercept/intercept.cc b/lib/atscppapi/examples/intercept/intercept.cc
new file mode 100644
index 0000000..4c98782
--- /dev/null
+++ b/lib/atscppapi/examples/intercept/intercept.cc
@@ -0,0 +1,55 @@
+#include <atscppapi/GlobalPlugin.h>
+#include <atscppapi/InterceptPlugin.h>
+#include <atscppapi/PluginInit.h>
+
+#include <iostream>
+
+using namespace atscppapi;
+using std::string;
+using std::cout;
+using std::endl;
+
+class Intercept : public InterceptPlugin {
+public:
+ 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 {
+public:
+ InterceptInstaller() : GlobalPlugin(true /* ignore internal transactions */) {
+ GlobalPlugin::registerHook(Plugin::HOOK_READ_REQUEST_HEADERS_PRE_REMAP);
+ }
+ void handleReadRequestHeadersPreRemap(Transaction &transaction) {
+ transaction.addPlugin(new Intercept(transaction));
+ cout << "Added intercept" << endl;
+ transaction.resume();
+ }
+};
+
+void TSPluginInit(int argc, const char *argv[]) {
+ new InterceptInstaller();
+}
+
+void Intercept::consume(const string &data, InterceptPlugin::RequestDataType type) {
+ if (type == InterceptPlugin::REQUEST_HEADER) {
+ cout << "Read request header data" << endl << data;
+ }
+ else {
+ cout << "Read request body data" << endl << data << endl;
+ }
+}
+
+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
+ response = "hello\r\n";
+ InterceptPlugin::produce(response);
+ InterceptPlugin::setOutputComplete();
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/src/Makefile.am
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/Makefile.am b/lib/atscppapi/src/Makefile.am
index 4e7d204..346bdee 100644
--- a/lib/atscppapi/src/Makefile.am
+++ b/lib/atscppapi/src/Makefile.am
@@ -51,7 +51,8 @@ libatscppapi_la_SOURCES = GlobalPlugin.cc \
RemapPlugin.cc \
GzipDeflateTransformation.cc \
GzipInflateTransformation.cc \
- AsyncTimer.cc
+ AsyncTimer.cc \
+ InterceptPlugin.cc
library_includedir=$(includedir)/atscppapi
base_include_folder = include/atscppapi/
@@ -82,4 +83,5 @@ library_include_HEADERS = $(base_include_folder)/GlobalPlugin.h \
$(base_include_folder)/AsyncHttpFetch.h \
$(base_include_folder)/GzipDeflateTransformation.h \
$(base_include_folder)/GzipInflateTransformation.h \
- $(base_include_folder)/AsyncTimer.h
+ $(base_include_folder)/AsyncTimer.h \
+ $(base_include_folder)/InterceptPlugin.h
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b4c34003/lib/atscppapi/src/include/utils_internal.h
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/include/utils_internal.h b/lib/atscppapi/src/include/utils_internal.h
index 0eb499e..c6ead71 100644
--- a/lib/atscppapi/src/include/utils_internal.h
+++ b/lib/atscppapi/src/include/utils_internal.h
@@ -37,6 +37,7 @@
#include "atscppapi/utils.h"
#include "atscppapi/AsyncHttpFetch.h"
#include "atscppapi/Transaction.h"
+#include "atscppapi/InterceptPlugin.h"
namespace atscppapi {
@@ -80,6 +81,11 @@ public:
static const std::list<TransactionPlugin *> &getTransactionPlugins(const Transaction &transaction) {
return transaction.getPlugins();
}
+
+ static void dispatchInterceptEvent(InterceptPlugin *plugin, TSEvent event, void *edata) {
+ plugin->handleEvent(static_cast<int>(event), edata);
+ }
+
}; /* internal */
} /* utils */