Ejemplo n.º 1
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 = (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, cached_access(fnv1a_32(full_fname, epd->vhost_root_len + len), full_fname) != -1);

    return 1;
}
Ejemplo n.º 2
0
int lua_f_router(lua_State *L)
{
    if(lua_routed) {
        lua_pushnil(L);
        lua_pushnil(L);
        return 2;
    }

    lua_routed = 1;

    if(!lua_isstring(L, 1)) {
        lua_pushnil(L);
        lua_pushstring(L, "excepted uri");
        return 2;
    }

    if(!lua_istable(L, 2)) {
        lua_pushnil(L);
        lua_pushstring(L, "excepted router table");
        return 2;
    }

    const char *uri = lua_tostring(L, 1);
    int uri_len = strlen(uri);

    if(uri_len < 1 || uri[0] != '/') {
        lua_pushnil(L);
        lua_pushstring(L, "not a uri");
        return 2;
    }

    if(lua_isstring(L, 3)) {
        // try local lua script file
        epdata_t *epd = NULL;

        lua_getglobal(L, "__epd__");

        if(lua_isuserdata(L, -1)) {
            epd = lua_touserdata(L, -1);
        }

        lua_pop(L, 1);

        if(epd) {

            size_t len = 0;
            const char *fname = lua_tolstring(L, 3, &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);
            memcpy(full_fname + epd->vhost_root_len + len , uri, uri_len);

            len = epd->vhost_root_len + len + uri_len;

            full_fname[len] = '\0';

            if(full_fname[len - 4] == '.' && full_fname[len - 3] == 'l' && full_fname[len - 1] == 'a') {
                if(cached_access(fnv1a_32(full_fname, len), full_fname) != -1) {
                    lua_pushnil(L);
                    lua_pushstring(L, full_fname + (len - uri_len));
                    return 2;
                }
            }

            if(full_fname[len - 1] != '/') {
                memcpy(full_fname + len, ".lua", 4);
                full_fname[len + 4] = '\0';

                //if(access(full_fname, F_OK) != -1) {
                if(cached_access(fnv1a_32(full_fname, len + 4), full_fname) != -1) {
                    lua_pushnil(L);
                    lua_pushstring(L, full_fname + (len - uri_len));
                    return 2;
                }

            } else {

                memcpy(full_fname + len, "index.lua", 9);
                full_fname[len + 9] = '\0';

                //if(access(full_fname, F_OK) != -1) {
                if(cached_access(fnv1a_32(full_fname, len + 9), full_fname) != -1) {
                    lua_pushnil(L);
                    lua_pushstring(L, full_fname + (len - uri_len));
                    return 2;
                }

                memcpy(full_fname + len - 1, ".lua", 4);
                full_fname[len - 1 + 4] = '\0';

                //if(access(full_fname, F_OK) != -1) {
                if(cached_access(fnv1a_32(full_fname, len + 3), full_fname) != -1) {
                    lua_pushnil(L);
                    lua_pushstring(L, full_fname + (len - uri_len));
                    return 2;
                }
            }
        }
    }

    int pat = 0;
    lua_pushvalue(L, 2);
    lua_pushnil(L);
    match_max = 0;

    while(lua_next(L, -2)) {
        if(lua_isstring(L, -2)) {
            //printf("== %s\n", lua_tostring(L, -2));
            if(is_match(lua_tostring(L, -2), uri)) {
                the_match_pat = pat;
            }
        }

        lua_pop(L, 1);
        pat++;

        if(pat >= 100) {
            break;
        }
    }

    pat = 0;
    lua_pushvalue(L, 2);
    lua_pushnil(L);

    while(lua_next(L, -2)) {
        if(lua_isstring(L, -2)) {
            if(match_max > 0 && pat == the_match_pat) {
                //printf("== %s\n", lua_tostring(L, -2));
                lua_pushvalue(L, -1);
                lua_pop(L, 1);

                lua_createtable(L, 0, match_max);
                int i = 0;

                for(i = 1; i < match_max; i++) {
                    if(!v_p[i]) {
                        continue;
                    }

                    lua_pushlstring(L, v_p[i], v_p_len[i]);
                    lua_pushstring(L, v_c[i]);
                    free(v_c[i]);
                    v_c[i] = NULL;
                    lua_settable(L, -3); /* 3rd element from the stack top */
                }

                return 2;
            }
        }

        lua_pop(L, 1);
        pat++;

        if(pat >= 100) {
            break;
        }
    }

    return 0;
}