Пример #1
0
 public static async Task <string> ReadStringAsync(this HttpListenerContext context)
 {
     if (!context.Request.HasEntityBody || context.Request.ContentLength64 == 0)
     {
         return(null);
     }
     //NOTE: Mono breaks keep-alive connection on disponsing HttpRequestStream
     //using(var stream = request.InputStream)
     try
     {
         //NOTE: Vulnerability #1 part #0: Static pool of request buffers
         using (var buffer = await RequestBuffersPool.AcquireAsync())
         {
             try
             {
                 await
                 context.Request.InputStream.ReadToBufferAsync(buffer.Item)
                 .WithTimeout(HttpServerSettings.ReadWriteTimeout)                                         //NOTE: HttpRequestStream is not cancellable with CancellationToken :(
                 .ConfigureAwait(false);
             }
             catch (Exception e)
             {
                 context.Close(408);
                 throw new HttpConnectionClosed(e);
             }
             //NOTE: Vulnerability #1 part #1: Do NOT rely on Content-Length field!
             return(Encoding.UTF8.GetString(buffer.Item, 0, (int)context.Request.ContentLength64));
         }
     }
     finally
     {
         context.Request.InputStream.Close();
     }
 }
Пример #2
0
        private static void HandleRequestThread()
        {
            HttpListenerResponse response = null;
            HttpListenerContext  context  = null;

            try
            {
                lock (m_responseQueue)
                {
                    context = (HttpListenerContext)m_responseQueue.Dequeue();
                }

                response = context.Response;
                HttpListenerRequest request = context.Request;
                switch (request.HttpMethod.ToUpper())
                {
                case "GET": ProcessClientGetRequest(context); break;

                case "POST": ProcessClientPostRequest(context); break;
                }
            }
            catch
            {
            }
            finally
            {
                if (context != null)
                {
                    context.Close();
                }
            }
        }
Пример #3
0
 private static void HandleRequestThread(HttpListenerContext context)
 {
     try
     {
         foreach (var httpHandlerObj in HttpHandlers)
         {
             IHttpHandler httpHandler = httpHandlerObj as IHttpHandler;
             httpHandler.ProcessRequest(context);
         }
     }
     catch (HttpException exception)
     {
         ExceptionHelper.RespondError(context.Response, exception);
     }
     catch (Exception exception)
     {
         ExceptionHelper.RespondError(context.Response, exception);
     }
     finally
     {
         if (context != null)
         {
             context.Close();
         }
     }
 }
Пример #4
0
        internal static void RunServer(string prefix)
        {
            HttpListener listener = new HttpListener(prefix, -1);

            // Loads certificate if it is https server.
            if (prefix == "https")
            {
                //
                // SKU == 3 indicates the device is the emulator.
                //
                // The emulator and the device use different certificate types.  The emulator requires the use of a .PFX certficate, whereas the device simply
                // requires a CER certificate with appended private key.  In addtion, the .PFX certificate requires a password ("NetMF").
                //
                if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
                {
                    listener.HttpsCert = new X509Certificate(m_emulatorCertData, "NetMF");
                }
                else
                {
                    string serverCertAsString = HttpServerSample.Resource1.GetString(HttpServerSample.Resource1.StringResources.cert_device_microsoft_com);
                    byte[] serverCertAsArray  = Encoding.UTF8.GetBytes(serverCertAsString);
                    listener.HttpsCert = new X509Certificate(serverCertAsArray);
                }
            }

            listener.Start();

            while (true)
            {
                HttpListenerResponse response = null;
                HttpListenerContext  context  = null;

                try
                {
                    context  = listener.GetContext();
                    response = context.Response;
                    HttpListenerRequest request = context.Request;
                    switch (request.HttpMethod.ToUpper())
                    {
                    case "GET":  ProcessClientGetRequest(context);  break;

                    case "POST": ProcessClientPostRequest(context); break;
                    }

                    if (response != null)
                    {
                        response.Close();
                    }
                }
                catch
                {
                    if (context != null)
                    {
                        context.Close();
                    }
                }
            }
        }
