home *** CD-ROM | disk | FTP | other *** search
- #ifndef __HTTPSERVER_H__
- #define __HTTPSERVER_H__
-
- /*
- * Xceed Winsock Library Sample: Chat connectionless
- * Copyright (c) 2000 Xceed Software Inc.
- *
- * [ HttpServer.h : CHttpServer declaration ]
- *
- * This is a minimal implementation of an HTTP server. It
- * only handles the "GET" HTTP command. When a "GET" command
- * is received from a connected client, the server will send
- * the requested file to the client. This is enough functionality
- * to display a web site with bitmaps and regular HTML files.
- *
- * In particular, it demonstrate:
- * - How to create and use a listening socket to handle
- * incoming connections.
- * - How to use string and file transfer interfaces.
- * - Using "#import" with the Xceed Winsock Library.
- * - Using ATL to help implement event interfaces.
- *
- * This file is part of the Xceed Winsock Library Samples.
- * The source code in this file is only intended as a supplement
- * to Xceed Winsock Library's documentation, and is provided "as is",
- * without warranty of any kind, either expressed or implied.
- *
- */
-
- #include "xcdProtectedList.h"
-
- class ATL_NO_VTABLE CHttpServer :
- // This is the base class of an ATL COM object
- public CComObjectRootEx< CComMultiThreadModel >,
- // These are the Xceed Winsock event interfaces we wish to implement
- public IXWIncomingConnectionEvents,
- public IXWConnectionEvents,
- public IXWUnicodeStringTransferEvents,
- public IXWFileTransferEvents
- {
- public:
- //
- // Constructor
- //
-
- CHttpServer( void )
- {
- // We just initialize stuff, but everything that can fail
- // should be done in FinalConstruct, since we can return an error.
- }
-
- //
- // Stuff required for ATL support
- //
-
- DECLARE_NO_REGISTRY()
-
- DECLARE_PROTECT_FINAL_CONSTRUCT()
-
- BEGIN_COM_MAP(CHttpServer)
- COM_INTERFACE_ENTRY(IXWIncomingConnectionEvents)
- COM_INTERFACE_ENTRY(IXWConnectionEvents)
- COM_INTERFACE_ENTRY(IXWUnicodeStringTransferEvents)
- COM_INTERFACE_ENTRY(IXWFileTransferEvents)
- END_COM_MAP()
-
- //
- // Final construction and release
- //
-
- HRESULT FinalConstruct( void );
- void FinalRelease( void );
-
- //
- // Actions to take on server
- //
-
- HRESULT Start( void );
- HRESULT Stop( void );
-
- //
- // IXWIncomingConnectionEvents
- //
-
- virtual HRESULT __stdcall raw_OnConnection (
- struct IXWSocket * piListeningSocket,
- struct IXWAddressInfo * piRemoteAddress,
- unsigned long dwCallerDataSize,
- unsigned char * pcCallerData,
- unsigned long dwExpectedCalleeDataSize,
- unsigned long * pdwCalleeDataSize,
- unsigned char * * ppcCalleeData,
- struct SXWQualityOfServiceInfo * * ppsQualityOfService,
- unsigned long * pdwUserParam,
- long * pbReject );
-
- virtual HRESULT __stdcall raw_OnConnectionProcessed (
- struct IXWSocket * piListeningSocket,
- struct IXWSocket * piIncomingSocket,
- unsigned long dwUserParam );
-
- virtual HRESULT __stdcall raw_OnListeningError (
- struct IXWSocket * piListeningSocket,
- unsigned long dwUserParam,
- HRESULT hResult );
-
- //
- // IXWConnectionEvents
- //
-
- virtual HRESULT __stdcall raw_OnDisconnected (
- struct IXWSocket * piSocket,
- unsigned long dwCallerDataSize,
- unsigned char * pcCallerData,
- unsigned long * pdwCalleeDataSize,
- unsigned char * * ppcCalleeData );
-
- //
- // IXWUnicodeStringTransferEvents
- //
-
- virtual HRESULT __stdcall raw_OnUnicodeStringSent (
- struct IXWUnicodeStringTransfer * piTransfer,
- unsigned long dwUserParam,
- HRESULT hResultCode );
-
- virtual HRESULT __stdcall raw_OnUnicodeStringReceived (
- struct IXWUnicodeStringTransfer * piTransfer,
- LPWSTR * ppwszString,
- unsigned long dwUserParam,
- HRESULT hResultCode );
-
- virtual HRESULT __stdcall raw_OnUnicodeStringAvailable (
- struct IXWUnicodeStringTransfer * piTransfer,
- unsigned long dwCharsReceived,
- unsigned long dwCharsAvailable );
-
- virtual HRESULT __stdcall raw_OnOutOfBandUnicodeStringReceived (
- struct IXWUnicodeStringTransfer * piTransfer,
- LPWSTR * ppwszString,
- HRESULT hResultCode );
-
- //
- // IXWFileTransferEvents
- //
-
- virtual HRESULT __stdcall raw_OnFileSent (
- struct IXWFileTransfer * piTransfer,
- LPWSTR pwszFilename,
- unsigned long dwStartOffset,
- unsigned long dwBytesSent,
- unsigned long dwBytesTotal,
- unsigned long dwUserParam,
- long bTransferCompleted,
- HRESULT hResult );
-
- virtual HRESULT __stdcall raw_OnFileReceived (
- struct IXWFileTransfer * piTransfer,
- LPWSTR pwszFilename,
- unsigned long dwStartOffset,
- unsigned long dwBytesReceived,
- unsigned long dwBytesTotal,
- unsigned long dwUserParam,
- long bTransferCompleted,
- HRESULT hResult );
-
- protected:
-
- private:
- //
- // Listening socket
- //
-
- IXWConnectionListenerPtr m_piListener;
-
- //
- // Information about each connection
- //
-
- class CXcdConnectionInfo
- {
- public:
- // new/delete operators
- void* operator new( size_t xSize ) { return HeapAlloc( GetProcessHeap(), 0, xSize ); }
- void operator delete( void* pvBuffer ) { HeapFree( GetProcessHeap(), 0, pvBuffer ); }
-
- // Public members
- _bstr_t m_sRequest;
- IUnknownPtr m_punkConnection; // We keep its base interface, for comparisons
-
- // Add a string to the complete request
- void AppendRequestString( _bstr_t sString )
- {
- m_sRequest += sString;
- }
-
- // Check if this request is complete
- bool IsRequestComplete( void )
- {
- WCHAR* pwszString = m_sRequest;
-
- return ( pwszString && wcsstr( pwszString, L"\r\n\r\n" ) != 0 );
- }
-
- // Retrieve the relative filename from the request
- _bstr_t GetRelativeFilename( void )
- {
- if( !IsRequestComplete() )
- return _bstr_t();
-
- WCHAR* pwszString = m_sRequest;
-
- if( wcsstr( pwszString, L"GET " ) != pwszString )
- return _bstr_t();
-
- pwszString += 4;
-
- WCHAR* pwszGetEnd = wcschr( pwszString, L' ' );
-
- WCHAR wszRelative[ MAX_PATH+1 ];
- wcsncpy( wszRelative, pwszString, pwszGetEnd - pwszString );
- wszRelative[ pwszGetEnd - pwszString ] = L'\0';
-
- if( wcscmp( wszRelative, L"/" ) == 0 )
- wcscpy( wszRelative, L"/index.html" );
-
- // Convert each slash to a backslash
- for( pwszGetEnd=wszRelative; *pwszGetEnd; pwszGetEnd++ )
- {
- if( *pwszGetEnd == L'/' )
- *pwszGetEnd = L'\\';
- }
-
- return _bstr_t( wszRelative );
- }
-
- // Retrieve the full filename from the string
- _bstr_t GetAbsoluteFilename( void )
- {
- TCHAR tszModuleFilename[ MAX_PATH + 1 ] = "";
-
- if( GetModuleFileName( NULL, tszModuleFilename, MAX_PATH ) )
- {
- TCHAR* c = &tszModuleFilename[ lstrlen( tszModuleFilename ) ];
-
- while( *c != '\\' && c > tszModuleFilename )
- c--;
-
- if( c != tszModuleFilename )
- *c = '\0';
- }
-
- // We map any file entry to a file in the "SampleSite" subfolder of the current sample
- return _bstr_t( tszModuleFilename ) + _bstr_t( L"\\..\\SampleSite" ) + GetRelativeFilename();
- }
- };
-
- typedef CXcdProtectedList< CXcdConnectionInfo* > CXcdConnectionList;
-
- CXcdConnectionList m_lstConnections;
-
- };
-
- #endif // __HTTPSERVER_H__
-