TweetFollow Us on Twitter

CGI Programming with REALbasic and Apache

Volume Number: 20 (2004)
Issue Number: 6
Column Tag: Programming

CGI Programming with REALbasic and Apache

by Mark Choate

With the recent release of REALbasic 5.5, RB has become an excellent tool to use for web development. The most recent version sports improved networking features and support for XML (including XSLT and Xquery), plus the ability to compile command-line applications, called console applications in REALbasic. Perhaps most interesting is the ability to compile applications for use on Windows and Linux, in addition to Macintosh platforms.

Traditionally, Mac web servers communicated with CGI applications through Apple events. This doesn't work with Apache, however, so a CGI application needs to be able to be able to receive information from the server in the normal CGI way - through environment variables. This article illustrates the steps necessary to implement this in REALbasic. One important thing to note: since many of the features that enable CGI programming in REALbasic are new, the current release (5.5.1) has some bugs, which I have had to work around. Some may be fixed by the time this article is released, but hopefully this will save you some time for those that have not been fixed.

The first step will be to review CGI programming for those who aren't familiar with it. CGI stands for the common gateway interface. It's called an interface because it provides the means for Apache (or any web server that supports CGI) to execute scripts and applications on the host machine of a web server. When a user types a URL into his or her web browser, that URL often represents the location of an HTML file that the server just picks up and sends back to the browser. In a CGI program, the URL represents a script or a program that gets executed. The output of the program then gets sent back to the user. In order to provide security, Apache allows the administrator to configure which directories allow CGI programs to be executed. On OS X the cgi-bin directory is here:

/Library/WebServer/CGI-Executables

This article assumes that you haven't made any changes to the default Apache configuration that comes with OS X. The configuration file that Apache uses is available at /etc/ httpd/http.conf. If you have never modified this file, now is not a good time to start - but you shouldn't need to. It's worth taking a look at it just to make sure that CGI is set up properly. My httpd.conf file has this, about 2/3 of the way through the document:

    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the realname directory are treated as applications and
    # run by the server when requested rather than as documents sent to the client.
    # The same rules about trailing "/" apply to ScriptAlias directives as to
    # Alias.
    #
    ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"

The last line indicates two things. "/cgi-bin/" is going to be part of the URL for the CGI application - something like: http://localhost/cgi-bin/ plus the name of your script. The second path is the absolute path for this directory on the server. For this example, we'll be placing our REALbasic CGI program in this directory. Sometimes you'll see CGI scripts that end with a ".cgi" extension, but we won't need to use that - in fact, you should avoid using any extensions because it will mess things up. Other scripting languages, like perl and Python, usually reside on the web server as text files that are executed by an interpreter. Apache uses extensions to map an interpreter to a particular file. Since REALbasic is a compiled program, it doesn't need an interpreter and it's better just to leave the extension off. It also provides for a much nicer URL, which is important, too.

Now we can start work on the program. The easiest way to work is to save the project in the CGI-Executables directory. This is because you'll need to compile the application in order to test it with Apache, and it's easier to just compile it and leave it there to test than it would be to compile it and copy it to the CGI directory.

In RB, a console application is one that does not have a graphical interface - it runs on the command line. In order to create a console application, simply create a new project in REALbasic 5.5+, and select the "Console Application" template. Once that is done, RB will provide you with the shell of an application with one class called "App".


Figure 1. Starting a new console application in REALbasic.

There are two default events in a console application - "UnhandledException" and "Run". The "Run" event is triggered when the program is launched - in the case of a CGI application, it is triggered when a user requests it by typing the application's URL in her web browser.


Figure 2. Blank console application project.

