Template Information

Trang

Achieving a fully functional webserver in Windows CEB

Thứ Ba, 20 tháng 9, 2011 / 10:11

Abstract

When you implement a webserver on Windows CE, developers would want to implement most of the features available with webservers on PC platforms. However, this is not the case the windows CE IIS doesn’t support some of the features like maintaining the session ID etc. This article portrays how by using the Internet server application programming interface (ISAPI) we can achieve these features. Finally, this article also gives an example of how to do this.

Windows CE Webserver Bock diagram
Block Diagram
Isapi Extension Dll

The Windows CE IIS functionality doesn’t support two features that are sometimes required by Web servers. They are
  • States between Transactions
  • Session Id Management
ISAPI(Internet server application programming interface) Extensions come to the rescue. We can use them to support these features. Here is how it can be done.
ISAPI is an API developed to provide the application developers with a powerful way to extend the functionality of IIS. To create an ISAPI extension for the Windows CE Web Server, a DLL that exports the following standard ISAPI entry points is needed to be built.
  • GetExtensionVersion
  • HttpExtensionProc and
  • TerminateExtension (optional)
GetExtensionVersion is called when the DLL is first loaded, HttpExtensionProc is called for each request, and TerminateExtension is called when the DLL is unloaded. Interaction with the Web server is performed through the standard ISAPI callback functions like ReadClient, WriteClient, etc.
Since all the requests are handled by this dll alone, the following features can be implemented in this.
  • Session Id management
  • Providing Login Authority
  • Sending Customized files
  • The real time variables such as CPU temperature, CPU Usage, Fan Speeds, connected devices and the IP settings are obtained by calling corresponding API’s and embedded with the pages easily.

How to load Isapi Extension Dll

In addition to the addition of web server component in the build, the following registry entries should be added to load the dll automatically.


[HKEY_LOCAL_MACHINE\COMM\HTTPD\VROOTS\/]
  @="\\windows\\sampleisapi.dll"
  "a"=dword: 0 ;to remove the authentication

Web server loads this dll, when it receives the first request from any of its clients. And this dll is loaded only once and can handle the requests from any number of clients.
Functions to be exported

HttpExtensionProc function is called for each request ; handling of request is done here.

1. GetExtensionVersion
This function is called when the Dll is loaded first time.

2. HttpExtensionProc
This is the only primary function of this dll. This function is called for every request from the client. GetServerVariable function can be used to get the Query String value and according to the query string that is received, the corresponding file that has been requested is written back to the client using the WriteClient function. To read the data from the client, ReadClient function is called.
The prototype of this function is as follows.

DWORD WINAPI HttpExtensionProc (LPEXTENSION_CONTROL_BLOCK lpECB);
Where LPEXTENSION_CONTROL_BLOCK is declared as follows.

typedef struct _ EXTENSION_CONTROL_BLOCK
{
  DWORD cbSize;
DWORD dwVersion;
HCONN ConnID;
DWORD dwHttpStatusCode;
CHAR lpszLogData[HSE_LOG_BUFFER_LEN];
LPSTR lpszMethod;
LPSTR lpszQueryString;
LPSTR lpszPathInfo;
LPSTR lpszPathTranslated;
DWORD cbTotalBytes;
DWORD cbAvailable;
LPBYTE lpbData;
LPSTR lpszContentType;
BOOL (WINAPI* GetServerVariable);
BOOL (WINAPI* WriteClient);
BOOL (WINAPI* ReadClient);
BOOL (WINAPI* ServerSupportFunction);
} EXTENSION_CONTROL_BLOCK, LPEXTENSION_CONTROL_BLOCK;

3. TerminateExtension
This function is called when the dll is being unloaded.

GetServerVariable

The GetServerVariable function is used to get the server variables like IP Address, Query String, etc

1. Prototype

BOOL WINAPI GetServerVariable(HCONN hConn, LPSTR lpszVariableName, VOID lpvBuffer, LPDWORD lpdwSizeofBuffer );

2. Parameters
hConn
Specifies the connection handle. This is the input of HttpExtensionProc function.

lpszVariableName
A null-terminated string that indicates which server variable is requested

lpvBuffer
Points to the buffer to receive the requested information.

lpdwSizeofBuffer
Points to a DWORD that indicates the size of the buffer pointed to by lpvBuffer. On successful completion, the DWORD contains the size of bytes transferred into the buffer, including the null-terminating byte.Some of the important variables that can be retrieved using GetServerVariable function are as follows.
PATH_INFO – The Path info given in the trailing part of the URL, as given by the client. QUERY_STRING – Specifies the information that follows the first question mark in the URL. REMOTE_ADDR – Specifies the IP address of the Client.
Sample Implementation

As already mentioned, The main uses of using Isapi Extension Dll are maintaining session id and also in getting the real time variables like IP address settings, etc.

1. Call External DLL with String Parameter and insert returned string into HTML output sent to the Client

As mention already, the html and java script files that to be sent to the client is available in the storage device. Now before stroing those file in the storage device, a particular symbol or sequence of symbols can be included in the html to indicate the ISAPI dll that this should be replaced by some other real time variable. For example the following format can be included in the html file


!!DllName!!FunctionName!!

For example if you want to show the IP address of the device, you can add the following sequwnce of symbols to tell ISAPI to replace that sequence of symbols with the IP address.


!!NetStatusDll!!GetIPAddress!!

Before sending the file to the client, the ISAPI dll will parse through the file for the sequence of symbols and when it finds the above sequence, it finds the dll name and the function name from the sequence and it calls the retrieved function after loading the dll and will replace that sequence with the string returned by the function. In above example, the NetStatus.dll is loaded using LoadLibrary API and the address for the function GetIPAddress is obtained by calling GetProcAddress API and that function is called.
In the same way, the post message data also can have sequence of symbols when the user wants to set real time variables in the device. For example if the user wants to set new IP address for the devide from the remote client, the sequence of symbols with dll name and function name can be added in the post message data when clicking some button in the web page.

2. Maintaining the Session Id
The following table can be created and maintained for the session id management.

  IP Address   Session Id   Last Tick Count (ms)
   176.234.11.23    12avcdefdef    10000
   23.123.45.6    234rfvdadds    23456
   145.67.89.90    123456asdfg    45678
 
When receiving the request from a new client (i,e new IP address), an random session id is created for that IP address and will be stored in the XML database. Whenever the request is received by any client, a check will be made for the pre existence of client in the table, and the page can be streamed to the client if the entry is available. If the entry was not available, only the login page will be thrown to the client. In this, the last tick time also can be added in the database which can be used for session expire.

Conclusion

Usage of Isapi Extension Dll in windows ce is explained. The Isapi Extension dlls are mostly used when the real time variables like CPU temperature, IP Address Settings etc, are used in the web page files.

0 nhận xét:

Đăng nhận xét