Connecting WebSTAR and 4D Business Kit using Rewrite
Technical Note 02-43
In this Technical Note, we will demonstrate the method of communications between 4D WebSTAR and 4D Business Kit. We will use WebSTAR Rewrite to intercept, process and forward those processed requests onto 4D Business Kit. You may choose to run 4D WebSTAR and 4D Business Kit both on the same machine, or on two separate machines.
Note: In this Technical Note, the two products under discussion, 4D WebSTAR V and 4D Business Kit will be referred to as WebSTAR and 4DBK, respectively. Also, I will be using the words method and rules interchangeably, and a request refers to an HTTP header that is sent from a client to 4D WebSTAR.
How this Technical Note will proceed:
1) A brief introduction to why WebSTAR Rewrite is necessary.
2) We will discuss the properties and functionalities of WebSTAR Rewrite,
mainly the Proxy method of Rewrite.
3) Connect 4DBK to WebSTAR using Rewrite proxy rules.
4) Security
The purpose of 4D WebSTAR as a web server is to respond to requests from clients. These requests must be evaluated and processed in order to serve the requested information. This process can be made more powerful for the benefit of webmasters and users with the Rewrite plug-in. The Rewrite plug-in alters the request before actually being passed on to WebSTAR. You can think of the Rewrite plug-in as a filter between the client (mainly browsers) and the server.
A browser makes a connection to a page served by WebSTAR. The client then sends a request (via clicking on a link), to WebSTAR. Before the request actually arrive at the web server, Rewrite will intercept the request and compare it to a set of previously defined conditions called "Rules". If the request meets the condition(s) of one of these "Rules", then that rule is activated. This process will usually involve some modification of the original request. Then a new request is constructed based on the "Rule" that was activated. WebSTAR will then see this newly constructed request, and return the results back to the browser. WebSTAR does not see the original requests, instead, it will see requests put together by the Rewrite plug-in. This is all happening transparent to the client. The client is only aware that a request has been made, and the result is returned.
With 4D WebSTAR Rewrite, you have three different methods of altering a request. The three methods are:
1) Modify
2) Redirect
3) Proxy
The first two, Modify and Redirect will alter the request to be sent out to WebSTAR. WebSTAR will then process this request and return the result back to the client. Proxy methods, on the other hand, will modify a request and send it out to an external processor to handle. Once the result of this external processor is returned to WebSTAR, WebSTAR will then forward this result back to the client. In this case, you may think of WebSTAR as a client, making a request to another server, hence the name "Proxy". Rewrite's proxy method allows for the integration of external resources without revealing them. As a result, the clients making the requests do not notice that the results are not coming from WebSTAR.
It is this third, Proxy method that we are going to focus on for this Technical Note. For more information regarding the WebSTAR Proxy Rewrite method and the other methods, please refer to the WebSTAR Technical Manual.
To answer this question, we have to take a look at the current situation and figure out how we are going to proceed. We basically already have a virtual host set up and want to add e-commerce functionality to our site. Here are a couple of scenarios:
1) We currently have only one machine, and we are using that machine to
serve our site. We want to set up a store and have it on the same machine.
2) We have a couple of machines available, one machine is running WebSTAR,
the other one we want to set up our e-commerce application on.
In this Technical Note we will focus on the former, the interaction between 4D WebSTAR and 4D Business Kit on the same machine.
We want the user to be able to visit our site, navigate freely and be able to shop on our site. We want the process of serving static pages and shopping cart functions transparent to the user when they visit out site. 4D WebSTAR allows us to do just that. Users can visit our site served by WebSTAR and when they want to do some shopping, the request will be passed onto 4DBK, 4DBK will then serve the result back to WebSTAR, and WebSTAR will relay that result back to the user. WebSTAR Rewrite will hide all this processing from the user.
Launch WebSTAR and then launch the Admin Client. Log into a virtual host with either full admin or host admin privileges. For full admin logged into a virtual host, you will have all Rewrite capabilities. This means you will have access to all Admin Rewrite rules as well as host Rewrite rules. For host admin logged into a virtual host, you will have only the Rewrite rules for that virtual host. Lastly, for full admin logged into "Admin", you will have access to Admin Rewrite rules only. The picture below shows a full admin logged into a virtual host.