Пример #5
0
        public static void Main()
        {
            var networkHerlpers = new NetworkHelpers();

            networkHerlpers.SetupAndConnectNetwork(true);

            Console.WriteLine("Waiting for network up and IP address...");
            NetworkHelpers.IpAddressAvailable.WaitOne();

            Console.WriteLine("Waiting for valid Date & Time...");
            NetworkHelpers.DateTimeAvailable.WaitOne();

            Thread.Sleep(2000);

            string prefix = "http";
            int    port   = 80;

            Console.WriteLine("* Creating Http Listener: " + prefix + " on port " + port);
            HttpListener listener = new HttpListener(prefix, port);

            // Start Listener
            listener.Start();
            // After Starting, several exceptions are thrown

            string ips = $"{NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address}, {NetworkInterface.GetAllNetworkInterfaces()[1].IPv4Address}, {NetworkInterface.GetAllNetworkInterfaces()[2].IPv4Address}";

            Console.WriteLine($"Listening for HTTP requests @ {ips}:{port} ...");

            while (true)
            {
                try
                {
                    // now wait on context for a connection
                    HttpListenerContext context = listener.GetContext();
                    if (context != null)
                    {
                        string url            = context.Request.Url.OriginalString;
                        string remoteHost     = context.Request.RemoteEndPoint.Address.ToString();
                        string responseString = $"<HTML><BODY>Hello world! This is nanoFramework.<br/>CurrentTime:{DateTime.UtcNow.ToString()}<br/>Requested URL:{url}<br/>RemoteHost: {remoteHost}<br/>Server IPs:{ips}</BODY></HTML>";
                        Console.WriteLine($"Request received: {responseString}");
                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

                        // Get the response stream and write the response content to it
                        context.Response.ContentLength64 = buffer.Length;
                        context.Response.OutputStream.Write(buffer, 0, buffer.Length);

                        // output stream must be closed
                        context.Response.Close();
                        // context must be closed
                        context.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("* Error getting context: " + ex.Message + "\r\nSack = " + ex.StackTrace);
                }
            }
        }
Пример #6
0
        public static void Main()
        {
            X509Certificate _myWebServerCertificate509 = new X509Certificate2(_myWebServerCrt, _myWebServerPrivateKey, "");

            var networkHerlpers = new NetworkHelpers();

            networkHerlpers.SetupAndConnectNetwork(true);

            Debug.WriteLine("Waiting for network up and IP address...");
            NetworkHelpers.IpAddressAvailable.WaitOne();

            Debug.WriteLine("Waiting for valid Date & Time...");
            NetworkHelpers.DateTimeAvailable.WaitOne();

            // setup HTTP response
            string responseString = "<HTML><BODY>Hello world! This is nanoFramework.</BODY></HTML>";

            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

            // Create a listener.
            string prefix = "https";
            int    port   = prefix == "http" ? 80 : 443;

            Debug.WriteLine("* Creating Http Listener: " + prefix + " on port " + port);
            HttpListener listener = new HttpListener(prefix, port);

            // assign server certificate
            listener.HttpsCert = _myWebServerCertificate509;

            listener.SslProtocols = System.Net.Security.SslProtocols.Tls | System.Net.Security.SslProtocols.Tls11 | System.Net.Security.SslProtocols.Tls12;

            Debug.WriteLine($"Listening for HTTP requests @ {NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address}:{port} ...");
            listener.Start();

            while (true)
            {
                try
                {
                    // now wait on context for a connection
                    HttpListenerContext context = listener.GetContext();

                    // Get the response stream and write the response content to it
                    context.Response.ContentLength64 = buffer.Length;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);

                    // output stream must be closed
                    context.Response.Close();

                    // context must be closed
                    context.Close();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("* Error getting context: " + ex.Message + "\r\nSack = " + ex.StackTrace);
                }
            }
        }
Пример #7
0
        private void StartWebServer()
        {
            // izvietot HttpListener objektu uz 80. porta
            HttpListener httpListener = new HttpListener("http", 80);

            // sakt klausities
            httpListener.Start();

            HttpListenerContext httpContext = null;

            while (true)
            {
                try
                {
                    // wait for incoming request
                    // iegut context'u kas satur pieprasijumu
                    // wait for the incoming request
                    httpContext = httpListener.GetContext();
                    if (httpContext == null)
                    {
                        continue;
                    }

                    // sagatabot atbildi
                    byte[] response = this.PrepareResponseV3LED(httpContext);
                    // nosutit atbildi
                    this.SendResponse(httpContext, response, 202); // 202 - ACCEPTED
                }
                catch (Exception e)
                {
                    Debug.Print("Error processing listener context: " + e.Message);
                }
                finally
                {
                    if (httpContext != null)
                    {
                        httpContext.Close();
                    }
                }
            }
        }
