From 410dcdf17255f52dab164498cdfb905f50f85870 Mon Sep 17 00:00:00 2001 From: Christopher Edwards Date: Thu, 21 Feb 2019 13:43:17 -0500 Subject: [PATCH] [#29] Remove global usage Add more checks to webserver code in places where globals are used. This removes debugging and logging support when the application is running. Setting the NOGBL flag to false will enable the debugging and logging again. In the future the web server may log to stdout: [https://github.com/shabiel/M-Web-Server/issues/23] Not all places that use globals have NOGBL checks, only the places where it is used in the GDE GUI application. Delete GDUSEDEFS error from error array as it isn't a real errror that should prevent GDE from running. This allows running the GUI against a system that only has ydb_gbldir set with no gde file created --- r/GDEWEB.m | 4 +++- webserver/_webapi.m | 4 ++-- webserver/_webhome.m | 4 +++- webserver/_webreq.m | 18 ++++++++++-------- webserver/_webrsp.m | 3 ++- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/r/GDEWEB.m b/r/GDEWEB.m index 0affc65..44c607f 100644 --- a/r/GDEWEB.m +++ b/r/GDEWEB.m @@ -55,7 +55,7 @@ ; Start the web server i $l($t(^%webreq)) d . w "Starting Web Server...",! - . d job^%webreq($g(portnum,8080),$s(ssl:"ydbgui",1:""),,$g(userpass)) + . d job^%webreq($g(portnum,8080),$s(ssl:"ydbgui",1:""),1,$g(userpass)) e d . w "Web server code not found in $zroutines, please make sure $zroutines is set correctly!",! quit @@ -523,6 +523,8 @@ s gdeEntryState("io")=$io d GDEINIT^GDEINIT,GDEMSGIN^GDEMSGIN,GDFIND^GDESETGD,CREATE^GDEGET:create,LOAD^GDEGET:'create s useio="io" s io=$io + ; Using the GDE Defaults isn't an error. Kill it so the webservices can move on + i ($g(gdeweberror("count"))=1),gdeweberror(1)["%GDE-I-GDUSEDEFS" k gdeweberror quit ; ; ========================================================================= diff --git a/webserver/_webapi.m b/webserver/_webapi.m index b173be6..799e383 100644 --- a/webserver/_webapi.m +++ b/webserver/_webapi.m @@ -263,8 +263,8 @@ S XWB(2,"RPC")=RP N ISCACHE S ISCACHE=$L($SY,":")=2 N PATH ; - ; Vhere is our home? If any home! - if $get(^%webhome)'="" D + ; Where is our home? If any home! + if ('$G(NOGBL)),($get(^%webhome)'="") D . I ISGTM S $ZD=^%webhome ; GT.M . I ISCACHE N % S %=$ZU(168,^%webhome) ; Cache ; diff --git a/webserver/_webhome.m b/webserver/_webhome.m index 8e12227..0772758 100644 --- a/webserver/_webhome.m +++ b/webserver/_webhome.m @@ -7,9 +7,11 @@ N ARGS S ARGS("*")="index.html" ; Retrieve index.html from filesystem before returning default page D FILESYS^%webapi(.RESULT,.ARGS) - I $D(^TMP("HTTPERR",$J)) K ^TMP("HTTPERR",$J),HTTPERR,RESULT + I ('$G(NOGBL)),$D(^TMP("HTTPERR",$J)) K ^TMP("HTTPERR",$J),HTTPERR,RESULT ; If we found an index.html don't return the default I $D(RESULT) QUIT + ; If we are in no global mode quit as well as the below loop won't tell us anything + I $G(NOGBL) S RESULT(1)="NO INDEX FOUND!" QUIT ; return default index.html S RESULT("mime")="text/html; charset=utf-8" N I F I=1:1 S RESULT(I)=$P($TEXT(HTML+I),";;",2,99) Q:RESULT(I)="" D diff --git a/webserver/_webreq.m b/webserver/_webreq.m index 47d9d70..3aea5c6 100644 --- a/webserver/_webreq.m +++ b/webserver/_webreq.m @@ -57,7 +57,7 @@ D job(PORT) I $G(DEBUG) D DEBUG($G(TLSCONFIG)) ; LOOP ; wait for connection, spawn process to handle it. GOTO favorite. - I '$G(NOGBL)&$E(^%webhttp(0,"listener"),1,4)="stop" C TCPIO S ^%webhttp(0,"listener")="stopped" Q + I '$G(NOGBL),$E(^%webhttp(0,"listener"),1,4)="stop" C TCPIO S ^%webhttp(0,"listener")="stopped" Q ; ; ---- CACHE CODE ---- I %WOS="CACHE" D G LOOP @@ -75,10 +75,10 @@ D job(PORT) . ; . ; Wait until we have a connection (inifinte wait). . ; Stop if the listener asked us to stop. - . FOR W /WAIT(10) Q:$KEY]"" Q:('$G(NOGBL)&$E(^%webhttp(0,"listener"),1,4)="stop") + . FOR W /WAIT(10) Q:$KEY]"" Q:$G(NOGBL) Q:($E(^%webhttp(0,"listener"),1,4)="stop") . ; . ; We have to stop! When we quit, we go to loop, and we exit at LOOP+1 - . I '$G(NOGBL)&$E(^%webhttp(0,"listener"),1,4)="stop" QUIT + . I '$G(NOGBL),$E(^%webhttp(0,"listener"),1,4)="stop" QUIT . ; . ; At connection, job off the new child socket to be served away. . ; I $P($KEY,"|")="CONNECT" QUIT ; before 6.1 @@ -146,7 +146,8 @@ D job(PORT) ; I %WOS="GT.M",'$G(NOGBL),$G(TRACE) VIEW "TRACE":1:"^%wtrace" ; Tracing for Unit Test Coverage ; - S HTTPLOG=$G(^%webhttp(0,"logging"),0) ; HTTPLOG remains set throughout + S:'$G(NOGBL) HTTPLOG=$G(^%webhttp(0,"logging"),0) ; HTTPLOG remains set throughout + S:$G(NOGBL) HTTPLOG=0 S HTTPLOG("DT")=+$H D INCRLOG ; set unique request id for log N $ET S $ET="G ETSOCK^%webreq" @@ -170,7 +171,7 @@ S HTTPLOG("DT")=+$H K:'$G(NOGBL) ^TMP($J),^TMP("HTTPERR",$J) ; TODO: change the namespace for the error global ; WAIT ; wait for request on this connection - I '$G(NOGBL)&$E($G(^%webhttp(0,"listener")),1,4)="stop" C %WTCP Q + I '$G(NOGBL),$E($G(^%webhttp(0,"listener")),1,4)="stop" C %WTCP Q X:%WOS="CACHE" "U %WTCP:(::""CT"")" ;VEN/SMH - Cache Only line; Terminators are $C(10,13) X:%WOS="GT.M" "U %WTCP:(delim=$C(13,10):chset=""M"")" ; VEN/SMH - GT.M Delimiters R TCPX:10 I '$T G ETDC @@ -284,8 +285,8 @@ S HTTPREQ("query")=$P($P(TCPX," ",2),"?",2,999) I $D(%WNULL) C %WNULL U %WTCP D LOGERR - D SETERROR^%webutils(501,"Log ID:"_HTTPLOG("ID")) ; sets HTTPERR - D RSPERROR^%webrsp ; switch to error response + D:'$G(NOGBL) SETERROR^%webutils(501,"Log ID:"_HTTPLOG("ID")) ; sets HTTPERR + D:'$G(NOGBL) RSPERROR^%webrsp ; switch to error response D SENDATA^%webrsp ; Leave $ECODE as non-null so that the error handling continues. ; This next line will 'unwind' the stack and got back to listening @@ -364,10 +365,11 @@ S HTTPLOG("ID")=ID QUIT ; LOGERR ; log error information + Q:$G(NOGBL) N %D,%I S %D=HTTPLOG("DT"),%I=HTTPLOG("ID") N ISGTM S ISGTM=$P($SYSTEM,",")=47 - S:'$G(NOGBL) ^%webhttp("log",%D,$J,%I,"error")=$S(ISGTM:$ZSTATUS,1:$ZERROR_" ($ECODE:"_$ECODE_")") + S ^%webhttp("log",%D,$J,%I,"error")=$S(ISGTM:$ZSTATUS,1:$ZERROR_" ($ECODE:"_$ECODE_")") N %LVL,%TOP,%N S %TOP=$STACK(-1)-1,%N=0 F %LVL=0:1:%TOP S %N=%N+1,^%webhttp("log",%D,$J,%I,"error","stack",%N)=$STACK(%LVL,"PLACE")_":"_$STACK(%LVL,"MCODE") diff --git a/webserver/_webrsp.m b/webserver/_webrsp.m index 01be911..ce705d2 100644 --- a/webserver/_webrsp.m +++ b/webserver/_webrsp.m @@ -110,7 +110,7 @@ D QSPLIT(HTTPREQ("query"),.HTTPARGS) I $G(HTTPERR) QUIT S ROUTINE="" ; Default. Routine not found. Error 404. ; ; If we have the %W file for mapping... - IF $D(^%web(17.6001)) DO MATCHF(.ROUTINE,.ARGS,.PARAMS,.AUTHNODE) + IF ('$G(NOGBL)),$D(^%web(17.6001)) DO MATCHF(.ROUTINE,.ARGS,.PARAMS,.AUTHNODE) ; ; Using built-in table if routine is still empty. I ROUTINE="" DO MATCHR(.ROUTINE,.ARGS) @@ -363,6 +363,7 @@ D W("Content-Length: "_SIZE_$C(13,10)_$C(13,10)) QUIT ; RSPERROR ; set response to be an error response + Q:$G(NOGBL) D encode^%webjson("^TMP(""HTTPERR"",$J,1)","^TMP(""HTTPERR"",$J,""JSON"")") S HTTPRSP="^TMP(""HTTPERR"",$J,""JSON"")" K HTTPRSP("pageable")