From d4fa6bdabc0fac821f9168b2ccb42725c7dcbf0b Mon Sep 17 00:00:00 2001 From: stefanocasazza Date: Wed, 29 Aug 2018 18:20:52 +0200 Subject: [PATCH] usp fix --- examples/IR/ir_web.cpp | 16 +++- include/ulib/all.h | 2 +- include/ulib/debug/objectDB.h | 8 +- include/ulib/internal/objectIO.h | 1 - include/ulib/net/server/usp_macro.h | 2 + include/ulib/string.h | 31 +++++++ include/ulib/utility/uhttp.h | 4 +- .../net/server/plugin/usp/usp_translator.cpp | 89 +++++++++---------- src/ulib/utility/data_session.cpp | 8 +- src/ulib/utility/uhttp.cpp | 16 ++-- tests/examples/TSA/tsaserial | 2 +- tests/examples/web_server.sh | 5 +- 12 files changed, 112 insertions(+), 72 deletions(-) diff --git a/examples/IR/ir_web.cpp b/examples/IR/ir_web.cpp index 2f37b2e3..9b613132 100644 --- a/examples/IR/ir_web.cpp +++ b/examples/IR/ir_web.cpp @@ -52,13 +52,15 @@ static bool usp_bSESSION; { \ UString2Object(varname##_value.data(), usp_sz, varname); \ } \ - U_INTERNAL_DUMP("%s(%u) = %.*S", #varname, usp_sz, usp_sz, varname##_value.data()) \ + U_INTERNAL_DUMP("%s(%u) = %V", #varname, usp_sz, varname##_value.rep) \ } #define USP_SESSION_VAR_PUT(index,varname) \ { \ usp_sz = UObject2String(varname, usp_buffer, sizeof(usp_buffer)); \ - UHTTP::putDataSession(index, usp_buffer, usp_sz); \ + UString varname##_value((void*)usp_buffer, usp_sz); \ + UHTTP::data_session->putValueVar(index, varname##_value); \ + U_INTERNAL_DUMP("%s(%u) = %V", #varname, usp_sz, varname##_value.rep) \ } static void usp_body_ir_web() @@ -66,6 +68,11 @@ static void usp_body_ir_web() U_TRACE(5, "::usp_body_ir_web()") usp_bSESSION = (UHTTP::getDataSession() ? true : (UHTTP::setSessionCookie(), false)); + + + if (usp_bSESSION) { + UHTTP::putDataSESSION(); + } usp_bSESSION = true; const char* ref = "?ext=help"; uint32_t num_args = UHTTP::processForm() / 2; @@ -219,7 +226,10 @@ extern U_EXPORT void runDynamicPage_ir_web(); U_TRACE_NO_PARAM(0, "::runDynamicPage_ir_web()") usp_body_ir_web(); - if (usp_bSESSION) UHTTP::putDataSession(); + if (usp_bSESSION) { + UHTTP::putDataSESSION(); + } + usp_bSESSION = true; UHTTP::mime_index = U_html; U_http_info.endHeader = 0; } } \ No newline at end of file diff --git a/include/ulib/all.h b/include/ulib/all.h index 7136ca0b..f2f02e6f 100644 --- a/include/ulib/all.h +++ b/include/ulib/all.h @@ -79,7 +79,7 @@ # include #endif -#ifdef USE_LIBXML2 +#if defined(USE_LIBXML2) && !defined(U_USP_PROCESS) # include "ulib/xml/libxml2/schema.h" #endif diff --git a/include/ulib/debug/objectDB.h b/include/ulib/debug/objectDB.h index e6f0c39d..5befcd9f 100644 --- a/include/ulib/debug/objectDB.h +++ b/include/ulib/debug/objectDB.h @@ -16,10 +16,6 @@ #include -#ifdef USE_LIBXML2 -# include -#endif - /** * class UObjectDumpable * @@ -104,7 +100,9 @@ public: virtual const char* dump() const __pure { return ((Concrete*)ptr_object)->dump(true); } }; -#ifdef USE_LIBXML2 +#if defined(USE_LIBXML2) && !defined(U_USP_PROCESS) +# include + template <> class U_NO_EXPORT UObjectDumpable_Adapter<_xmlNode> : public UObjectDumpable { public: diff --git a/include/ulib/internal/objectIO.h b/include/ulib/internal/objectIO.h index 0f901f7b..a7be3279 100644 --- a/include/ulib/internal/objectIO.h +++ b/include/ulib/internal/objectIO.h @@ -139,5 +139,4 @@ template inline char* U_OBJECT_TO_TRACE(T& object) return str; } #endif - #endif diff --git a/include/ulib/net/server/usp_macro.h b/include/ulib/net/server/usp_macro.h index 24ce7f5d..7dff7f6f 100644 --- a/include/ulib/net/server/usp_macro.h +++ b/include/ulib/net/server/usp_macro.h @@ -14,7 +14,9 @@ #ifndef U_USP_MACRO_H #define U_USP_MACRO_H 1 +#define U_USP_PROCESS 1 #include +#undef U_USP_PROCESS #define USP_PUTS_BUFFER (void)UClientImage_Base::wbuffer->append(usp_buffer,usp_sz) #define USP_PUTS_CHAR(c) (void)UClientImage_Base::wbuffer->push_back((c)) diff --git a/include/ulib/string.h b/include/ulib/string.h index f0ade0ac..0b6f3333 100644 --- a/include/ulib/string.h +++ b/include/ulib/string.h @@ -2698,6 +2698,37 @@ inline bool operator>=(const UString& lhs, const UString& rhs) { return lh inline bool operator>=(const char* lhs, const UString& rhs) { return rhs.compare(lhs) <= 0; } inline bool operator>=(const UString& lhs, const char* rhs) { return lhs.compare(rhs) >= 0; } +// template specialization for UString2Object() and UObject2String() + +template <> inline void UString2Object(const char* t, uint32_t tlen, UString& object) +{ + U_TRACE(0, "UString2Object(%.*S,%u,%V)", tlen, t, tlen, object.rep) + + object.setConstant(t, tlen); +} + +template <> inline char* UObject2String(UString& object) +{ + U_TRACE(0, "UObject2String(%V)", object.rep) + + U_INTERNAL_ASSERT(object.isNullTerminated()) + + return object.data(); +} + +template <> inline uint32_t UObject2String(UString& object, char* pbuffer, uint32_t buffer_size) +{ + U_TRACE(0, "UObject2String(%V,%p,%u)", object.rep, pbuffer, buffer_size) + + uint32_t sz = object.size(); + + U_INTERNAL_ASSERT_MINOR(sz, buffer_size) + + U_MEMCPY(pbuffer, object.data(), sz); + + return sz; +} + // by Victor Stewart #if defined(U_STDCPP_ENABLE) && defined(HAVE_CXX11) diff --git a/include/ulib/utility/uhttp.h b/include/ulib/utility/uhttp.h index bd1c3744..764b7960 100644 --- a/include/ulib/utility/uhttp.h +++ b/include/ulib/utility/uhttp.h @@ -673,8 +673,8 @@ public: static bool getDataStorage(uint32_t index, UString& value); static bool getDataSession(uint32_t index, UString& value); - static void putDataStorage(); - static void putDataSession(); + static void putDataSTORAGE(); + static void putDataSESSION(); static void putDataStorage(uint32_t index, const char* val, uint32_t sz); static void putDataSession(uint32_t index, const char* val, uint32_t sz); diff --git a/src/ulib/net/server/plugin/usp/usp_translator.cpp b/src/ulib/net/server/plugin/usp/usp_translator.cpp index 8207d028..c9735660 100644 --- a/src/ulib/net/server/plugin/usp/usp_translator.cpp +++ b/src/ulib/net/server/plugin/usp/usp_translator.cpp @@ -103,43 +103,50 @@ public: U_INTERNAL_DUMP("token = %V", token.rep) } - void manageDirectiveSessionOrStorage(const char* name) + void manageDirectiveSessionOrStorage(const char* name, uint32_t name_len) { - U_TRACE(5, "Application::manageDirectiveSessionOrStorage(%S)", name) + U_TRACE(5, "Application::manageDirectiveSessionOrStorage(%.*S,%u)", name_len, name, name_len) - U_INTERNAL_ASSERT(token) + (void) output0.reserve(500U + token.size()); + (void) output1.reserve(500U); - UString id, tmp; - const char* ptr; - uint32_t pos, size; - UVector vec(token, "\t\n;"); + output0.snprintf_add(U_CONSTANT_TO_PARAM("\n\t%v\n\t\n\tif (usp_b%.*s) {\n"), token.rep, name_len, name); + output1.snprintf_add(U_CONSTANT_TO_PARAM( "\n\tif (usp_b%.*s) {\n"), name_len, name); - bvar = true; - - (void) output0.reserve(20U + token.size()); - - output0.snprintf_add(U_CONSTANT_TO_PARAM("\n\t%v\n\t\n"), token.rep); - - for (uint32_t i = 0, n = vec.size(); i < n; ++i) + if (token.empty()) output0.snprintf_add(U_CONSTANT_TO_PARAM("\n\tUHTTP::putData%.*s();\n"), name_len, name); + else { - ptr = (tmp = UStringExt::trim(vec[i])).data(); + UString id, tmp; + const char* ptr; + uint32_t pos, size; + UVector vec(token, "\t\n;"); - do { ++ptr; } while (u__isspace(*ptr) == false); - do { ++ptr; } while (u__isspace(*ptr) == true); + bvar = true; - pos = (id = tmp.substr(tmp.distance(ptr))).find('('); - size = (pos == U_NOT_FOUND ? id.size() : pos); + for (uint32_t i = 0, n = vec.size(); i < n; ++i) + { + ptr = (tmp = UStringExt::trim(vec[i])).data(); - (void) output0.reserve(50U + size); - (void) output1.reserve(50U + size); + do { ++ptr; } while (u__isspace(*ptr) == false); + do { ++ptr; } while (u__isspace(*ptr) == true); - output0.snprintf_add(U_CONSTANT_TO_PARAM("\n\tif (usp_b%s) USP_%s_VAR_GET(%u,%.*s);\n"), name, name, i, size, ptr); - output1.snprintf_add(U_CONSTANT_TO_PARAM("\n\tif (usp_b%s) USP_%s_VAR_PUT(%u,%.*s);\n"), name, name, i, size, ptr); + pos = (id = tmp.substr(tmp.distance(ptr))).find('('); + size = (pos == U_NOT_FOUND ? id.size() : pos); -# ifdef DEBUG - id.clear(); // NB: to avoid DEAD OF SOURCE STRING WITH CHILD ALIVE... -# endif + (void) output0.reserve(50U + size); + (void) output1.reserve(50U + size); + + output0.snprintf_add(U_CONSTANT_TO_PARAM("\n\tUSP_%.*s_VAR_GET(%u,%.*s);\n"), name_len, name, i, size, ptr); + output1.snprintf_add(U_CONSTANT_TO_PARAM("\n\tUSP_%.*s_VAR_PUT(%u,%.*s);\n"), name_len, name, i, size, ptr); + +# ifdef DEBUG + id.clear(); // NB: to avoid DEAD OF SOURCE STRING WITH CHILD ALIVE... +# endif + } } + + output0.snprintf_add(U_CONSTANT_TO_PARAM( "\n\t}\n\tusp_b%.*s = true;\n"), name_len, name); + output1.snprintf_add(U_CONSTANT_TO_PARAM("\n\tUHTTP::putData%.*s();\n\t}\n\tusp_b%.*s = true;\n"), name_len, name, name_len, name); } void manageDirectiveArgsOrCpath(const char* name, bool binc) @@ -152,7 +159,7 @@ public: UString id, tmp; UVector vec(token, "\t\n;"); - (void) output0.reserve(200U + token.size()); + (void) output0.reserve(500U + token.size()); for (uint32_t i = 0, n = vec.size(); i < n; ++i) { @@ -307,13 +314,7 @@ public: setDirectiveItem(directive, U_CONSTANT_SIZE("session")); - if (token) manageDirectiveSessionOrStorage("SESSION"); - else - { - (void) output1.append(U_CONSTANT_TO_PARAM("\n\tif (usp_bSESSION) UHTTP::putDataSession();\n")); - } - - (void) output0.append(U_CONSTANT_TO_PARAM("\n\tusp_bSESSION = true;\n")); + manageDirectiveSessionOrStorage(U_CONSTANT_TO_PARAM("SESSION")); } else if (strncmp(directive, U_CONSTANT_TO_PARAM("storage")) == 0) { @@ -326,13 +327,7 @@ public: setDirectiveItem(directive, U_CONSTANT_SIZE("storage")); - if (token) manageDirectiveSessionOrStorage("STORAGE"); - else - { - (void) output1.append(U_CONSTANT_TO_PARAM("\n\ttif (usp_bSTORAGE) UHTTP::putDataStorage();\n")); - } - - (void) output0.append(U_CONSTANT_TO_PARAM("\n\tusp_bSTORAGE = true;\n")); + manageDirectiveSessionOrStorage(U_CONSTANT_TO_PARAM("STORAGE")); } else if (strncmp(directive, U_CONSTANT_TO_PARAM("args")) == 0) { @@ -896,13 +891,15 @@ loop: distance = t.getDistance(); " { \\\n" " UString2Object(varname##_value.data(), usp_sz, varname); \\\n" " } \\\n" - " U_INTERNAL_DUMP(\"%s(%u) = %.*S\", #varname, usp_sz, usp_sz, varname##_value.data()) \\\n" + " U_INTERNAL_DUMP(\"%s(%u) = %V\", #varname, usp_sz, varname##_value.rep) \\\n" " }" "\n\t\n" "#define USP_SESSION_VAR_PUT(index,varname) \\\n" " { \\\n" " usp_sz = UObject2String(varname, usp_buffer, sizeof(usp_buffer)); \\\n" - " UHTTP::putDataSession(index, usp_buffer, usp_sz); \\\n" + " UString varname##_value((void*)usp_buffer, usp_sz); \\\n" + " UHTTP::data_session->putValueVar(index, varname##_value); \\\n" + " U_INTERNAL_DUMP(\"%s(%u) = %V\", #varname, usp_sz, varname##_value.rep) \\\n" " }\n")); } @@ -919,13 +916,15 @@ loop: distance = t.getDistance(); " { \\\n" " UString2Object(varname##_value.data(), usp_sz, varname); \\\n" " } \\\n" - " U_INTERNAL_DUMP(\"%s(%u) = %.*S\", #varname, usp_sz, usp_sz, varname##_value.data()) \\\n" + " U_INTERNAL_DUMP(\"%s(%u) = %V\", #varname, usp_sz, varname##_value.rep) \\\n" " }" "\n\t\n" "#define USP_STORAGE_VAR_PUT(index,varname) \\\n" " { \\\n" " usp_sz = UObject2String(varname, usp_buffer, sizeof(usp_buffer)); \\\n" - " UHTTP::putDataStorage(index, usp_buffer, usp_sz); \\\n" + " UString varname##_value((void*)usp_buffer, usp_sz); \\\n" + " UHTTP::data_storage->putValueVar(index, varname##_value); \\\n" + " U_INTERNAL_DUMP(\"%s(%u) = %V\", #varname, usp_sz, varname##_value.rep) \\\n" " }\n")); } diff --git a/src/ulib/utility/data_session.cpp b/src/ulib/utility/data_session.cpp index 18f36446..1438112b 100644 --- a/src/ulib/utility/data_session.cpp +++ b/src/ulib/utility/data_session.cpp @@ -114,7 +114,7 @@ void UDataSession::fromStream(istream& is) # ifdef DEBUG const char* UDataStorage::dump(bool reset) const { - *UObjectIO::os << "keyid (UString " << (void*)&keyid << ')'; + *UObjectIO::os << "keyid (UString " << (void*)&keyid << ')'; if (reset) { @@ -131,9 +131,9 @@ const char* UDataSession::dump(bool reset) const UDataStorage::dump(false); *UObjectIO::os << '\n' - << "creation " << creation << '\n' - << "last_access " << last_access << '\n' - << "vec_var (UVector " << (void*)&vec_var << ')'; + << "creation " << creation << '\n' + << "last_access " << last_access << '\n' + << "vec_var (UVector " << (void*)&vec_var << ')'; if (reset) { diff --git a/src/ulib/utility/uhttp.cpp b/src/ulib/utility/uhttp.cpp index 5c2d8241..a2cbb4e7 100644 --- a/src/ulib/utility/uhttp.cpp +++ b/src/ulib/utility/uhttp.cpp @@ -6039,9 +6039,9 @@ bool UHTTP::getDataSession(uint32_t index, UString& value) U_RETURN(false); } -void UHTTP::putDataSession() +void UHTTP::putDataSESSION() { - U_TRACE_NO_PARAM(0, "UHTTP::putDataSession()") + U_TRACE_NO_PARAM(0, "UHTTP::putDataSESSION()") U_INTERNAL_ASSERT_POINTER(db_session) U_INTERNAL_ASSERT_POINTER(data_session) @@ -6060,17 +6060,17 @@ void UHTTP::putDataSession(uint32_t index, const char* value, uint32_t size) if (size == 0) data_session->putValueVar(index, UString::getStringNull()); else { - UString _value((void*)value, size); + UString x((void*)value, size); - data_session->putValueVar(index, _value); + data_session->putValueVar(index, x); } - putDataSession(); + putDataSESSION(); } -void UHTTP::putDataStorage() +void UHTTP::putDataSTORAGE() { - U_TRACE_NO_PARAM(0, "UHTTP::putDataStorage()") + U_TRACE_NO_PARAM(0, "UHTTP::putDataSTORAGE()") U_INTERNAL_ASSERT_POINTER(db_session) U_INTERNAL_ASSERT_POINTER(data_storage) @@ -6095,7 +6095,7 @@ void UHTTP::putDataStorage(uint32_t index, const char* value, uint32_t size) data_storage->putValueVar(index, _value); } - putDataStorage(); + putDataSTORAGE(); } #ifdef DEBUG diff --git a/tests/examples/TSA/tsaserial b/tests/examples/TSA/tsaserial index d5d19647..11b731e0 100644 --- a/tests/examples/TSA/tsaserial +++ b/tests/examples/TSA/tsaserial @@ -1 +1 @@ -0366 +036D diff --git a/tests/examples/web_server.sh b/tests/examples/web_server.sh index d17e426c..c352032c 100755 --- a/tests/examples/web_server.sh +++ b/tests/examples/web_server.sh @@ -19,7 +19,7 @@ rm -f tmp/usp_compile.sh.err /tmp/*.hpack.* \ $DOC_ROOT/trace.*userver_*.[0-9]* $DOC_ROOT/object.*userver_*.[0-9]* $DOC_ROOT/stack.*userver_*.[0-9]* $DOC_ROOT/mempool.*userver_*.[0-9]* UTRACE="0 50M 0" - UTRACE_SIGNAL="0 50M -1" +#UTRACE_SIGNAL="0 50M -1" UTRACE_FOLDER=/tmp TMPDIR=/tmp #UOBJDUMP="0 10M 100" @@ -73,7 +73,8 @@ userver { #LOAD_BALANCE_CLUSTER 10.30.0.1,10.30.0.2 #LOAD_BALANCE_DEVICE_NETWORK enp0s20u1 #LOAD_BALANCE_LOADAVG_THRESHOLD 4.0 - PREFORK_CHILD 2 + PREFORK_CHILD 0 +#PREFORK_CHILD 2 #CRASH_COUNT 1 #CRASH_EMAIL_NOTIFY mail.unirel.com:stefano.casazza@unirel.com #DOS_SITE_COUNT 1