Example #1
0
        // http://en.wikipedia.org/wiki/Internet_media_type
        internal void SendGenericWebResponse( byte[] Buffer, ulong ModifiedIndex, ulong UniqueEntity, string ContentType )
        {
            if( Client == null )
              return;

            try
            {
            // Set the initial UniqueEntity to the current date time index and then just
            // keep incrementing it.
            ECTime RightNow = new ECTime();
            RightNow.SetToNow();

            ECTime ExpireTime = new ECTime();
            ExpireTime.SetToNow();
            ExpireTime.AddSeconds( 120 );

            ECTime ModifiedTime = new ECTime( ModifiedIndex );

            // ETag is an Entity Tag.
            // "An entity tag MUST be unique across all versions of all entities
            // associated with a particular resource."
            string Header = "HTTP/1.1 200 OK\r\n" +
               "Date: " + RightNow.GetHTTPHeaderDateTime() + "\r\n" +
               "Server: Eric Example\r\n" +
               "Last-Modified: " + ModifiedTime.GetHTTPHeaderDateTime() + "\r\n" +
               "ETag: " + UniqueEntity.ToString() + "\r\n" +
               "Accept-Ranges: bytes\r\n" +
               "Content-Length: " + Buffer.Length.ToString() + "\r\n" +
               // "Cache-Control: max-age=5184000
               "Expires: " + ExpireTime.GetHTTPHeaderDateTime() + "\r\n" +
               "Keep-Alive: timeout=5, max=100\r\n" +
               "Connection: Keep-Alive\r\n" +
               "Content-Type: " + ContentType + "\r\n" +
               "\r\n"; // Empty line and then the actual bytes.

            byte[] HeaderBytes = UTF8Strings.StringToBytes( Header );
            if( HeaderBytes == null )
              return;

            byte[] AllSendBytes;

            if( GetIsHeadOnly() )
              AllSendBytes = new byte[HeaderBytes.Length];
            else
              AllSendBytes = new byte[HeaderBytes.Length + Buffer.Length];

            int Where = 0;
            for( int Count = 0; Count < HeaderBytes.Length; Count++ )
              {
              AllSendBytes[Where] = HeaderBytes[Count];
              Where++;
              }

            if( !GetIsHeadOnly() )
              {
              for( int Count = 0; Count < Buffer.Length; Count++ )
            {
            AllSendBytes[Where] = Buffer[Count];
            Where++;
            }
              }

            // This returns immediately.
            WriteBytesAsync( AllSendBytes );

            }
            catch( Exception Except )
              {
              MForm.ShowStatus( "Exception in SendHTMLOrText():" );
              MForm.ShowStatus( Except.Message );
              }
        }