We will create a new rule to handle the requests for serving a 4D Business Kit store. These requests can be in the form of links on the web pages served by WebSTAR, or they can be links on pages served by 4D Business Kit. Either way, we need a way to have Rewrite intercept them before it gets to the WebSTAR server. We will call this the "BKStore" rule. Under the condition section, we have three separate rows. Each of the rows defines a condition for execution of this rule. Up to three rows can be combined into a single compound condition. In the first column we will select a part of the request to be tested. The second column we will select the comparison operator. The third column we will select the "match value" for the request to be compared to. The last column allows for the joining of the rows. Set the values of the first and second column of the first row to "URL" and "Matches" respectively. "URL" will specify that it is the URL that we are trying to test. "Matches" indicates to Rewrite that the match value will be a regular expression, which will allow us to perform powerful search and replace operations. It is this third column, our "match value", which will introduce us to the concept of regular expression.
he language of Regular expression is quite cryptic, however, because of its syntax, regular expressions allow us to perform complex and powerful search and replace operations. 4D WebSTAR uses Perl Compatible Regular Expressions (PCRE) derived from the programming language Perl. The power of regular expressions lies in the ability to perform search and replace functions with pattern matching. You can also break up the results of your search into pieces so that you can replace only a portion of the search result in your replace string.
Some of the more important syntax that we will be using in our set up are the "." wild card character and the "*" repetition character. The "." wild card character will match any single character including a space character. The only character that this wild card will not catch is a line break. The "*" repetition character will match zero or more characters. A simple example here would be "(.*)", which means that we are matching any character or characters zero or more times. This means that "aa", "aaa", "abcd" or even "_" will match.
For the third column or our "match value" field, we will insert in a regular expression pattern. This pattern would be specific to our store, and the store operations. In our example, the store we are going to use is a DVD store. All the links on the pages, whether served by WebSTAR or 4DBK will have the string of characters "/DVDS_Site/" contained within that link. Thus, this would be a good place to start our pattern matching. What we will actually place in this field is: "^/DVDS_Site/(.*)". Rewrite will activate this rule if the URL request from the client contains a match of any part of "/DVDS_Site/" with zero or more occurrence of any character except the line break after it.
After we have found a match, a new URL must be put together and sent out to WebSTAR. The new URL created from the matched patterns will then be sent from WebSTAR to 4DBK. The character "$", when used in the "Proxy To" or "Result" fields, denotes the entire matched string. For example, "$1" corresponds to the first row of the current rule, "$2" corresponds to the second row and "$3" corresponds to the last row. Rewrite allows us to refer to sub-patterns within each row by using the pattern "$a.b". Each sub-pattern is separated by parentheses. For example, "$1.2" corresponds to the second sub-pattern matched of the first row.
In our case, we are running WebSTAR and 4DBK on the same machine and that means only one web server can have access to the default web port, port 80. Since we want WebSTAR to be at the center of our operations, we set 4DBK to serve on port 8080. The "Proxy To" field should be:
http://(your IP address:port number)/DVDS_Site/$1.1
Rewrite will take the "(.*)"from the "match value" field, whatever it may be, and insert it in place of the "$1.1". This is the new request that Rewrite will send to WebSTAR. The picture below shows what your rule should look like.

Note: The first 1024 ports of a system running Mac OS X is reserved for the root user. This means that only the root can use those ports. To avoid conflict, we should choose ports out of this range.
On any WebSTAR served page, we can put the link:
When the user clicks on this link, the rule BKStore will execute. This rule is executed from the links and commands on the pages served by both WebSTAR and 4DBK.
Before we can have a fully operational connection, we must make we have rules to cover all the possible connections to the store. We have to set up a rule for when a user clicks on a button or link for a call 4daction and 4DMETHOD. 4daction will be called when a user invoke a call to retrieve dynamic information from the database such as performing a search or add an items to their shopping cart. 4DMETHOD will be called when entering the admin area of the site. The picture below describes what a rule for 4daction should look like. The rule 4DMETHOD is the same as that for 4daction.
To make an even more powerful rule, we can perform some minor tweaking of the code. Instead of having "^/DVDS_Site/(.*)" in the "match value" field, we now have this tidbit of code: "^/(.*)_Site/(.*)". Also, replace the "Proxy To" field with:
http://(your IP address:port number)/$1.1_Site/$1.2
This will allow you to serve to multiple stores using one rule.
You can allow your set up to handle SSL connections via several methods. One method is a direct connection to the 4DBK server. Another is to secure the connection from the beginning of the session.
First situation: You want 4DBK to handle only the secure connections. While WebSTAR handles the rest. Making a direct connection to the 4DBK server is quite simple. You will need to have a certificate for the 4DBK server, not for WebSTAR. On the link to the page that requires SSL, hard code the URL directly to the 4DBK server. For example:
https://(your IP address:port number of 4DBK server)/Path to secure page
This method will allow you to discontinue the use of the proxy server (WebSTAR Rewrite). The browser will be connected directly to the 4DBK server on port 443 for the duration of the session.
The second situation: You want a secure connection from the browser to WebSTAR. The certificate will be stored in WebSTAR. All incoming "https" request will establish a secure connection with WebSTAR. This link will initiate the https protocol on your browser:
https://(your IP address)/Path to your store
Two things will occur here. The first is that your browser will switch to port 443, using SSL to connect to the WebSTAR server. WebSTAR Rewrite will then execute the proxy rule, and build an http request from the path in this URL and connect to 4DBK. This method secures the connection between the browser and WebSTAR, while Rewrite forwards on the modified request to 4DBK.
With WebSTAR as the web server and 4DBK as your complete e-commerce application, you gain the advantages of WebSTAR serving your static content, ftp requests, email clients, and other web server related functions while 4DBK serves all your e-commerce related dynamic content. At the same time, the user does not know that another application is involved. To the rest of the Internet, your site is a powerful, dynamic and a complete solution. Enjoy the combination of power and flexibility of WebSTAR and 4DBK.
Windows
Pentium II minimum
800 X 600 monitor resolution
Windows 2000, Windows XP.
256 MB RAM minimum
MacOS:
G3 or better processor
800 X 600 monitor resolution
Mac OS X v10.2.8 or later
256 MB RAM minimum