Beispiel #1
0
static int rum_post_config(apr_pool_t *p, apr_pool_t *pLog, apr_pool_t *pTmp,
                           server_rec *s)
{
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                 "BEG post_config hook");


    // since creating the RUM Config instance can be time consuming,
    // do it only once
    void *data;
    const char *userdata_key = "rum_post_config_only_once_key";
    apr_pool_userdata_get(&data, userdata_key, s->process->pool);
    if (!data) {
        apr_pool_userdata_set((const void *)1, userdata_key,
                              apr_pool_cleanup_null, s->process->pool);
        return OK;
    }


    rum_server_config *sc_main = NULL;
    server_rec *s2;
    for (s2 = s; s2; s2 = s2->next)
    {
        rum_server_config *sc2 =
            static_cast<rum_server_config *>
            (ap_get_module_config(s2->module_config, &rum_module));

        // is this the main server
        bool isMain = (s == s2);

        if (isMain)
        {
            sc_main = sc2;
        }

        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s2,
                     "RUM: processing "
                     "server: %s, defined at %s:%d",
                     s2->server_hostname, s2->defn_name,
                     s2->defn_line_number);

        if (sc2->useMain)
        {
            if (isMain)
            {
                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                             "RumUseMain not allowed for main server");
                return HTTP_INTERNAL_SERVER_ERROR;
            }
            else
            {
                if (sc2->configFiles->size() > 0)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumConfigFile directive not allowed here "
                                 "because RumUseMain specified for virtual "
                                 "server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if (sc2->baseDir)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumBaseDir directive not allowed here "
                                 "because RumUseMain specified for virtual "
                                 "server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if (sc2->maxLookups)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumMaxLookups directive not allowed here "
                                 "because RumUseMain specified for virtual "
                                 "server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if (sc2->fullLuaGC)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumFullLuaGC directive not allowed here "
                                 "because RumUseMain specified for virtual "
                                 "server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if (sc2->initLuaStatePoolSize)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumInitialLuaStatePoolSize "
                                 "directive not allowed here "
                                 "because RumUseMain specified for virtual "
                                 "server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }

                // refer to the main server's RUM Config
                if (!sc_main->conf)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumConfigFile directive not specified "
                                 "for main server but "
                                 "RumUseMain specified for virtual "
                                 "server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s2,
                             "using main RUM config for "
                             "server: %s, defined at %s:%d",
                             s2->server_hostname, s2->defn_name,
                             s2->defn_line_number);
                sc2->conf = sc_main->conf;
            }
        }
        else
        {
            // RumUseMain not specified

            if ((sc2->server == s2) && (sc2->configFiles->size() > 0))
            {
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s2,
                             "configuring RUM for "
                             "server: %s, defined at %s:%d",
                             s2->server_hostname, s2->defn_name,
                             s2->defn_line_number);

                rum::ServerLogger *logger =
                    new (p) rum::ServerLogger(0,
#if RUM_AP22
                                              s2->loglevel,
#else
                                              s2->log.level,
                                              APLOG_MODULE_INDEX,
#endif
                                              s2);
                logger->destroyWithPool();

                const char *baseDir;
                if (sc2->baseDir)
                {
                    const char *rootpath;
                    const char *filepath = sc2->baseDir;
                    if (apr_filepath_root(&rootpath, &filepath,
                                          APR_FILEPATH_TRUENAME, pTmp) ==
                        APR_SUCCESS)
                    {
                        baseDir = sc2->baseDir;
                    }
                    else
                    {
                        // relative RumBaseDir is relative to server root
                        baseDir = rum_base_dir_relative(pTmp,
                                                        ap_server_root,
                                                        sc2->baseDir);
                    }

                }
                else
                {
                    baseDir = ap_server_root;
                }

                apr_ssize_t maxLookups =
                    sc2->maxLookups ? sc2->maxLookups : 10;

                LuaManager *luaManager =
                    new (p, PoolAllocated::UseSubPools)
                    LuaManager(0, logger, baseDir);
                luaManager->destroyWithPool();

                sc2->conf = new (p) Config(0, logger, pTmp, baseDir,
                                           *sc2->configFiles, maxLookups,
                                           luaManager);
                sc2->conf->destroyWithPool();

                if (sc2->conf->ctorError())
                {
                    return HTTP_INTERNAL_SERVER_ERROR;
                }

                // pre-initialize some Lua states
                if (sc2->initLuaStatePoolSize >= 0)
                {
                    apr_size_t ilsps = sc2->initLuaStatePoolSize;

                    // initialize at least one state so that run-time errors
                    // in the definitions section are caught
                    apr_size_t minInitStates = ilsps ? ilsps : 1;

                    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s2,
                                 "initializing %d Lua states for "
                                 "server: %s, defined at %s:%d",
                                 static_cast<int>(minInitStates),
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);

                    PtrVec<LuaManager::LuaRef *> *luaRefVec =
                        new (p, PoolAllocated::UseSubPools)
                        PtrVec<LuaManager::LuaRef *>(0, minInitStates);
                    apr_size_t i;
                    for (i = 0; i < minInitStates; i++)
                    {
                        LuaManager::LuaRef *luaRef =
                            new (p) LuaManager::LuaRef(p, luaManager);
                        if (! luaRef->isValid())
                        {
                            ap_log_error(APLOG_MARK, APLOG_ERR,
                                         0, s2,
                                         "failed to initialize Lua state for "
                                         "server: %s, defined at %s:%d",
                                         s2->server_hostname, s2->defn_name,
                                         s2->defn_line_number);
                            return HTTP_INTERNAL_SERVER_ERROR;
                        }
                        luaRefVec->push_back(luaRef);
                    }
                    delete luaRefVec;
                }
            }
            else
            {
                if ((sc2->server == s2) && sc2->baseDir)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumBaseDir "
                                 "directive not allowed here "
                                 "because RumConfigFile not specified "
                                 "for server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if ((sc2->server == s2) && sc2->maxLookups)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumMaxLookups "
                                 "directive not allowed here "
                                 "because RumConfigFile not specified "
                                 "for server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if ((sc2->server == s2) && sc2->fullLuaGC)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumFullLuaGC "
                                 "directive not allowed here "
                                 "because RumConfigFile not specified "
                                 "for server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }
                if ((sc2->server == s2) && sc2->initLuaStatePoolSize)
                {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s2,
                                 "RumInitialLuaStatePoolSize "
                                 "directive not allowed here "
                                 "because RumConfigFile not specified "
                                 "for server: %s, defined at %s:%d",
                                 s2->server_hostname, s2->defn_name,
                                 s2->defn_line_number);
                    return HTTP_INTERNAL_SERVER_ERROR;
                }

                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s2,
                             "RUM not configured for "
                             "server: %s, defined at %s:%d",
                             s2->server_hostname, s2->defn_name,
                             s2->defn_line_number);
            }
        }
    }

    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                 "END post_config");

    return OK;
}