int lua_f_readfile(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(!lua_isstring(L, -1)) { lua_pushnil(L); lua_pushstring(L, "Need a file path!"); return 2; } size_t len = 0; const char *fname = lua_tolstring(L, 1, &len); //char *full_fname = malloc(epd->vhost_root_len + len); char *full_fname = (char *)&temp_buf; memcpy(full_fname, epd->vhost_root, epd->vhost_root_len); memcpy(full_fname + epd->vhost_root_len , fname, len); full_fname[epd->vhost_root_len + len] = '\0'; char *buf = NULL; off_t reads = 0; int fd = open(full_fname, O_RDONLY, 0); //printf("readfile: %s\n", full_fname); //free(full_fname); if(fd > -1) { reads = lseek(fd, 0L, SEEK_END); lseek(fd, 0L, SEEK_SET); if(reads > 8192) { buf = malloc(reads); } else { buf = (char *)&temp_buf; } read(fd, buf, reads); close(fd); lua_pushlstring(L, buf, reads); if(buf != (char *)&temp_buf) { free(buf); } return 1; } lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2; }
int lua_flush(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { return 0; } int nargs = lua_gettop(L); _lua_echo(epd, L, nargs, 0); if(epd->status != STEP_PROCESS) { return 0; } if(epd->websocket || epd->status == STEP_SEND) { return 0; } if(network_flush(epd) == 1) { return lua_yield(L, 0); } return 0; }
int lua_f_file_exists(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(!lua_isstring(L, 1)) { lua_pushnil(L); lua_pushstring(L, "Need a file path!"); return 2; } size_t len = 0; const char *fname = lua_tolstring(L, 1, &len); //char *full_fname = malloc(epd->vhost_root_len + len); char *full_fname = (char *)&temp_buf; memcpy(full_fname, epd->vhost_root, epd->vhost_root_len); memcpy(full_fname + epd->vhost_root_len, fname, len); full_fname[epd->vhost_root_len + len] = '\0'; lua_pushboolean(L, access(full_fname, F_OK) != -1); //free(full_fname); return 1; }
int lua_read_request_body(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(epd->websocket) { return 0; } if(epd->contents) { epd->contents = NULL; lua_pushlstring(L, epd->headers + epd->_header_length, epd->data_len - epd->_header_length); return 1; } if(!epd->se_ptr || epd->content_length <= epd->data_len - epd->_header_length) { lua_pushnil(L); lua_pushstring(L, "eof"); return 2; } epd->status = STEP_READ; serv_status.reading_counts++; se_be_read(epd->se_ptr, network_be_read_request_body); return lua_yield(L, 0); }
int lua_echo(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } int nargs = lua_gettop(L); if(nargs < 1) { luaL_error(L, "miss content!"); return 0; } size_t len = 0; if(epd->websocket) { return 0; } if(_lua_echo(epd, L, nargs, 1)) { return lua_yield(L, 0); } if(longtime() - epd->start_time > STEP_PROCESS_TIMEOUT) { epd->keepalive = 0; lua_pushstring(L, "Process Time Out!"); lua_error(L); /// stop lua script } return 0; }
int lua_die(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { return 0; } int nargs = lua_gettop(L); _lua_echo(epd, L, nargs, 0); if(epd->status != STEP_PROCESS) { return 0; } if(epd->websocket || epd->status == STEP_SEND) { return 0; } lua_pushnil(L); lua_error(L); /// stop lua script //network_be_end(epd); return 0; }
int lua_f_filemtime(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(!lua_isstring(L, -1)) { lua_pushnil(L); lua_pushstring(L, "Need a file path!"); return 2; } size_t len = 0; const char *fname = lua_tolstring(L, 1, &len); char *full_fname = (char *)&temp_buf; memcpy(full_fname, epd->vhost_root, epd->vhost_root_len); memcpy(full_fname + epd->vhost_root_len , fname, len); full_fname[epd->vhost_root_len + len] = '\0'; struct stat fst; if(stat(full_fname, &fst) < 0) { lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2; } lua_pushnumber(L, fst.st_mtime); return 1; }
int lua_f_get_boundary(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(epd->boundary) { lua_pushstring(L, epd->boundary); } else { lua_pushnil(L); } return 1; }
int lua_end(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { return 0; } if(epd->status != STEP_PROCESS) { return 0; } if(epd->websocket || epd->status == STEP_SEND) { return 0; } network_be_end(epd); return 0; }
int lua_sendfile(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(epd->header_sended != 0) { lua_pushnil(L); lua_pushstring(L, "respone header has been sended"); return 2; } if(!lua_isstring(L, 1)) { lua_pushnil(L); lua_pushstring(L, "Need a file path!"); return 2; } size_t len = 0; const char *fname = lua_tolstring(L, 1, &len); //char *full_fname = malloc(epd->vhost_root_len + len); char *full_fname = (char *)&temp_buf; memcpy(full_fname, epd->vhost_root, epd->vhost_root_len); memcpy(full_fname + epd->vhost_root_len , fname, len); full_fname[epd->vhost_root_len + len] = '\0'; network_sendfile(epd, full_fname); //free(full_fname); lua_pushnil(L); lua_error(L); /// stop lua script network_be_end(epd); return 0; }
int lua_clear_header(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(epd->header_sended != 0) { lua_pushnil(L); lua_pushstring(L, "respone header has been sended"); return 2; } epd->response_header_length = 0; free(epd->iov[0].iov_base); epd->iov[0].iov_base = NULL; epd->iov[0].iov_len = 0; return 0; }
int lua_check_timeout(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(epd->websocket) { return 0; } if(longtime() - epd->start_time > STEP_PROCESS_TIMEOUT) { epd->keepalive = 0; lua_pushstring(L, "Process Time Out!"); lua_error(L); /// stop lua script } return 0; }
int lua_header(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } if(epd->websocket) { return 0; } if(lua_gettop(L) < 1) { return 0; } if(epd->header_sended != 0) { lua_pushnil(L); lua_pushstring(L, "respone header has been sended"); return 2; } int t = lua_type(L, 1); size_t dlen = 0; const char *data = NULL; int ret = 0; if(t == LUA_TSTRING) { data = lua_tolstring(L, 1, &dlen); if(stristr(data, "content-length", dlen) != data) { ret = network_send_header(epd, data); } } else if(t == LUA_TTABLE) { int len = lua_objlen(L, 1), i = 0; for(i = 0; i < len; i++) { lua_pushinteger(L, i + 1); lua_gettable(L, -2); if(lua_isstring(L, -1)) { data = lua_tolstring(L, -1, &dlen); if(stristr(data, "content-length", dlen) != data) { ret = network_send_header(epd, lua_tostring(L, -1)); } } lua_pop(L, 1); } } if(ret == -1) { lua_pushnil(L); lua_pushstring(L, "respone header too big"); return 2; } else if(ret == 0) { lua_pushnil(L); } else { lua_pushboolean(L, 1); } return 1; }
int lua_print_error(lua_State *L) { epdata_t *epd = get_epd(L); if(!epd) { lua_pushnil(L); lua_pushstring(L, "miss epd!"); return 2; } lua_Debug ar; lua_getstack(L, 1, &ar); lua_getinfo(L, "nSl", &ar); if(!ar.source) { return 0; } snprintf(temp_buf_1024, 1024, "%s:%d", ar.source + 1, ar.currentline); lua_getfield(L, LUA_GLOBALSINDEX, "debug"); if(!lua_istable(L, -1)) { lua_pop(L, 1); return 1; } lua_getfield(L, -1, "traceback"); if(!lua_isfunction(L, -1)) { lua_pop(L, 2); return 1; } lua_pushvalue(L, 1); /* pass error message */ lua_pushinteger(L, 2); /* skip this function and traceback */ lua_call(L, 2, 1); /* call debug.traceback */ size_t len = 0; char *msg = (char *)lua_tolstring(L, -1, &len); int i = 0; memcpy(temp_buf2, "<h3>Error: ", 11); int j = 11; int is_first_line = 1; for(i = 0; i < len; i++) { temp_buf[i] = (msg[i] != '\n' ? msg[i] : ' '); if(is_first_line && msg[i] == '\n') { memcpy(temp_buf2 + j, "</h3>\n<pre>", 11); j += 11; } temp_buf2[j++] = msg[i]; } temp_buf[len] = '\0'; memcpy(temp_buf2 + j, "\n</pre>", 7); j += 7; temp_buf2[j] = '\0'; _LOGF(ERR, temp_buf_1024, "%s", temp_buf); network_send(epd, temp_buf2, j); return 1; }