static void ts_lua_http_intercept_setup_read(ts_lua_http_intercept_ctx *ictx) { ictx->input.buffer = TSIOBufferCreate(); ictx->input.reader = TSIOBufferReaderAlloc(ictx->input.buffer); ictx->input.vio = TSVConnRead(ictx->net_vc, ictx->contp, ictx->input.buffer, INT64_MAX); }
void ts_http_fetcher_launch(http_fetcher *fch) { int64_t hdr_len; // post body , content-length TSIOBufferWrite(fch->req_buffer, "\r\n", sizeof("\r\n")-1); hdr_len = TSIOBufferReaderAvail(fch->req_reader); fch->http_vc = TSHttpConnect(&(fch->aip)); fch->hdr_buffer = TSIOBufferSizedCreate(TS_IOBUFFER_SIZE_INDEX_8K); fch->hdr_reader = TSIOBufferReaderAlloc(fch->hdr_buffer); fch->hdr_bufp = TSMBufferCreate(); fch->hdr_loc = TSHttpHdrCreate(fch->hdr_bufp); fch->resp_buffer = TSIOBufferCreate(); fch->resp_reader = TSIOBufferReaderAlloc(fch->resp_buffer); fch->http_parser = TSHttpParserCreate(); fch->fetch_contp = TSContCreate(ts_http_fetch_handler, fch->mutexp); TSContDataSet(fch->fetch_contp, fch); fch->read_vio = TSVConnRead(fch->http_vc, fch->fetch_contp, fch->resp_buffer, INT64_MAX); if (fch->method == TS_FETCH_METHOD_POST && fch->post_cl >= 0) { fch->write_vio = TSVConnWrite(fch->http_vc, fch->fetch_contp, fch->req_reader, hdr_len + fch->post_cl); } else { fch->write_vio = TSVConnWrite(fch->http_vc, fch->fetch_contp, fch->req_reader, hdr_len); } fch->launched = 1; }
static void stats_process_accept(TSCont contp, stats_state *my_state) { my_state->req_buffer = TSIOBufferCreate(); my_state->resp_buffer = TSIOBufferCreate(); my_state->resp_reader = TSIOBufferReaderAlloc(my_state->resp_buffer); my_state->read_vio = TSVConnRead(my_state->net_vc, contp, my_state->req_buffer, INT64_MAX); }
static int transform_read_status(TSCont contp, TransformData *data) { data->state = STATE_READ_STATUS; data->output_buf = TSIOBufferCreate(); data->output_reader = TSIOBufferReaderAlloc(data->output_buf); if (data->output_reader != NULL) { data->server_vio = TSVConnRead(data->server_vc, contp, data->output_buf, sizeof(int)); } else { TSError("[%s] Error in Allocating a Reader to output buffer. TSIOBufferReaderAlloc returns NULL", PLUGIN_NAME); } return 0; }
static int transform_read(TSCont contp, TransformData *data) { data->state = STATE_READ; TSIOBufferDestroy(data->input_buf); data->input_buf = NULL; data->input_reader = NULL; data->server_vio = TSVConnRead(data->server_vc, contp, data->output_buf, data->content_length); data->output_vc = TSTransformOutputVConnGet((TSVConn)contp); if (data->output_vc == NULL) { TSError("[%s] TSTransformOutputVConnGet returns NULL", PLUGIN_NAME); } else { data->output_vio = TSVConnWrite(data->output_vc, contp, data->output_reader, data->content_length); if (data->output_vio == NULL) { TSError("[%s] TSVConnWrite returns NULL", PLUGIN_NAME); } } return 0; }
static int ts_lua_cache_read(lua_State *L) { int64_t length, n, avail; char *dst; ts_lua_cont_info *ci; ts_lua_cache_info *info; ci = ts_lua_get_cont_info(L); if (ci == NULL) return 0; n = lua_gettop(L); if (n < 1) { return luaL_error(L, "'ts.cache_read' requires parameter"); } /* length */ if (n >= 2) { if (!lua_isnumber(L, 2)) { return luaL_error(L, "'ts.cache_read' second param is not number"); } length = lua_tonumber(L, 2); if (length <= 0) { return luaL_error(L, "'ts.cache_read' second param is a not valid number"); } } else { length = INT64_MAX; } /* table */ if (!lua_istable(L, 1)) { return luaL_error(L, "'ts.cache_read' first param is not valid"); } lua_pushlstring(L, "info", sizeof("info") - 1); lua_gettable(L, 1); if (!lua_islightuserdata(L, -1)) { return luaL_error(L, "'ts.cache_read' first param is not valid"); } info = (ts_lua_cache_info*)lua_touserdata(L, -1); lua_pop(L, 1); if (info->hit == 0) { return luaL_error(L, "'ts.cache_read' is reading from unhit doc"); } if (info->optype != TS_LUA_CACHE_READ) { return luaL_error(L, "'ts.cache_read' is reading from invalid vc"); } if (info->eof || info->err) { lua_pushnil(L); return 1; } if (length != INT64_MAX) { info->need += length; } else { info->need = INT64_MAX; } info->current_handler = &ts_lua_cache_handle_read; avail = TSIOBufferReaderAvail(info->reserved.reader); if (avail + info->already >= info->need) { n = info->need - info->already; dst = (char*)TSmalloc(n); IOBufferReaderCopy(info->reserved.reader, dst, n); lua_pushlstring(L, (char*)dst, n); TSfree(dst); info->already = info->need; TSIOBufferReaderConsume(info->reserved.reader, n); return 1; } if (info->ioh.vio == NULL) { info->ioh.vio = TSVConnRead(info->cache_vc, info->contp, info->ioh.buffer, INT64_MAX); } else { TSVIOReenable(info->ioh.vio); } info->wait = 1; return lua_yield(L, 0); }