Exemple #1
0
 public ServerSettings()
 {
     Connections  = 50;
     Host         = "localhost";
     Logger       = NullLogger.GetInstance();
     Port         = "1234";
     PublicFolder = new PublicFolder();
     Router       = new Router();
     UseHttps     = false;
 }
Exemple #2
0
        private void Worker()
        {
            WaitHandle[] wait = { ReadyEvent, StopEvent };
            while (0 == WaitHandle.WaitAny(wait))
            {
                HttpContext context;

                lock (Queue)
                {
                    if (Queue.Count > 0)
                    {
                        HttpListenerContext ctx;
                        Queue.TryDequeue(out ctx);
                        if (ctx == null)
                        {
                            continue;
                        }
                        context = new HttpContext(ctx, this);
                    }
                    else
                    {
                        ReadyEvent.Reset(); continue;
                    }
                }

                try
                {
                    if (!string.IsNullOrWhiteSpace(PublicFolder.Prefix) && context.Request.PathInfo.StartsWith(PublicFolder.Prefix))
                    {
                        PublicFolder.SendPublicFile(context);
                        if (!context.WasRespondedTo)
                        {
                            context.Response.SendResponse(HttpStatusCode.NotFound);
                        }
                        return;
                    }

                    context = (HttpContext)PublicFolder.SendPublicFile(context);

                    if (!context.WasRespondedTo)
                    {
                        if (!Router.Route(context))
                        {
                            throw new RouteNotFoundException(context);
                        }
                    }
                }
                catch (RouteNotFoundException)
                {
                    if (EnableThrowingExceptions)
                    {
                        throw;
                    }
                    context.Response.SendResponse(HttpStatusCode.NotFound);
                }
                catch (NotImplementedException)
                {
                    if (EnableThrowingExceptions)
                    {
                        throw;
                    }
                    context.Response.SendResponse(HttpStatusCode.NotImplemented);
                }
                catch (Exception e)
                {
                    if (EnableThrowingExceptions)
                    {
                        throw;
                    }
                    Logger.Error(e);
                    context.Response.SendResponse(HttpStatusCode.InternalServerError, e);
                }
            }
        }