Пример #8
0
        public static void Main()
        {
            X509Certificate _myWebServerCertificate509 = new X509Certificate2(_myWebServerCrt, _myWebServerPrivateKey, "");

            Debug.WriteLine("Waiting for network up and IP address...");
            bool success;
            CancellationTokenSource cs = new(60000);

#if BUILD_FOR_ESP32
            success = NetworkHelper.ConnectWifiDhcp(MySsid, MyPassword, setDateTime: true, token: cs.Token);
#else
            success = NetworkHelper.WaitForValidIPAndDate(true, System.Net.NetworkInformation.NetworkInterfaceType.Ethernet, cs.Token);
#endif
            if (!success)
            {
                Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.ConnectionError.Error}.");
                if (NetworkHelper.ConnectionError.Exception != null)
                {
                    Debug.WriteLine($"Exception: {NetworkHelper.ConnectionError.Exception}");
                }
                return;
            }

            // setup HTTP response
            string responseString = "<HTML><BODY>Hello world! This is nanoFramework.</BODY></HTML>";
            byte[] buffer         = System.Text.Encoding.UTF8.GetBytes(responseString);

            // Create a listener.
            string prefix = "https";
            int    port   = prefix == "http" ? 80 : 443;

            Debug.WriteLine("* Creating Http Listener: " + prefix + " on port " + port);
            HttpListener listener = new HttpListener(prefix, port);

            // assign server certificate
            listener.HttpsCert = _myWebServerCertificate509;

            listener.SslProtocols = System.Net.Security.SslProtocols.Tls | System.Net.Security.SslProtocols.Tls11 | System.Net.Security.SslProtocols.Tls12;

            Debug.WriteLine($"Listening for HTTP requests @ {NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address}:{port} ...");
            listener.Start();

            while (true)
            {
                try
                {
                    // now wait on context for a connection
                    HttpListenerContext context = listener.GetContext();

                    // Get the response stream and write the response content to it
                    context.Response.ContentLength64 = buffer.Length;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);

                    // output stream must be closed
                    context.Response.Close();

                    // context must be closed
                    context.Close();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("* Error getting context: " + ex.Message + "\r\nSack = " + ex.StackTrace);
                }
            }
        }
Пример #9
0
        private async Task ProcessClient(HttpListenerContext httpContext)
        {
            string ip = httpContext.GetIP();

            // Check authentication
            var authResult = auth.Authenticate(httpContext.Request);

            if (false == authResult.Success || authResult.ClientID == null)
            {
                Log.Debug($"Connection from {ip} denied" +
                          (authResult.ClientID != null ? $" (client ID {authResult.ClientID})" : ""));
                httpContext.Close(HttpStatusCode.Unauthorized);
                return;
            }
            string clientID = authResult.ClientID;

            // Accept web socket
            var clientInfo = RpcPeerInfo.Client(clientID, ip);
            WebSocketContext context;

            try {
                context = await httpContext.AcceptWebSocketAsync(subProtocol : null);

                Log.Debug($"Connected {clientInfo}");
            } catch (Exception ex) {
                Log.Debug($"Could not accept WebSocket to {clientInfo}: {ex.Message}");
                httpContext.Close(HttpStatusCode.InternalServerError);
                return;
            }

            // WebSocket loop
            WebSocket webSocket = context.WebSocket;

            try {
                var connection = new WebSocketRpcConnection(clientInfo, webSocket);
                var channel    = await RpcChannel.Create(clientInfo, connection, this, Settings.Backlog);

                if (channelsByClientID.TryGetValue(clientID, out var oldChannel))
                {
                    Log.Debug($"Channel for client {clientID} was already open; close it and open a new one after 3 seconds.");
                    oldChannel.Stop();
                    await Task.Delay(3000);
                }
                channelsByClientID[clientID] = channel;
                await channel.Start();

                Log.Debug($"Connection to {clientInfo} closed");
            } catch (Exception ex) {
                if (ex is WebSocketException wsEx)
                {
                    Log.Debug($"Connection to {clientInfo} unexpectedly closed: " + wsEx.WebSocketErrorCode);
                }
                else
                {
                    Log.Debug($"Connection to {clientInfo} unexpectedly closed: " + ex.Message);
                }
            } finally {
                webSocket?.Dispose();
            }
            channelsByClientID.Remove(clientID);
        }
