Esempio n. 1
0
        private void HandleRequest(RequestContext context)
        {
            RegisterRoutes();

            try
            {
                bool handled = false;

                for (; context.CurrentRoute < m_registeredRoutes.Count; ++context.CurrentRoute)
                {
                    RouteAttribute route = m_registeredRoutes[context.CurrentRoute];
                    Match          match = route.Route.Match(context.Path);
                    if (!match.Success)
                    {
                        continue;
                    }

                    if (!route.Methods.IsMatch(context.Request.HttpMethod))
                    {
                        continue;
                    }

                    // Upgrade to main thread if necessary
                    if (route.RunOnMainThread && Thread.CurrentThread != m_mainThread)
                    {
                        lock (m_mainRequests)
                        {
                            m_mainRequests.Enqueue(context);
                        }
                        return;
                    }

                    context.Match = match;
                    route.Cbk(context);
                    handled = !context.Pass;
                    if (handled)
                    {
                        break;
                    }
                }

                if (!handled)
                {
                    context.Response.StatusCode        = (int)HttpStatusCode.NotFound;
                    context.Response.StatusDescription = "Not Found";
                }
            }
            catch (Exception exception)
            {
                context.Response.StatusCode        = (int)HttpStatusCode.InternalServerError;
                context.Response.StatusDescription = string.Format("Fatal error:\n{0}", exception);

                Debug.LogException(exception);
            }

            context.Response.OutputStream.Close();
        }
Esempio n. 2
0
        private static void RegisterFileHandlers()
        {
            string         pattern       = string.Format("({0})", string.Join("|", fileTypes.Select(x => x.Key).ToArray()));
            RouteAttribute downloadRoute = new RouteAttribute(string.Format(@"^/download/(.*\.{0})$", pattern));
            RouteAttribute fileRoute     = new RouteAttribute(string.Format(@"^/(.*\.{0})$", pattern));

            bool needsRequest = m_fileRoot.Contains("://");

            downloadRoute.RunOnMainThread = needsRequest;
            fileRoute.RunOnMainThread     = needsRequest;

            Action <RequestContext, bool> cbk = FileHandler;

            if (needsRequest)
            {
                cbk = RequestFileHandler;
            }

            downloadRoute.Cbk = delegate(RequestContext context) { cbk(context, true); };
            fileRoute.Cbk     = delegate(RequestContext context) { cbk(context, false); };

            m_registeredRoutes.Add(downloadRoute);
            m_registeredRoutes.Add(fileRoute);
        }