Debugging unit for CGI application

Gabriel Corneanu
Version 2.2
Solved some small bugs.
Version 2.1
Minor changes. Demo project!
Version 2.0
Added support for WinCGI Applications
Version 1.0
Primary support for Standard CGI Applications

I needed a way for debugging CGI applications in real conditions (parameters, etc). For an ISAPI application you can load as host the web server, and set a breakpoint and wait for a request. This can be done, but usually you start with a standard CGI application; it's easier and safer. But it's very hard to test the behavior of your app if it gives dynamic content depending of some input parameters. The way of sending the request data from the server to the app is complicated, and hard to simulate. That's why we should let the web server to do his job and you can test your app with your favorite browser.

How it works
There are at least 2 instances of your app. One run from the IDE for debugging, and one run by the web server when a request is coming. Normally the app would handle that request; in this case it passes the request to the debugging instance and then waits for the result. This result is returned to the web server.
You set a breakpoint in your app and run normally from the IDE. When a request arrives, it is passed to your application and it will stop on your breakpoint.
Happy debugging!

This is a debug replacement of standard Delphi unit CGIApp; it only works with standard web broker (web module).
The compiled unit is only for Delphi 5. If you need this for a previous version, email me. Or you can buy the source (see License.txt) and compile yourself.

How to do it

  1. First choose a Web application or create a new one.
  2. Modify the uses clause of the main program from "cgiapp" to "dbgcgiapp".
  3. Make sure that the output file of the IDE is the actual file executed by the web server (something like "C:\webshare\wwwroot\myapp"). If the web server is on a remote machine, you must have access to the app directory through a mapped network drive.
  4. The app must have write permission to its directory, because the transfer is made using files in this dir (Using other methods would make things more difficult when the web server is a remote machine).
  5. For the IDE application you need to set a "debug" parameter. This instructs the unit to act like a debugging instance.
  6. It's possible to set a second "loop" parameter: this tells the app to work in a loop. After processing one request it does not end, but waits for another request. This might be very helpful when you want to debug a lot of requests to your app (otherwise you need to start the debug instance for every request). The loop ends by manually deleting the file with ".debug.dbg" extension in the same directory. If you reset the program (Ctrl+F2) or this file remains there some other way the instances run by the web server will block waiting for the debug instance to handle the request. This happens only as long as your app is compiled with the debug unit.
    The "CacheConnections" property of the Application instance is set by default to false, to mimic normal behavior of a CGI app (for each request the webmodule is instantiated and then destroyed). Setting this to true, might cause other problems: for example: unclosed queries at the end of the request; normally they are closed because the app is ending after one request. In case of a loop, you can receive a second request with the same instance of web module: if you set a parameter of an active query and "open" it, nothing will happen; the active query ignores this actions and you will get wrong result. Developers of ISAPI applications should know about these issues.
  7. Set some breakpoints in the application. Typically this is at the beginning of handling a request.
  8. Press F9 (run the app).
  9. Open the browser and go to a page served by your application. Then your instance running in Delphi should stop at a breakpoint.

That's all (and I hope it's not that difficult - if you need further help you can email me).

Gabriel Corneanu