Commit 89031091 authored by jan.koester's avatar jan.koester
Browse files

plugin api easier

parent e787e767
Loading
Loading
Loading
Loading
+9 −9
Changes for src/blogi.cpp: 9 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -193,7 +193,7 @@ void blogi::Blogi::loginPage(libhttppp::HttpRequest &curreq,const int tid,const
        if(index.getElementbyID("main"))
            index.getElementbyID("main")->insertChild(condat.parse());

        for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
            if(curreq.isMobile())
                curplg->getInstace()->Rendering(tid,curreq,&index,RenderingType::Mobile,sessionid);
            else
@@ -301,7 +301,7 @@ void blogi::Blogi::settingsPage(libhttppp::HttpRequest& curreq,const int tid,con
    char url[512];

    setgui << "<div id=\"settings\"><table><tr><td id=\"setnav\" ><ul>";
    for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
    for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        if(curplg->getInstace()->haveSettings())
            setgui << "<li><a href=\"" << PlgArgs->config->buildurl("settings/",url,512) << curplg->getInstace()->getName()  << "\">"  << curplg->getInstace()->getName() << "</a></li>";
    }
@@ -309,7 +309,7 @@ void blogi::Blogi::settingsPage(libhttppp::HttpRequest& curreq,const int tid,con

    int relen=strlen(PlgArgs->config->buildurl("settings/",url,512))-curreq.getRequestURL().length();
    if( relen < 0){
        for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
            if(curreq.getRequestURL().compare(strlen(PlgArgs->config->buildurl("settings/",url,512)),strlen(curplg->getInstace()->getName()),
                curplg->getInstace()->getName() )==0)
                curplg->getInstace()->Settings(tid,curreq,setgui,sessiondid);
@@ -326,7 +326,7 @@ void blogi::Blogi::settingsPage(libhttppp::HttpRequest& curreq,const int tid,con

    index.getElementbyID("main")->appendChild(setgui.parse());

    for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
    for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        if(curreq.isMobile())
            curplg->getInstace()->Rendering(tid,curreq,&index,RenderingType::Mobile);
        else
@@ -360,7 +360,7 @@ void blogi::Blogi::searchPage(libhttppp::HttpRequest& curreq, const int tid, con
        word.push_back('\0');
        searchstr.push_back('\0');

        for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
            PluginApi *api=curplg->getInstace();
            api->Search(tid,searchstr.data(),sdat,sessionid);
        }
@@ -373,7 +373,7 @@ void blogi::Blogi::searchPage(libhttppp::HttpRequest& curreq, const int tid, con
    else
        index=Index;

    for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
    for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        if(curreq.isMobile())
            curplg->getInstace()->Rendering(tid,curreq,&index,RenderingType::Mobile);
        else
@@ -537,14 +537,14 @@ RETRY_REQUEST:


            if(!PlgArgs->theme->Controller(tid,curreq)){
                for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
                for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
                    if(curreq.isMobile())
                        curplg->getInstace()->Rendering(tid,curreq,&index,RenderingType::Mobile,sessid);
                    else
                        curplg->getInstace()->Rendering(tid,curreq,&index,RenderingType::Desktop,sessid);
                }

                for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
                for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
                    PluginApi *api=curplg->getInstace();
                    std::string url=PlgArgs->config->getprefix();
                    url+="/";
@@ -601,7 +601,7 @@ void blogi::Blogi::ResponseEvent(libhttppp::HttpRequest& curreq,const int tid,UL
    if(PlgArgs->theme->Response(tid,curreq))
        return;

    for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
    for(const blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        PluginApi *api=curplg->getInstace();
        std::string url=PlgArgs->config->getprefix();
        url+="/";
+37 −92
Changes for src/plugin.cpp: 37 added lines, 92 removed lines.
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <dirent.h>
#include <dlfcn.h>
#endif
#include "plugin.h"
@@ -131,132 +130,90 @@ void blogi::PluginApi::setArgs(blogi::PluginArgs* args){
}

blogi::Plugin::Plugin(){
    _firstPlugin=nullptr;
}


void blogi::Plugin::loadPlugins(const char *path, PluginArgs *args){
#ifdef Windows
    // Use C++17 std::filesystem for directory traversal
    try {
        if (std::filesystem::is_directory(path)) {
            for (const auto& entry : std::filesystem::directory_iterator(path)) {
                // Check for the .dll file extension

#ifdef Windows
                if (entry.path().extension() == ".dll") {
                    try {
                        std::string ppath = entry.path().string();
                        PluginData* ldplg = new PluginData();

                        // Load the DLL using LoadLibraryA for char* paths
                        std::unique_ptr<PluginData> ldplg = std::make_unique<PluginData>();
                        ldplg->pldata = LoadLibraryA(ppath.c_str());

                        if (ldplg->pldata == nullptr) {
                            DWORD lastError = GetLastError();
                            delete ldplg;
                            // Throw a standard exception for simplicity
                            throw std::runtime_error("Cannot load library. Error code: " + std::to_string(lastError));
                        }

                        // Get the address of the 'create' function using GetProcAddress
                        create_t *create_plugin = (create_t*)GetProcAddress(ldplg->pldata, "create");

                        if (create_plugin == nullptr) {
                            DWORD lastError = GetLastError();
                            // If GetProcAddress fails, we must free the library
                            FreeLibrary(ldplg->pldata);
                            delete ldplg;
                            throw std::runtime_error("Cannot load symbol 'create'. Error code: " + std::to_string(lastError));
                        }

                        // Rest of the plugin handling logic is portable
                        PluginApi* plg = create_plugin();
                        plg->setArgs(args);
                        plg->initPlugin();
                        ldplg->ins = plg;

                        if (_firstPlugin) {
                            _lastPluging->next = ldplg;
                            _lastPluging = _lastPluging->next;
                        }
                        else {
                            _firstPlugin = ldplg;
                            _lastPluging = _firstPlugin;
                        }

                    }
                    catch (const std::exception& e) {
                        std::cerr << "Error processing file " << entry.path().filename() << ": " << e.what() << std::endl;
                    }
                }
            }
        }
    } catch (const std::filesystem::filesystem_error& e) {
        std::cerr << "Error accessing directory: " << e.what() << std::endl;
    }
#else
    DIR *directory = opendir(path);
    struct dirent *direntStruct;

    if (directory != nullptr) {
        while((direntStruct = readdir(directory))) {
            if((direntStruct->d_name[0]=='.' && strlen(direntStruct->d_name)==1) ||
              ((direntStruct->d_name[0]=='.') &&  (direntStruct->d_name[1]=='.') &&
              strlen(direntStruct->d_name)==2))
                continue;
#else // Linux/Unix
                if (entry.path().extension() == ".so") {
                    try{
                std::string ppath = path;
                ppath.append("/");
                ppath.append(direntStruct->d_name);
                        std::unique_ptr<PluginData> ldplg(new PluginData());

                        ldplg->pldata = dlopen(entry.path().c_str(), RTLD_LAZY);

                PluginData *ldplg=new PluginData();
                        const char * dlsym_error = nullptr;
                ldplg->pldata = dlopen(ppath.c_str(),RTLD_LAZY);

                if (!ldplg->pldata && (dlsym_error=dlerror()) ){
                        delete ldplg; libhttppp::HTTPException err; 
                        err[libhttppp::HTTPException::Error] << "Cannot load library: " << dlsym_error; 
                        if (!ldplg->pldata) { // FIX: Check ldplg->pldata, not !ldplg->pldata && dlsym_error
                            dlsym_error = dlerror();
                            libhttppp::HTTPException err;
                            err[libhttppp::HTTPException::Error] << "Cannot load library: " << (dlsym_error ? dlsym_error : "Unknown error");
                            throw err;
                        }

                // load the symbols
                        // Get the address of the 'create' function
                        create_t* create_plugin= (create_t*) dlsym(ldplg->pldata, "create");
                if ( !ldplg->pldata && (dlsym_error=dlerror()) ) {
                    delete ldplg;

                        if (create_plugin == nullptr) {
                            dlsym_error = dlerror();
                            libhttppp::HTTPException err;
                    err[libhttppp::HTTPException::Error]  << "Cannot load symbol create: " << dlsym_error << '\n';
                            // The custom deleter will call dlclose when ldplg goes out of scope/is destroyed
                            err[libhttppp::HTTPException::Error] << "Cannot load symbol create: " << (dlsym_error ? dlsym_error : "Unknown error") << '\n';
                            throw err;
                        }


                dlerror();

#endif
                        PluginApi *plg=create_plugin();
                        plg->setArgs(args);
                        plg->initPlugin();
                        ldplg->ins=plg;

                if(_firstPlugin){
                    _lastPluging->next=ldplg;
                    _lastPluging=_lastPluging->next;
                        if(_firstPlugin.get()){
                            _lastPlugin->next.swap(ldplg);
                            _lastPlugin = std::move(_lastPlugin->next);
                        }else{
                    _firstPlugin=ldplg;
                    _lastPluging=_firstPlugin;
                            _firstPlugin.swap(ldplg);
                            _lastPlugin = std::move(_lastPlugin->next);
                        }
            }catch(libhttppp::HTTPException &e){
                std::cerr << e.what() << std::endl;
                    }
                    catch (const std::exception& e) {
                        std::cerr << "Error processing file " << entry.path().filename().string() << ": " << e.what() << std::endl;
                    }
        closedir(directory);
                }
#endif
            }
        }
    }catch (const std::exception& e) {
        std::cerr << "Error loading plugins directory: " << e.what() << std::endl;
    }
}

blogi::Plugin::~Plugin(){
    delete _firstPlugin;
}

blogi::Plugin::PluginData * blogi::Plugin::getFirstPlugin(){
    return _firstPlugin;
const blogi::Plugin::PluginData* blogi::Plugin::getFirstPlugin() const{
    return _firstPlugin.get();
}

blogi::Plugin::PluginData::~PluginData(){
@@ -282,21 +239,9 @@ blogi::Plugin::PluginData::~PluginData(){
        // It's crucial to call this even if GetProcAddress failed, to prevent a resource leak.
        FreeLibrary(pldata);
    }

    // The rest of the code is portable
    delete next;
#else
    const char* dlsym_error = dlerror();
    destroy_t* destroy_plugin = (destroy_t*)dlsym(pldata, "destroy");
    dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';;
    }
    else {
        destroy_plugin(ins);
    if (pldata)
        dlclose(pldata);
    }
    delete next;
#endif //  Windows
}

@@ -305,10 +250,10 @@ blogi::Plugin::PluginData::PluginData(){
    ins=nullptr;
}

blogi::PluginApi * blogi::Plugin::PluginData::getInstace(){
blogi::PluginApi * blogi::Plugin::PluginData::getInstace() const{
    return ins;
}

blogi::Plugin::PluginData * blogi::Plugin::PluginData::getNextPlg(){
    return next;
const blogi::Plugin::PluginData *blogi::Plugin::PluginData::getNextPlg() const{
    return next.get();
}
+9 −7
Changes for src/plugin.h: 9 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <memory>

#include <httppp/exception.h>
#include <httppp/http.h>

@@ -125,24 +127,24 @@ namespace blogi {
        class PluginData{
        private:
            PluginData();
            ~PluginData();
#ifdef Windows
			HMODULE pldata;
#else
            void* pldata;
#endif // Windows
            PluginApi  *ins;
            PluginData *next;
            std::unique_ptr<PluginData> next = nullptr;
        public:
            PluginApi  *getInstace();
            PluginData *getNextPlg();
            ~PluginData();
            PluginApi  *getInstace() const;
            const PluginData  *getNextPlg() const;
            friend class Plugin;
       };

       PluginData *getFirstPlugin();
      const PluginData* getFirstPlugin() const;

    private:
       PluginData *_firstPlugin;
       PluginData *_lastPluging;
       std::unique_ptr<PluginData> _firstPlugin = nullptr;
       std::unique_ptr<PluginData> _lastPlugin = nullptr;
   };
};