Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Build Simple Asynchronous Pluggable Protocols for Windows : Page 3

You're not limited to the familiar asynchronous pluggable protocols (APPs) such as ftp://, http://, and javascript:// that are built into Windows; you can implement your own protocols, add them to Windows' recognized protocol list, and then use them from any application—and you can implement your protocol in any language. This article uses the Rebol scripting language to create a protocol that works similarly to the javascript:// protocol, compiling and evaluating arbitrary Rebol statements and displaying the results.




Application Security Testing: An Integral Part of DevOps

Branching Out
Now that you've created one link to see how it works you can quickly test others. The next link, 'browse this page,' is a JavaScript function that builds a reb:// link where the protocol body causes a browser to browse to a URL (the value of location.href). For local files the location.href will be a file:/// link—the location of your HTML document; for files on the Web, the location.href is the location of the file over http. In either case, the browse protocol opens the default protocol handler for either file:/// or http:// (probably Internet Explorer in the first case, and your default browser in the second). So, for example, if you happen to be viewing a page in your non-default browser you can switch and view it in your default browser by using Rebol's browse function. Simply add reb://browse in front of the http URL in the address bar.

The next link (another Rebol one-liner) is similar to the first one; it reads the address http://www.devx.com and writes it to a local file called text.htm.

You might want to steel yourself before clicking on the third link, it can be a shock if you're unprepared. It uses the do function to execute a file found at an http address. (This can also be used to execute a file found at an ftp address.) The particular file being executed is called websplitter.r and can be found at http://www.reboltech.com/library/scripts/websplit.r. You can find the text of the file at http://www.reboltech.com/library/html/websplit.html. Clicking this link or pushing the Do button from the sample form opens the Rebol shell, gets an HTML file from Rebol.com, and strips out all the tags. It prints the tags minus the text content to the shell and then the text content minus tags.

The next link sets the value of two Rebol variables, string and webpage, to 'microsoft' and the content at http://www.yahoo.com, respectively. If the string 'microsoft' is found anywhere on the Web page, then the shell opens and the code prints the contents of the Web page to it:

<a onclick='callreb("reb://string: {Microsoft} webpage:
read http://www.yahoo.com if find webpage string [print webpage]")'>
search yahoo front page for instances of "microsoft"</a>

Rebol has numerous ways to deal with strings and putting them in curly brackets—as shown above with Microsoft—works from many different interfaces.

The following are examples of building a reb: link with insertPrompt() using user input:

<a onclick="insertPrompt('reb://send','Please write your email address here','
{hello from reb protocol}')">send an email</a> <a onclick="insertPrompt('reb://send','Please write your email address here','
read http://www.rebol.com')">send a page</a>

So clicking on one of the links displays the prompt: "Please write your email address here." When you do the code calls the reb:// protocol and you will either receive an e-mail with the text "hello from reb protocol" in the body, or the HTML of the page www.rebol.com as text.

Finally, to write a local file containing the contents of a file located on an ftp site, you can use the following link:

<a onclick="callreb('reb://write %ftpget.html read
ftp://ftp.ncsa.uiuc.edu/Documentation/CopyrightStatement')">test ftp </a>

That's enough simple examples. Here's a brief explanation of the sample reb.r code (see Listing 1). First, reb.ra parses the input to a separate executable script from the protocol body. The following two lines perform the separation:

argsstring: to-string system/script/args parsestring: remove/part argsstring 6

System/script is an object that applies to the script argument passed in (think of it as the Rebol DOM). One subsidiary object of system/script is args, which contains the command line arguments. The word argsstring turns this object into a string value. The word parsestring then removes the first six characters of the argsstring string. Remove is a Rebol function.

Author's Note: Rebol calls these variables "words." In Rebol, a word may or may not be a variable, depending on how it is used.

Clean Up
Next there's a little problem with command arguments that originate in browser address bars: The URL ends with a forward slash (/). The slash can cause some real problems, so you need to remove it, using this code:

if #"/" = last parsestring [ remove back tail parsestring ]

This checks if the last of parsestring is the character "/"; if so, it executes the code inside the square brackets, called a block in Rebol. It also removes the last character of parsestring by going to the end and moving one character backward. One of the goals of Rebol's design was to make it more like a natural language, which I think has succeeded, although at some points the grammatical methods of verb inflexion quite maddening can be. ;)

Next I set the value of the word filename to be %reb-protoLog.txt. This is the name of the log file where the script saves the protocol body when you save.

Now a look at the code that creates the form. As I stated earlier Rebol/View includes a dialect for creating GUIs. (Read more about the View dialect.) You call this dialect with the code:

view layout[ ]

Everything within the square brackets gets evaluated as being code in the View dialect.

The next three lines should be pretty clear:

vh2 "result:" f1: area parsestring return f2: field

The code creates a headline (think of it as being analogous to the h2 tag in HTML) with the text "result:" beneath that is an area, analogous to an HTML <textarea> that you fill with the value of parsestring and then make a return. F2 is the name of the field where the operations output the text.

Next: button "Save"[write/append filename join newline join now join ":" join newline
join f1/text join newline "_________________________"]

The form has a button with the text value "Save." When you click on it the Rebol code in the block associated with the button gets evaluated. This code could have been written in a cleaner manner, but doing so might have been more confusing for someone unfamiliar with Rebol. Basically all the preceding code does is write or append a text value to the file %reb-protoLog.txt that I associated with the word filename earlier. The code builds up the text value written to the file by concatenating other values together. Concatenation is handled via the word join.

The Save button code concatenates linefeeds/carriage returns (made by the word newline), the present date/time (made by the word now) followed by a colon followed by the text in the area F1, which, will be the same as the text passed in the protocol body if the user does not edit it first. Finally, the code appends another newline and a line "________________________" to break up various log entries.

Next: button "Do" [ clear f2/text err: error? try[returnstring: to-string do f1/text] either err = true[ append f2/text "No string output returned" show f2][append f2/text returnstring show f2] ]

Another button with the text "Do," causes the application to execute code sent to it. The first thing the button code does is clear any text already in the F2 field. This is necessary in case a user edits the value of F1 several times, as in the calculator examples. The word err evaluates f1/text as well as checking if there is an error. By breaking the preceding code down you can get a better feel for the way Rebol evaluates code:

do f1/text

This code evaluates the string in the F1 area as Rebol code. The word to-string before the 'do' line (see above) attempts to turn the result of that evaluation into a string ('do' does not return a value unless the operation it evaluates returns a value). This whole process is associated with a word— returnstring—wrapped within a simple error-checking process.

Next it checks to see if an error occurred with the line:

either err = true

If an error occured, then Rebol executes the first block, right next to the true, if no error occurred then Rebol evaluates the second block. The first block writes "No string output returned" to F2. If the second block gets evaluated then the process of evaluating whatever was in F1 did return a value, and the code writes that value to F2 using the word holding that value—returnstring.

button "quit" [quit]

This last bit of code should be self-explanatory. When a user clicks on the quit button the code inside the block evaluates the quit function.

If this article inspires you to learn more about Rebol, please check out the related resources in the left column. One of these is an advanced article about writing protocols in Rebol. The techniques presented there, combined with those presented here, will let you implement many protocols that are still missing in the Windows system with relative ease.

Author's Note: The author wishes to thank Gabrielle Santilli, Gregg Irwin, Carl Read, and others from the Rebol List for their help.

Bryan Rasmussen is an R&D engineer at Xdocs Danmark, specializing in XML technologies. This project was for personal enjoyment, hopefully you enjoyed it too. Reach him at bry@itnisk.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date