home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedWinsock.Cab / F112763_HttpServer.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-15  |  7.3 KB  |  265 lines

  1. #ifndef __HTTPSERVER_H__
  2. #define __HTTPSERVER_H__
  3.  
  4. /*
  5.  * Xceed Winsock Library Sample: Chat connectionless
  6.  * Copyright (c) 2000 Xceed Software Inc.
  7.  *
  8.  * [ HttpServer.h : CHttpServer declaration ]
  9.  *
  10.  * This is a minimal implementation of an HTTP server. It
  11.  * only handles the "GET" HTTP command. When a "GET" command
  12.  * is received from a connected client, the server will send
  13.  * the requested file to the client. This is enough functionality
  14.  * to display a web site with bitmaps and regular HTML files.
  15.  *
  16.  * In particular, it demonstrate:
  17.  *  - How to create and use a listening socket to handle
  18.  *    incoming connections.
  19.  *  - How to use string and file transfer interfaces.
  20.  *  - Using "#import" with the Xceed Winsock Library.
  21.  *  - Using ATL to help implement event interfaces.
  22.  *
  23.  * This file is part of the Xceed Winsock Library Samples.
  24.  * The source code in this file is only intended as a supplement
  25.  * to Xceed Winsock Library's documentation, and is provided "as is", 
  26.  * without warranty of any kind, either expressed or implied.
  27.  *
  28.  */
  29.  
  30. #include "xcdProtectedList.h"
  31.  
  32. class ATL_NO_VTABLE CHttpServer : 
  33.   // This is the base class of an ATL COM object
  34.   public CComObjectRootEx< CComMultiThreadModel >,
  35.   // These are the Xceed Winsock event interfaces we wish to implement
  36.   public IXWIncomingConnectionEvents,
  37.   public IXWConnectionEvents,
  38.   public IXWUnicodeStringTransferEvents,
  39.   public IXWFileTransferEvents
  40. {
  41. public:
  42.   //
  43.   // Constructor
  44.   //
  45.  
  46.   CHttpServer( void )
  47.   {
  48.     // We just initialize stuff, but everything that can fail
  49.     // should be done in FinalConstruct, since we can return an error.
  50.   }
  51.  
  52.   //
  53.   // Stuff required for ATL support
  54.   //
  55.  
  56. DECLARE_NO_REGISTRY()
  57.  
  58. DECLARE_PROTECT_FINAL_CONSTRUCT()
  59.  
  60. BEGIN_COM_MAP(CHttpServer)
  61.   COM_INTERFACE_ENTRY(IXWIncomingConnectionEvents)
  62.   COM_INTERFACE_ENTRY(IXWConnectionEvents)
  63.   COM_INTERFACE_ENTRY(IXWUnicodeStringTransferEvents)
  64.   COM_INTERFACE_ENTRY(IXWFileTransferEvents)
  65. END_COM_MAP()
  66.  
  67.   //
  68.   // Final construction and release
  69.   //
  70.  
  71.   HRESULT FinalConstruct( void );
  72.   void    FinalRelease( void );
  73.  
  74.   //
  75.   // Actions to take on server
  76.   //
  77.  
  78.   HRESULT Start( void );
  79.   HRESULT Stop( void );
  80.  
  81.   //
  82.   // IXWIncomingConnectionEvents 
  83.   //
  84.  
  85.   virtual HRESULT __stdcall raw_OnConnection (
  86.       struct IXWSocket * piListeningSocket,
  87.       struct IXWAddressInfo * piRemoteAddress,
  88.       unsigned long dwCallerDataSize,
  89.       unsigned char * pcCallerData,
  90.       unsigned long dwExpectedCalleeDataSize,
  91.       unsigned long * pdwCalleeDataSize,
  92.       unsigned char * * ppcCalleeData,
  93.       struct SXWQualityOfServiceInfo * * ppsQualityOfService,
  94.       unsigned long * pdwUserParam,
  95.       long * pbReject );
  96.  
  97.   virtual HRESULT __stdcall raw_OnConnectionProcessed (
  98.       struct IXWSocket * piListeningSocket,
  99.       struct IXWSocket * piIncomingSocket,
  100.       unsigned long dwUserParam );
  101.  
  102.   virtual HRESULT __stdcall raw_OnListeningError (
  103.       struct IXWSocket * piListeningSocket,
  104.       unsigned long dwUserParam,
  105.       HRESULT hResult );
  106.  
  107.   //
  108.   // IXWConnectionEvents 
  109.   //
  110.  
  111.   virtual HRESULT __stdcall raw_OnDisconnected (
  112.       struct IXWSocket * piSocket,
  113.       unsigned long dwCallerDataSize,
  114.       unsigned char * pcCallerData,
  115.       unsigned long * pdwCalleeDataSize,
  116.       unsigned char * * ppcCalleeData );
  117.  
  118.   //
  119.   // IXWUnicodeStringTransferEvents 
  120.   //
  121.  
  122.   virtual HRESULT __stdcall raw_OnUnicodeStringSent (
  123.       struct IXWUnicodeStringTransfer * piTransfer,
  124.       unsigned long dwUserParam,
  125.       HRESULT hResultCode );
  126.  
  127.   virtual HRESULT __stdcall raw_OnUnicodeStringReceived (
  128.       struct IXWUnicodeStringTransfer * piTransfer,
  129.       LPWSTR * ppwszString,
  130.       unsigned long dwUserParam,
  131.       HRESULT hResultCode );
  132.  
  133.   virtual HRESULT __stdcall raw_OnUnicodeStringAvailable (
  134.       struct IXWUnicodeStringTransfer * piTransfer,
  135.       unsigned long dwCharsReceived,
  136.       unsigned long dwCharsAvailable );
  137.  
  138.   virtual HRESULT __stdcall raw_OnOutOfBandUnicodeStringReceived (
  139.       struct IXWUnicodeStringTransfer * piTransfer,
  140.       LPWSTR * ppwszString,
  141.       HRESULT hResultCode );
  142.  
  143.   //
  144.   // IXWFileTransferEvents 
  145.   //
  146.  
  147.   virtual HRESULT __stdcall raw_OnFileSent (
  148.       struct IXWFileTransfer * piTransfer,
  149.       LPWSTR pwszFilename,
  150.       unsigned long dwStartOffset,
  151.       unsigned long dwBytesSent,
  152.       unsigned long dwBytesTotal,
  153.       unsigned long dwUserParam,
  154.       long bTransferCompleted,
  155.       HRESULT hResult );
  156.  
  157.   virtual HRESULT __stdcall raw_OnFileReceived (
  158.       struct IXWFileTransfer * piTransfer,
  159.       LPWSTR pwszFilename,
  160.       unsigned long dwStartOffset,
  161.       unsigned long dwBytesReceived,
  162.       unsigned long dwBytesTotal,
  163.       unsigned long dwUserParam,
  164.       long bTransferCompleted,
  165.       HRESULT hResult );
  166.  
  167. protected:
  168.  
  169. private:
  170.   //
  171.   // Listening socket
  172.   //
  173.  
  174.   IXWConnectionListenerPtr  m_piListener;
  175.  
  176.   //
  177.   // Information about each connection
  178.   //
  179.  
  180.   class CXcdConnectionInfo
  181.   {
  182.   public:
  183.     // new/delete operators
  184.     void* operator new( size_t xSize ) { return HeapAlloc( GetProcessHeap(), 0, xSize ); }
  185.     void  operator delete( void* pvBuffer ) { HeapFree( GetProcessHeap(), 0, pvBuffer ); }
  186.  
  187.     // Public members
  188.     _bstr_t     m_sRequest;
  189.     IUnknownPtr m_punkConnection; // We keep its base interface, for comparisons
  190.  
  191.     // Add a string to the complete request
  192.     void AppendRequestString( _bstr_t sString )
  193.     {
  194.       m_sRequest  += sString;
  195.     }
  196.  
  197.     // Check if this request is complete
  198.     bool IsRequestComplete( void )
  199.     {
  200.       WCHAR*  pwszString  = m_sRequest;
  201.  
  202.       return ( pwszString && wcsstr( pwszString, L"\r\n\r\n" ) != 0 );
  203.     }
  204.  
  205.     // Retrieve the relative filename from the request
  206.     _bstr_t GetRelativeFilename( void )
  207.     {
  208.       if( !IsRequestComplete() )
  209.         return _bstr_t();
  210.  
  211.       WCHAR*  pwszString  = m_sRequest;
  212.  
  213.       if( wcsstr( pwszString, L"GET " ) != pwszString )
  214.         return _bstr_t();
  215.  
  216.       pwszString  += 4;
  217.  
  218.       WCHAR*  pwszGetEnd  = wcschr( pwszString, L' ' );
  219.  
  220.       WCHAR wszRelative[ MAX_PATH+1 ];
  221.       wcsncpy( wszRelative, pwszString, pwszGetEnd - pwszString );
  222.       wszRelative[ pwszGetEnd - pwszString ]  = L'\0';
  223.  
  224.       if( wcscmp( wszRelative, L"/" ) == 0 )
  225.         wcscpy( wszRelative, L"/index.html" );
  226.  
  227.       // Convert each slash to a backslash
  228.       for( pwszGetEnd=wszRelative; *pwszGetEnd; pwszGetEnd++ )
  229.       {
  230.         if( *pwszGetEnd == L'/' )
  231.           *pwszGetEnd = L'\\';
  232.       }
  233.  
  234.       return _bstr_t( wszRelative );
  235.     }
  236.  
  237.     // Retrieve the full filename from the string
  238.     _bstr_t GetAbsoluteFilename( void )
  239.     {
  240.       TCHAR tszModuleFilename[ MAX_PATH + 1 ] = "";
  241.  
  242.       if( GetModuleFileName( NULL, tszModuleFilename, MAX_PATH ) )
  243.       {
  244.         TCHAR* c = &tszModuleFilename[ lstrlen( tszModuleFilename ) ];
  245.         
  246.         while( *c != '\\' && c > tszModuleFilename )
  247.           c--;
  248.  
  249.         if( c != tszModuleFilename )
  250.           *c = '\0';
  251.       }
  252.  
  253.       // We map any file entry to a file in the "SampleSite" subfolder of the current sample
  254.       return _bstr_t( tszModuleFilename ) + _bstr_t( L"\\..\\SampleSite" ) + GetRelativeFilename();
  255.     }
  256.   };
  257.  
  258.   typedef CXcdProtectedList< CXcdConnectionInfo* > CXcdConnectionList;
  259.  
  260.   CXcdConnectionList  m_lstConnections;
  261.  
  262. };
  263.  
  264. #endif // __HTTPSERVER_H__
  265.