| 46 | } |
| 47 | |
| 48 | static int |
| 49 | ts_lua_fetch(lua_State *L) |
| 50 | { |
| 51 | int sz; |
| 52 | size_t n; |
| 53 | const char *url; |
| 54 | size_t url_len; |
| 55 | TSCont contp; |
| 56 | ts_lua_cont_info *ci; |
| 57 | ts_lua_async_item *ai; |
| 58 | ts_lua_fetch_info *fi; |
| 59 | ts_lua_fetch_multi_info *fmi; |
| 60 | |
| 61 | ci = ts_lua_get_cont_info(L); |
| 62 | if (ci == nullptr) { |
| 63 | TSError("[ts_lua][%s] no cont info found", __FUNCTION__); |
| 64 | TSReleaseAssert(!"Unexpected fetch of cont info"); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | n = lua_gettop(L); |
| 69 | if (n < 1) { |
| 70 | return luaL_error(L, "'ts.fetch' requires parameter"); |
| 71 | } |
| 72 | |
| 73 | /* url */ |
| 74 | if (!lua_isstring(L, 1)) { |
| 75 | return luaL_error(L, "'ts.fetch' first param is not string"); |
| 76 | } |
| 77 | |
| 78 | url = luaL_checklstring(L, 1, &url_len); |
| 79 | |
| 80 | /* replicate misc table */ |
| 81 | if (n >= 2) { |
| 82 | lua_pushvalue(L, 2); |
| 83 | |
| 84 | } else { |
| 85 | lua_pushnil(L); |
| 86 | } |
| 87 | |
| 88 | contp = TSContCreate(ts_lua_fetch_multi_handler, ci->mutex); |
| 89 | |
| 90 | sz = sizeof(ts_lua_fetch_multi_info) + 1 * sizeof(ts_lua_fetch_info); |
| 91 | fmi = (ts_lua_fetch_multi_info *)TSmalloc(sz); |
| 92 | |
| 93 | memset(fmi, 0, sz); |
| 94 | fmi->total = 1; |
| 95 | fmi->contp = contp; |
| 96 | |
| 97 | fi = &fmi->fiv[0]; |
| 98 | fi->fmi = fmi; |
| 99 | fi->buffer = TSIOBufferCreate(); |
| 100 | fi->reader = TSIOBufferReaderAlloc(fi->buffer); |
| 101 | |
| 102 | ts_lua_fetch_one_item(L, url, url_len, fi); |
| 103 | |
| 104 | // pop the replicated misc table |
| 105 | lua_pop(L, 1); |
nothing calls this directly
no test coverage detected