WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: JTaylor on April 29, 2017, 06:09:45 PM

Title: Copy Functions From Winsock to Main DLL
Post by: JTaylor on April 29, 2017, 06:09:45 PM
Not sure if it is just me but something I would find VERY helpful would be to copy the urlEncode, urlDecode and httpStripHTML functions from the Winsock extender to the main WinBatch DLL.   Not sure why I haven't asked before (maybe I have???) but I have to include the Winsock Extender to projects for no other reason than these three functions on a regular basis.  Would be nice to have access to them directly.

Thanks.

Jim
Title: Re: Copy Functions From Winsock to Main DLL
Post by: bottomleypotts on May 17, 2017, 04:40:32 AM
Does this help?

#DefineFunction JS_encodeURI(uri)
o=CreateObject(`MSScriptControl.ScriptControl`)
o.Language=`JScript`
Ret=o.Eval(:`encodeURI("`:StrReplace(uri,`"`,`\"`):`")`)
Return StrCat(Ret)
#EndFunction

#DefineFunction JS_decodeURI(enc)
o=CreateObject(`MSScriptControl.ScriptControl`)
o.Language=`JScript`
Ret=o.Eval(:`decodeURI("`:enc:`")`)
Return StrCat(Ret)
#EndFunction
Title: Re: Copy Functions From Winsock to Main DLL
Post by: JTaylor on May 17, 2017, 05:18:46 AM
Thanks.
Title: Re: Copy Functions From Winsock to Main DLL
Post by: kdmoyers on May 17, 2017, 05:23:03 AM
Thanks!
Title: Re: Copy Functions From Winsock to Main DLL
Post by: td on May 19, 2017, 02:06:23 PM
For us burger flippers there is always:

Code (winbatch) Select
ObjectClrOption("useany","System.Web")
objHttpUtil = ObjectClrNew("System.Web.HttpUtility")

strUrl ="name=Joe Smoe&discount=10%%"
strEncoded = objHttpUtil.UrlEncode(strUrl)
strDecoded = objHttpUtil.UrlDecode(strEncoded)
Title: Re: Copy Functions From Winsock to Main DLL
Post by: td on May 19, 2017, 03:06:04 PM
You can always role your own:

Code (winbatch) Select
;; Not debugged!
#DefineFunction UrlEncode(_strUrl)
   aUrl = ArrayFromStr(_strURl)
   nMax = ArrInfo(aUrl, 1) - 1
   
   strReturn = ''
   for i = 0 to nMax
      switch 1
         case ' ' == aUrl[i]
            strReturn := '+'
            break
         case '@' == aUrl[i]
         case '*' == aUrl[i]
         case '_' == aUrl[i]
         case '-' == aUrl[i]
         case '.' == aUrl[i]
            strReturn := aUrl[i]   
            break
         case 1
            if StrTypeInfo(aUrl[i], 0) & 260 ; Digit or alpha.
               strReturn := aUrl[i]
            else
               strReturn := "%%":ChrStringToHex(aUrl[i])
            endif
      endswitch
   next

   return strReturn
#EndFunction

strUrl ="name=Joe Smoe&discount=10%%"
strEncoded = UrlEncode(strUrl)
Title: Re: Copy Functions From Winsock to Main DLL
Post by: td on May 20, 2017, 09:43:10 AM
Forgot the decode function:

Code (winbatch) Select
;; Not debugged!
#DefineFunction UrlDecode(_strUrl)
   aUrl = ArrayFromStr(_strURl)
   nMax = ArrInfo(aUrl, 1) - 1
   
   strReturn = ''
   for i = 0 to nMax
      switch 1
         case '+' == aUrl[i]
            strReturn := ' '
            break
         case '%' == aUrl[i]
            if i + 2 > nMax  then return '' ; Invalid URL.
            i += 1   ; Unwound loop for speed.
            strHex = aUrl[i]
            i += 1
            strHex := aUrl[i]
            nMode = ErrorMode(@off)
            LastError()
            strReturn := ChrHexToString(strHex)
            ErrorMode(nMode)
            if LastError() then return '' ; Invalid URL.
            break   
         case 1
            strReturn := aUrl[i]
      endswitch
   next

   return strReturn
#EndFunction
Title: Re: Copy Functions From Winsock to Main DLL
Post by: td on May 21, 2017, 09:57:09 AM
This UDF doesn't handle Javascript but then neither does httpStripHTML

Code (winbatch) Select
;; Not debugged!
#DefineFunction HtmlStrip(_strHtml)
   strReturn = ''
   nIndex = 1
   nLt = StrIndex(_strHtml, '<', nIndex, @Fwdscan)   
   nGt = StrIndex(_strHtml, '>', nIndex, @Fwdscan)   
   
   while 1
      if (!nLt && nGt) || (nGt && (nGt < nLt) ) ; > before any <.
         nIndex = nGt + 1
         nGt = StrIndex(_strHtml, '>', nIndex, @Fwdscan)
      elseif nLt && nGt && (nLt < nGt) ; < before >.
         strReturn := StrSub(_strHtml, nIndex, nLt - nIndex)
         nIndex = nGt + 1
         nLt = StrIndex(_strHtml, '<', nIndex, @Fwdscan)   
         nGt = StrIndex(_strHtml, '>', nIndex, @Fwdscan)   
      elseif nLt && (!nGt) ; < but no >
         strReturn := StrSub(_strHtml, nIndex, nLt - nIndex)
         break
      else  ; Only plan text left.
         strReturn := StrSub(_strHtml, nIndex, -1)       
         break
      endif
   endwhile

   return strReturn
#EndFunction

strHtml = FileGet('C:\website\index.html')
strStripped = HtmlStrip(strHtml)
Pause('Stripped HTML',strStripped)
Title: Re: Copy Functions From Winsock to Main DLL
Post by: JTaylor on May 21, 2017, 11:38:15 AM
Thanks. 

Jim
Title: Re: Copy Functions From Winsock to Main DLL
Post by: stanl on May 22, 2017, 04:49:55 AM
Quote from: td on May 21, 2017, 09:57:09 AM
This UDF doesn't handle Javascript

Interesting. Be nice to start a conversation about what is important with web-scraping.
Title: Re: Copy Functions From Winsock to Main DLL
Post by: td on May 22, 2017, 06:47:09 AM
Quote from: JTaylor on May 21, 2017, 11:38:15 AM
Thanks. 


Noticed a glaring flaw in the UrlDecode UDF.   The line

Code (winbatch) Select
if i > nMax + 2 then return '' ; Invalid URL.

should be

Code (winbatch) Select
if i + 2 > nMax then return '' ; Invalid URL.

Sorry about that.
Title: Re: Copy Functions From Winsock to Main DLL
Post by: kdmoyers on May 30, 2017, 11:33:50 AM
Got it.  Thanks!