Пример #10
0
        public void Start(string prefix, int port)
        {
            if (listener != null)
            {
                return;
            }

            listener = new HttpListener(prefix, port);

            // Loads certificate if it is https server.
            if (prefix == "https")
            {
                // SKU == 3 indicates the device is the emulator.
                // The emulator and the device use different certificate types.  The emulator requires the use of a .PFX certficate, whereas the device simply
                // requires a CER certificate with appended private key. In addtion, the .PFX certificate requires a password ("NetMF").
                if (Utils.IsEmulator)
                {
                    listener.HttpsCert = new X509Certificate(m_emulatorCertData, "NetMF");
                }
                else
                {
                    string serverCertAsString = Resource1.GetString(Resource1.StringResources.cert_device_microsoft_com);
                    listener.HttpsCert = new X509Certificate(Encoding.UTF8.GetBytes(serverCertAsString));
                }
            }

            listener.Start();
            isStopped = false;

            new Thread(() =>
            {
                while (!isStopped)
                {
                    try
                    {
                        if (!listener.IsListening)
                        {
                            listener.Start();
                        }

                        HttpListenerContext context = listener.GetContext();

                        // see http://netmf.codeplex.com/workitem/2157
                        //new Thread(() =>
                        {
                            try
                            {
                                if (OnRequest != null)
                                {
                                    OnRequest(context.Request);
                                }

                                switch (context.Request.HttpMethod.ToUpper())
                                {
                                case "GET": ProcessClientGetRequest(context); break;

                                case "POST": ProcessClientPostRequest(context); break;
                                }

                                // for test:
                                //SendStream(Encoding.UTF8.GetBytes("<html><body>" + DateTime.Now + "</body></html>"), "text/html", context.Response);

                                context.Close();
                            }
                            catch (Exception e)
                            {
                                Debug.Print(e.Message);
                            }
                        }
                        //).Start();
                    }
                    //catch (InvalidOperationException ex)
                    //{
                    //    listener.Stop();
                    //    Thread.Sleep(1000);
                    //}
                    //catch (ObjectDisposedException ex)
                    //{
                    //    listener.Start();
                    //}
                    //catch (SocketException ex)
                    //{
                    //    if (ex.ErrorCode == 10053)
                    //        listener.Stop();
                    //}
                    catch (Exception ex)
                    {
                        //Thread.Sleep(1000);
                        //if (context != null)
                        //    context.Close();
                    }
                }

                // stopped
                listener.Close();
                listener = null;
            })
            {
                Priority = ThreadPriority.Normal
            }.Start();
        }
