4 append_transform插件理解
来源:互联网 发布:过四级用哪个软件 编辑:程序博客网 时间:2024/06/10 05:58
先看一下代码
#include <limits.h>#include <stdio.h>#include <string.h>#include "ts/ts.h"#include "ink_defs.h"#define ASSERT_SUCCESS(_x) TSAssert((_x) == TS_SUCCESS)typedef struct { TSVIO output_vio; TSIOBuffer output_buffer; TSIOBufferReader output_reader; int append_needed;} MyData;static TSIOBuffer append_buffer;static TSIOBufferReader append_buffer_reader;static int append_buffer_length;static MyData *my_data_alloc(){ MyData *data; data = (MyData *)TSmalloc(sizeof(MyData)); TSReleaseAssert(data); data->output_vio = NULL; data->output_buffer = NULL; data->output_reader = NULL; data->append_needed = 1; return data;}static voidmy_data_destroy(MyData *data){ if (data) { if (data->output_buffer) TSIOBufferDestroy(data->output_buffer); TSfree(data); }}static voidhandle_transform(TSCont contp){ TSVConn output_conn; TSVIO write_vio; MyData *data; int64_t towrite; int64_t avail; /* Get the output connection where we'll write data to. */ output_conn = TSTransformOutputVConnGet(contp); /* Get the write VIO for the write operation that was performed on ourself. This VIO contains the buffer that we are to read from as well as the continuation we are to call when the buffer is empty. */ write_vio = TSVConnWriteVIOGet(contp); //input_vio /* Get our data structure for this operation. The private data structure contains the output VIO and output buffer. If the private data structure pointer is NULL, then we'll create it and initialize its internals. */ //注意append.txt不是实时加载的 它在启动的时候生成buf -----------这也许就是添加的时候只调用一个copy函数而不需要判断有效buf长度什么的了 data = TSContDataGet(contp); if (!data) { //现以 每重启一下ats 再进行一次实验的情况下 data第一次肯定为null towrite = TSVIONBytesGet(write_vio); //html网页实际大小 从http首部读出来的 if (towrite != INT64_MAX) { //第一次 0 towrite += append_buffer_length; //源网页长度 + 预加入文件长度 } data = my_data_alloc(); data->output_buffer = TSIOBufferCreate(); data->output_reader = TSIOBufferReaderAlloc(data->output_buffer); data->output_vio = TSVConnWrite(output_conn, contp, data->output_reader, towrite); TSContDataSet(contp, data); } /* We also check to see if the write VIO's buffer is non-NULL. A NULL buffer indicates that the write operation has been shutdown and that the continuation does not want us to send any more WRITE_READY or WRITE_COMPLETE events. For this simplistic transformation that means we're done. In a more complex transformation we might have to finish writing the transformed data to our output connection. */ if (!TSVIOBufferGet(write_vio)) { //input_vio关闭 即使关闭我们也得把数据加上去 if (data->append_needed) { data->append_needed = 0; TSIOBufferCopy(TSVIOBufferGet(data->output_vio), append_buffer_reader, append_buffer_length, 0); } TSVIONBytesSet(data->output_vio, TSVIONDoneGet(write_vio) + append_buffer_length); TSVIOReenable(data->output_vio); return; } /* Determine how much data we have left to read. For this append transform plugin this is also the amount of data we have left to write to the output connection. */ towrite = TSVIONTodoGet(write_vio); //源文件总长度 if (towrite > 0) { /* The amount of data left to read needs to be truncated by the amount of data actually in the read buffer. */ avail = TSIOBufferReaderAvail(TSVIOReaderGet(write_vio)); if (towrite > avail) { towrite = avail; } //-----------------------------------------这里的towrite变味了 很有可能这个towrite远小于上面的towrite if (towrite > 0) { /* Copy the data from the read buffer to the output buffer. */ TSIOBufferCopy(TSVIOBufferGet(data->output_vio), TSVIOReaderGet(write_vio), towrite, 0); //从input_vio的reader里拷贝towrite多个数据到output-buf里 /* Tell the read buffer that we have read the data and are no longer interested in it. */ TSIOBufferReaderConsume(TSVIOReaderGet(write_vio), towrite); /* Modify the write VIO to reflect how much data we've completed. */ TSVIONDoneSet(write_vio, TSVIONDoneGet(write_vio) + towrite); } } /* Now we check the write VIO to see if there is data left to read. */ if (TSVIONTodoGet(write_vio) > 0) { //copy的towrite值 小于 源towrite值 所以再次读input_vio时 会有余量towrite if (towrite > 0) { //即有余量 /* If there is data left to read, then we reenable the output connection by reenabling the output VIO. This will wakeup the output connection and allow it to consume data from the output buffer. */ TSVIOReenable(data->output_vio); /* Call back the write VIO continuation to let it know that we are ready for more data. */ TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_READY, write_vio); //向input_vio发送事件 } } else { //copy的towrite值 等于 源towrite值 即把input_vio中的数据全读完了 但还要读文件中的 if (data->append_needed) { data->append_needed = 0; //TSIOBufferCopy(TSVIOBufferGet(data->output_vio), TSVIOReaderGet(write_vio), towrite, 0); TSIOBufferCopy(TSVIOBufferGet(data->output_vio), append_buffer_reader, append_buffer_length, 0); } /* If there is no data left to read, then we modify the output VIO to reflect how much data the output connection should expect. This allows the output connection to know when it is done reading. We then reenable the output connection so that it can consume the data we just gave it. */ //TSVIONBytesSet(data->output_vio, TSVIONDoneGet(input_vio)); //结束标志 nbytes=ndone TSVIONBytesSet(data->output_vio, TSVIONDoneGet(write_vio) + append_buffer_length); TSVIOReenable(data->output_vio); /* Call back the write VIO continuation to let it know that we have completed the write operation. */ TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_COMPLETE, write_vio); }}static intappend_transform(TSCont contp, TSEvent event, void *edata ATS_UNUSED){ /* Check to see if the transformation has been closed by a call to TSVConnClose. */ if (TSVConnClosedGet(contp)) { my_data_destroy(TSContDataGet(contp)); TSContDestroy(contp); return 0; } else { switch (event) { case TS_EVENT_ERROR: { TSVIO write_vio; /* Get the write VIO for the write operation that was performed on ourself. This VIO contains the continuation of our parent transformation. */ write_vio = TSVConnWriteVIOGet(contp); /* Call back the write VIO continuation to let it know that we have completed the write operation. */ TSContCall(TSVIOContGet(write_vio), TS_EVENT_ERROR, write_vio); } break; case TS_EVENT_VCONN_WRITE_COMPLETE: /* When our output connection says that it has finished reading all the data we've written to it then we should shutdown the write portion of its connection to indicate that we don't want to hear about it anymore. */ TSVConnShutdown(TSTransformOutputVConnGet(contp), 0, 1); break; case TS_EVENT_VCONN_WRITE_READY: default: /* If we get a WRITE_READY event or any other type of event (sent, perhaps, because we were reenabled) then we'll attempt to transform more data. */ handle_transform(contp); break; } } return 0;}static inttransformable(TSHttpTxn txnp){ TSMBuffer bufp; TSMLoc hdr_loc; TSMLoc field_loc; TSHttpStatus resp_status; const char *value; int val_length; TSHttpTxnServerRespGet(txnp, &bufp, &hdr_loc); /* * We are only interested in "200 OK" responses. */ if (TS_HTTP_STATUS_OK == (resp_status = TSHttpHdrStatusGet(bufp, hdr_loc))) { /* We only want to do the transformation on documents that have a content type of "text/html". */ field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, "Content-Type", 12); if (!field_loc) { ASSERT_SUCCESS(TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc)); return 0; } value = TSMimeHdrFieldValueStringGet(bufp, hdr_loc, field_loc, -1, &val_length); //调查发现 只要获取mime头 第四个参数必须是-1 if (value && (strncasecmp(value, "text/html", sizeof("text/html") - 1) == 0)) { ASSERT_SUCCESS(TSHandleMLocRelease(bufp, hdr_loc, field_loc)); ASSERT_SUCCESS(TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc)); return 1; } else { ASSERT_SUCCESS(TSHandleMLocRelease(bufp, hdr_loc, field_loc)); ASSERT_SUCCESS(TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc)); return 0; } } return 0; /* not a 200 */}static voidtransform_add(TSHttpTxn txnp){ TSVConn connp; connp = TSTransformCreate(append_transform, txnp); TSHttpTxnHookAdd(txnp, TS_HTTP_RESPONSE_TRANSFORM_HOOK, connp);}static inttransform_plugin(TSCont contp ATS_UNUSED, TSEvent event, void *edata){ TSHttpTxn txnp = (TSHttpTxn)edata; switch (event) { case TS_EVENT_HTTP_READ_RESPONSE_HDR: if (transformable(txnp)) { transform_add(txnp); } TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); return 0; default: break; } return 0;}static intload(const char *filename){ TSFile fp; TSIOBufferBlock blk; char *p; int64_t avail; int err; fp = TSfopen(filename, "r"); if (!fp) { return 0; } append_buffer = TSIOBufferCreate(); append_buffer_reader = TSIOBufferReaderAlloc(append_buffer); for (;;) { blk = TSIOBufferStart(append_buffer); p = TSIOBufferBlockWriteStart(blk, &avail); //传出个数 返回将被写入的位置 err = TSfread(fp, p, avail); if (err > 0) { TSIOBufferProduce(append_buffer, err); } else { break; } } append_buffer_length = TSIOBufferReaderAvail(append_buffer_reader); //通过reader获取buffer中的总长度 TSfclose(fp); return 1;}voidTSPluginInit(int argc, const char *argv[]){ TSPluginRegistrationInfo info; info.plugin_name = "append-transform"; info.vendor_name = "intcache"; info.support_email = "ts-api-support@MyCompany.com"; if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) { TSError("Plugin registration failed.\n"); goto Lerror; } if (argc != 2) { TSError("usage: %s <filename>\n", argv[0]); goto Lerror; } if (!load(argv[1])) { TSError("[append-transform] Could not load %s\n", argv[1]); goto Lerror; } TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(transform_plugin, NULL)); return;Lerror: TSError("[append-transform] Unable to initialize plugin\n");}
这个插件功能是 接收源站发送过来的数据 根据实际缓冲区大小 再不断的发给客户端
源文件发送完毕 则再发送自定义文本中的数据
下面记录一下我的调试过程
b append-transform.c:append_transform
b append-transform.c:handle_transform
curl -vx 10.10.110.40:8081 http://10.10.110.45/ifeng6.html
到断点处后 curl的结果为:
* About to connect() to proxy 10.10.110.40 port 8081 (#0)
* Trying 10.10.110.40... connected
* Connected to 10.10.110.40 (10.10.110.40) port 8081 (#0)
> GET http://10.10.110.45/ifeng6.html HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 10.10.110.45
> Accept: */*
> Proxy-Connection: Keep-Alive
handle_transform函数里将获取http响应中的Content-Length: 35601
然后做各种初始化操作
缓冲区太小 没有发完 TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_READY, write_vio)
curl结果为
< HTTP/1.1 200 OK
< Server: ats1
< Date: Wed, 20 Sep 2017 06:37:33 GMT
< Content-Type: text/html
< Last-Modified: Wed, 20 Sep 2017 03:42:45 GMT
< ETag: "59c1e3b5-7ede"
< Accept-Ranges: bytes
< Content-Length: 35601
< Age: 0
< Proxy-Connection: keep-alive
< Via: http/1.1 hk_ats (ats2 [cMsSfW])
第二次缓冲区足够大 可以一并发完 然后再
TSIOBufferCopy(TSVIOBufferGet(data->output_vio), append_buffer_reader, append_buffer_length, 0);
TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_COMPLETE, write_vio);
第三次 input_vio已经关闭
TSVIONBytesSet(data->output_vio, TSVIONDoneGet(write_vio) + append_buffer_length);
TSVIOReenable(data->output_vio);
调试过程有点问题 :
为什么调用 TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_COMPLETE, write_vio); 后
还会到ready事件中去
- 4 append_transform插件理解
- 理解Jquery插件开发
- 插件化理解
- 理解 Babel 插件
- 插件化的理解
- 理解Jquery插件
- 深入理解插件系统
- 理解插件,Tab切换插件形式。
- 深入理解jQuery插件开发
- 深入理解jQuery插件开发
- 深入理解jQuery插件开发
- 理解chrome插件文件组成
- 自定义JQuery插件----深入理解
- 深入理解jQuery插件开发
- 深入理解jQuery插件开发
- 深入理解jQuery插件开发
- 深入理解jQuery插件开发
- 深入理解jQuery插件开发
- ES6 推荐网络文档书籍
- 小司机带你撸一个简单的RPC框架
- Centos7.3_64位服务器下配置Apache+MySQL+PHP基础环境
- Wireless Charging How-to
- 29岁,转行到IT做java,前途何解???
- 4 append_transform插件理解
- checkbox ,select,radio,得到选中值,设置显示的值
- Tensorflow学习(一)使用flags定义命令行参数
- Myeclipse重新安装新版本svn失效问题解决
- java 获取对象的数据类型
- FTPrep, 81 Search in Rotated Sorted Array II
- 常用小技巧之仿微信功能:退出主界面不销毁、拨打电话进入拨号界面(不直接拨打)
- IDEA上传项目到git和将文件项目上传到git
- EXKMP模版:最长共同前缀长度