Copy Functions From Winsock to Main DLL

Started by JTaylor, April 29, 2017, 06:09:45 PM

Previous topic - Next topic

JTaylor

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

bottomleypotts

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

JTaylor


kdmoyers

The mind is everything; What you think, you become.

td

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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor


stanl

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.

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

The mind is everything; What you think, you become.