19.3.1 HTTP Server Example
To give an example of how a workflow acting as a HTTP server may be designed, suppose there is an HTML login page requiring login name and password. The login parameters are checked against a user database, opening a new HTML page if authentication succeeded. If not, a new login attempt is requested.
The login page and the resulting message page, in case of failed authentication
An example of how such a workflow may be designed is shown below. Each of the Analysis agents will perform a validation, resulting in either the HTTP UDR being sent back with an updated response
field to the collector, or it is sent unchanged to a subsequent agent.
The
HTTPD
agent sends a request to theQuery
agent which checks that the correct page is requested. If the page does not exist, an error message is sent back to the HTTPD agent on thenot_found
route.If the page exists, the HTTP UDR is routed on to the
Welcome
orLogin
agents, depending on if the login page was addressed.The
Login
agent checks if the user exists in the database. If authentication fails, the HTTP UDR is routed to theFailure
agent which displays a new page stating the username/password is incorrect.If authentication succeeds, a new page is opened.
A workflow acting as an HTTP Server
Note!
The data sent to the HTTP agent in the example (the content
field) is an HTML page. Since UFDL cannot handle the HTML protocol, the field is defined as a string. If, for instance, XML was used instead (which may be handled with UFDL), this would require an Analysis agent, which turned the field into a bytearray and then decoded it (use the APL functions strToBA
and udrDecode
).
The Format Definition
Create an instance of the built-in HTTP format definition. Additional fields may be entered. This is useful mainly for transport of variable values to subsequent agents. In this case, re-usage of the username as parsed from the login page is desired.
internal MYHTTPD: extends_class ("com.digitalroute.wfc.http.HttpdUDR") { string username; };
The Analysis Agents
The Analysis agents handle all validation and response handling.
Query
The query agent checks if the addressed page exists. If it does not, an error message is inserted into the response
field and the UDR is returned to the collector.
consume { if (input.query == "/") { // Show welcome page. udrRoute(input, "index"); return; } if (input.query == "/login") { // Try to log in. udrRoute(input, "form"); return; } // The request is not found here. input.response = "HTTP ERROR: 404, Not Found"; input.responseStatusCode = "404 Not Found"; input.responseType = "text/plain"; udrRoute(input, "not_found"); }
Welcome
Populates the response
field with an HTML page, and send the UDR back to the collector.
final string welcome = "<html > <title >Welcome</title > <body bgcolor=#3b7d73 > <font face=Verdana,Arial > <h2 >HTTPD Doc Example</h2 > Login<p ><form action=/login method=post > <table ><tr ><td align=right > Username</td ><td ><input name=username ></td ></tr > <tr ><td align=right >Password</td > <td ><input name=passwd type=password ></td ></tr > <tr ><td > </td > <td ><input name=Login type=submit ></td ></tr> </table></form ></body ></html >"; consume { input.responseType="text/html"; input.response = welcome; udrRoute(input); }
Login
Verifies that the user is authenticated, by performing a lookup against a database table.
table tmp_tab; initialize { tmp_tab = tableCreate("select user_Name, user_PW from users"); } consume { if (input.requestMethod != "POST") { input.response = "Wrong method."; udrRoute(input, "No"); return; } // Find username. string p = input.content; int i = strIndexOf(p, "username="); p = strSubstring(p, i + 9, strLength(p)); i = strIndexOf(p, "&"); string u = strSubstring(p, 0, i); p = strSubstring(p, i + 1, strLength(p)); i = strIndexOf(p, "passwd="); p = strSubstring(p, i + 7, strLength(p)); i = strIndexOf(p, "&"); p = strSubstring(p, 0, i); // Verify the login. table rowFound_u = tableLookup(tmp_tab, "user_Name", "=", u); int hit_u = tableRowCount(rowFound_u); if (hit_u == 1) { if (p == (string)tableGet(rowFound_u,0,1)) { input.username = (string)tableGet(rowFound_u,0,0); udrRoute( input, "Yes" ); } else { input.response = "Wrong password."; udrRoute(input, "No"); } } else { input.response = "Wrong username."; udrRoute(input, "No");; } }
Success
The response
field is populated with an HTML page.
final string success1 = " <html > <title >Welcome </title > <body bgcolor=#3b7d73 > <font face=Verdana,Arial > <h2 >HTTPD Doc Example </h2 >Welcome "; final string success2 = "! <p > <a href=/ >Back to start page </a > </body > </html >"; consume { input.responseType = "text/html"; input.response = success1 + input.username + success2; udrRoute(input); }
Failure
The response
field is populated with an HTML page.
final string failure1 = "<html><title>Welcome</title> <body bgcolor=#3b7d73><font face=Verdana,Arial> <h2>HTTPD Doc Example</h2>Failed to log in; "; final string failure2 = "<p><a href=/>Back to start page</a> </body></html>"; consume { input.responseType = "text/html"; input.response = failure1 + input.response + failure2; udrRoute(input); }