This article mainly describes how to make DELPHI and the Internet work together. Two specialized technologies will be detailed in this article:
WININET: Build FTP, HTTP and Gopher client programs ISAPI: Expand Internet information services, for example, obtain information on the server and display them on the browser.
In today's computer world, a huge development trend has been set off due to Microsoft's Internet strategy. those making
The days of CGI (Common Gateway Interface) and third-party tools for even the most basic Internet tools are finally gone. There will always be a need for sophisticated third-party tools, but now programmers will find the vast array of Internet tools they need embedded in the operating system. In short, without further investment, you can use free DELPHI resources come to:
*Develop WEB browser
*Run FTP, HTTP and Gopher between two DELPHI applications or a DELPHI application and TCP
(Transmission Control Protocol) operates TCP between servers
Because DELPHI can easily call Windows API and it supports OCX/ActiveX, Microsoft's new strategy
The strategy fit perfectly with our plan. Microsoft produces tools, and DELPHI programmers reap the rewards!
What's in this article?
This article contains three major parts and some small parts, with three major themes:
*Find the Materials: The technical documentation mentioned in this article is available there and includes a brief description of the hardware and software you will need to run the code in this article.
*ISAPI: How to use ISAPI
*WININET: How to use WININET
In most cases, the ISAPI and WININET sections in this article are completely independent, and you are free to choose the order in which you read them.
Find information, hardware and software requirements
You will need a copy of Microsoft Windows NT 3.51 Server or NT 4.0 Server, which should be accompanied by the Internet Information Services documentation, because you will need to understand the technologies mentioned therein. This document should be included with NT Server4.0, and users of NT 3.51 can download it from Microsoft's website. To run Windows NT, the minimum configuration of your machine should be 486 compatible and 20 MB or more of memory.
You must have another computer with a web browser installed. In order to make the ISAPI part of this article run smoothly, the second
The machine must be able to run all software that supports web browsers. If your machine is running Windows 95 or Windows NT
Then the WININET code piece in this article will run best. Any eligible web browser can be used in this technical environment.
In versions of Delphi 2.0 and above released after June 1996, there are the tools you need to connect Delphi to the Internet.
almost all resources.
If you don't have the latest Delphi version [Note: The author here refers to version 2.0 (translator)], then you need this article
The special files mentioned in the file, all of which are available almost free of charge on the World Wide Web [Note: If you are using Delphi2.0 or above, you are not allowed to consider (Translator)]. All the techniques mentioned in this article can work smoothly in the Delphi2.0 environment, but they may not work smoothly in the 16-bit Delphi environment.
If you need to download information from the World Wide Web, the link is: http://www.borland.com/TechInfo/delphi/i
index.html
[Note: It no longer exists! :-(( (Translator)]
The new version of Delphi2.0 comes with the WININET.PAS document. If your copy does not contain it, then the one above
World Wide Web Node can provide this for you. WININET.PAS includes a list of variables, functions, types, and properties designed to extend Microsoft Windows Internet. This means you can easily add FTP, HTTP and Gopher support to your applications. Microsoft's WININET.DLL is distributed for free. If it is not in your Windows/System or Windows/System32 directory, you can get it from Microsoft. Here is the World Wide Web node where the WININET.H window help file is available:
http://www.microsoft.com/intdev/sdk/docs/wininet/default.htm [Note: It seems that it is no longer available! : -( (Translator)]
Generally speaking, the online home for Microsoft Internet Developers is the INTDEV portion of the Microsoft node.
In addition to WININET and ICP, another key technology supported by Delphi is ISAP. As stated in Microsoft's corporate documentation
As described, this technology enables you to "'write' server-side scripts and filter scripts, thereby extending Microsoft Internet Information Services and other ISAPI Web services."
If you need to find a description about ISAPI, you can go to:
http://www.microsoft.com/intdev/sdk/servapi.htm [Note: God bless you! ;-) (Translator)]
At the end of this article, a copy of the key ISAPI document named HTTPEXT.PAS is attached.
The Internet Control Pack (ICP) released for free by Microsoft is an OCX/ActiveX control set that you can use in Delphi
Drag and drop them onto the application (Delphi 2.0 includes these controls). They provide immediate support for creating Delphi applications, and they know how to browse the web, use FTP, WINSOCK, and other Internet technologies. If your copy of Delphi does not contain these controls, you should add these documents to the Lib directory in the directory where Delphi is located before using them. The documents are located under Borland's INDEX.HTML site at the link mentioned above. I have not mentioned ICP controls in this article, but anyone interested in this technology should definitely confirm that he has these
A copy of the control.
You can download my Pascal application files from my site, their names are STRBOX.PAS and MATHBOX.PAS.
It would be beneficial to check this site frequently for updates to the information mentioned in this article.
Here I assume that the reader is familiar with Delphi and Object Pascal, and that the reader is familiar with the Internet, HTML,
Have a basic understanding of browsers and World Wide Web servers.
ISAPI
ISAPI is an easy-to-use yet powerful technology that allows you to extend the capabilities of Internet information services.
This technology comes with Windows NT 4.0 and allows you to create WEB, FTP and GOPHER sites on your server. At the same time, this technology is compatible with WindowsNT3.51 Server [Note: refers to the server version, the other version is the workstation version (translator)].
In the past, the best way to scale a web server was to build CGI applications. They are powerful tools, but they are also
Limited by their execution format [Note: For example, PERL is interpreted and executed (translator)]. When you make a CGI-based request to the server from a browser, the CGI application will most likely be forced into memory first, which will consume a lot of time. Moreover, in some circumstances, CGI technology appears to be a little more difficult to use.
ISAPI is a method of replacing CGI applications by writing DLLs [Note: Dynamic Link Program (Translator)]. You can also
It is possible to write filtered text through ISAPI, but I will not mention this technology in this article. Compared with CGI, ISAPI is easier to use, it is faster, and it makes better use of system resources. In the following points, I will explain in detail why ISAPI DLLs are better than CGI applications:
ISAPI DLLs are located at the same address as the HTTP service, so they can access the HTTP service directly from the server. Compared with CGI applications, they can load into memory faster; when they make a request on the server, they require less pause time [Note: refers to the time between making a request and receiving a response from the server (Translator)] Much. This is even more important when the server is under heavy load.
You can control when DLLs are loaded and unloaded. For example: you can preload DLLs on the first request; when
Unload the ISAPI application DLLs when they are not in use to free up system resources.
As mentioned before, you can use ISAPI to write filtered text [Note: generally refers to scripts in the C/S structure (translator)], more
According to Microsoft's documentation, you can filter text via ISAPI to do the following:
User authorization scheme
compression
encryption
login
Traffic analysis or other request analysis (for example, looking for requests in "....etcpassWord")
In this article, I will focus on how to write DLLs that return data sets, or how to interact with users running browsers.
Make a simple contact.
ISAPI Basics
The HTTPEXT.PAS file contains key statements for using ISAPI. This document should accompany the
Delphi version distributed. It can also be found on Borland's site, which is attached to the ISAPI section of this article. Because this is based on NT technology, you must use Delphi2.0 or above to apply this technology. You can't use this on a 16-bit editor.
HTTPEXT.PAS contains the interface of the ISAPI technology created by Microsoft [Note: refers to the Delphi interface, ISAPI is compiled in C++
writing (translator)]. When writing Delphi, there is no ISAPI user interface provided. I will only describe how to use Microsoft's existing technology. However, ISAPI is too easy to use, and for most users, the user's version of the Delphi object is not necessary.
There are three functions that can be used as entrances to ISAPI DLLs. The first two are required, and the third is optional.
GetExtensionVersion: Check the minimum version
HttpExtensionPRoc: This is the entry point of the DLL, just like the begin...end block in a Delphi application
TerminateExtension: This is an optional program that can be used as a thread to clean up other memory allocations.
When you create an ISAPI DLL, you must reference the first two of the three functions listed above, execute this
Two functions are the key to all ISAPI programming.
All three statements contain "word output," a term used because ISAPI DLLs extend Internet Information Services
device. (Remember, Internet Information Server refers to Microsoft Server. If you want to use an NT server as a physical web server, then this is exactly the tool you need. ISAPI DLLs are distributed with NT4.0 and are installed when installing the operating system. is installed automatically.)
ISAPI provides a standard that production servers can follow. For example, it can take Netscape's complex NSAPI interface
Compressed to the relevant concise and beautiful ISAPI to operate the NSAPI interface.
The following is the declaration of these two important functions
function GetExtensionVersion(var Ver: THSE_VERSION_INFO): BOOL; stdcall;
function HttpExtensionProc(var ECB: TExtensionControlBlock): DWORD; stdcall;
You just paste GetExtensionVersion into your DLLs and you're done. You only need to make minor changes when ISAPI releases new versions to the public.
function GetExtensionVersion(var Ver: THSE_VERSION_INFO):
BOOL; stdcall;
begin
Ver.dwExtensionVersion := $00010000; // 1.0 support
Ver.lpszExtensionDesc := 'Delphi 2.0 ISAPI DLL'; // Description
Result := True;
end;
The parameter passed to this function is declared in HTTPEXT.PAS as follows:
The relevant parameters are declared in HTTPEXT.PAS as follows:
PHSE_VERSION_INFO = ^THSE_VERSION_INFO;
THSE_VERSION_INFO = packed record
dwExtensionVersion: DWORD;
lpszExtensionDesc: array[0..HseMaxExtDLLNameLen-1] of Char;
end;
The constant HseMaxExtDllNameLen has a value of 256 in the declaration. The two variables in the record are "self-declared". The former contains the ISAPI version number [Note: the variable dwExtensionVersion (Translator)], and the latter represents a user-defined string used to describe DLLs.
When you reference the GetExtensionVersion statement, you must add input to the DPR file section of your DLL program.
out part. As you write this statement you should also write:
exports
GetExtensionVersion
HttpExtensionProc;
This is what you do when setting up functions for these two important ISAPI DLLs. The next step, using HttpExtensionProc, is a little more complicated, so I'll leave it as a separate section.
Working with HttpExtensionProc
The HttpExtensionProc statement is the entry point of the DLL. Its function is like the main() statement in C language, or
The begin...end part in Delphi
Here is a simple example using the GetExtensionVersion statement
function HttpExtensionProc(var ECB: TExtensionControlBlock):
DWORD; stdcall;
var
ResStr: string;
StrLen: Integer;
begin
ECB.lpszLogData := 'Delphi DLL Log';
ECB.dwHTTPStatusCode := 200;
ResStr := ' +
'
'Hello from ISAPI
' +
';
ResStr := Format(
'HTTP/1.0 200 OK'#13#10+
'Content-Type: text/html'#13#10+
'Content-Length: %d'#13#10+
'Content:'#13#10#13#10'%s'
[Length(ResStr)
ResStr]);
StrLen := Length(ResStr);
ECB.WriteClient(ECB.ConnID
Pointer(ResStr)
StrLen
0);
Result := HSE_STATUS_SUCCESS;
end;
If you make a request to this DLL while browsing, you will get a response like this:
Test Server Results
Hello from ISAPI
Most fields within the function body are closely related to simple HTML code that provides basic information. You also need to fill in some fields in the TExtensionControlBlock as shown below.
Notice that there is a function pointer called WriteClient in this record. You can refer to this function to pass information
Send back to the browser. When calling this function, you use the ConnID field in the TExtensionControl block mentioned below. When the function is called, the ConnID is automatically populated for you.
Before looking at the code of the function, please let me demonstrate for you all the HttpExtensionProc functions mentioned above
Complete program of ISAPI DLL
library Isapi1;
library Isapi1;
uses
Windows
SysUtils
HTTPExt;
function GetExtensionVersion( var Ver: THSE_VERSION_INFO ): BOOL; stdcall;
begin
Ver.dwExtensionVersion := $00010000; // We're expecting version 1.0 support
Ver.lpszExtensionDesc := 'Written in Delphi 2.0';
Result := True;
end;
function HttpExtensionProc( var ECB: TEXTENSION_CONTROL_BLOCK ): DWORD;
stdcall;
var
ResStr: string;
StrLen: Integer;
begin
ECB.lpszLogData := 'Delphi DLL Log';
ECB.dwHTTPStatusCode := 200;
ResStr := '
' +
'
Test server results
' +
'
Isapi says hello to DevRel
';
ResStr := Format(
'HTTP/1.0 200 OK'#13#10+
'Content-Type: text/html'#13#10+
'Content-Length: %d'#13#10+
'Content:'#13#10#13#10'%s'
[Length(ResStr)
ResStr]);
StrLen := Length(ResStr);
ECB.WriteClient(ECB.ConnID
Pointer(ResStr)
StrLen
0);
Result := HSE_STATUS_SUCCESS;
end;
exports
GetExtensionVersion
HttpExtensionProc;
begin
end.
In order to run this DLL program, you should copy it to the scripts directory under your NT server. In my NT4.0 machine
It goes like this:
c:winntsystem32inetsrvscriptsmystuffisapi1.dll
In this example I have created my directory called "mystuff"
It's just used to store what I created
ISAPI DLLs. Your directory, of course, will not be exactly the same as it is on my machine, depending on the location of your "inetsrv" directory and other factors.
To successfully call this DLL, you should add this hyperlink to your HTML page:
ISAPI One
When the user clicks this hyperlink, the ISAPI1 Dll will be called, and then the string "Hello from ISAPI" will be displayed
displayed on the user's browser. If you do not place ISAPI.DLL in the mystuff directory, then you should modify the above HTML code to adapt it to your situation. Note that your directory must be relative to the directory inetsrv and should not, and cannot, contain the directory where your entire DLL is located.
Here is the complete HTML script of the call:
This is the home page for my home computer.
ISAPI One
Note that if you copy the program ISAPI1.DLL to the mystuff directory multiple times, you should
Close the web server's World Wide Web port. This is because, the first time you copy this DLL, you are free from restrictions, but after that, it belongs to the server. Therefore, when you copy the updated version of the first copy, you should turn off the World Wide Web service. You can use a network management program to turn off the World Wide Web service. This program should be under the Microsoft Internet Server group and be installed under the Explorer/Program Manager when installing Network Information Services.
Works with TExtensionControlBlock
With this point in this article, you will be able to build your first ISAPI DLL and run it online on a second machine.
The web browser calls it.
The rest of ISAPI will be more in-depth in this article.
Here is the more complicated part of the HttpExtensionProc parameters
PExtensionControlBlock = ^TExtensionControlBlock;
TExtensionControlBlock = packed record
cbSize: DWORD; // = sizeof(TExtensionControlBlock)
dwVersion: DWORD; // version info of this spec
ConnID: HCONN; // Context Do not modify!
dwHttpStatusCode: DWORD; // HTTP Status code
// null terminated log info specific to this Extension DLL
lpszLogData: array [0..HSE_LOG_BUFFER_LEN-1] of Char;
lpszMethod: PChar; // REQUEST_METHOD
lpszQueryString: PChar; // QUERY_STRING
lpszPathInfo: PChar; // PATH_INFO
lpszPathTranslated: PChar; // PATH_TRANSLATED
cbTotalBytes: DWORD; // Total bytes from client
cbAvailable: DWORD; // Available number of bytes
lpbData: Pointer; // pointer to cbAvailable bytes
lpszContentType: PChar; // Content type of client data
GetServerVariable: TGetServerVariableProc;
WriteClient: TWriteClientProc;
ReadClient: TReadClientProc;
ServerSupportFunction: TServerSupportFunctionProc;
end;
Notice that this record contains the ConnID field mentioned above, and passes the first parameter to WriteClient.
The first parameter in this record is for version control. It should be the size of the TExtensionControlBlock specified. If Microsoft changes its structure, they can determine which version of the structure they are dealing with by checking the record size. You never want the first three fields in this record, they have already been populated by ISAPI, and they can only be accessed, not changed, in your program.
Probably the most important field in this record is lpszQueryString, which contains the request from the server
information. For example, assume you have created a file called ISAPI1.Dll. In order to call this DLL, you create a HREF like this [Note: a format in HTML language (translator)] on a page in your browser:
Test One
If you want to respond to this DLL, you need to change the above line like this:
Test One
If the HTML code snippet contains something like the second of the two lines above, then your DLL will have the lpszQueryString parameter
Get the string of "MyQuery", pay special attention to the use of the request flag following the request string.
Of course, you can change the request string however you like. For example, you can write:
Test One
In this request, this DLL will reply with the name of the server. You are not subject to any restrictions when passing this parameter. you
You can pass whatever you want, and it's up to you how you want to analyze the information in the DLL.
When you return information from the server to the browser, you use the "WriteClient" function pointer in this record
. You don't need to do anything when initializing this pointer; it is automatically passed to you by the network information server.
Authors of CGI applications will note that the syntax for passing request strings is very familiar. In fact, ISAPI followed CGI
As with most conventions, most fields in TExtensionControlBlock can simply be borrowed by CGI technology.
Another key field in TExtensionControlBlock is lpbData, which contains additional information uploaded to you from browsing.
For example, if you have an HTML form with several fields, the information contained in these fields will be called "
lpData" pointer. The next topic in this article, "Obtaining information from the 'OK' button," will focus on how to handle this situation.
So far I have introduced the four key fields in TExtensionControlBlock:
WriteClient: A pointer that allows you to pass formatted HTML data to the browser. This function is used
ConnID field of TExtensionControlBlock.
lpszQueryString: The request coming from the browser.
lpbData: Additional data uploaded to you from the browser. Usually the content of any field in an HTML form
. I'll discuss this further in the "Confirm Button" section.
To get a feel for how the other fields in TExtensionControlBlock work, the best way is to
Browse and compare them. In other words, you will want to create an HTML page that enables the user to call the client's ISAPI DLL. The purpose of this ISAPI DLL is simply to format each field in the TExtensionControlBlock in HTML and then pass them back to the browser. This turns your browser into a somewhat scary debugger that displays all the fields in the TExtensionControlBlock.
Here is a program, written by Danny Thorpe of Borland, that performs this task:
library test1;
uses
Windows
SysUtils
HTTPExt;
function GetExtensionVersion( var Ver: THSE_VERSION_INFO ): BOOL; stdcall;
begin
Ver.dwExtensionVersion := $00010000; // 1.0 support
Ver.lpszExtensionDesc := 'A test DLL written in Delphi 2.0';
Result := True;
end;
function HttpExtensionProc( var ECB: TEXTENSION_CONTROL_BLOCK ):
DWORD; stdcall;
var
ResStr: string;
StrLen: Integer;
Buf: array [0..1024] of Char;
begin
ECB.lpszLogData := 'Delphi DLL Log';
ECB.dwHTTPStatusCode := 200;
ResStr := Format(
' +
'
'Size = %d
'+
'Version = %.8x
'+
'ConnID = %.8x
'+
'Method = %s
' +
'Query = %s
' +
'PathInfo = %s
'+
'PathTranslated = %s
'+
'TotalBytes = %d
'+
'AvailableBytes = %d
'+
'ContentType = %s
'+
'
[ECB.cbSize
ECB.dwVersion
ECB.ConnID
ECB.lpszMethod
ECB.lpszQueryString
ECB.lpszPathInfo
ECB.lpszPathTranslated
ECB.cbTotalBytes
ECB.cbAvailable
ECB.lpszContentType]);
with ECB do
begin
StrLen := Sizeof(Buf);
GetServerVariable(ConnID
'REMOTE_ADDR'
@Buf
StrLen);
ResStr := ResStr + 'REMOTE_ADDR = '+Buf+'
';
StrLen := SizeOf(Buf);
GetServerVariable(ConnID
'REMOTE_HOST'
@Buf
StrLen);
ResStr := ResStr + 'Remote_Host = '+Buf+'
';
StrLen := SizeOf(Buf);
GetServerVariable(ConnID
'REMOTE_USER'
@Buf
StrLen);
ResStr := ResStr + 'Remote_User = '+Buf+'
';
StrLen := SizeOf(Buf);
GetServerVariable(ConnID
'SERVER_NAME'
@Buf
StrLen);
ResStr := ResStr + 'SERVER_NAME = '+Buf+'
';
StrLen := SizeOf(Buf);
GetServerVariable(ConnID
'SERVER_PORT'
@Buf
StrLen);
ResStr := ResStr + 'SERVER_PORT = '+Buf+'
';
StrLen := SizeOf(Buf);
GetServerVariable(ConnID
'SERVER_PROTOCOL'
@Buf
StrLen);
ResStr := ResStr + 'SERVER_PROTOCOL = '+Buf+'
';
StrLen := SizeOf(Buf);
GetServerVariable(ConnID
'SERVER_SOFTWARE'
@Buf
StrLen);
ResStr := Format('%sSERVER_SOFTWARE = %s
'+
'ThreadID = %.8x
'
[ResStr
Buf
GetCurrentThreadID]);
end;
ResStr := ResStr + ';
ResStr := Format(
'HTTP/1.0 200 OK'#13#10+
'Content-Type: text/html'#13#10+
'Content-Length: %d'#13#10+
'Content:'#13#10#13#10'%s'
[Length(ResStr)
ResStr]);
StrLen := Length(ResStr);
ECB.WriteClient(ECB.ConnID
Pointer(ResStr)
StrLen
0);
Result := HSE_STATUS_SUCCESS;
end;
xports
GetExtensionVersion
HttpExtensionProc;
egin
end.
In order to call this DLL, you should create an HRML script that includes the following line
Test One
Get information from "Confirm" button
Usually HTML forms that send you information have a confirm button in them. As long as the amount of information is less than 49KB, you can
Consider the lpbData field in TExetensionControlBlock to be available. Here's how you can
In most cases the information sent by the pointer to this field is obtained:
var
S: string;
begin
…
S := PChar(ECB.lpbData);
…
end;
If the information coming from this field is larger than 48KB, then you must call ReadClient to get the remaining information.
If you want to know exactly what information is available in the lpbData field, you can use the following two functions to pass the data back to your web browser:
function SetUpResString: string;
begin
Result := ' +
' +
'
'lpbData = %s ' +
';
end;
function HttpExtensionProc(var ECB: TExtensionControlBlock):
DWORD; stdcall;
var
ResStr: string;
StrLen: Integer;
S
S1: string;
begin
ECB.lpszLogData := 'Delphi DLL Log';
ECB.dwHTTPStatusCode := 200;
ResStr := SetUpResString;
S := PChar(ECB.lpbData);
ResStr := Format(ResStr
[S]);
StrLen := Length(ResStr);
ECB.WriteClient(ECB.ConnID
Pointer(ResStr)
StrLen
0);
Result := HSE_STATUS_SUCCESS;
end;
Assume you already have an HTML form with the following code attached:
This code will produce a form with a text area for you to enter numbers and a "submit" button called "GetSquare". If you have this form, then you can expect that the above two programs will return the following string, assuming the user enters the number 23 in the text area of the form:
lpbData = GetSquare=23&GetSquare=Submit
In order to understand what is happening at this time, pay attention to the main part of the HTML statement excerpted from the above function. This part of the statement resides on the server and is reflected as follows:
'lpbData = %s ' +
If you study the code in the HttpExtensionProc function above, you will find that just before this sentence, it uses the %s parameter in the Format statement instead of the value in ECB.lpbData. (If you don't know how the statement Format works, please refer to the relevant Delphi documentation) [Note: In the author's Delphi2 Programming Encyclopedia (Delphi2
There are detailed instructions in Chapter 3 "Strings and Text Files" in Unleashed (Translator)]
Assume that in the form shown above, when the user presses the "Confirm" button, the value passed by lpbData to the ISAPI DLL is:
GetSquare=23&GetSquare=Submit
To give you a clear idea, let me repeat that the information passed back to the browser by the above two statements is the following string, which you have already seen:
lpbData = GetSquare=23&GetSquare=Submit
The best way to see this process is to test run the ISAPI2 program listed below. ISAPI2 is similar to ISAPI1, but it includes the new HttpExtensionProc function shown above, and it also includes the SetUpResString utility function.
library Isapi2;
uses
Windows
SysUtils
HTTPExt;
function GetExtensionVersion(var Ver: THSE_VERSION_INFO):
BOOL; stdcall;
begin
Ver.dwExtensionVersion := $00010000; // 1.0 support
Ver.lpszExtensionDesc := 'DLL written in Delphi 2.0';
Result := True;
end;
function SetUpResString: string;
begin
Result := ' +
' +
'
'lpbData = %s ' +
';
end;
function HttpExtensionProc( var ECB: TEXTENSION_CONTROL_BLOCK ):
DWORD; stdcall;
var
ResStr: string;
StrLen: Integer;
S
S1: string;
Len: Integer;
begin
ECB.lpszLogData := 'Delphi DLL Log';
ECB.dwHTTPStatusCode := 200;
ResStr := SetUpResString;
S := PChar(ECB.lpbData);
ResStr := Format(ResStr
[S]);
StrLen := Length(ResStr);
ECB.WriteClient(ECB.ConnID
Pointer(ResStr)
StrLen
0);
Result := HSE_STATUS_SUCCESS;
end;
exports
GetExtensionVersion
HttpExtensionProc;
begin
end.
Once you have obtained the information passed by the lpbData variable from the form, you can parse the information or return it to the user. For example, you could extract the number 23 from the example above, square it and return it to the user. Doing this allows you to get information from the user, in this case numbers, perform some mathematical operations on the numbers, and finally return the result to the user. This means you can create interactive web pages right on the airwaves, which is the most popular aspect of Internet programming right now!
The following is a complete program code that submits the square of a number to the browser over the network:
library Isapi3;
{ This code shows how to take input from the user via a browser
parse that information
and then return an answer to the user. In particular
the user submits a number
this code squares it
and then sends the result back to user. Here is the form from the browser that submits the information for parsing:
}
uses
Windows
SysUtils
HTTPExt
StrBox;
function GetExtensionVersion(var Ver: THSE_VERSION_INFO):
BOOL; stdcall;
begin
Ver.dwExtensionVersion := $00010000; // version 1.0 support
Ver.lpszExtensionDesc := 'ISAPI3.DLL';
Result := True;
end;
// Parse lpbData and retrieve the number the user passed to us.
function ParseData(S: string): Integer;
begin
S := StripLastToken(S
'&');
S := StripFirstToken(S
'=');
Result := StrToInt(S);
end;
function SetUpResString: string;
begin
Result := ' +
' +
'
'Answer = %d ' +
';
end;
function HttpExtensionProc( var ECB: TEXTENSION_CONTROL_BLOCK ):
DWORD; stdcall;
var
ResStr: string;
StrLen: Integer;
S
S1: string;
Num: Integer;
begin
ECB.lpszLogData := 'Delphi DLL Log';
ECB.dwHTTPStatusCode := 200;
ResStr := SetUpResString;
S := PChar(ECB.lpbData);
Num := ParseData(S);
Num := Sqr(Num);
ResStr := Format(ResStr
[Num]);
StrLen := Length(ResStr);
ECB.WriteClient(ECB.ConnID
Pointer(ResStr)
StrLen
0);
Result := HSE_STATUS_SUCCESS;
end;
exports
GetExtensionVersion
HttpExtensionProc;
begin
end.
This code accepts the following string from the user who pressed the confirm button and asked for the squared number:
GetSquare=5&GetSquare=Submit
Assuming input like this, this code will return the following string to the user over the Internet:
Answer = 25
In one sentence, the user enters the number 5, and you return the user the number 25. If the user submits the number 10, then you return the number 100. This may seem trivial, but what matters here is what happens on the Internet [Note: refers to interactive web pages (translated
who)]
Analyze the function passed by the user like this:
// Parse lpbData and retrieve the number the user passed to us.
function ParseData(S: string): Integer;
begin
S := StripLastToken(S
'&');
S := StripFirstToken(S
'=');
Result := StrToInt(S);
end;
These two statements are in the unit mentioned at the beginning of this article and are also included on my site. [Note: This document can be found almost everywhere on the Internet
You can also request it from the translator (Translator)][In this article
I just want to talk about ISAPI so much. This should be enough to inspire you to have fun taking advantage of this great technology. Next I want to talk about the two statements GetServerVariable and ReadClient. I have only conducted extremely limited experiments in this regard. In this article, I've attached the HTTPEXT.PAS file because you won't find it anywhere else but this critical document.
GetServerVariable and ReadClient statements
Just like requesting information in your CGI application, you can use statements to get information from the server. Here is an example of calling this statement:
Len := HseMaxExtDllNameLen;
SetLength(S1
Len);
Dec(Len);
ECB.GetServerVariable(ECB.ConnID
'CONTENT_LENGTH'
PChar(S1)
Len);
First, this code sets the length of the buffer that holds information retrieved from the server. It then calls the server and makes a request, in this case, it asks for the "CONTENT_LENGTH" of the message from the server.
Microsoft documentation tells us that you can pass the following string as the second parameter of GetServerVariable:
AUTH_TYPE It contains the type of authorization used. For example, if you are using basic authorization, then
The string is "basic"; if it is an NT challenge response, the string is "NTLM". Other authorizations have specific corresponding strings. Because new authorization types are constantly being added to the server, it is not feasible to list all possible strings. If the string is empty, no authorization is used.
CONTENT_LENGTH The number of bytes the script expects to reclaim from the client.
CONTENT_TYPE The content type of the information provided by the body of the request advertisement. [Note: The younger brother is less talented and less knowledgeable, a
POST request is temporarily translated as "request notice", please correct me (translator)]
PATH_INFO Additional routing information, provided by the client. It contains the path to the URL following the script name
Depend on. If present, it precedes the request string.
PATH_TRANSLATED This is the value of PATH_INFO but contains the names of all virtual routes expanded to a path flag.
QUERY_STRING The information following the "?" in the URL that refers to this script.
REMOTE_ADDR The IP address of the requesting client or its agent (for example, a gateway or firewall).
REMOTE_HOST The host name of the requesting client or its agent (for example, a gateway or firewall).
REMOTE_USER This contains the user name provided by the client and authorized by the server. If an empty string is returned then the user
in your name (but with authorization).
UNMAPPED_REMOTE_USER It is the name of a user with the following characteristics: This user makes a request to the NT user account (this is his identity), before which the ISAPI application filter mapped the user.
REQUEST_METHOD is the HTTP request method.
SCRIPT_NAME The name of the script executed.
SERVER_NAME The hostname or IP address when it appears in self-referential URLs.
SERVER_PORT The TCP/IP port that accepts requests.
SERVER_PORT_SECURE A string that is either 0 or 1. It is 1 when the request is handled by a secure port; 0 otherwise.
SERVER_PROTOCOL Accepts the name and version of the protocol information associated with this request. It is usually HTTP/1.0.
SERVER_SOFTWARE is the name and version of the web server where the ISAPI application DLL program runs. ALL_HTTP The previous variable did not parse all HTTP headers. These variables are derived from HTTP_<field header name>. Field headers (separated by line markers) contain individual strings, which are not terminated.
HTTP_ACCEPT Special case of HTTP field header. Accepted values are: fields separated by commas (,). For example: if
The following lines are part of the HTTP header:
Accept: */*, q=0.1
Then the URL (a new feature in version 2.0) gives its basic part.
Note that the information piece given above is automatically passed by the TExtensionControlBlock record. because
You don't need to call GetServerVariable for this. However, you can call it if you really need it, especially if you want to get information from ReadClient and need to know how much information to read.
Many times, you don't need to call ReadClient. However, the size of the message sent by your browser is greater than 48KB
time, you need to call ReadClient to get the rest of the information.