Exemple #1
0
        /// <summary>
        ///  This method receives requests from other peers and maps the URI
        ///  namespace or resource name to an actual namespace handler.  If one
        ///  exists then the namespace handler is invoked to process the request.
        ///  If not some default processing is used.
        /// </summary>
        /// <param name="asyncResult">The Async object for the request.</param>
        public void GetRequestCallback(IAsyncResult asyncResult)
        {
            try
            {
                OnBeforeProcessRequest();

                var stateObject = asyncResult.AsyncState as HttpApplication;
                stateObject.HttpRequest = _listener.EndGetRequest(asyncResult);

                if (stateObject.HttpRequest.Method == "GET")
                {
                    var ns = stateObject.HttpRequest.RequestUri.Segments[1];
                    if (_nsModules.Contains(ns))
                    {
                        var nsHandler = (IHttpNamespaceHandler)_nsModules[ns];
                        nsHandler.ProcessRequest(stateObject);
                    }
                    else
                    {
                        HandleUnsupportedNamespace(stateObject);
                    }
                }
                else if (stateObject.HttpRequest.Method == "POST")
                {
                    if (stateObject.HttpRequest.ContentLength <= 0)
                    {
                        var message = "<html><body>Bad Request - POST with no data";
                        message += "</body></html>";
                        HandleBadRequest(stateObject, message);
                    }
                    else
                    {
                        stateObject.RequestStream = stateObject.HttpRequest.GetRequestStream();
                        try
                        {
                            stateObject.RequestStream.BeginRead(stateObject.ReadBuffer, 0, stateObject.ReadBuffer.Length,
                                                                _readCB, stateObject);
                        }
                        catch (Exception exception)
                        {
#if DEBUG
                            if (HttpTraceHelper.ExceptionCaught.TraceVerbose)
                            {
                                HttpTraceHelper.WriteLine("Caught exception:" + exception);
                            }
#endif
                        }
                    }
                }
                else
                {
                    HandleUnsupportedNamespace(stateObject);
                }

                // Queue up another _listener
                var newStateObject = new HttpApplication();
                _listener.BeginGetRequest(_getRequestCB, newStateObject);
            }
            catch (Exception ex)
            {
#if DEBUG
                if (HttpTraceHelper.ExceptionCaught.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("Caught exception:" + ex);
                }
#endif
                var stateObject = asyncResult.AsyncState as HttpApplication;
                stateObject.Reset();
                _listener.BeginGetRequest(_getRequestCB, stateObject);
            }
            finally
            {
                OnAfterProcessRequest();
            }
        }