Пример #11
0
                #pragma warning restore 0649

        private static void AddComment(HttpListenerContext client, int categoryIndex)
        {
            var r = client.Request;

            string name = null, email = null, text = null;
            int    replyTo = -1;
            var    buf     = new byte[r.ContentLength64];
            var    query   = Encoding.UTF8.GetString(buf, 0, r.InputStream.Read(buf, 0, buf.Length));
            var    param   = query.Split('&');

            foreach (var p in param)
            {
                var segments = p.Split(new[] { '=' }, 2);
                if (segments.Length < 2)
                {
                    client.Error(HttpStatusCode.BadRequest);
                    return;
                }
                var value = Uri.UnescapeDataString(segments[1]).Replace('+', ' ');
                if (segments[0] == "name")
                {
                    name = value;
                }
                else if (segments[0] == "email")
                {
                    email = value;
                }
                else if (segments[0] == "comment")
                {
                    text = value;
                }
                else if (segments[0] == "replyTo")
                {
                    replyTo = int.Parse(segments[1]);
                }
            }
            if (name == null || text == null)
            {
                client.Error(HttpStatusCode.BadRequest);
                return;
            }

            var article = articles[categoryIndex][r.Url.Segments[2]];
            var comment = new Comment(name, text, article.CommentCount, replyTo);

            if (!Validate(comment, article))
            {
                client.Error(HttpStatusCode.BadRequest);
                return;
            }
            article.AddComment(comment);

            if (client.Request.Url.Query.Contains("returnSelf"))
            {
                client.WriteAndClose(GenerateCommentHtml(comment, ""), "html", HttpStatusCode.Created);
            }
            else
            {
                // This should prevent accidently submitting a form multiple times
                client.SetHeader("Location", client.Request.Url);
                client.Close(HttpStatusCode.SeeOther);
            }
        }
        /// <summary>
        /// Processes a message
        /// </summary>
        /// <param name="msg">The message being processed.</param>
        /// <param name="ctx">The context associated with the message.</param>
        /// <returns>The handling status for this operation.</returns>
        protected override ChainResult OnProcessOutputMessage(ref WsMessage msg, BindingContext ctx)
        {
            ArrayList props = ctx.BindingProperties;

            byte[] message     = null;
            string contentType = "text/plain";

            if (msg != null)
            {
                message = msg.Body as byte[];

                if (msg.BodyParts != null)
                {
                    contentType = "Multipart/Related;boundary=" +
                                  msg.MtomPropeties.boundary +
                                  ";type=\"application/xop+xml\";start=\"" +
                                  msg.MtomPropeties.start +
                                  "\";start-info=\"application/soap+xml\"";

                    ctx.BindingProperties.Add(new BindingProperty("header", HttpKnownHeaderNames.Server, "Microsoft-MF HTTP 1.0"));
                    ctx.BindingProperties.Add(new BindingProperty("header", HttpKnownHeaderNames.MimeVersion, "1.0"));
                    ctx.BindingProperties.Add(new BindingProperty("header", HttpKnownHeaderNames.Date, DateTime.Now.ToString()));
                }
                else
                {
                    contentType = "application/soap+xml; charset=utf-8";
                }
            }

            if (ctx is ClientBindingContext)
            {
                if (message == null)
                {
                    return(ChainResult.Abort);
                }

                HttpWebRequest request;

                try
                {
                    if (!m_persistConn || ctx.ContextObject == null)
                    {
                        request                  = HttpWebRequest.Create(new Uri(m_transportUri.AbsoluteUri)) as HttpWebRequest;
                        request.Timeout          = (int)(ctx.OpenTimeout.Ticks / TimeSpan.TicksPerMillisecond);
                        request.ReadWriteTimeout = (int)(ctx.ReceiveTimeout.Ticks / TimeSpan.TicksPerMillisecond);

                        ctx.ContextObject = request;
                    }
                    else
                    {
                        request = (HttpWebRequest)ctx.ContextObject;

                        request.Reset();
                    }


                    // Post method
                    request.Method = "POST";

                    WebHeaderCollection headers = request.Headers;

                    request.ContentType = contentType;
                    request.UserAgent   = "MFWsAPI";

                    request.Headers.Add(HttpKnownHeaderNames.CacheControl, "no-cache");
                    request.Headers.Add(HttpKnownHeaderNames.Pragma, "no-cache");

                    if (props != null)
                    {
                        int len = props.Count;
                        for (int i = 0; i < len; i++)
                        {
                            BindingProperty prop      = (BindingProperty)props[i];
                            string          container = prop.Container;

                            if (container == "header")
                            {
                                headers.Add(prop.Name, (string)prop.Value);
                            }
                            else if (container == null || container == "")
                            {
                                string name = prop.Name;

                                if (name == HttpKnownHeaderNames.ContentType)
                                {
                                    request.ContentType = (string)prop.Value;
                                }
                                else if (name == HttpKnownHeaderNames.UserAgent)
                                {
                                    request.UserAgent = (string)prop.Value;
                                }
                            }
                        }
                    }

                    if (message != null)
                    {
                        System.Ext.Console.Write("Http message sent: ");
                        System.Ext.Console.Write(message);

                        request.ContentLength = message.Length;

                        using (Stream stream = request.GetRequestStream())
                        {
                            // Write soap message
                            stream.Write(message, 0, message.Length);

                            // Flush the stream and force a write
                            stream.Flush();
                        }
                    }
                }
                catch
                {
                    ctx.ContextObject = null;

                    throw;
                }
            }
            else
            {
                HttpListenerContext listenerContext = ctx.ContextObject as HttpListenerContext;

                if (listenerContext == null)
                {
                    return(ChainResult.Abort);
                }

                HttpListenerResponse listenerResponse = listenerContext.Response;

                if (listenerResponse == null || listenerResponse.OutputStream == null)
                {
                    ctx.ContextObject = null;
                    return(ChainResult.Abort);
                }

                try
                {
                    StreamWriter streamWriter = new StreamWriter(listenerResponse.OutputStream);

                    // Write Header, if message is null write accepted
                    if (message == null || (msg != null && msg.Header != null && msg.Header.IsFaultMessage))
                    {
                        listenerResponse.StatusCode = 202;
                    }
                    else
                    {
                        listenerResponse.StatusCode = 200;
                    }

                    // Check to see it the hosted service is sending mtom
                    WebHeaderCollection headers = listenerResponse.Headers;

                    listenerResponse.ContentType = contentType;

                    bool isChunked = false;

                    if (props != null)
                    {
                        int len = props.Count;
                        for (int i = 0; i < len; i++)
                        {
                            BindingProperty prop      = (BindingProperty)props[i];
                            string          container = prop.Container;
                            string          name      = prop.Name;
                            string          value     = (string)prop.Value;

                            if (container == "header")
                            {
                                if (!isChunked && name == HttpKnownHeaderNames.TransferEncoding && value.ToLower() == "chunked")
                                {
                                    isChunked = true;
                                }

                                headers.Add(name, (string)prop.Value);
                            }
                            else if (container == null || container == "")
                            {
                                if (name == HttpKnownHeaderNames.ContentType)
                                {
                                    listenerResponse.ContentType = (string)prop.Value;
                                    System.Ext.Console.Write(HttpKnownHeaderNames.ContentType + ": " + listenerResponse.ContentType);
                                }
                            }
                        }
                    }

                    // If chunked encoding is enabled write chunked message else write Content-Length
                    if (isChunked)
                    {
                        // Chunk message
                        int bufferIndex      = 0;
                        int chunkSize        = 0;
                        int defaultChunkSize = 0xff;
#if DEBUG
                        byte[] displayBuffer = new byte[defaultChunkSize];
#endif
                        while (bufferIndex < message.Length)
                        {
                            // Calculate chunk size and write to stream
                            chunkSize = message.Length - bufferIndex < defaultChunkSize ? message.Length - bufferIndex : defaultChunkSize;
                            streamWriter.WriteLine(chunkSize.ToString("{0:X}"));
                            System.Ext.Console.Write(chunkSize.ToString("{0:X}"));

                            // Write chunk
                            streamWriter.WriteBytes(message, bufferIndex, chunkSize);
                            streamWriter.WriteLine();
#if DEBUG
                            Array.Copy(message, bufferIndex, displayBuffer, 0, chunkSize);
                            System.Ext.Console.Write(displayBuffer, bufferIndex, chunkSize);
#endif

                            // Adjust buffer index
                            bufferIndex = bufferIndex + chunkSize;
                        }

                        // Write 0 length and blank line
                        streamWriter.WriteLine("0");
                        streamWriter.WriteLine();
                        System.Ext.Console.Write("0");
                        System.Ext.Console.Write("");
                    }
                    else
                    {
                        if (message == null)
                        {
                            listenerResponse.ContentLength64 = 0;
                        }
                        else
                        {
                            listenerResponse.ContentLength64 = message.Length;
                        }

                        System.Ext.Console.Write("Content Length: " + listenerResponse.ContentLength64);

                        // If an empty message is returned (i.e. oneway request response) don't send
                        if (message != null && message.Length > 0)
                        {
                            System.Ext.Console.Write(message);

                            // Write soap message
                            streamWriter.WriteBytes(message, 0, message.Length);
                        }
                    }

                    // Flush the stream and return
                    streamWriter.Flush();
                }
                catch
                {
                    return(ChainResult.Abort);
                }
                finally
                {
                    if (m_persistConn)
                    {
                        listenerResponse.Detach();
                    }
                    else
                    {
                        listenerContext.Close(ctx.CloseTimeout.Seconds);
                        ctx.ContextObject = null;
                    }
                }
            }
            return(ChainResult.Handled);
        }