Now is a good time to select the FILE a Build Settings... menu and configure the application. Select "Build for OS X" (this program has only been tested on OS X, although it should work on other platforms as well. Click on the top popup menu on the page, and select "Mac OS Settings". The only thing to change here is the name - be sure to give it a name without an extension and without spaces or punctuation. In this example, I've chosen the name "CGI", which is short and easy to type into a browser window.

Once that is done, it's time to write some code.

Since console applications do not have a graphical interface, they have to be able to input data and output data in some other fashion. For programs that are executed on the command line, this is typically referred to as "Standard Input" and "Standard Output" respectively. With a REALbasic console application, the command "INPUT" represents (you guessed it) standard input. "PRINT" sends data to standard output. In addition to standard input and output, CGI applications also make use of environment variables that are set by the web server. In order to access environment variables, you need the system object, which includes the method: System.EnvironmentVariable(), which returns the value for the environment variable that is passed to it. In the current version (5.5.1) there is a bug that causes REALbasic to crash if you try to access a variable that does not exist. This places some real limitations on what you can do, but it is supposed to be fixed in 5.5.2.

The console application "App" class is where the action is. It has two events: "Run", and "UnhandledException". The "Run" event is triggered when the application is invoked by the web server, so it is in the "Run" event that we put the main part of our code. I also created a "request" object, which is created when the "Run" method is executed. It is a sub class of Dictionary and it is used to hold the data that is passed to the CGI application from Apache. It also executes a "Write" method, that sends data back to the client browser.

The "Run" method should look like this:

App.Run
#pragma disableBackgroundTasks 
  
request = new request
  
request.value("SERVER_SOFTWARE") = system.environmentVariable("SERVER_SOFTWARE")
request.value("SERVER_NAME") = system.environmentVariable("SERVER_NAME")
    
request.value("REQUEST_METHOD") = system.environmentVariable("REQUEST_METHOD")
    
request.value("QUERY_STRING") = system.environmentVariable("QUERY_STRING")
request.value("REMOTE_ADDR") = system.environmentVariable("REMOTE_ADDR")
    
request.getQueryString
request.handleRequest

Background tasks are disabled because Apache doesn't work well with them. If you don't disable them, every time you do a loop, or execute anything that triggers a new thread or background task, the application crashes mercilessly.

In this example, I have only gathered the minimal environment variables necessary to execute the program, because of the bug mentioned earlier. One notable environment variable missing is "HTTP_COOKIE", which is very useful if you use cookies, which provide a way to track a visitor to the site. A complete list of variables is included in the sample script, but commented out.

The two variables that matter most to use are "REQUEST_METHOD" and "QUERY_STRING". There are several kinds of requests a web server can accept. The two that concern us are "Post" requests and "Get" requests. The distinction between the two in actual practice is virtually non-existent, except that it changes the way that form data is passed to the CGI program.

Any time you fill out a form on a web page, either to log in or make a purchase, the information that you enter needs to be transferred to the server so that it can take some appropriate action. When you create a form in HTML, you have the option of selecting the request method you want to use - either "Get" or "Post". If you choose "Get", then the data from the form is encoded and sent across as part of the URL. If you use "Post", then the data is sent to the CGI program as standard input. Here is an example of a "Get" request URL:

http://localhost/cgi-bin/test?cat=dog

The first step in processing a CGI request is to find out what kind of request it is, and process it accordingly. In the request class, I have implemented the following method:

App.request.getQueryString
#pragma disableBackgroundTasks // Throws an error during the loop
  
  Dim query_string, field, key, value As String
  Dim x As Integer
  
  query = New Dictionary
//If the REQUEST_METHOD is a "post", then get the string from standard input, 
   otherwise get it from QUERY_STRING
  If me.hasKey("REQUEST_METHOD") then
    if me.value("REQUEST_METHOD") = "POST" Then
      query_string = Input
    Else
      query_string = System.EnvironmentVariable("QUERY_STRING")
    End If
  end if
  
  if query_string <> "" then
    //parse the query string
    For x = 1 to CountFields(query_string, "&")
      field = NthField(query_string, "&", x)
      key = NthField(field, "=", 1)
      value = NthField(field, "=", 2)
      value = ReplaceAll(value, "+", " ")
      value = DecodeURLComponent(value)
      query.value(key) = value
    Next
  end if
  

The method creates a new dictionary to hold the values of the query (the data from the form). If the request method is a "Post", then the method grabs the string from standard input. If it is a "Get", then it grabs it from the environment variable "QUERY_STRING". Beyond that, everything else is the same and the string is parsed and the dictionary values are set.

We now have a request object that contains all the needed values from the request, plus the query parsed into a dictionary. Normally, this would be sent to some method that would provide a response based upon the content of the query. For our example, we'll just send back to the client all the information stored in the request object.

To send data back to the client, we need to send some header information followed by an HTML string.

App.request.write
#pragma disableBackgroundTasks
  // simple write method that returns the data in the request.
  dim output as string
  dim html as string
  dim requestString, queryString as string  
  dim x,y as integer 
  
  // set the value for "Content-type", followed by a blank line
  output = "Content-type: text/html" + chr(13) + chr(10) + chr(13) + chr(10)
  
  // create the html string 
  html = "<html><head><title>TestOutput</title></head><body>"
  
  y = me.count
  for x = 0 to y-1
    requestString = requestString + me.key(x) + ": " + me.value(me.key(x)) + "<br />"
  next
  
  y = me.query.count
  for x = 0 to y-1
    queryString = queryString + me.query.key(x) + ": " + me.query.value(me.query.key(x)) + "<br />"
  next
  
  html = html + requestString + queryString + "</body></html>"
  
  output = output + html
  
  print output

If you placed the application in the /Library/WebServer/CGI-Executables directory, and set the application name as "CGI", then you should be able to access the script from the following URL:

Localhost/cgi-bin/CGI/CGI

You should be able to paste it in the browser, hit return, and then get back a list of the variables. If you want to test the query string, then enter a URL like the following:

Localhost/cgi-bin/CGI/CGI?key=value


Figure 3. Results of CGI application.

You now have a good starting point for writing CGI programs in REALbasic for Apache. One thing you'll notice, especially if you have a lot of traffic on your site, is that CGI can be slow at times. The reason for this is that the program has to be started up with each request, which produces a lot of overhead. The downside to RB is that it produces large executable files - about 1.3 MB for this simple CGI program, so the particular solution is best limited to low-traffic sites. Because of this, there have been a variety of CGI workarounds that speed up the process. They way they work is that instead of invoking the program each time it is requested, the program stays resident in memory and handles the requests as they come in. This is usually accomplished with an Apache plug-in. This is an interesting approach that can be used with REALbasic as well - and you don't need to rely on console programming.

I developed an RB application that worked with an Apache plug-in called "mod_scgi". Mod_scgi works by taking the data that Apache would normally send as environment variables to a CGI program, and instead sends it as a block of data over a TCP connection. Using REALbasic's networking abilities, you can create a SocketServer that creates a pool of TCPSockets that listen on the appropriate port, gets the data when it is available, parses it and acts on it just like a CGI program. As soon as the individual socket is done, instead of exiting, it returns to listening on the port for the next request. This creates a huge performance boost, and is a tactic that should be considered if you expect a lot of traffic to your site.

The original (and best) guide to CGI from the inventor's of Mosaic, NCSA:

http://hoohoo.ncsa.uiuc.edu/cgi/


Mark Choate

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Tor Browser 11.5.8 - Anonymize Web brows...
Using Tor Browser you can protect yourself against tracking, surveillance, and censorship. Tor was originally designed, implemented, and deployed as a third-generation onion-routing project of the U.... Read more
Alarm Clock Pro 15.0 - $19.95 (91% off)
Alarm Clock Pro isn't just an ordinary alarm clock. Use it to wake you up in the morning, send and compose e-mails, remind you of appointments, randomize the iTunes selection, control an internet... Read more
Google Chrome 107.0.5304.121 - Modern an...
Google Chrome is a Web browser by Google, created to be a modern platform for Web pages and applications. It utilizes very fast loading of Web pages and has a V8 engine, which is a custom built... Read more
calibre 6.9.0 - Complete e-book library...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
Safari Technology Preview 16.4 - The new...
Safari Technology Preview contains the most recent additions and improvements to WebKit and the latest advances in Safari web technologies. And once installed, you will receive notifications of... Read more
FileZilla 3.62.2 - Fast and reliable FTP...
FileZilla (ported from Windows) is a fast and reliable FTP client and server with lots of useful features and an intuitive interface. The FileZilla Client not only supports FTP, but also FTP over TLS... Read more
djay Pro 4.0.13 - Transform your Mac int...
djay Pro provides a complete toolkit for performing DJs. Its unique modern interface is built around a sophisticated integration with iTunes and Spotify, giving you instant access to millions of... Read more
Opera 93.0.4585.21 - High-performance We...
Opera is a fast and secure browser trusted by millions of users. With the intuitive interface, Speed Dial and visual bookmarks for organizing favorite sites, news feature with fresh, relevant content... Read more
AppCleaner 3.6.6 - Uninstall your apps e...
AppCleaner allows you to uninstall your apps easily. It searches the files created by the applications and you can delete them quickly. Supports macOS Ventura. Fixed an issue causing failed updates... Read more
QuickBooks 21.0.7.1248 - Financial manag...
QuickBooks helps you manage your business easily and efficiently. Organize your finances all in one place, track money going in and out of your business, and spot areas where you can save. Built for... Read more

Latest Forum Discussions

See All

‘Top Hunter Roddy & Cathy’ Review –...
The NEOGEO is generally characterized by, with only a few notable exceptions, fighting games and Metal Slug. Within a couple of years of its launch, the vast majority of the output on the console seemed to be mining (quite successfully) a few... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for November 28th, 2022. In today’s article, we’ve got a pair of reviews to check out. Full reviews of Pokemon Scarlet and Violet and The Oregon Trail are waiting for you to read. There’... | Read more »
‘OPUS: Echo of Starsong’ Interview: Port...
With OPUS: Echo of Starsong ($8.99) having finally launched on iOS after hitting PC and consoles, I had a chance to talk to Scott Chen who is the co-founder and executive producer of Sigono. In our chat, I touched on topics like game subscription... | Read more »
Best iPhone Game Updates: ‘Rush Rally 3’...
Hello everyone, and welcome to the week! It’s time once again for our look back at the noteworthy updates of the last seven days. As November breaths its last, the holiday season is right around the corner. That means we should start seeing more... | Read more »
‘Total Football’ is an Arcade-Style Socc...
GALA SPORTS recently launched its brand new soccer title, Total Football, and, true to its name, it is a pure arcade-style soccer game in the same vein as FIFA Mobile and PES Mobile. It also features official licensing from FIFPro and Manchester... | Read more »
Genshin Impact will recieve two new char...
HoYoverse has announced that Genshin Impacts version 3.3 will be arriving on December 7th. Titled All Senses Clear, All Existence Void, the update will bring two powerful new characters and a brand new card-based minigame. [Read more] | Read more »
‘Wreckfest’ Mobile Compared With Console...
HandyGames’ mobile version of Bugbear’s demolition derby-style racer Wreckfest ($9.99) released on iOS and Android recently, and we featured it as our Game of the Week. | Read more »
Black Friday Deals Here – The TouchArcad...
After taking a couple of weeks off we return on this glorious Black Friday with another episode of The TouchArcade Show. We get into a big discussion about virtual assistants like Alexa, Siri, and Google, and their place in the greater smarthome... | Read more »
TouchArcade Game of the Week: ‘Station 1...
I’m a big fan of Glitch Games and their unique brand of point-and-click adventure/escape room/puzzle games, and while they’re a tiny outfit and there’d typically be a couple years gap in-between their new releases, they were always worth the wait.... | Read more »
SwitchArcade Round-Up: ‘Super Lone Survi...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for November 25th, 2022. Today we look at the remaining releases for the week, and I’ll be honest with you: it’s not a great assortment. Still, there are at least a couple of things... | Read more »

Price Scanner via MacPrices.net

Cyber Monday: 24″ Apple M1 iMacs for $150 off...
Amazon has Apple’s 24″ M1 iMacs on Black Friday sale for $150 off MSRP. Their prices are currently the lowest available for new iMacs among the Apple retailers we track: – 24″ M1 iMacs (8-Core CPU/7-... Read more
Cyber Monday Sale: 25% off Apple MagSafe acce...
Apple retailers are offering MagSafe accessories for up to 25% off MSRP for Cyber Monday. Here are the best deals available, currently from Verizon and Amazon: (1) Verizon has Apple MagSafe Chargers... Read more
Cyber Monday Sale: Apple AirPods for up to $1...
Looking for Apple AirPods, AirPods Pro, or AirPods Max this Cyber Monday? Look no further than our Apple AirPods Price Tracker. We track prices from 20+ Apple retailers and update the tracker... Read more
Final day for Apple’s Black Friday/Cyber Mond...
CYBER MONDAY Apple’s four day Black Friday/Cyber Monday 2022 event is now live and will run from November 25, 2022 to November 28, 2022 (ends today!). Receive a free $100-$250 Apple Gift Card with... Read more
Cyber Monday: Apple 13″ M2 MacBook Airs for $...
Apple retailers have posted their Cyber Monday prices on 13″ MacBook Airs. Take up to $200 off MSRP on M2-powered Airs with these sales with prices starting at only $1049. Free shipping is available... Read more
The best Cyber Monday iPhone sale? This $500...
If you switch to Xfinity Mobile and open a new line of service, they will take $500 off the price of a new iPhone, no trade-in required. This is the best no trade-in Cyber Monday Apple iPhone 14 deal... Read more
Cyber Monday Sale: Apple 16″ MacBook Pros for...
Amazon is offering $500 off MSRP discounts on Apple 16″ MacBook Pros with M1 Pro CPUs as part of their Cyber Monday sale. Their prices are the lowest available for these models from any Apple... Read more
Cyber Monday Sale: Apple 14″ MacBook Pros for...
Amazon is offering $300-$500 off MSRP discounts on Apple 14-inch MacBook Pros with M1 Pro CPUs as part of their Cyber Monday sale. Their prices are the lowest available for these models from any... Read more
Cyber Monday Sale: Apple Watch Ultra for $60...
Amazon has Apple Watch Ultra models (Alpine Loop, Trail Loop, and Opean Bans) on sale for $60 off MSRP as part of their Cyber Monday sale, each including free shipping, reducing the price for an... Read more
Cyber Monday MacBook Sale: 13″ M1 Apple MacBo...
Amazon has Apple 13″ M1 MacBook Airs back on sale for $200 off MSRP, starting at only $799, for Cyber Monday 2022. Their prices are the lowest available for new MacBooks this Cyber Monday. Stock may... Read more

Jobs Board

*Apple* Electronic Repair Technician - PlanI...
…a highly motivated individual to join our Production Department as an Apple Electronic Repair Technician. The computer repair technician will diagnose, assemble, Read more
Product Manager II - *Apple* - DISH (United...
…you will be doing We seek an ambitious, data-driven thinker to assist the Apple Product Development team as our new Retail Wireless division continues to grow and Read more
Staff Engineer 5G Protocol, *Apple* - DISH...
…metrics. Essential Functions and Responsibilities for a Staff Engineer 5G protocol( Apple ) Knowledge of 5G and 4G/LTE protocols and system architectures Experience Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.