Lines Matching refs:L

186 #define lua_newuserdata0(L, sz) lua_newuserdata((L),(sz))  argument
188 #define lua_newuserdata0(L, sz) lua_newuserdatauv((L),(sz),0) argument
192 #define lua_getfield_and_type(L,idx,k) \ argument
193 (lua_getfield((L),(idx),(k)), lua_type((L),-1))
194 #define lua_getglobal_and_type(L,name) \ argument
195 (lua_getglobal((L),(name)), lua_type((L),-1))
197 #define lua_getfield_and_type(L,idx,k) \ argument
198 lua_getfield((L),(idx),(k))
199 #define lua_getglobal_and_type(L,name) \ argument
200 lua_getglobal((L),(name))
205 lua_tointegerx (lua_State * const L, int idx, int *isnum) in lua_tointegerx() argument
209 *isnum = lua_isnumber(L, idx); in lua_tointegerx()
210 return *isnum ? lua_tointeger(L, idx) : 0; in lua_tointegerx()
216 static void lua_pushglobaltable(lua_State *L) { /* (-0, +1, -) */ in lua_pushglobaltable() argument
217 lua_pushvalue(L, LUA_GLOBALSINDEX); in lua_pushglobaltable()
221 static void magnet_setfenv_mainfn(lua_State *L, int funcIndex) { /* (-1, 0, -) */ in magnet_setfenv_mainfn() argument
227 const char* first_upvalue_name = lua_getupvalue(L, funcIndex, 1); in magnet_setfenv_mainfn()
229 lua_pop(L, 1); /* only need the name of the upvalue, not the value */ in magnet_setfenv_mainfn()
233 if (NULL == lua_setupvalue(L, funcIndex, 1)) { in magnet_setfenv_mainfn()
235 lua_pop(L, 1); in magnet_setfenv_mainfn()
238 lua_setfenv(L, funcIndex); in magnet_setfenv_mainfn()
248 #define luaL_setfuncs(L, l, nup) luaL_register((L), NULL, (l)) argument
257 static int magnet_pairs(lua_State *L) { in magnet_pairs() argument
258 luaL_checkany(L, 1); /* "self" */ in magnet_pairs()
260 if (luaL_getmetafield(L, 1, "__pairs")) { in magnet_pairs()
262 lua_pushvalue(L, 1); in magnet_pairs()
263 lua_call(L, 1, 3); in magnet_pairs()
266 lua_pushvalue(L, lua_upvalueindex(1)); in magnet_pairs()
267 lua_pushvalue(L, 1); in magnet_pairs()
268 lua_call(L, 1, 3); in magnet_pairs()
285 static int magnet_readdir_iter(lua_State *L) { in magnet_readdir_iter() argument
286 DIR ** const d = (DIR **)lua_touserdata(L, lua_upvalueindex(1)); in magnet_readdir_iter()
298 lua_pushlstring(L, de->d_name, _D_EXACT_NAMLEN(de)); in magnet_readdir_iter()
308 static int magnet_readdir_gc(lua_State *L) { in magnet_readdir_gc() argument
310 DIR ** const d = lua_touserdata(L, 1); in magnet_readdir_gc()
315 static void magnet_readdir_metatable(lua_State * const L) { in magnet_readdir_metatable() argument
316 if (luaL_newmetatable(L, "li.DIR")) { /* (sp += 1) */ in magnet_readdir_metatable()
317 lua_pushcclosure(L, magnet_readdir_gc, 0); /* (sp += 1) */ in magnet_readdir_metatable()
318 lua_setfield(L, -2, "__gc"); /* (sp -= 1) */ in magnet_readdir_metatable()
319 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_readdir_metatable()
320 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_readdir_metatable()
324 static int magnet_readdir(lua_State *L) { in magnet_readdir() argument
325 const char * const s = luaL_checkstring(L, 1); in magnet_readdir()
328 *(DIR **)lua_newuserdata0(L, sizeof(DIR *)) = d; in magnet_readdir()
329 magnet_readdir_metatable(L); in magnet_readdir()
330 lua_setmetatable(L, -2); in magnet_readdir()
331 lua_pushcclosure(L, magnet_readdir_iter, 1); in magnet_readdir()
334 lua_pushnil(L); in magnet_readdir()
340 static int magnet_newindex_readonly(lua_State *L) { in magnet_newindex_readonly() argument
341 lua_pushliteral(L, "lua table is read-only"); in magnet_newindex_readonly()
342 return lua_error(L); in magnet_newindex_readonly()
345 static void magnet_push_buffer(lua_State *L, const buffer *b) { in magnet_push_buffer() argument
347 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_push_buffer()
349 lua_pushnil(L); in magnet_push_buffer()
353 static int magnet_array_get_element(lua_State *L, const array *a) {
356 const char * const k = luaL_checklstring(L, 2, &klen);
359 magnet_push_buffer(L, NULL != ds ? &ds->value : NULL);
365 static int magnet_array_next(lua_State *L) { in magnet_array_next() argument
366 lua_settop(L, 0); in magnet_array_next()
367 const uint32_t pos = lua_tointeger(L, lua_upvalueindex(1)); in magnet_array_next()
368 const array * const a = lua_touserdata(L, lua_upvalueindex(2)); in magnet_array_next()
372 lua_pushlstring(L, BUF_PTR_LEN(&du->key)); in magnet_array_next()
375 magnet_push_buffer(L, &((const data_string *)du)->value); in magnet_array_next()
378 lua_pushinteger(L, ((const data_integer *)du)->value); in magnet_array_next()
381 lua_pushnil(L); in magnet_array_next()
386 lua_pushinteger(L, pos+1); in magnet_array_next()
387 lua_replace(L, lua_upvalueindex(1)); in magnet_array_next()
395 static int magnet_array_pairs(lua_State *L, array *a) { in magnet_array_pairs() argument
396 lua_pushinteger(L, 0); /* Push our current pos (the start) into upval 1 */ in magnet_array_pairs()
397 lua_pushlightuserdata(L, a); /* Push our array *a into upval 2 */ in magnet_array_pairs()
398 lua_pushcclosure(L, magnet_array_next, 2); /* Push our new closure with 2 upvals */ in magnet_array_pairs()
409 static request_st * magnet_get_request(lua_State *L) { in magnet_get_request() argument
411 lua_getfield(L, LUA_REGISTRYINDEX, LUA_RIDX_LIGHTTPD_REQUEST); in magnet_get_request()
412 request_st * const r = lua_touserdata(L, -1); in magnet_get_request()
413 lua_pop(L, 1); in magnet_get_request()
416 return *(request_st **)lua_getextraspace(L); in magnet_get_request()
420 static void magnet_set_request(lua_State *L, request_st * const r) { in magnet_set_request() argument
422 lua_pushlightuserdata(L, r); in magnet_set_request()
423 lua_setfield(L, LUA_REGISTRYINDEX, LUA_RIDX_LIGHTTPD_REQUEST); in magnet_set_request()
425 *(request_st **)lua_getextraspace(L) = r; in magnet_set_request()
432 static buffer * magnet_tmpbuf_acquire(lua_State *L) in magnet_tmpbuf_acquire() argument
435 UNUSED(L); in magnet_tmpbuf_acquire()
438 request_st * const r = magnet_get_request(L); in magnet_tmpbuf_acquire()
460 static const_buffer magnet_checkconstbuffer(lua_State *L, int idx) { in magnet_checkconstbuffer() argument
462 if (!lua_isnoneornil(L, idx)) in magnet_checkconstbuffer()
463 cb.ptr = luaL_checklstring(L, idx, &cb.len); in magnet_checkconstbuffer()
471 static const buffer* magnet_checkbuffer(lua_State *L, int idx, buffer *b) { in magnet_checkbuffer() argument
472 const_buffer cb = magnet_checkconstbuffer(L, idx); in magnet_checkbuffer()
482 static int magnet_return_upvalue2(lua_State *L) { in magnet_return_upvalue2() argument
484 lua_pushvalue(L, lua_upvalueindex(1)); in magnet_return_upvalue2()
485 lua_pushvalue(L, lua_upvalueindex(2)); in magnet_return_upvalue2()
489 static int magnet_stat_field(lua_State *L) { in magnet_stat_field() argument
490 if (lua_gettop(L) != 2) in magnet_stat_field()
493 stat_cache_entry * const sce = *(stat_cache_entry **)lua_touserdata(L, -2); in magnet_stat_field()
494 const_buffer k = magnet_checkconstbuffer(L, -1); in magnet_stat_field()
498 request_st * const r = magnet_get_request(L); in magnet_stat_field()
501 lua_pushlstring(L, BUF_PTR_LEN(content_type)); in magnet_stat_field()
503 lua_pushnil(L); in magnet_stat_field()
508 request_st * const r = magnet_get_request(L); in magnet_stat_field()
511 lua_pushlstring(L, BUF_PTR_LEN(etag)); in magnet_stat_field()
513 lua_pushnil(L); in magnet_stat_field()
518 request_st * const r = magnet_get_request(L); in magnet_stat_field()
522 lua_pushinteger(L, r->http_status); in magnet_stat_field()
530 lua_pushboolean(L, S_ISBLK(sce->st.st_mode)); in magnet_stat_field()
536 lua_pushboolean(L, S_ISCHR(sce->st.st_mode)); in magnet_stat_field()
542 lua_pushboolean(L, S_ISDIR(sce->st.st_mode)); in magnet_stat_field()
548 lua_pushboolean(L, S_ISREG(sce->st.st_mode)); in magnet_stat_field()
552 lua_pushboolean(L, S_ISFIFO(sce->st.st_mode)); in magnet_stat_field()
558 lua_pushboolean(L, S_ISLNK(sce->st.st_mode)); in magnet_stat_field()
564 lua_pushboolean(L, S_ISSOCK(sce->st.st_mode)); in magnet_stat_field()
577 lua_pushinteger(L, TIME64_CAST(sce->st.st_atime)); in magnet_stat_field()
581 lua_pushinteger(L, TIME64_CAST(sce->st.st_atime)); in magnet_stat_field()
584 lua_pushinteger(L, sce->st.st_atimespec.tv_nsec); in magnet_stat_field()
586 lua_pushinteger(L, sce->st.st_atim.tv_nsec); in magnet_stat_field()
589 lua_pushinteger(L, 0); in magnet_stat_field()
591 lua_pushcclosure(L, magnet_return_upvalue2, 2); in magnet_stat_field()
597 lua_pushinteger(L, TIME64_CAST(sce->st.st_ctime)); in magnet_stat_field()
601 lua_pushinteger(L, TIME64_CAST(sce->st.st_ctime)); in magnet_stat_field()
604 lua_pushinteger(L, sce->st.st_ctimespec.tv_nsec); in magnet_stat_field()
606 lua_pushinteger(L, sce->st.st_ctim.tv_nsec); in magnet_stat_field()
609 lua_pushinteger(L, 0); in magnet_stat_field()
611 lua_pushcclosure(L, magnet_return_upvalue2, 2); in magnet_stat_field()
617 lua_pushinteger(L, sce->st.st_ino); in magnet_stat_field()
623 lua_pushinteger(L, TIME64_CAST(sce->st.st_mtime)); in magnet_stat_field()
627 lua_pushinteger(L, TIME64_CAST(sce->st.st_mtime)); in magnet_stat_field()
630 lua_pushinteger(L, sce->st.st_mtimespec.tv_nsec); in magnet_stat_field()
632 lua_pushinteger(L, sce->st.st_mtim.tv_nsec); in magnet_stat_field()
635 lua_pushinteger(L, 0); in magnet_stat_field()
637 lua_pushcclosure(L, magnet_return_upvalue2, 2); in magnet_stat_field()
641 lua_pushinteger(L, sce->st.st_mode); in magnet_stat_field()
647 lua_pushinteger(L, sce->st.st_gid); in magnet_stat_field()
653 lua_pushinteger(L, sce->st.st_size); in magnet_stat_field()
659 lua_pushinteger(L, sce->st.st_uid); in magnet_stat_field()
671 lua_pushliteral(L, "stat[\"field\"] invalid: "); in magnet_stat_field()
672 lua_pushvalue(L, -2); /* field */ in magnet_stat_field()
673 lua_concat(L, 2); in magnet_stat_field()
674 lua_error(L); in magnet_stat_field()
680 static int magnet_stat_pairs_noimpl_iter(lua_State *L) { in magnet_stat_pairs_noimpl_iter() argument
681 request_st * const r = magnet_get_request(L); in magnet_stat_pairs_noimpl_iter()
690 static int magnet_stat_pairs_noimpl(lua_State *L) { in magnet_stat_pairs_noimpl() argument
691 lua_pushcclosure(L, magnet_stat_pairs_noimpl_iter, 0); in magnet_stat_pairs_noimpl()
696 static void magnet_stat_metatable(lua_State *L) { in magnet_stat_metatable() argument
697 if (luaL_newmetatable(L, "li.stat")) { /* (sp += 1) */ in magnet_stat_metatable()
698 lua_pushcfunction(L, magnet_stat_field); /* (sp += 1) */ in magnet_stat_metatable()
699 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_stat_metatable()
700 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_stat_metatable()
701 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_stat_metatable()
702 lua_pushcfunction(L, magnet_stat_pairs_noimpl); /* (sp += 1) */ in magnet_stat_metatable()
703 lua_setfield(L, -2, "__pairs"); /* (sp -= 1) */ in magnet_stat_metatable()
704 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_stat_metatable()
705 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_stat_metatable()
710 static int magnet_stat(lua_State *L) { in magnet_stat() argument
712 const buffer * const sb = magnet_checkbuffer(L, 1, &stor); in magnet_stat()
717 lua_pushnil(L); in magnet_stat()
727 lua_newuserdata0(L, sizeof(stat_cache_entry *)); in magnet_stat()
730 magnet_stat_metatable(L); /* (sp += 1) */ in magnet_stat()
731 lua_setmetatable(L, -2); /* (sp -= 1) */ in magnet_stat()
736 static int magnet_time(lua_State *L) { in magnet_time() argument
737 lua_pushinteger(L, (lua_Integer)log_epoch_secs); in magnet_time()
742 static int magnet_hrtime(lua_State *L) { in magnet_hrtime() argument
746 lua_pushinteger(L, (lua_Integer)ts.tv_sec); in magnet_hrtime()
747 lua_pushinteger(L, (lua_Integer)ts.tv_nsec); in magnet_hrtime()
752 static int magnet_rand(lua_State *L) { in magnet_rand() argument
753 lua_pushinteger(L, (lua_Integer)li_rand_pseudo()); in magnet_rand()
758 static int magnet_md_once(lua_State *L) { in magnet_md_once() argument
759 if (lua_gettop(L) != 2) { in magnet_md_once()
760 lua_pushliteral(L, in magnet_md_once()
762 return lua_error(L); in magnet_md_once()
764 const_buffer algo = magnet_checkconstbuffer(L, -2); in magnet_md_once()
765 const_buffer msg = magnet_checkconstbuffer(L, -1); in magnet_md_once()
810 lua_pushlstring(L, dighex, dlen*2); in magnet_md_once()
813 lua_pushnil(L); in magnet_md_once()
818 static int magnet_hmac_once(lua_State *L) { in magnet_hmac_once() argument
819 if (lua_gettop(L) != 3) { in magnet_hmac_once()
820 lua_pushliteral(L, in magnet_hmac_once()
822 return lua_error(L); in magnet_hmac_once()
824 const_buffer algo = magnet_checkconstbuffer(L, -3); in magnet_hmac_once()
825 const_buffer secret = magnet_checkconstbuffer(L, -2); in magnet_hmac_once()
826 const_buffer msg = magnet_checkconstbuffer(L, -1); in magnet_hmac_once()
873 lua_pushlstring(L, dighex, dlen*2); in magnet_hmac_once()
876 lua_pushnil(L); in magnet_hmac_once()
881 static int magnet_digest_eq(lua_State *L) { in magnet_digest_eq() argument
882 if (lua_gettop(L) != 2) { in magnet_digest_eq()
883 lua_pushliteral(L, in magnet_digest_eq()
885 return lua_error(L); in magnet_digest_eq()
887 const_buffer d1 = magnet_checkconstbuffer(L, -2); in magnet_digest_eq()
888 const_buffer d2 = magnet_checkconstbuffer(L, -1); in magnet_digest_eq()
896 lua_pushboolean(L, rc); in magnet_digest_eq()
900 static int magnet_secret_eq(lua_State *L) { in magnet_secret_eq() argument
901 if (lua_gettop(L) != 2) { in magnet_secret_eq()
902 lua_pushliteral(L, in magnet_secret_eq()
904 return lua_error(L); in magnet_secret_eq()
906 const_buffer d1 = magnet_checkconstbuffer(L, -2); in magnet_secret_eq()
907 const_buffer d2 = magnet_checkconstbuffer(L, -1); in magnet_secret_eq()
908 lua_pushboolean(L, ck_memeq_const_time(d1.ptr, d1.len, d2.ptr, d2.len)); in magnet_secret_eq()
912 static int magnet_b64dec(lua_State *L, base64_charset dict) { in magnet_b64dec() argument
913 if (lua_isnoneornil(L, -1)) { in magnet_b64dec()
914 lua_pushlstring(L, "", 0); in magnet_b64dec()
917 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_b64dec()
919 lua_pushvalue(L, -1); in magnet_b64dec()
922 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_b64dec()
924 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_b64dec()
926 lua_pushnil(L); in magnet_b64dec()
931 static int magnet_b64enc(lua_State *L, base64_charset dict) { in magnet_b64enc() argument
932 if (lua_isnoneornil(L, -1)) { in magnet_b64enc()
933 lua_pushlstring(L, "", 0); in magnet_b64enc()
936 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_b64enc()
938 lua_pushvalue(L, -1); in magnet_b64enc()
941 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_b64enc()
943 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_b64enc()
948 static int magnet_b64urldec(lua_State *L) { in magnet_b64urldec() argument
949 return magnet_b64dec(L, BASE64_URL); in magnet_b64urldec()
952 static int magnet_b64urlenc(lua_State *L) { in magnet_b64urlenc() argument
953 return magnet_b64enc(L, BASE64_URL); in magnet_b64urlenc()
956 static int magnet_b64stddec(lua_State *L) { in magnet_b64stddec() argument
957 return magnet_b64dec(L, BASE64_STANDARD); in magnet_b64stddec()
960 static int magnet_b64stdenc(lua_State *L) { in magnet_b64stdenc() argument
961 return magnet_b64enc(L, BASE64_STANDARD); in magnet_b64stdenc()
964 static int magnet_hexdec(lua_State *L) { in magnet_hexdec() argument
965 if (lua_isnoneornil(L, -1)) { in magnet_hexdec()
966 lua_pushlstring(L, "", 0); in magnet_hexdec()
969 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_hexdec()
971 lua_pushvalue(L, -1); in magnet_hexdec()
974 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_hexdec()
978 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_hexdec()
983 static int magnet_hexenc(lua_State *L) { in magnet_hexenc() argument
984 if (lua_isnoneornil(L, -1)) { in magnet_hexenc()
985 lua_pushlstring(L, "", 0); in magnet_hexenc()
988 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_hexenc()
990 lua_pushvalue(L, -1); in magnet_hexenc()
993 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_hexenc()
995 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_hexenc()
1000 static int magnet_quoteddec(lua_State *L) { in magnet_quoteddec() argument
1001 if (lua_isnoneornil(L, -1)) { in magnet_quoteddec()
1002 lua_pushlstring(L, "", 0); in magnet_quoteddec()
1005 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_quoteddec()
1007 lua_pushvalue(L, -1); in magnet_quoteddec()
1010 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_quoteddec()
1024 lua_pushlstring(L, b->ptr, (size_t)(p - b->ptr)); in magnet_quoteddec()
1029 static int magnet_quotedenc(lua_State *L) { in magnet_quotedenc() argument
1030 if (lua_isnoneornil(L, -1)) { in magnet_quotedenc()
1031 lua_pushlstring(L, "", 0); in magnet_quotedenc()
1034 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_quotedenc()
1036 lua_pushvalue(L, -1); in magnet_quotedenc()
1039 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_quotedenc()
1050 lua_pushlstring(L, b->ptr, (size_t)(p - b->ptr)); in magnet_quotedenc()
1140 static int magnet_bsdec(lua_State *L) { in magnet_bsdec() argument
1141 if (lua_isnoneornil(L, -1)) { in magnet_bsdec()
1142 lua_pushlstring(L, "", 0); in magnet_bsdec()
1145 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_bsdec()
1147 lua_pushvalue(L, -1); in magnet_bsdec()
1157 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_bsdec()
1159 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_bsdec()
1164 static int magnet_bsenc(lua_State *L, const int esc_json) { in magnet_bsenc() argument
1165 if (lua_isnoneornil(L, -1)) { in magnet_bsenc()
1166 lua_pushlstring(L, "", 0); in magnet_bsenc()
1169 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_bsenc()
1171 lua_pushvalue(L, -1); in magnet_bsenc()
1174 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_bsenc()
1179 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_bsenc()
1184 static int magnet_bsenc_default(lua_State *L) { in magnet_bsenc_default() argument
1185 return magnet_bsenc(L, 0); in magnet_bsenc_default()
1188 static int magnet_bsenc_json(lua_State *L) { in magnet_bsenc_json() argument
1189 return magnet_bsenc(L, 1); in magnet_bsenc_json()
1192 static int magnet_xmlenc(lua_State *L) { in magnet_xmlenc() argument
1193 if (lua_isnoneornil(L, -1)) { in magnet_xmlenc()
1194 lua_pushlstring(L, "", 0); in magnet_xmlenc()
1197 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_xmlenc()
1199 lua_pushvalue(L, -1); in magnet_xmlenc()
1202 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_xmlenc()
1224 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_xmlenc()
1229 static int magnet_urldec(lua_State *L) { in magnet_urldec() argument
1233 if (lua_isnoneornil(L, -1)) { in magnet_urldec()
1234 lua_pushlstring(L, "", 0); in magnet_urldec()
1237 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_urldec()
1239 lua_pushvalue(L, -1); in magnet_urldec()
1242 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_urldec()
1245 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urldec()
1250 static int magnet_urlenc(lua_State *L) { in magnet_urlenc() argument
1255 if (lua_isnoneornil(L, -1)) { in magnet_urlenc()
1256 lua_pushlstring(L, "", 0); in magnet_urlenc()
1259 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_urlenc()
1261 lua_pushvalue(L, -1); in magnet_urlenc()
1264 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_urlenc()
1266 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urlenc()
1279 static int magnet_urldec_query(lua_State *L) { in magnet_urldec_query() argument
1285 lua_createtable(L, 0, 0); in magnet_urldec_query()
1286 if (lua_isnoneornil(L, 1)) { in magnet_urldec_query()
1289 const_buffer s = magnet_checkconstbuffer(L, 1); in magnet_urldec_query()
1293 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_urldec_query()
1301 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urldec_query()
1303 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urldec_query()
1307 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urldec_query()
1308 lua_pushlstring(L, "", 0); /*(lua_pushnil() would delete key)*/ in magnet_urldec_query()
1310 lua_rawset(L, -3); in magnet_urldec_query()
1355 static int magnet_urlenc_query(lua_State *L) { in magnet_urlenc_query() argument
1359 if (!lua_istable(L, 1)) { in magnet_urlenc_query()
1360 lua_pushlstring(L, "", 0); in magnet_urlenc_query()
1363 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_urlenc_query()
1365 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { in magnet_urlenc_query()
1366 if (lua_isstring(L, -2)) { in magnet_urlenc_query()
1369 s = magnet_checkconstbuffer(L, -2); in magnet_urlenc_query()
1371 if (!lua_isnil(L, -1)) { in magnet_urlenc_query()
1372 s = magnet_checkconstbuffer(L, -1); in magnet_urlenc_query()
1378 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urlenc_query()
1383 static int magnet_urlenc_normalize(lua_State *L) { in magnet_urlenc_normalize() argument
1392 if (lua_isnoneornil(L, -1)) { in magnet_urlenc_normalize()
1393 lua_pushlstring(L, "", 0); in magnet_urlenc_normalize()
1396 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_urlenc_normalize()
1398 lua_pushvalue(L, -1); in magnet_urlenc_normalize()
1401 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_urlenc_normalize()
1404 request_st * const r = magnet_get_request(L); in magnet_urlenc_normalize()
1416 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_urlenc_normalize()
1422 static int magnet_fspath_simplify(lua_State *L) { in magnet_fspath_simplify() argument
1424 if (lua_isnoneornil(L, -1)) { in magnet_fspath_simplify()
1425 lua_pushlstring(L, "", 0); in magnet_fspath_simplify()
1428 const_buffer s = magnet_checkconstbuffer(L, -1); in magnet_fspath_simplify()
1430 lua_pushvalue(L, -1); in magnet_fspath_simplify()
1433 buffer * const b = magnet_tmpbuf_acquire(L); in magnet_fspath_simplify()
1436 lua_pushlstring(L, BUF_PTR_LEN(b)); in magnet_fspath_simplify()
1455 static const char * magnet_push_quoted_string_range (lua_State *L, const char *b, const char *s) { in magnet_push_quoted_string_range() argument
1459 lua_pushlstring(L, b, (size_t)(++s-b)); in magnet_push_quoted_string_range()
1462 lua_pushlstring(L, b, (size_t)(s-b)); in magnet_push_quoted_string_range()
1464 lua_pushlstring(L, "\"", 1); in magnet_push_quoted_string_range()
1466 lua_pushlstring(L, "\\\\\"", 3); in magnet_push_quoted_string_range()
1469 lua_concat(L, 2); in magnet_push_quoted_string_range()
1474 static const char * magnet_push_quoted_string(lua_State *L, const char *s) { in magnet_push_quoted_string() argument
1475 return magnet_push_quoted_string_range(L, s, magnet_scan_quoted_string(s)); in magnet_push_quoted_string()
1478 static const char * magnet_cookie_param_push_token(lua_State *L, const char *s) { in magnet_cookie_param_push_token() argument
1483 lua_pushlstring(L, b, (size_t)(s-b)); in magnet_cookie_param_push_token()
1487 static int magnet_cookie_tokens(lua_State *L) { in magnet_cookie_tokens() argument
1489 lua_createtable(L, 0, 0); in magnet_cookie_tokens()
1490 if (lua_isnoneornil(L, 1)) in magnet_cookie_tokens()
1492 const char *s = luaL_checkstring(L, 1); in magnet_cookie_tokens()
1497 s = magnet_cookie_param_push_token(L, s); in magnet_cookie_tokens()
1505 lua_pushlstring(L, "", 0); /*(lua_pushnil() would delete key)*/ in magnet_cookie_tokens()
1507 s = magnet_cookie_param_push_token(L, s); in magnet_cookie_tokens()
1509 s = magnet_push_quoted_string(L, s); in magnet_cookie_tokens()
1512 lua_pushlstring(L, "", 0); /*(lua_pushnil() would delete key)*/ in magnet_cookie_tokens()
1514 lua_settable(L, -3); in magnet_cookie_tokens()
1520 static const char * magnet_push_token(lua_State *L, const char *s) { in magnet_push_token() argument
1525 lua_pushlstring(L, b, (size_t)(s-b)); in magnet_push_token()
1529 static int magnet_header_tokens(lua_State *L) { in magnet_header_tokens() argument
1544 lua_createtable(L, 0, 0); in magnet_header_tokens()
1545 if (lua_isnoneornil(L, 1)) in magnet_header_tokens()
1547 const char *s = luaL_checkstring(L, 1); in magnet_header_tokens()
1554 lua_pushlstring(L, s++, 1); in magnet_header_tokens()
1556 s = magnet_push_token(L, s); in magnet_header_tokens()
1558 s = magnet_push_quoted_string(L, s); in magnet_header_tokens()
1559 lua_rawseti(L, -2, ++i); in magnet_header_tokens()
1564 static int magnet_reqhdr_get(lua_State *L) { in magnet_reqhdr_get() argument
1566 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_reqhdr_get()
1567 const request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_reqhdr_get()
1569 magnet_push_buffer(L, http_header_request_get(r, id, k, klen)); in magnet_reqhdr_get()
1573 static int magnet_reqhdr_set(lua_State *L) { in magnet_reqhdr_set() argument
1574 const_buffer k = magnet_checkconstbuffer(L, 2); in magnet_reqhdr_set()
1575 const_buffer v = magnet_checkconstbuffer(L, 3); in magnet_reqhdr_set()
1577 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_reqhdr_set()
1636 static int magnet_reqhdr_pairs(lua_State *L) { in magnet_reqhdr_pairs() argument
1637 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_reqhdr_pairs()
1638 return magnet_array_pairs(L, &r->rqst_headers); in magnet_reqhdr_pairs()
1641 static int magnet_resphdr_get(lua_State *L) { in magnet_resphdr_get() argument
1645 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_resphdr_get()
1646 const request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_resphdr_get()
1648 magnet_push_buffer(L, http_header_response_get(r, id, k, klen)); in magnet_resphdr_get()
1652 static int magnet_resphdr_set_kv(lua_State *L, request_st * const r) { in magnet_resphdr_set_kv() argument
1653 const const_buffer k = magnet_checkconstbuffer(L, -2); in magnet_resphdr_set_kv()
1654 const const_buffer v = magnet_checkconstbuffer(L, -1); in magnet_resphdr_set_kv()
1700 static int magnet_resphdr_set(lua_State *L) { in magnet_resphdr_set() argument
1703 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_resphdr_set()
1704 return magnet_resphdr_set_kv(L, r); in magnet_resphdr_set()
1707 static int magnet_resphdr_pairs(lua_State *L) { in magnet_resphdr_pairs() argument
1708 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_resphdr_pairs()
1709 return magnet_array_pairs(L, &r->resp_headers); in magnet_resphdr_pairs()
1712 static int magnet_plugin_stats_get(lua_State *L) { in magnet_plugin_stats_get() argument
1713 const_buffer k = magnet_checkconstbuffer(L, 2); in magnet_plugin_stats_get()
1714 lua_pushinteger(L, (lua_Integer)*plugin_stats_get_ptr(k.ptr, k.len)); in magnet_plugin_stats_get()
1718 static int magnet_plugin_stats_set(lua_State *L) { in magnet_plugin_stats_set() argument
1719 const_buffer k = magnet_checkconstbuffer(L, 2); in magnet_plugin_stats_set()
1720 plugin_stats_set(k.ptr, k.len, luaL_checkinteger(L, 3)); in magnet_plugin_stats_set()
1724 static int magnet_plugin_stats_pairs(lua_State *L) { in magnet_plugin_stats_pairs() argument
1725 return magnet_array_pairs(L, &plugin_stats); in magnet_plugin_stats_pairs()
1730 magnet_req_item_get (lua_State *L) in magnet_req_item_get() argument
1733 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_req_item_get()
1734 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_req_item_get()
1738 lua_pushinteger(L, (lua_Integer)http_request_stats_bytes_in(r)); in magnet_req_item_get()
1744 lua_pushinteger(L, (lua_Integer)http_request_stats_bytes_out(r)); in magnet_req_item_get()
1748 lua_pushinteger(L, (lua_Integer)r->h2id); in magnet_req_item_get()
1752 lua_pushinteger(L, (lua_Integer)r->con->request_count); in magnet_req_item_get()
1758 lua_pushinteger(L, (lua_Integer)r->start_hp.tv_sec); in magnet_req_item_get()
1759 lua_pushinteger(L, (lua_Integer)r->start_hp.tv_nsec); in magnet_req_item_get()
1760 lua_pushcclosure(L, magnet_return_upvalue2, 2); in magnet_req_item_get()
1764 lua_pushinteger(L, (lua_Integer)r->keep_alive); in magnet_req_item_get()
1770 lua_pushinteger(L, (lua_Integer)r->http_status); in magnet_req_item_get()
1776 lua_pushinteger(L, (lua_Integer)r->rqst_header_len); in magnet_req_item_get()
1782 lua_pushinteger(L, (lua_Integer)r->resp_header_len); in magnet_req_item_get()
1789 return luaL_error(L, "r.req_item['%s'] invalid", k); in magnet_req_item_get()
1793 magnet_req_item_set (lua_State *L) in magnet_req_item_set() argument
1796 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_req_item_set()
1797 int v = (int)luaL_checkinteger(L, 3); in magnet_req_item_set()
1799 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_req_item_set()
1810 return luaL_error(L, "r.req_item['%s'] invalid or read-only", k); in magnet_req_item_set()
2020 static int magnet_env_get(lua_State *L) { in magnet_env_get() argument
2022 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_env_get()
2024 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_env_get()
2025 magnet_push_buffer(L, magnet_env_get_buffer_by_id(r, env_id)); in magnet_env_get()
2031 magnet_env_set_raddr_by_id (lua_State *L, request_st * const r, const int id, in magnet_env_set_raddr_by_id() argument
2051 return luaL_error(L, in magnet_env_set_raddr_by_id()
2080 static int magnet_env_set(lua_State *L) { in magnet_env_set() argument
2082 const char * const key = luaL_checklstring(L, 2, &klen); in magnet_env_set()
2083 const_buffer val = magnet_checkconstbuffer(L, 3); in magnet_env_set()
2086 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_env_set()
2095 return magnet_env_set_raddr_by_id(L, r, env_id, &val); in magnet_env_set()
2105 return luaL_error(L, "couldn't store '%s' in r.req_attr[]", key); in magnet_env_set()
2107 if (lua_isnoneornil(L, 3)) { in magnet_env_set()
2141 static int magnet_env_next(lua_State *L) { in magnet_env_next() argument
2143 lua_settop(L, 0); in magnet_env_next()
2144 const int pos = lua_tointeger(L, lua_upvalueindex(1)); in magnet_env_next()
2148 lua_pushinteger(L, pos + 1); in magnet_env_next()
2149 lua_replace(L, lua_upvalueindex(1)); in magnet_env_next()
2152 lua_pushlstring(L, magnet_env[pos].name, magnet_env[pos].nlen); in magnet_env_next()
2155 request_st * const r = lua_touserdata(L, lua_upvalueindex(2)); in magnet_env_next()
2156 magnet_push_buffer(L, magnet_env_get_buffer_by_id(r, magnet_env[pos].type)); in magnet_env_next()
2162 static int magnet_env_pairs(lua_State *L) { in magnet_env_pairs() argument
2163 lua_pushinteger(L, 0); /* Push our current pos (the start) into upval 1 */ in magnet_env_pairs()
2164 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_env_pairs()
2165 lua_pushlightuserdata(L, r); /* Push request_st *r into upval 2 */ in magnet_env_pairs()
2166 lua_pushcclosure(L, magnet_env_next, 2); /* Push new closure with 2 upvals*/ in magnet_env_pairs()
2170 static int magnet_envvar_get(lua_State *L) { in magnet_envvar_get() argument
2172 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_envvar_get()
2173 const request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_envvar_get()
2174 magnet_push_buffer(L, http_header_env_get(r, k, klen)); in magnet_envvar_get()
2178 static int magnet_envvar_set(lua_State *L) { in magnet_envvar_set() argument
2179 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_envvar_set()
2180 const_buffer key = magnet_checkconstbuffer(L, 2); in magnet_envvar_set()
2181 if (__builtin_expect( (lua_isnil(L, 3)), 0)) { in magnet_envvar_set()
2186 const_buffer val = magnet_checkconstbuffer(L, 3); in magnet_envvar_set()
2191 static int magnet_envvar_pairs(lua_State *L) { in magnet_envvar_pairs() argument
2192 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_envvar_pairs()
2193 return magnet_array_pairs(L, &r->env); in magnet_envvar_pairs()
2197 static int magnet_respbody_add(lua_State *L) { in magnet_respbody_add() argument
2198 request_st * const r = lua_touserdata(L, lua_upvalueindex(1)); in magnet_respbody_add()
2199 if (lua_isstring(L, -1)) { in magnet_respbody_add()
2200 const_buffer data = magnet_checkconstbuffer(L, -1); in magnet_respbody_add()
2204 else if (!lua_istable(L, -1)) in magnet_respbody_add()
2211 for (int i=1, end=0, n=(int)lua_rawlen(L,-1); !end && i <= n; ++i) { in magnet_respbody_add()
2212 lua_rawgeti(L, -1, i); in magnet_respbody_add()
2214 if (lua_isstring(L, -1)) { in magnet_respbody_add()
2215 const_buffer data = magnet_checkconstbuffer(L, -1); in magnet_respbody_add()
2218 else if (lua_istable(L, -1)) { in magnet_respbody_add()
2219 lua_getfield(L, -1, "filename"); in magnet_respbody_add()
2220 lua_getfield(L, -2, "length"); in magnet_respbody_add()
2221 lua_getfield(L, -3, "offset"); in magnet_respbody_add()
2223 if (lua_isstring(L, -3)) { /* filename has to be a string */ in magnet_respbody_add()
2224 off_t off = (off_t) luaL_optinteger(L, -1, 0); in magnet_respbody_add()
2225 off_t len = (off_t) luaL_optinteger(L, -2, -1); in magnet_respbody_add()
2228 const buffer * const fn = magnet_checkbuffer(L, -3, &stor); in magnet_respbody_add()
2258 lua_pop(L, 3); in magnet_respbody_add()
2260 else if (lua_isnil(L, -1)) { /* end of list */ in magnet_respbody_add()
2269 lua_pop(L, 1); /* pop the content[...] entry value */ in magnet_respbody_add()
2276 static int magnet_respbody(lua_State *L) { in magnet_respbody() argument
2277 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_respbody()
2279 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_respbody()
2283 lua_pushlightuserdata(L, r); in magnet_respbody()
2284 lua_pushcclosure(L, magnet_respbody_add, 1); in magnet_respbody()
2290 lua_pushinteger(L, r->write_queue.bytes_in); in magnet_respbody()
2294 lua_pushinteger(L, r->write_queue.bytes_out); in magnet_respbody()
2307 ? magnet_push_buffer(L, in magnet_respbody()
2309 : (void)lua_pushlstring(L, "", 0); in magnet_respbody()
2312 lua_pushnil(L); /*(?maybe return -1 instead if len unknown?)*/ in magnet_respbody()
2319 lua_pushinteger(L, chunkqueue_length(&r->write_queue)); in magnet_respbody()
2321 lua_pushnil(L); /*(?maybe return -1 instead if len unknown?)*/ in magnet_respbody()
2328 lua_pushlightuserdata(L, r); in magnet_respbody()
2329 lua_pushcclosure(L, magnet_respbody_add, 1); in magnet_respbody()
2336 lua_pushliteral(L, "r.resp_body invalid method or param"); in magnet_respbody()
2337 lua_error(L); in magnet_respbody()
2342 static int magnet_reqbody_add(lua_State *L) { in magnet_reqbody_add() argument
2343 request_st * const r = lua_touserdata(L, lua_upvalueindex(1)); in magnet_reqbody_add()
2346 if (lua_isstring(L, -1)) { in magnet_reqbody_add()
2347 const_buffer data = magnet_checkconstbuffer(L, -1); in magnet_reqbody_add()
2356 else if (!lua_istable(L, -1)) in magnet_reqbody_add()
2359 for (int i=1, end=0, n=(int)lua_rawlen(L,-1); !end && i <= n; ++i) { in magnet_reqbody_add()
2360 lua_rawgeti(L, -1, i); in magnet_reqbody_add()
2362 if (lua_isstring(L, -1)) { in magnet_reqbody_add()
2363 const_buffer data = magnet_checkconstbuffer(L, -1); in magnet_reqbody_add()
2371 else if (lua_isnil(L, -1)) { /* end of list */ in magnet_reqbody_add()
2380 lua_pop(L, 1); /* pop the content[...] entry value */ in magnet_reqbody_add()
2387 static int magnet_reqbody(lua_State *L) { in magnet_reqbody() argument
2388 request_st * const r = **(request_st ***)lua_touserdata(L, 1); in magnet_reqbody()
2390 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_reqbody()
2396 lua_pushlightuserdata(L, r); in magnet_reqbody()
2397 lua_pushcclosure(L, magnet_reqbody_add, 1); in magnet_reqbody()
2400 lua_pushnil(L); in magnet_reqbody()
2406 lua_pushinteger(L, r->reqbody_queue.bytes_in); in magnet_reqbody()
2410 lua_pushinteger(L, r->reqbody_queue.bytes_out); in magnet_reqbody()
2418 lua_pushboolean(L, 1); in magnet_reqbody()
2423 lua_pushboolean(L, 0); in magnet_reqbody()
2427 lua_pushboolean(L, 0); in magnet_reqbody()
2435 lua_pushnil(L); in magnet_reqbody()
2445 ? magnet_push_buffer(L, in magnet_reqbody()
2447 : (void)lua_pushlstring(L, "", 0); in magnet_reqbody()
2449 lua_pushnil(L); /*(?maybe return -1 instead if len unknown?)*/ in magnet_reqbody()
2455 lua_pushinteger(L, r->reqbody_length); in magnet_reqbody()
2465 lua_pushlightuserdata(L, r); in magnet_reqbody()
2466 lua_pushcclosure(L, magnet_reqbody_add, 1); in magnet_reqbody()
2469 lua_pushnil(L); in magnet_reqbody()
2476 lua_pushliteral(L, "r.req_body invalid method or param"); in magnet_reqbody()
2477 lua_error(L); in magnet_reqbody()
2483 static int magnet_lighty_result_get(lua_State *L) { in magnet_lighty_result_get() argument
2485 lua_pushvalue(L, 2); in magnet_lighty_result_get()
2486 lua_rawget(L, lua_upvalueindex(1)); in magnet_lighty_result_get()
2487 if (lua_isnil(L, -1)) { in magnet_lighty_result_get()
2488 const_buffer k = magnet_checkconstbuffer(L, 2); in magnet_lighty_result_get()
2491 lua_pop(L, 1); /* pop nil */ in magnet_lighty_result_get()
2492 lua_createtable(L, 0, 0); /* create "header","content" on demand */ in magnet_lighty_result_get()
2493 lua_pushvalue(L, 2); /* k: "header" or "content" */ in magnet_lighty_result_get()
2494 lua_pushvalue(L, -2); /* v: table */ in magnet_lighty_result_get()
2495 lua_rawset(L, lua_upvalueindex(1)); /* set in result table */ in magnet_lighty_result_get()
2502 static int magnet_lighty_result_set(lua_State *L) { in magnet_lighty_result_set() argument
2505 lua_rawset(L, lua_upvalueindex(1)); /* set in result table */ in magnet_lighty_result_set()
2512 static void magnet_copy_response_header(lua_State * const L, request_st * const r) { in magnet_copy_response_header() argument
2513 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { in magnet_copy_response_header()
2514 if (lua_isstring(L, -1) && lua_isstring(L, -2)) in magnet_copy_response_header()
2515 magnet_resphdr_set_kv(L, r); in magnet_copy_response_header()
2529 static void magnet_attach_content(lua_State * const L, request_st * const r) { in magnet_attach_content() argument
2531 for (int i=1, end=0, n=(int)lua_rawlen(L,-1); !end && i <= n; ++i) { in magnet_attach_content()
2532 lua_rawgeti(L, -1, i); in magnet_attach_content()
2535 if (lua_isstring(L, -1)) { in magnet_attach_content()
2536 const_buffer data = magnet_checkconstbuffer(L, -1); in magnet_attach_content()
2538 } else if (lua_istable(L, -1)) { in magnet_attach_content()
2539 lua_getfield(L, -1, "filename"); in magnet_attach_content()
2540 lua_getfield(L, -2, "length"); /* (0-based) end of range (not actually "length") */ in magnet_attach_content()
2541 lua_getfield(L, -3, "offset"); /* (0-based) start of range */ in magnet_attach_content()
2543 if (lua_isstring(L, -3)) { /* filename has to be a string */ in magnet_attach_content()
2548 off_t off = lua_isnil(L, -1) ? 0 : (off_t) lua_tointegerx(L, -1, &isnum); in magnet_attach_content()
2555 off_t len = lua_isnil(L, -2) ? -1 : (off_t) lua_tointegerx(L, -2, &isnum); in magnet_attach_content()
2564 "offset for '%s' is negative", lua_tostring(L, -3)); in magnet_attach_content()
2570 "offset > length for '%s'", lua_tostring(L, -3)); in magnet_attach_content()
2576 const buffer * const fn = magnet_checkbuffer(L, -3, &stor); in magnet_attach_content()
2588 lua_tostring(L, -3), (long long)off); in magnet_attach_content()
2598 lua_pop(L, 3); in magnet_attach_content()
2599 } else if (lua_isnil(L, -1)) { in magnet_attach_content()
2608 lua_pop(L, 1); /* pop the content[...] entry value */ in magnet_attach_content()
2613 static void magnet_mainenv_metatable(lua_State * const L) { in magnet_mainenv_metatable() argument
2614 if (luaL_newmetatable(L, "li.mainenv")) { /* (sp += 1) */ in magnet_mainenv_metatable()
2615 lua_pushglobaltable(L); /* (sp += 1) */ in magnet_mainenv_metatable()
2616 lua_setfield(L, -2, "__index"); /* { __index = _G } (sp -= 1) */ in magnet_mainenv_metatable()
2617 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_mainenv_metatable()
2618 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_mainenv_metatable()
2623 magnet_request_userdata_method (lua_State * const L, request_st ** const rr, const char *meta) in magnet_request_userdata_method() argument
2626 *(request_st ***)lua_newuserdata0(L, sizeof(request_st **)) = rr; in magnet_request_userdata_method()
2628 if (luaL_newmetatable(L, meta)) { } in magnet_request_userdata_method()
2630 luaL_newmetatable(L, meta); /*(should not fail; init'd in script setup)*/ in magnet_request_userdata_method()
2632 lua_setmetatable(L, -2); in magnet_request_userdata_method()
2633 lua_setfield(L, -2, meta+3); /*(meta+3 to skip over "li." prefix)*/ in magnet_request_userdata_method()
2637 magnet_request_table (lua_State * const L, request_st ** const rr) in magnet_request_table() argument
2656 lua_createtable(L, 0, 7); /* (sp += 1) */ in magnet_request_table()
2660 magnet_request_userdata_method(L, rr, "li.req_header"); /* req_header */ in magnet_request_table()
2661 magnet_request_userdata_method(L, rr, "li.req_attr"); /* req_attr */ in magnet_request_table()
2662 magnet_request_userdata_method(L, rr, "li.req_item"); /* req_item */ in magnet_request_table()
2663 magnet_request_userdata_method(L, rr, "li.req_env"); /* req_env */ in magnet_request_table()
2664 magnet_request_userdata_method(L, rr, "li.resp_header");/* resp_header */ in magnet_request_table()
2665 magnet_request_userdata_method(L, rr, "li.resp_body"); /* resp_body */ in magnet_request_table()
2666 magnet_request_userdata_method(L, rr, "li.req_body"); /* req_body */ in magnet_request_table()
2668 lua_createtable(L, 0, 2); /* metatable for r table (sp += 1) */ in magnet_request_table()
2669 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_request_table()
2670 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_request_table()
2671 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_request_table()
2672 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_request_table()
2673 lua_setmetatable(L, -2); /* tie the metatable to r (sp -= 1) */ in magnet_request_table()
2677 magnet_request_iter (lua_State *L) in magnet_request_iter() argument
2683 connection *con = lua_touserdata(L, lua_upvalueindex(1)); in magnet_request_iter()
2695 i = lua_tointeger(L, lua_upvalueindex(2)); in magnet_request_iter()
2698 *(request_st **)lua_touserdata(L,lua_upvalueindex(3))=&con->request; in magnet_request_iter()
2700 *(request_st **)lua_touserdata(L,lua_upvalueindex(3))=con->h2->r[i]; in magnet_request_iter()
2704 lua_pushinteger(L, i); in magnet_request_iter()
2705 lua_replace(L, lua_upvalueindex(2)); in magnet_request_iter()
2709 *(request_st **)lua_touserdata(L, lua_upvalueindex(3)) = &con->request; in magnet_request_iter()
2715 lua_pushlightuserdata(L, con); in magnet_request_iter()
2716 lua_replace(L, lua_upvalueindex(1)); in magnet_request_iter()
2720 lua_pushvalue(L, lua_upvalueindex(4)); in magnet_request_iter()
2725 magnet_irequests (lua_State *L) in magnet_irequests() argument
2743 request_st *r = magnet_get_request(L); in magnet_irequests()
2744 lua_pushlightuserdata(L, r->con->srv->conns); in magnet_irequests()
2745 lua_pushinteger(L, -1); in magnet_irequests()
2747 (request_st **)lua_newuserdata0(L, sizeof(request_st *)); in magnet_irequests()
2748 magnet_request_table(L, r_userdata); in magnet_irequests()
2749 lua_pushcclosure(L, magnet_request_iter, 4); in magnet_irequests()
2754 magnet_server_stats_get (lua_State *L) in magnet_server_stats_get() argument
2757 const char * const k = luaL_checklstring(L, 2, &klen); in magnet_server_stats_get()
2758 const request_st * const r = magnet_get_request(L); in magnet_server_stats_get()
2763 lua_pushinteger(L, (lua_Integer)(log_epoch_secs - srv->startup_ts)); in magnet_server_stats_get()
2769 lua_pushlstring(L, BUF_PTR_LEN(srv->default_server_tag)); in magnet_server_stats_get()
2776 lua_pushinteger(L, (lua_Integer) in magnet_server_stats_get()
2784 return luaL_error(L, "server.stats['%s'] invalid", k); in magnet_server_stats_get()
2790 magnet_req_header_metatable (lua_State * const L) in magnet_req_header_metatable() argument
2792 if (luaL_newmetatable(L, "li.req_header") == 0) /* (sp += 1) */ in magnet_req_header_metatable()
2795 lua_pushcfunction(L, magnet_reqhdr_get); /* (sp += 1) */ in magnet_req_header_metatable()
2796 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_req_header_metatable()
2797 lua_pushcfunction(L, magnet_reqhdr_set); /* (sp += 1) */ in magnet_req_header_metatable()
2798 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_req_header_metatable()
2799 lua_pushcfunction(L, magnet_reqhdr_pairs); /* (sp += 1) */ in magnet_req_header_metatable()
2800 lua_setfield(L, -2, "__pairs"); /* (sp -= 1) */ in magnet_req_header_metatable()
2801 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_req_header_metatable()
2802 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_req_header_metatable()
2807 magnet_req_attr_metatable (lua_State * const L) in magnet_req_attr_metatable() argument
2809 if (luaL_newmetatable(L, "li.req_attr") == 0) /* (sp += 1) */ in magnet_req_attr_metatable()
2812 lua_pushcfunction(L, magnet_env_get); /* (sp += 1) */ in magnet_req_attr_metatable()
2813 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_req_attr_metatable()
2814 lua_pushcfunction(L, magnet_env_set); /* (sp += 1) */ in magnet_req_attr_metatable()
2815 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_req_attr_metatable()
2816 lua_pushcfunction(L, magnet_env_pairs); /* (sp += 1) */ in magnet_req_attr_metatable()
2817 lua_setfield(L, -2, "__pairs"); /* (sp -= 1) */ in magnet_req_attr_metatable()
2818 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_req_attr_metatable()
2819 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_req_attr_metatable()
2824 magnet_req_item_metatable (lua_State * const L) in magnet_req_item_metatable() argument
2826 if (luaL_newmetatable(L, "li.req_item") == 0) /* (sp += 1) */ in magnet_req_item_metatable()
2829 lua_pushcfunction(L, magnet_req_item_get); /* (sp += 1) */ in magnet_req_item_metatable()
2830 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_req_item_metatable()
2831 lua_pushcfunction(L, magnet_req_item_set); /* (sp += 1) */ in magnet_req_item_metatable()
2832 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_req_item_metatable()
2833 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_req_item_metatable()
2834 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_req_item_metatable()
2839 magnet_req_env_metatable (lua_State * const L) in magnet_req_env_metatable() argument
2841 if (luaL_newmetatable(L, "li.req_env") == 0) /* (sp += 1) */ in magnet_req_env_metatable()
2844 lua_pushcfunction(L, magnet_envvar_get); /* (sp += 1) */ in magnet_req_env_metatable()
2845 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_req_env_metatable()
2846 lua_pushcfunction(L, magnet_envvar_set); /* (sp += 1) */ in magnet_req_env_metatable()
2847 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_req_env_metatable()
2848 lua_pushcfunction(L, magnet_envvar_pairs); /* (sp += 1) */ in magnet_req_env_metatable()
2849 lua_setfield(L, -2, "__pairs"); /* (sp -= 1) */ in magnet_req_env_metatable()
2850 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_req_env_metatable()
2851 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_req_env_metatable()
2856 magnet_resp_header_metatable (lua_State * const L) in magnet_resp_header_metatable() argument
2858 if (luaL_newmetatable(L, "li.resp_header") == 0) /* (sp += 1) */ in magnet_resp_header_metatable()
2861 lua_pushcfunction(L, magnet_resphdr_get); /* (sp += 1) */ in magnet_resp_header_metatable()
2862 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_resp_header_metatable()
2863 lua_pushcfunction(L, magnet_resphdr_set); /* (sp += 1) */ in magnet_resp_header_metatable()
2864 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_resp_header_metatable()
2865 lua_pushcfunction(L, magnet_resphdr_pairs); /* (sp += 1) */ in magnet_resp_header_metatable()
2866 lua_setfield(L, -2, "__pairs"); /* (sp -= 1) */ in magnet_resp_header_metatable()
2867 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_resp_header_metatable()
2868 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_resp_header_metatable()
2873 magnet_resp_body_metatable (lua_State * const L) in magnet_resp_body_metatable() argument
2875 if (luaL_newmetatable(L, "li.resp_body") == 0) /* (sp += 1) */ in magnet_resp_body_metatable()
2878 lua_pushcfunction(L, magnet_respbody); /* (sp += 1) */ in magnet_resp_body_metatable()
2879 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_resp_body_metatable()
2880 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_resp_body_metatable()
2881 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_resp_body_metatable()
2882 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_resp_body_metatable()
2883 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_resp_body_metatable()
2888 magnet_req_body_metatable (lua_State * const L) in magnet_req_body_metatable() argument
2890 if (luaL_newmetatable(L, "li.req_body") == 0) /* (sp += 1) */ in magnet_req_body_metatable()
2893 lua_pushcfunction(L, magnet_reqbody); /* (sp += 1) */ in magnet_req_body_metatable()
2894 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_req_body_metatable()
2895 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_req_body_metatable()
2896 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_req_body_metatable()
2897 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_req_body_metatable()
2898 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_req_body_metatable()
2902 static int magnet_atpanic(lua_State *L) { in magnet_atpanic() argument
2903 request_st * const r = magnet_get_request(L); in magnet_atpanic()
2905 lua_isstring(L, 1) ? lua_tostring(L, 1) : ""); in magnet_atpanic()
2911 static int magnet_print(lua_State *L) { in magnet_print() argument
2912 const_buffer cb = magnet_checkconstbuffer(L, 1); in magnet_print()
2913 request_st * const r = magnet_get_request(L); in magnet_print()
2920 magnet_plugin_stats_table (lua_State * const L) in magnet_plugin_stats_table() argument
2922 lua_createtable(L, 0, 0); /* {} (sp += 1) */ in magnet_plugin_stats_table()
2923 lua_createtable(L, 0, 4); /* metatable for plugin_stats (sp += 1) */ in magnet_plugin_stats_table()
2924 lua_pushcfunction(L, magnet_plugin_stats_get); /* (sp += 1) */ in magnet_plugin_stats_table()
2925 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_plugin_stats_table()
2926 lua_pushcfunction(L, magnet_plugin_stats_set); /* (sp += 1) */ in magnet_plugin_stats_table()
2927 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_plugin_stats_table()
2928 lua_pushcfunction(L, magnet_plugin_stats_pairs); /* (sp += 1) */ in magnet_plugin_stats_table()
2929 lua_setfield(L, -2, "__pairs"); /* (sp -= 1) */ in magnet_plugin_stats_table()
2930 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_plugin_stats_table()
2931 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_plugin_stats_table()
2932 lua_setmetatable(L, -2); /* (sp -= 1) */ in magnet_plugin_stats_table()
2937 magnet_server_stats_table (lua_State * const L) in magnet_server_stats_table() argument
2939 lua_createtable(L, 0, 0); /* {} (sp += 1) */ in magnet_server_stats_table()
2940 lua_createtable(L, 0, 3); /* metatable for stats (sp += 1) */ in magnet_server_stats_table()
2941 lua_pushcfunction(L, magnet_server_stats_get); /* (sp += 1) */ in magnet_server_stats_table()
2942 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_server_stats_table()
2943 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_server_stats_table()
2944 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_server_stats_table()
2945 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_server_stats_table()
2946 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_server_stats_table()
2947 lua_setmetatable(L, -2); /* (sp -= 1) */ in magnet_server_stats_table()
2952 magnet_server_table (lua_State * const L) in magnet_server_table() argument
2954 lua_createtable(L, 0, 3); /* {} (sp += 1) */ in magnet_server_table()
2955 lua_pushcfunction(L, magnet_irequests); /* (sp += 1) */ in magnet_server_table()
2956 lua_setfield(L, -2, "irequests"); /* iterate over requests (sp -= 1) */ in magnet_server_table()
2957 magnet_plugin_stats_table(L); /* (sp += 1) */ in magnet_server_table()
2958 lua_setfield(L, -2, "plugin_stats"); /* (sp -= 1) */ in magnet_server_table()
2959 magnet_server_stats_table(L); /* (sp += 1) */ in magnet_server_table()
2960 lua_setfield(L, -2, "stats"); /* (sp -= 1) */ in magnet_server_table()
2961 lua_createtable(L, 0, 2); /* metatable for server table (sp += 1) */ in magnet_server_table()
2962 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_server_table()
2963 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_server_table()
2964 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_server_table()
2965 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_server_table()
2966 lua_setmetatable(L, -2); /* tie the metatable to server (sp -= 1) */ in magnet_server_table()
2971 magnet_script_setup_global_state (lua_State * const L) in magnet_script_setup_global_state() argument
2973 lua_atpanic(L, magnet_atpanic); in magnet_script_setup_global_state()
2975 lua_pushglobaltable(L); /* (sp += 1) */ in magnet_script_setup_global_state()
2977 lua_pushcfunction(L, magnet_print); /* (sp += 1) */ in magnet_script_setup_global_state()
2978 lua_setfield(L, -2, "print"); /* (sp -= 1) */ in magnet_script_setup_global_state()
2981 lua_getglobal(L, "pairs"); /* push original pairs() (sp += 1) */ in magnet_script_setup_global_state()
2982 lua_pushcclosure(L, magnet_pairs, 1); /* (sp -= 1; sp += 1) */ in magnet_script_setup_global_state()
2983 lua_setfield(L, -2, "pairs"); /* (sp -= 1) */ in magnet_script_setup_global_state()
2985 lua_pop(L, 1); /* pop global table */ /* (sp -= 1) */ in magnet_script_setup_global_state()
2987 magnet_req_header_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2988 magnet_req_attr_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2989 magnet_req_item_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2990 magnet_req_env_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2991 magnet_resp_header_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2992 magnet_resp_body_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2993 magnet_req_body_metatable(L); /* init for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2994 magnet_stat_metatable(L); /* init table for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2995 magnet_readdir_metatable(L); /* init table for mem locality (sp += 1) */ in magnet_script_setup_global_state()
2996 lua_pop(L, 9); /* pop init'd metatables (sp -= 9) */ in magnet_script_setup_global_state()
3001 magnet_init_lighty_table (lua_State * const L, request_st **rr, in magnet_init_lighty_table() argument
3026 lua_createtable(L, 0, 9); /* lighty.* (returned on stack) (sp += 1) */ in magnet_init_lighty_table()
3028 magnet_request_table(L, rr); /* lighty.r (sp += 1) */ in magnet_init_lighty_table()
3029 lua_setfield(L, -2, "r"); /* lighty.r = {} (sp -= 1) */ in magnet_init_lighty_table()
3031 magnet_server_table(L); /* lighty.server (sp += 1) */ in magnet_init_lighty_table()
3032 lua_setfield(L, -2, "server"); /* server = {} (sp -= 1) */ in magnet_init_lighty_table()
3035 lua_getfield(L, -1, "r"); /* (sp += 1) */ in magnet_init_lighty_table()
3037 lua_getfield(L, -1, "req_header"); /* (sp += 1) */ in magnet_init_lighty_table()
3038 lua_setfield(L, -3, "request"); /* request = {} (sp -= 1) */ in magnet_init_lighty_table()
3040 lua_getfield(L, -1, "req_attr"); /* (sp += 1) */ in magnet_init_lighty_table()
3041 lua_setfield(L, -3, "env"); /* env = {} (sp -= 1) */ in magnet_init_lighty_table()
3043 lua_getfield(L, -1, "req_env"); /* (sp += 1) */ in magnet_init_lighty_table()
3044 lua_setfield(L, -3, "req_env"); /* req_env = {} (sp -= 1) */ in magnet_init_lighty_table()
3045 lua_pop(L, 1); /* (sp -= 1) */ in magnet_init_lighty_table()
3048 lua_getfield(L, -1, "server"); /* (sp += 1) */ in magnet_init_lighty_table()
3049 lua_getfield(L, -1, "plugin_stats"); /* (sp += 1) */ in magnet_init_lighty_table()
3050 lua_setfield(L, -3, "status"); /* status = {} (sp -= 1) */ in magnet_init_lighty_table()
3051 lua_pop(L, 1); /* (sp -= 1) */ in magnet_init_lighty_table()
3053 lua_pushinteger(L, MAGNET_RESTART_REQUEST); in magnet_init_lighty_table()
3054 lua_setfield(L, -2, "RESTART_REQUEST"); in magnet_init_lighty_table()
3057 lua_pushcfunction(L, magnet_stat); /* (sp += 1) */ in magnet_init_lighty_table()
3058 lua_setfield(L, -2, "stat"); /* -1 is the env we want to set (sp -= 1) */ in magnet_init_lighty_table()
3093 lua_createtable(L, 0, sizeof(cmethods)/sizeof(luaL_Reg)-1);/*(sp += 1) */ in magnet_init_lighty_table()
3094 luaL_setfuncs(L, cmethods, 0); in magnet_init_lighty_table()
3095 lua_createtable(L, 0, 2); /* metatable for c table (sp += 1) */ in magnet_init_lighty_table()
3096 lua_pushcfunction(L, magnet_newindex_readonly); /* (sp += 1) */ in magnet_init_lighty_table()
3097 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_init_lighty_table()
3098 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_init_lighty_table()
3099 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_init_lighty_table()
3100 lua_setmetatable(L, -2); /* tie the metatable to c (sp -= 1) */ in magnet_init_lighty_table()
3101 lua_setfield(L, -2, "c"); /* c = {} (sp -= 1) */ in magnet_init_lighty_table()
3105 lua_createtable(L, 0, 3); /* metatable for lighty table (sp += 1) */ in magnet_init_lighty_table()
3106 lua_pushvalue(L, result_ndx); /* (sp += 1) */ in magnet_init_lighty_table()
3107 lua_pushcclosure(L, magnet_lighty_result_get, 1);/* (sp -= 1; sp += 1) */ in magnet_init_lighty_table()
3108 lua_setfield(L, -2, "__index"); /* (sp -= 1) */ in magnet_init_lighty_table()
3109 lua_pushvalue(L, result_ndx); /* (sp += 1) */ in magnet_init_lighty_table()
3110 lua_pushcclosure(L, magnet_lighty_result_set, 1);/* (sp -= 1; sp += 1) */ in magnet_init_lighty_table()
3111 lua_setfield(L, -2, "__newindex"); /* (sp -= 1) */ in magnet_init_lighty_table()
3112 lua_pushboolean(L, 0); /* (sp += 1) */ in magnet_init_lighty_table()
3113 lua_setfield(L, -2, "__metatable"); /* protect metatable (sp -= 1) */ in magnet_init_lighty_table()
3114 lua_setmetatable(L, -2); /* tie the metatable to lighty (sp -= 1) */ in magnet_init_lighty_table()
3119 static void magnet_clear_table(lua_State * const L, int ndx) { in magnet_clear_table() argument
3124 for (lua_pushnil(L); lua_next(L, ndx); ) { in magnet_clear_table()
3125 lua_pop(L, 1); in magnet_clear_table()
3126 lua_pushvalue(L, -1); in magnet_clear_table()
3127 lua_pushnil(L); in magnet_clear_table()
3128 lua_rawset(L, ndx < 0 ? ndx - 2 : ndx); in magnet_clear_table()
3133 static int magnet_traceback(lua_State *L) { in magnet_traceback() argument
3134 if (!lua_isstring(L, 1)) /* 'message' not a string? */ in magnet_traceback()
3136 if (lua_getglobal_and_type(L, "debug") != LUA_TTABLE) { in magnet_traceback()
3137 lua_pop(L, 1); in magnet_traceback()
3140 if (lua_getfield_and_type(L, -1, "traceback") != LUA_TFUNCTION) { in magnet_traceback()
3141 lua_pop(L, 2); in magnet_traceback()
3144 lua_pushvalue(L, 1); /* pass error message */ in magnet_traceback()
3145 lua_pushinteger(L, 2); /* skip this function and traceback */ in magnet_traceback()
3146 lua_call(L, 2, 1); /* call debug.traceback */ in magnet_traceback()
3155 lua_State * const L = sc->L; in magnet_script_setup() local
3157 if (lua_isfunction(L, func_ndx)) { in magnet_script_setup()
3159 magnet_script_setup_global_state(L); in magnet_script_setup()
3161 lua_pushcfunction(L, magnet_traceback);/*errfunc*//* (sp += 1) */ in magnet_script_setup()
3166 lua_createtable(L, 0, 1); /* env_ndx = 3 */ /* (sp += 1) */ in magnet_script_setup()
3167 magnet_mainenv_metatable(L); /* (sp += 1) */ in magnet_script_setup()
3168 lua_setmetatable(L, -2); /* (sp -= 1) */ in magnet_script_setup()
3170 lua_pushvalue(L, -1); /* (sp += 1) */ in magnet_script_setup()
3171 magnet_setfenv_mainfn(L, func_ndx); /* (sp -= 1) */ in magnet_script_setup()
3174 lua_createtable(L, 0, 2); /* (result_ndx = 4) */ /* (sp += 1) */ in magnet_script_setup()
3178 (request_st **)lua_newuserdata0(L, sizeof(request_st *)); in magnet_script_setup()
3180 magnet_init_lighty_table(L,r_userdata,result_ndx);/* (sp += 1) */ in magnet_script_setup()
3184 if (lua_isstring(L, func_ndx)) in magnet_script_setup()
3187 lua_tostring(L, func_ndx)); in magnet_script_setup()
3191 lua_settop(L, 0); in magnet_script_setup()
3205 lua_State * const L = sc->L; in magnet_attract() local
3213 if (__builtin_expect( (lua_gettop(L) != lighty_table_ndx), 0)) { in magnet_attract()
3220 magnet_set_request(L, r); in magnet_attract()
3222 *(request_st **)lua_touserdata(L, ud_ndx) = r; in magnet_attract()
3226 lua_pushvalue(L, lighty_table_ndx); /* (sp += 1) */ in magnet_attract()
3227 lua_setfield(L, env_ndx, "lighty"); /* lighty.* (sp -= 1) */ in magnet_attract()
3230 lua_pushvalue(L, func_ndx); /* (sp += 1) */ in magnet_attract()
3231 int ret = lua_pcall(L, 0, 1, errfunc_ndx); /* (sp -= 1; sp += 1) */ in magnet_attract()
3236 const char * const err = lua_tolstring(L, -1, &errlen); in magnet_attract()
3250 int lua_return_value = lua_isnil(L, -1) in magnet_attract()
3252 : (int) lua_tointegerx(L, -1, &isnum); in magnet_attract()
3256 luaL_typename(L, -1)); in magnet_attract()
3262 if (lua_getfield_and_type(L, result_ndx, "header") == LUA_TTABLE) { in magnet_attract()
3263 magnet_copy_response_header(L, r); /* deprecated legacy API */ in magnet_attract()
3270 if (lua_getfield_and_type(L, result_ndx, "content") == LUA_TTABLE) { in magnet_attract()
3271 magnet_attach_content(L, r); /* deprecated legacy API */ in magnet_attract()
3309 magnet_clear_table(L, env_ndx); in magnet_attract()
3310 magnet_clear_table(L, result_ndx); in magnet_attract()
3312 lua_settop(L, lighty_table_ndx); /*(handle deferred lua_pop()s)*/ in magnet_attract()