Ejemplo n.º 1
0
        /// <summary>
        /// The entire method that is called if a connection was established.
        /// </summary>
        /// <param name="myResult">The IAsyncResult object that was created be HttpListener.BeginGetContext.</param>
        private void AsyncGetContext(IAsyncResult myResult)
        {
            HttpListenerContext context = null;

            lock (_lock)
            {
                if (!_listener.IsListening)
                {
                    return;
                }

                context = _listener.EndGetContext(myResult);

                HttpContext = context;
            }
            //gets the method that will be invoked
            var callback = _parser.GetCallback(context.Request.RawUrl, context.Request.HttpMethod);

            try
            {
                if (callback == null || callback.Item1 == null)
                {
                    NoPatternFound404(context);
                }
                else
                {
                    #region Check authentification

                    try
                    {
                        if (callback.Item1.NeedsExplicitAuthentication)
                        {
                            _security.Authentificate(context.User.Identity);
                        }
                    }
                    catch (Exception)
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        return;
                    }

                    #endregion

                    try
                    {
                        var method = callback.Item2 == null ? null : callback.Item2.ToArray();

                        var targetInvocationResult = callback.Item1.Callback.Invoke(_logic, method);

                        if (context.Response.ContentLength64 == 0)
                        {
                            // The user did not write into the stream itself - we will add header and the invocation result

                            #region Get invocation result and create header and body

                            if (targetInvocationResult is Stream)
                            {
                                var result = targetInvocationResult as Stream;
                                result.Seek(0, SeekOrigin.Begin);
                                result.CopyTo(context.Response.OutputStream);
                            }
                            else if (targetInvocationResult is String)
                            {
                                var toWrite = Encoding.UTF8.GetBytes((string)(targetInvocationResult));
                                context.Response.OutputStream.Write(toWrite, 0, toWrite.Length);
                            }

                            #endregion
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                        var msg = Encoding.UTF8.GetBytes(ex.ToString());
                        context.Response.OutputStream.Write(msg, 0, msg.Length);

                        return;
                    }
                }
            }
            finally
            {
                try
                {
                    context.Response.Close();
                }
                catch (Exception ex)
                {
                    // no need to handle this exception, because the underlying connection is already closed
                }
            }
        }