dzone_thumbWould you like to create your own protocol similar to http:// ? Why would you like to do so ? Because you will save a lot of time by implementing it the way you really want as the protocol is just an interface and the rest is up to you.

So for a pure exercice let’s create a dzone:// protocol. If one types for example dzone://refcardz in a browser, it would go to http://refcardz.dzone.com. Here’s how on Windows - sorry on Linux it’s not as easy - as explained on past Devx article: “Build Simple Asynchronous Pluggable Protocols for Windows”.

First create this Dzone.reg file with this content:


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\dzone]
@="\"URL:dzone Protocol\""
"URL Protocol"=""

[HKEY_CLASSES_ROOT\dzone\shell]

[HKEY_CLASSES_ROOT\dzone\shell\open]

[HKEY_CLASSES_ROOT\dzone\shell\open\command]
@="d:\\program files\\rebol\\rebol.exe -s C:\\customprotocol\\dzone-protocol.r %1"

Of course substitute the paths above with your own then execute this registry file with Regedit.

Create this rebol script


rebol[]

args: system/script/args
obj-args: decode-url system/script/args
host: obj-args/host
target: obj-args/target

;probe allows to print the value of a variable with its type whereas print will convert to string
probe args
probe obj-args
probe host
probe target
input

Test dzone: in your browser, the result should be:

Once you have tested that your protocol is correct, you can comment the probe lines with “;”.
Now let’s implement the protocol:


rebol[]

args: system/script/args
obj-args: decode-url system/script/args
host: obj-args/host
target: obj-args/target

;probe args
;probe obj-args
;probe host
;probe target

either (host = none) [
  browse http://dzone.com
] [
  browse rejoin ["http://" host "." "dzone.com"]
]

Test the script with these 2 links (type them in your browser):

  • dzone:
  • dzone:refcardz

In the first case the browser should go to Dzone homepage, in second case it should go to http://refcardz.dzone.com

Let’s now polish our program: detect if the url is valid (exists):


rebol[
]

args: system/script/args
obj-args: decode-url system/script/args
host: obj-args/host
options: obj-args/target

either (host = none) [
  browse http://dzone.com
][
    either (exists? url: to-url rejoin ["http://" host "." "dzone.com"]) [
       browse url
    ][
       print "Url doesn't exist"
       input
    ]
]

Last refinement: create shortcuts


rebol[
]

map: [
"n" http://www.dzone.com/links/queue.html
"p" http://www.dzone.com/links/
"r" http://refcardz.dzone.com
"s" http://www.dzone.com/links/spy
"a" http://www.dzone.com/links/add.html
]

args: system/script/args
obj-args: decode-url system/script/args
host: obj-args/host
options: obj-args/target

either (host = none) [
  browse http://dzone.com
][
    either (exists? url: to-url rejoin ["http://" host "." "dzone.com"]) [
       browse url
    ][
       either ((url: select map host)  none) [
           browse url
       ][
            print "Url doesn't exist"
            input
       ]
    ]
]

Test these links

  • dzone:n
  • dzone:p
  • dzone:r
  • dzone:s
  • dzone:a

Optionally you can shorten your Dzone Protocol by creating another Dz protocol entry into Windows Registry so that you could write:

  • dz:n
  • dz:p
  • dz:r
  • dz:s
  • dz:a

To do so, copy and paste the windows registry code below:


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\dz]
@="\"URL:dz Protocol\""
"URL Protocol"=""

[HKEY_CLASSES_ROOT\dz\shell]

[HKEY_CLASSES_ROOT\dz\shell\open]

[HKEY_CLASSES_ROOT\dz\shell\open\command]
@="d:\\program files\\rebol\\rebol.exe -s c:\\customprotocol\\dz-protocol.r %1"

Bookmark and Share