Ide Indy Delphi

Title: Developing Webserver-Applications with Delphi - PART II
Question: How can I write my own Web server application with Delphi?
Answer:
Release 1 last update: [02.06.2000]
---------------------------------------------
Developing Webserver-Applications in Delphi
---------------------------------------------
----------------------------------------------------
PART II: Building Web-Apps with Delphi in practice
----------------------------------------------------
- Introduction
In the first part of this memo we got something to know about
CGI and ISAPI Webserver Applications. In this second part we
will write our own little Web-App with Delphi in practice.
- Requirements
1. Delphi-version with support for Webserver-Applications
2. HTTP-Webserver with CGI/ISAPI-support
- Preperations
Now we want to write our own Webserver-Application with Delphi.
We will use the newest technology ISAPI to do this. First,
we must configure our Webserver on our local computer. A good
HTTP-Server which I can recommend is OmniHTTP, you can download
it at the www.omnicron.ab.ca. When you installed your Webserver,
configure your ISAPI-directory where you put your ISAPI-DLLs
(e.g. c:\httpserver\isapi\). Our project will be like the example
of the first part of this memo: The client can request an
bmp-image from the server, our application will convert this
image to the web-compatible jpeg-format and it will respond with
the "image/gif"-MIME-content which can be viewed by every web-
browser.
- Building our project
Run Delphi and click File/New/Web Server Application and choose
ISAPI/NSAPI Dynamic Link Libary. Delphi will create a new project
with a WebModule and an Action editor on it, which handles the
requested messages. Now save your project anywhere you want and
set the output-directory to your ISAPI-directory of your Web-
server, like c:\httpserver\isapi. To do this click on Project/
Options/Directory&Conditions/Output directory, enter the
directory path-name and click OK. After you compiled your
project, you can execute it when you start your webbrowser and
enter something like this in the address-prompt:
http://localhost/isapi/project1.dll
--------- ----- ------------
1 2 3
Explenation:
1. Your local hostname (always "localhost" or "127.0.0.1")
2. ISAPI-directory-alias, configured in your local webserver
3. Name of your project + DLL-extention
You will see a blank page in your webbrowser because we
defined no action properties yet. To to so, return to Delphi and
right click on Actions in your WebModule of your Project. Delphi
creates a new Action property. Click on it and choose a name like
SHOWIMAGE for your Action-Item in the Object Inspector. Enter
the same name in the PathInfo-property. Because this is our
main Action which our Application must do, set Default to True.
Now change the property-page to Events and double-click the
OnAction property to assign a procedure to our Action-item. Now
assign the folwing variables to the procedure:
bmppicture: TBitmap; // BMP-Picture on local machine
jpgpicture: TJpegImage; // BMP converted to JPEG
ms: TMemorystream; // Stream to provide MIME-content
picfile: string; // Filename of the BMP
You have to add the units "jpeg" and "graphics" to your uses-
section, in order to use the objects TBitmap and TJpegImage.
Now assign the folowing code to the procedure:
begin
// Check if a picture file is given
if request.query='' then
begin
response.contenttype := 'text/plain';
response.content:='No Picture given';
response.sendresponse;
end else
// Then cut off the first and last characters from the filename
// So that "c:\test.bmp" becomes c:\test.bmp
begin
picfile:=request.Query;
picfile[1]:=' ';
picfile[length(picfile)]:=' ';
picfile:=trim(picfile);
end;
// Check if the file exists, when it doesn't exist than
// response with an error message as text/plain.
if not fileexists(picfile) or (picfile='') then
begin
response.contenttype := 'text/plain';
response.content:='Error: File not found';
response.sendresponse;
end else
// Load the bitmap, convert it to jpeg and send the response
// as image/jpeg mime-type.
begin
jpgpicture:=TJpegImage.Create;
bmppicture:=TBitmap.Create;
bmppicture.loadfromfile(picfile);
jpgpicture.Assign(bmppicture);
ms:=TMemorystream.create;
jpgpicture.savetostream(ms);
ms.position:=0;
response.contenttype:='image/jpeg';
response.contentstream:=ms;
response.sendresponse;
end;
handled:=true;
end;
Now compile your project and don't forget to flush the server,
so that the can web server reload the new isapi-dll. You can test
your project by entering something like this in your webbrowser:
http://localhost/isapi/project1.dll/SHOWIMAGE?"c:\test.bmp"
Our ISAPI-program will automaticly convert this given BMP-file
into a browser-compatiple JPEG-file and display it.
Now it's time you can write your own ISAPI-project!
Good luck!
---------------------------------------------------------------
End of this part