diff options
Diffstat (limited to 'storage/connect/tabrest.cpp')
-rw-r--r-- | storage/connect/tabrest.cpp | 112 |
1 files changed, 90 insertions, 22 deletions
diff --git a/storage/connect/tabrest.cpp b/storage/connect/tabrest.cpp index 9e1a643c89f..f9acbc28ddc 100644 --- a/storage/connect/tabrest.cpp +++ b/storage/connect/tabrest.cpp @@ -1,5 +1,5 @@ /*************** Rest C++ Program Source Code File (.CPP) **************/ -/* PROGRAM NAME: Rest Version 1.5 */ +/* PROGRAM NAME: Rest Version 1.6 */ /* (C) Copyright to the author Olivier BERTRAND 2018 - 2019 */ /* This program is the REST Web API support for MariaDB. */ /* When compiled without MARIADB defined, it is the EOM module code. */ @@ -36,17 +36,7 @@ #include "tabfmt.h" #include "tabrest.h" -/***********************************************************************/ -/* Get the file from the Web. */ -/***********************************************************************/ -int restGetFile(PGLOBAL g, PCSZ http, PCSZ uri, PCSZ fn); - -#if defined(__WIN__) -static PCSZ slash = "\\"; -#else // !__WIN__ -static PCSZ slash = "/"; -#define stricmp strcasecmp -#endif // !__WIN__ +static XGETREST getRestFnc = NULL; #if !defined(MARIADB) /***********************************************************************/ @@ -76,6 +66,74 @@ PTABDEF __stdcall GetREST(PGLOBAL g, void *memp) #endif // !MARIADB /***********************************************************************/ +/* GetREST: get the external TABDEF from OEM module. */ +/***********************************************************************/ +XGETREST GetRestFunction(PGLOBAL g) +{ + if (getRestFnc) + return getRestFnc; + +#if !defined(REST_SOURCE) + if (trace(515)) + htrc("Looking for GetRest library\n"); + +#if defined(__WIN__) + HANDLE Hdll; + const char* soname = "GetRest.dll"; // Module name + + if (!(Hdll = LoadLibrary(soname))) { + char buf[256]; + DWORD rc = GetLastError(); + + sprintf(g->Message, MSG(DLL_LOAD_ERROR), rc, soname); + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0, + (LPTSTR)buf, sizeof(buf), NULL); + strcat(strcat(g->Message, ": "), buf); + return NULL; + } // endif Hdll + +// Get the function returning an instance of the external DEF class + if (!(getRestFnc = (XGETREST)GetProcAddress((HINSTANCE)Hdll, "restGetFile"))) { + char buf[256]; + DWORD rc = GetLastError(); + + sprintf(g->Message, MSG(PROCADD_ERROR), rc, "restGetFile"); + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0, + (LPTSTR)buf, sizeof(buf), NULL); + strcat(strcat(g->Message, ": "), buf); + FreeLibrary((HMODULE)Hdll); + return NULL; + } // endif getRestFnc +#else // !__WIN__ + void* Hso; + const char* error = NULL; + const char* soname = "GetRest.so"; // Module name + + // Load the desired shared library + if (!(Hso = dlopen(soname, RTLD_LAZY))) { + error = dlerror(); + sprintf(g->Message, MSG(SHARED_LIB_ERR), soname, SVP(error)); + return NULL; + } // endif Hdll + +// Get the function returning an instance of the external DEF class + if (!(getRestFnc = (XGETREST)dlsym(Hso, "restGetFile"))) { + error = dlerror(); + sprintf(g->Message, MSG(GET_FUNC_ERR), "restGetFile", SVP(error)); + dlclose(Hso); + return NULL; + } // endif getdef +#endif // !__WIN__ +#else + getRestFnc = restGetFile; +#endif + + return getRestFnc; +} // end of GetRestFunction + +/***********************************************************************/ /* Return the columns definition to MariaDB. */ /***********************************************************************/ #if defined(MARIADB) @@ -87,6 +145,10 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) PQRYRES qrp= NULL; char filename[_MAX_PATH + 1]; // MAX PATH ??? PCSZ http, uri, fn, ftype; + XGETREST grf = GetRestFunction(g); + + if (!grf) + return NULL; http = GetStringTableOption(g, tp, "Http", NULL); uri = GetStringTableOption(g, tp, "Uri", NULL); @@ -100,11 +162,11 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) // We used the file name relative to recorded datapath strcat(strcat(strcat(strcpy(filename, "."), slash), db), slash); - strncat(filename, fn, _MAX_PATH); + strncat(filename, fn, _MAX_PATH - strlen(filename)); // Retrieve the file from the web and copy it locally - if (http && restGetFile(g, http, uri, filename)) { - // sprintf(g->Message, "Failed to get file at %s", http); + if (http && grf(g->Message, trace(515), http, uri, filename)) { + // sprintf(g->Message, "Failed to get file at %s", http); } else if (!stricmp(ftype, "XML")) qrp = XMLColumns(g, db, tab, tp, info); else if (!stricmp(ftype, "JSON")) @@ -124,9 +186,14 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) /***********************************************************************/ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) { - char filename[_MAX_PATH + 1]; + char filename[_MAX_PATH + 1]; int rc = 0, n; - LPCSTR ftype; + bool xt = trace(515); + LPCSTR ftype; + XGETREST grf = GetRestFunction(g); + + if (!grf) + return true; #if defined(MARIADB) ftype = GetStringCatInfo(g, "Type", "JSON"); @@ -135,7 +202,7 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) ftype = GetStringCatInfo(g, "Ftype", "JSON"); #endif // !MARIADB - if (trace(515)) + if (xt) htrc("ftype = %s am = %s\n", ftype, SVP(am)); n = (!stricmp(ftype, "JSON")) ? 1 @@ -154,12 +221,13 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) // We used the file name relative to recorded datapath //PlugSetPath(filename, Fn, GetPath()); - strncat(strcpy(filename, GetPath()), Fn, _MAX_PATH); + strcpy(filename, GetPath()); + strncat(filename, Fn, _MAX_PATH - strlen(filename)); // Retrieve the file from the web and copy it locally - rc = restGetFile(g, Http, Uri, filename); + rc = grf(g->Message, xt, Http, Uri, filename); - if (trace(515)) + if (xt) htrc("Return from restGetFile: rc=%d\n", rc); if (rc) @@ -175,7 +243,7 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) if (Tdp && Tdp->Define(g, Cat, Name, Schema, "REST")) Tdp = NULL; // Error occured - if (trace(515)) + if (xt) htrc("Tdp defined\n", rc); // Return true in case of error |