URLEncode Question

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Joe F.
Posts: 17
Joined: Thu Jul 16, 2009 11:37 pm

URLEncode Question

Post by Joe F. » Sat Jul 25, 2009 6:27 am

Okay, I'm new to Rev but an old hand at Hypercard.
Maybe I'm missing something, but why does URLEncode convert spaces to + instead of %20?
As far as I know most systems only accept + in the string after the URL.

And why would it bother to convert forward slashes?

Is there a way to fix the function, besides rolling my own post-conversion-conversion?
ie. can I replace the entries in whatever lookup table the function uses?

Thanks,
Joe F.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Jul 25, 2009 11:16 am

Hi Joe,

Most, if not all, servers convert the plus sign in form data back to a space. Using the plus sign is more efficient than using %20.

The slash is converted to make sure that the server reads it as a slash rather than a delimiter in the directory path. This is necessary for the server to read the data correctly.

It would be wrong to "fix" the function. Whenever you are preparing a url to send data to a cgi or php script, you need to make a distinction between the base url and the data being sent.

For example, if you want to send "hello world" to www.server.com?q= then you don't want to encode everything up to the equal sign, because if you encoded the equal sign, the server wouldn't understand your query. If your query contains multiple parts, e.q. "hello world" and "©2009" your need to encode these parts separately, when assembling the url:

Code: Select all

put "http://www.server.com?q=" & urlencode("hello world") & "&c=" & urlencode("©2009") into myUrl
If you encoded the whole thing at once, you would get gibberish that neither your browser nor the put url command know what to do with.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Joe F.
Posts: 17
Joined: Thu Jul 16, 2009 11:37 pm

Post by Joe F. » Sat Jul 25, 2009 4:26 pm

Okay, fair enough.
But then "URLEncode" isn't quite what it's name (and dictionary description) would suggest. It's really POST_ARGSEncode.

What I want to do is use the revbrowser window within my app to view local as well as server based files.

On the Mac, dragging a local file to the window works perfectly, translating the filepath as expected with %20 spaces and leaving slashes alone. But I suspect that the Safari engine is responsible for that.

But doing it from a script is another story.
Then, even with doing all my own parsing and sending it to the browser I'm getting WindowID errors.

I thought I was understanding the difference between AltBrowser and RevBrowser, but apparently not. Too bad none of that code is commented. I thought this was going to be easy!

Joe F.
Posts: 17
Joined: Thu Jul 16, 2009 11:37 pm

Post by Joe F. » Sat Jul 25, 2009 4:35 pm

Rude of me to not say thanks for the pointer (and sanity check).

I realize these things after my second cup of coffee!

Cheers

Joe F.
Posts: 17
Joined: Thu Jul 16, 2009 11:37 pm

Solved

Post by Joe F. » Sun Jul 26, 2009 2:57 am

I always hate finding a forum post where someone had the exact problem I'm having but never followed up with posting the solution. So...

Code: Select all

on mouseUp
   answer file "Select a file:"
   put URLEncode (it) into tMyFileLoc
   replace "%2F" with "/" in tMyFileLoc
   replace "+" with "%20" in tMyFileLoc
   put "file://"&(tMyFileLoc) into tMyFileLoc
     if "InetBrowser" is among the lines of the openStacks then
      close stack "InetBrowser" of stack "MainStackNameHere"
     end if
   set the altHomeURL of stack "InetBrowser" to tMyFileLoc
   go stack "InetBrowser" of stack "MainStackNameHere"
end mouseUp
I have Strict Compilation Mode turned off in the prefs.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Jul 26, 2009 8:06 am

Hi Joe,

Your script will only work with your stack "InetBrowser". I really wonder why you would have to "encode" this URL. The following works fine for me, without encoding the URL (this is a real example from a stack of mine):

Code: Select all

global gBrowserID
on mouseUp
     answer file "" with type "HTML file|html|HTML"
     if it is not empty then
          put "file:///" & it into fld "Url"
          if gbrowserID is not empty then
               xBrowser_Navigate fld "Url",gBrowserID
          else
               xBrowser_Open the windowID of this stack,fld "Url"
               put the result into gBrowserID
          end if
            put the rect of this cd into myRect
            add 24 to item 2 of myRect
            subtract 14 from item 4 of myRect
            xbrowser_Set "Rect",myRect
     end if
end mouseUp
Even the fact that on Mac OS X the browser contains four instead of three slashes at the start doesn't seem to matter.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Joe F.
Posts: 17
Joined: Thu Jul 16, 2009 11:37 pm

Post by Joe F. » Wed Jul 29, 2009 5:29 am

Hi Mark,

Well, the "InetBrowser" is a copy of the stack included within "browser_sampler.rev".
They say "you can just copy this into your own stack...", unfortunately it seems you're on your own when it comes to figuring out how it works. The code isn't commented at all and it uses lots of undocumented stuff like the custom property "altHomeURL". There's even a filter to disallow "xxx.com"

In my example the web browser can be used to open any file that the browser engine can handle, so PDF or text or jpg, media files, even a page with a revlet on it.

You have to undo the conversion of spaces and slashes done by URLEncode because the browser won't open the files otherwise. A folder or file with a space in its name gets encoded with "+" for the spaces, and the browser won't accept those. That's what I was originally questioning, and it seems that the function is actually intended only to be used in the HTTP_ARGS.

On the Mac you need "file:///dir/dir/file.wtf ". The answer command returns the filepath with a leading slash denoting either the home directory or system directory, depending on which switch you use. So you only need the standard 2 for a protocol.

I'm new to all this and it's confusing; especially when I get it working perfectly on the Mac and take a standalone to a Vista PC where it throws errors left and right.

I actually have my old Hypercard implementation of all the parsing stuff, which I was hoping would be covered better in Rev, but it looks like I'm going to have to dig it out and dust it off. Maybe once I learn a bit more I can turn it into an add-on library that others can use.

What the browser stack really needs is the kind of documentation they did for data grids. I think it's a screen steps thing? The data grids is the same deal: great functionality but you've got to figure out the language it's strung with. That screen steps page has all the answers for the DG; I wish they had the same for the browser.

Thanks for the tips,
Joe F.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Jul 29, 2009 9:28 am

Hi Joe,

You haven't told me which version of Revolution you are using. It seems you are focussing on the very old and obsolute version of Altuit, whilte a new version has been released by Runtime Revolution several years ago already. If you have a newer version, you should just have a look in the dictionary and search for "brows".

I have just created an application, which automatically loads a local html file every 15 minutes. For this, I did the following.

Code: Select all

global gBrowserID
put the effective filename of this stack into myPath
replace space with "%20" in myPath
put "file//" & myPath into myUrl
if gBrowserID is empty then
  put revOpenBrowser(the windowID of this stack,myUrl) into myTempID
  if myTempID is an integer then
    put myTempID into gBrowserID
  end if
else
  revBrowserNavigate gBrowserID,myUrl
end if
(Please, mind typos)

If you send me an e-mail (see profile for contact information) I'll send you the stack.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Joe F.
Posts: 17
Joined: Thu Jul 16, 2009 11:37 pm

Post by Joe F. » Fri Jul 31, 2009 7:22 am

Hi Mark,
I'm up to date with the latest 3.5, and using the DP3 version of 4.
I was wondering about the xBrowser command on your first script because they say they're deprecated.

I actually wound up doing something very similar to your second script because I found I needed to have globals for the browser instance and all that.

The "altHomeURL" is a custom property that they used in the demo stack.

Once I got rid of the demo stack and made my own everything worked the way I expected. Now if it works in the standalone on a Vista PC...

Post Reply