ISAPI (Internet Server API) was initially a CGI application development interface provided by Microsoft for the IIS Server. Its main purpose is to provide a good development interface for CGI development, if ISAPI is not responsible, it can be considered as a development mode similar to WinCGI, except that ISAPI obtains parameters transmitted by user forms through ing macros. This is similar to the Message ing macro of MFC. Of course, the launch of ISAPI also has many other features:
- ISAPI is implemented through DLL dynamic connection library. Therefore, it is faster to load normal EXE programs, and the system will not immediately remove the DLL space in the memory after use, therefore, it will get a faster speed when used again.
- The ISAPI runs in the thread mode inside the caller, so it requires less runtime space than the CGI process.
- Multiple processing functions can be centralized in the same DLL, and the macro is mapped to indicate the types of requests processed by different functions.
- Because it is integrated with IIS, you can use ISAPI to develop ISAPI filters. You can use a filter to perform functions such as user permission detection, data encryption, compression, and logs. The functions of the IIS server can also be enhanced using the ISAPI filter.
So far, many non-M $ WEB servers have added support for isapis. Multiple processing functions can be concentrated in the same DLL. Therefore, there is a difference between an ISAPI request and a common CGI request. In the URL, you must enter the following form: http ://... /cgi-bin/test. dll? Function_name & name = xxx & email = yyy, function_name indicates the function name. to be correct, you must map it to a handler in the DLL. A default function is used to process unmapped functions. VC4.2 and later versions provide a Wizard for creating an ISAPI program. When creating a project, select ISAPI Extension Wizard and select create Server Extension and enter relevant information in the subsequent dialog box. A simplest ISAPI program contains at least a new class derived from the CHttpServer class, and performs basic ing in the class, reloading BOOL GetExtensionVersion (HSE_VERSION_INFO * pVer) function and a member function in the form of void Default (CHttpServerContext * pCtxt. Fortunately, the Wizard program has done everything for us and created the most basic code. The Default function is used to process the code in the Default function without parameters. First, we should write the relevant Code as follows: Void CTestisaExtension: Default (CHttpServerContext * pCtxt) {// Print the <HTML> <BODY> tags. startContent (pCtxt); // Print the title. writeTitle (pCtxt); * pCtxt <_ T ("<p> demo </p> "); * pCtxt <_ T ("<p> currently the Default member function </p>"); // Print </HTML> </BODY> tags. endContent (pCtxt );} Then, use the following method to call: http: //.../cgi-bin/test. dll? Default or http: //.../cgi-bin/test. dll ?. You will see the output HTML page. <FORM ACTION="test.dll?Add" METHOD=POST><INPUT NAME="name"><INPUT NAME="id"><INPUT TYPE=SUBMIT></FORM> The above form requires the user to enter the user name and ID, so the form of transmitted data should be like: http: //.../test. dll? Add & name = xxx & id = yyy, the decomposed data is stored in the specified variable. Usage: ON_PARSE_COMMAND(Add, CTestExtension, ITS_PSTR ITS_I4)ON_PARSE_COMMAND_PARAMS("name id") In the ON_PARSE_COMMAND macro, you must specify the function name, class name, and parameter type list. In the ON_PARSE_COMMAND_PARAMS macro, you must specify the variable name list in the form based on the list of parameters listed in the previous macro. The last step is to define a name and request URL? After the command name is the same as the member function, the parameter settings must be consistent with the parameter list definition in the ON_PARSE_COMMAND macro, in this example: Void CTestExtension: Add (CHttpServerContext * pCtxt, LPTSTR pszName, int iID ). The following types can be used in the parameter type list for description:
- ITS_EMPTY no data
- ITS_PSTR string LPCSTR
- ITS_I2 short
- ITS_I4 long
- ITS_R4 float
- ITS_R8 double
The following shows a more complex form, which is defined as follows: <FORM ACTION="test.dll?Delete" METHOD=POST><INPUT NAME="name"><INPUT NAME="month"><SELECT NAME="mode"><OPTION VALUE=1>All<OPTION VALUE=2>Before<OPTION VALUE=3>After<INPUT TYPE=HIDDEN NAME=pwd VALUE=xxx><INPUT TYPE=SUBMIT></FORM> The ing and processing functions are defined as follows: ON_PARSE_COMMAND(Add, CTestExtension, ITS_PSTR ITS_I4 ITS_I4 ITS_PSTR)ON_PARSE_COMMAND_PARAMS("name month mode pwd")void CTestExtension::Add(CHttpServerContext* pCtxt,LPTSTR pszName,int iMonth,int iMode,LPTSTR pszPwd)。 ISAPI program output: In all command processing functions, the first parameter is the CHttpServerContext * pointer, and all our output must be carried out through it. The <operator is defined in the CHttpServerContext class, with this operator, we can easily output strings, numbers, and binary data. The output method is as follows: *pCtxt<<"this is a string";*pCtxt<<'c';*pCtxt<<10; It can be seen that the output is very simple. A complete output process should be in the following format: StartContent (pCtxt); // start to output WriteTitle (pCtxt); // output header information, equivalent to output <TITLE> * pCtxt <"your string"; EndContent (pCtxt ); // end output The ISAPI extension class we use is derived from CHttpServer. You can call CHttpServer: AddHeader to specify the type of returned data. For example, the following code demonstrates the output of plain text: void CTestisaExtension::Default(CHttpServerContext* pCtxt){AddHeader(pCtxt, "Content-type = text/plain/r/n");(*pCtxt) << "Hello world!/r/n"; } We can reload some functions to enhance control. The following functions can be reloaded:
- Virtual LPCTSTR CHttpServer: GetTitle () const; <TITLE> partial information is returned.
- Virtual BOOL CHttpServer: OnParseError (CHttpServerContext * pCtxt, int nCause); error handling
- Virtual void CHttpServer: StartContent (CHttpServerContext * pCtxt) const; Output <HTML> <BODY> partial information
- Virtual void CHttpServer: EndContent (CHttpServerContext * pCtxt) const; Output </BODY> </HTML> partial information
- Virtual void CHttpServer: WriteTitle (CHttpServerContext * pCtxt) const; Output <TITLE> </TITLE> partial information
In addition, we can use the CHttpServerContext pointer in the command processing function to obtain the relevant environment variables in CGI. In CHttpServerContext, a member variable m_pECB is the following structure pointer. Typedef struct _ EXTENSION_CONTROL_BLOCK {DWORD cbSize; // IN this structure length DWORD dwVersion // IN version HCONN ConnID; // IN connection with the context DWORD dwHttpStatusCode; // OUT status code CHAR lpszLogData [encoding]; // out lpstr lpszMethod; // IN environment variable REQUEST_METHOD LPSTR lpszQueryString; // IN QUERY_STRING LPSTR lpszPathInfo; // IN PATH_INFO LPSTR encoding; // IN PATH_TRANSLATED DWORD cbTotalBytes; // IN CONTENT_LENGTH DWORD cbAvailable; // in lpbyte lpbData; // in lpstr lpszContentType; // IN CONTENT_TYPE... other information is ignored here} EXTENSION_CONTROL_BLOCK, * LPEXTENSION_CONTROL_BLOCK; Return |