首页 > 插件开发 > 测试代码-replace_maxage

测试代码-replace_maxage

2013年11月9日     浏览数:2,027 发表评论 阅读评论

不一定是有什么价值的插件代码,测试了下替换maxage的值,顺便贴出来让大伙参考参考。测试的效果是会把所有的maxage替换为代码中指定的10s。直接贴代码,不解释了。

/*
 *   replace_maxage.c
 *
 *
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "ts/ts.h"
#include "/root/trafficserver/lib/ts/ink_defs.h"
//上面这行的路径要改为自己ats的安装源码路径

static void
replace_maxage(TSHttpTxn txnp)
{
  TSMBuffer resp_bufp;
  TSMLoc resp_loc;
  TSMLoc field_loc;
  if (TSHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc) != TS_SUCCESS) {
    TSError("couldn't retrieve server response header.\n");
    goto done;
  }
  field_loc = TSMimeHdrFieldFind(resp_bufp, resp_loc, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL);
  if (field_loc == TS_NULL_MLOC) {
    /* field was not found */

    /* create a new field in the header */
    TSMimeHdrFieldCreate(resp_bufp, resp_loc, &field_loc); /* Probably should check for errors. */
    /* set its name */
    TSMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL);
    /* set its value */
    TSMimeHdrFieldValueAppend(resp_bufp, resp_loc, field_loc, -1, "max-age=10", 10);
    /* insert it into the header */
    TSMimeHdrFieldAppend(resp_bufp, resp_loc, field_loc);
    TSHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    TSHandleMLocRelease(resp_bufp, TS_NULL_MLOC, resp_loc);
  } else {
    /* clear the field */
    TSMimeHdrFieldValuesClear(resp_bufp, resp_loc, field_loc);
    /* set the value to "none" */
    TSMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, "max-age=10", 10);
    TSHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    TSHandleMLocRelease(resp_bufp, TS_NULL_MLOC, resp_loc);
  }
  
done:
  TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
}

static int
replace_maxage_plugin(TSCont contp ATS_UNUSED, TSEvent event, void *edata)
{
  TSHttpTxn txnp = (TSHttpTxn) edata;

  switch (event) {
  case TS_EVENT_HTTP_READ_RESPONSE_HDR:
    replace_maxage(txnp);
    return 0;
  default:
    break;
  }
  return 0;
}

void
TSPluginInit(int argc ATS_UNUSED, const char *argv[] ATS_UNUSED)
{
  TSPluginRegistrationInfo info;

  info.plugin_name = "TetstReplace";
  info.vendor_name = "yuan";
  info.support_email = "nybywan@gmail.com";

  if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) {
    TSError("Plugin registration failed. \n");
  }

  TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(replace_maxage_plugin, NULL));
}

what are energy conservation principles research paper on is it better to build or buy a computer – tutors online tutoring
分类: 插件开发 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.