You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by Brian Geffon <br...@gmail.com> on 2012/09/04 07:38:29 UTC

C++11 API

Hello All,

The C++11 TS API is about 50% done, but as of now there is enough
there to handle probably 75% of plugin use cases.

How It Works
Obviously you will need a C++11 compiler, you'll also need to build
traffic server with that C++11 compiler (i use g++ 4.7 and it works
well), but Clang should also work. When you build TS make sure to
specify the --with-cpp11-api flag to configure, and then when you
install you'll two new header files installed namely ts-cpp11.h and
ts-cpp11-headers.h, but you'll only need to include ts-cpp11.h. Next,
to get all the fun adaptor code you'll need to link your plugin with
libatscpp11api by adding either -latscpp11api or by statically linking
with libatscpp11api.a. The former is probably preferred.

I hope everyone enjoys this new way of writing plugins :) Please let
me know if you have any ideas on how this can be improved, and finally
please feel free to fill in anything missing that you think will help.
In the next few weeks i'll add in Server Intercepts and
Transformations and many more methods for TXNs.

Example:
The API is fairly self explanatory, but i'll include a short getting
started example, this plugin will add a response X-My-Header: Woot to
every request who's path contains foo.

#include <string>
#include <iostream>
#include <algorithm>
#include <ts-cpp11.h>

using namespace ats::api;
using namespace ats::api::headers;
using namespace std;

void PluginRegister(const StringVector &arguments) {
  for (string s : arguments)
    cout << s << endl;

  CreateGlobalHook(HookType::HOOK_POST_REMAP, [](Transaction &txn) {
    if(GetRequestUrlPath(txn).find("foo") != string::npos) {
      // Closure created
      int x = 24;
      CreateTransactionHook(txn, HookType::HOOK_SEND_RESPONSE_HEADERS,
[&](Transaction &txn) {
            SetClientResponseHeader(txn, "X-My-Header", "Woot");
            // c++11 is awesome, a reference to x is available here!
            return NextState::HTTP_CONTINUE;
      });
    }
    return NextState::HTTP_CONTINUE;
  });
}



Have Fun,
Brian