Request Handler DLL

The request handler DLL produced by the ATL Server Project Wizard is designed so that the developer edits code in a header file named after a project name. The prototype for a class named C<<Project Name>>Handler is in the header file. In the case of the project NewSimpleATLServer, the class was named CNewSimpleATLServerHandler. In the following code listing for NewSimpleATLServer.h, this class is designed to be the gateway between the classes that are the web application software you are making and the SRFs. All the code could be placed in the handler class, but it would make the code unmanageable. The best strategy is to use this class to host only the attributes for SRF replacement tags and to build classes for the system according to the software design.

// NewSimpleATLServer.h : Defines the ATL Server request handler class // #pragma once [ request_handler("Default") ] class CNewSimpleATLServerHandler { private:    //locals for holding the state of the class    CString FName;             //first name    CString LName;             //last name    CString FavoriteColor;   //name of color    //flag to identify if color and name are known    bool HaveNameandColor;    public:    //initialization function     HTTP_CODE ValidateAndExchange()    {     //for debug purposes     ATLTRACE("ValidateAndExchange started \n");     //used to validate data passed in     CValidateContext c;     //assume that we have everything     HaveNameandColor = true;       //get the form fields      const CHttpRequestParams& Formdata =         m_HttpRequest.GetFormVars();     //Check validation failures     if (VALIDATION_S_OK !=      Formdata.Validate("firstname", &FName, 1, 10000, &c))           HaveNameandColor = false;     if (VALIDATION_S_OK !=      Formdata.Validate("lastname", &LName, 1, 10000, &c))           HaveNameandColor = false;     if (VALIDATION_S_OK !=     Formdata.Validate("color", &FavoriteColor, 1, 10000, &c))           HaveNameandColor = false;     // Set the content-type     m_HttpResponse.SetContentType("text/html");       ATLTRACE("ValidateAndExchange completed \n");     return HTTP_SUCCESS;  }   protected:  [ tag_name(name="Hello") ]  HTTP_CODE OnHello(void)  {     ATLTRACE("OnHello started \n");     m_HttpResponse <<<< "Hello World!";       ATLTRACE("OnHello completed \n");     return HTTP_SUCCESS;  }  [ tag_name(name = "HaveNameAndColor") ]  HTTP_CODE OnHaveNameAndColor(void)  {     ATLTRACE("OnHaveNameAndColor started \n");     if (HaveNameandColor)        return HTTP_SUCCESS;     else        return HTTP_S_FALSE;  }  [ tag_name(name = "YourName") ]  HTTP_CODE OnYourName(void)  {     m_HttpResponse <<<< FName + " " + LName;     return HTTP_SUCCESS;  }  [ tag_name(name = "YourFavoriteColor") ]  HTTP_CODE OnYourFavoriteColor(void)  {     m_HttpResponse <<<< FavoriteColor;     return HTTP_SUCCESS;  } }; // class CNewSimpleATLServerHandler

In addition to satisfying the purpose of hosting the replacement tag attributes, the class CNewSimpleATLServerHandler exists primarily for the following purposes:

  • Saying Hello World

  • Validating the user input

  • Reporting back to the user his or her name and favorite color

All the implementation code for the project NewSimpleATLServer is in header file for the sake of simplifying the demonstration of this example. The ValidateAndExchange function is the first function called when a request is processed through the handler DLL, so the m_HttpRequest class instance will be inspected for HTTP-posted data using the m_HttpRequest.GetFormVars function and placing the data into an instance of ChttpRequestParams using a reference named Formdata. The Validate function of Formdata will determine whether the specified input value was submitted, will determine whether it meets the range specified, and will place the value in a variable. For example, the first name input value is validated using the following line in the code snippet

Validate("firstname", &FName, 1, 10000, &c)

where FName is a CString variable in which the first name will be placed if it is longer than 1 character and shorter than 10,000 characters.

The ATLTRACE macro is sprinkled throughout the listing for CNewSimpleATLServer Handler with various debugging phrases as the argument. The ATLTRACE will place the argument in the WebDbg window if it is running on the host, as shown in Figure 16-12. WebDbg is a utility that ships with the Visual Studio .NET tools. The start link is usually found under the Visual Studio .NET Tools program group and is labeled ISAPI Web Debug Tool in the Start menu.

click to expand
Figure 16-12: ATLTRACE phrases written to WebDbg

After it is started, WebDbg needs to be attached to the pipe name AtlsDbgPipe, after you choose File | Select Pipe | Pipe Name. The process that is being debugged must have the permission to write to the named pipe. The credentials under which IIS is running should be set by choosing File | Permissions. Allowing the group Everyone to have permission to write to the pipe should work if WebDbg is running on the same machine as the web site, but if the web site is running remotely, the credentials for the specific machine or domain will likely need to be specified as well.

Once WebDbg is running, it listens to the pipe for messages from software writing to the pipe. As the messages are written, they appear instantly in the WebDgb window. Although stepping through the code using the Visual Studio .NET IDE (Integrated Development Environment) is a good way to test the code initially, the trace commands may be useful for environments in which the software will be hosted after it is moved from the development server environment. The ATLTRACE works only in the debug configuration. If the solution was compiled in the release configuration, the ATLTRACE macros are ignored, so there is no need to remove any of the debug code. Running the project NewSimpleATLServer will tell the user hello world because it always tells the user hello world. The second function called is the OnHaveNameAndColor function using the HaveNameandColor replacement tag in the SRF. OnHaveNameAndColor simply checks the value of the local variable HaveNameandColor to see whether or not it returns true and returns the corresponding answer so that the SRF can either display the information or prompt the user for his or her name and favorite color. Figure 16-13 shows an example of NewSimpleATLServer running after the user entered information. In the figure, the SRF checked the value returned from HaveNameandColor and since it was positive, the OnYourFavoriteColor and OnYourName functions were called using the replacement tags YourFavoriteColor and YourName, respectively.

click to expand
Figure 16-13: NewSimpleATLServer running after the user entered information




IIS 6(c) The Complete Reference
IIS 6: The Complete Reference
ISBN: 0072224959
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net