ForRouteWithAuth() private method

private ForRouteWithAuth ( RouteMatch route, IPrincipal principal ) : void
route RouteMatch
principal IPrincipal
return void
Ejemplo n.º 1
0
        private void ProcessAllMessages(RequestInfo request, HttpSocketContext ctx, IPrincipal principal, ManualResetEvent resetEvent, bool canReschedule)
        {
            RouteMatch?  routeMatch;
            RouteHandler route;
            var          socket = request.Socket;

            while (ctx.Parse(socket, out routeMatch, out route))
            {
                var match = routeMatch.Value;
                var auth  = Authentication.TryAuthorize(ctx.GetRequestHeader("authorization"), ctx.RawUrl, route);
                if (auth.Principal != null)
                {
                    if (canReschedule && route.IsAsync)
                    {
                        resetEvent.Reset();
                        ThreadPool.QueueUserWorkItem(ProcessInThread, new ThreadArgs(request, ctx, resetEvent, auth, route, match));
                        Thread.Yield();
                        resetEvent.WaitOne();
                        return;
                    }
                    else
                    {
                        ctx.ForRouteWithAuth(match, auth.Principal);
                        if (principal != auth.Principal)
                        {
                            Thread.CurrentPrincipal = principal = auth.Principal;
                        }
                        using (var stream = route.Handle(match.OrderedArgs, ctx, ctx, ctx.InputStream, ctx.OutputStream))
                        {
                            var keepAlive = ctx.Return(stream, socket, !canReschedule);
                            if (keepAlive)
                            {
                                if (ctx.Pipeline)
                                {
                                    continue;
                                }
                                else if (socket.Connected && Requests.TryAdd(request.Processed()))
                                {
                                    return;
                                }
                            }
                            socket.Close();
                            return;
                        }
                    }
                }
                else
                {
                    CheckAuth(socket, auth, ctx);
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        private void ProcessAllMessages(Socket socket, HttpSocketContext ctx, IPrincipal principal, ManualResetEventSlim threadSync)
        {
            RouteMatch   match;
            RouteHandler route;

            while (ctx.Parse(socket, out match, out route))
            {
                var auth = Authentication.TryAuthorize(ctx.GetRequestHeader("authorization"), ctx.RawUrl, route);
                if (auth.Principal != null)
                {
                    if (route.IsAsync && threadSync != null)
                    {
                        threadSync.Reset();
                        ThreadPool.QueueUserWorkItem(ProcessInThread, new ThreadArgs(socket, ctx, threadSync, auth, route, match));
                        threadSync.Wait();
                        return;
                    }
                    else
                    {
                        ctx.ForRouteWithAuth(match, auth.Principal);
                        if (principal != auth.Principal)
                        {
                            Thread.CurrentPrincipal = principal = auth.Principal;
                        }
                        using (var stream = route.Handle(match.OrderedArgs, ctx, ctx, ctx.InputStream, ctx.OutputStream))
                        {
                            var keepAlive = ctx.Return(stream, socket, threadSync == null);
                            if (keepAlive)
                            {
                                if (ctx.Pipeline)
                                {
                                    continue;
                                }
                                else if (socket.Connected)
                                {
                                    if (Requests.TryAdd(new RequestInfo(socket, socket.Available > 0)))
                                    {
                                        return;
                                    }
                                }
                            }
                            socket.Close();
                            return;
                        }
                    }
                }
                else
                {
                    CheckAuth(socket, auth, ctx);
                    return;
                }
            }
        }
Ejemplo n.º 3
0
 private void ProcessAllMessages(RequestInfo request, HttpSocketContext ctx, IPrincipal principal, ManualResetEvent resetEvent, bool canReschedule)
 {
     RouteMatch? routeMatch;
     RouteHandler route;
     var socket = request.Socket;
     while (ctx.Parse(socket, out routeMatch, out route))
     {
         var match = routeMatch.Value;
         var auth = Authentication.TryAuthorize(ctx.GetRequestHeader("authorization"), ctx.RawUrl, route);
         if (auth.Principal != null)
         {
             if (canReschedule && route.IsAsync)
             {
                 resetEvent.Reset();
                 ThreadPool.QueueUserWorkItem(ProcessInThread, new ThreadArgs(request, ctx, resetEvent, auth, route, match));
                 Thread.Yield();
                 resetEvent.WaitOne();
                 return;
             }
             else
             {
                 ctx.ForRouteWithAuth(match, auth.Principal);
                 if (principal != auth.Principal)
                     Thread.CurrentPrincipal = principal = auth.Principal;
                 using (var stream = route.Handle(match.OrderedArgs, ctx, ctx, ctx.InputStream, ctx.OutputStream))
                 {
                     var keepAlive = ctx.Return(stream, socket, !canReschedule);
                     if (keepAlive)
                     {
                         if (ctx.Pipeline) continue;
                         else if (socket.Connected && Requests.TryAdd(request.Processed())) return;
                     }
                     socket.Close();
                     return;
                 }
             }
         }
         else
         {
             CheckAuth(socket, auth, ctx);
             return;
         }
     }
 }
Ejemplo n.º 4
0
 private void ProcessSocket(Socket socket, HttpSocketContext ctx, IPrincipal principal)
 {
     ctx.Reset();
     try
     {
         while (ctx.Parse(socket))
         {
             RouteMatch match;
             var route = Routes.Find(ctx.HttpMethod, ctx.RawUrl, ctx.AbsolutePath, out match);
             if (route == null)
             {
                 var unknownRoute = "Unknown route " + ctx.RawUrl + " on method " + ctx.HttpMethod;
                 ctx.ReturnError(socket, 404, unknownRoute, false);
                 break;
             }
             var auth = Authentication.TryAuthorize(ctx.GetRequestHeader("authorization"), ctx.RawUrl, route);
             if (auth.Principal != null)
             {
                 ctx.ForRouteWithAuth(match, auth.Principal);
                 ThreadContext.Request = ctx;
                 ThreadContext.Response = ctx;
                 if (principal != auth.Principal)
                     Thread.CurrentPrincipal = principal = auth.Principal;
                 using (var stream = route.Handle(match.OrderedArgs, ctx.Stream))
                 {
                     var keepAlive = ctx.Return(stream, socket);
                     if (keepAlive)
                     {
                         if (ctx.Pipeline) continue;
                         else if (socket.Connected)
                         {
                             if (Requests.TryAdd(new RequestInfo(socket, socket.Available > 0)))
                                 return;
                         }
                     }
                     socket.Close();
                     return;
                 }
             }
             else if (auth.SendAuthenticate)
             {
                 ctx.AddHeader("WWW-Authenticate", MissingBasicAuth);
                 ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
                 return;
             }
             else
             {
                 ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
                 return;
             }
         }
     }
     catch (SecurityException sex)
     {
         try { ctx.ReturnError(socket, (int)HttpStatusCode.Forbidden, sex.Message, true); }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex);
         }
     }
     catch (ActionNotSupportedException anse)
     {
         try { ctx.ReturnError(socket, 404, anse.Message, true); }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         TraceSource.TraceEvent(TraceEventType.Error, 5403, "{0}", ex);
         try { ctx.ReturnError(socket, 500, ex.Message, false); }
         catch (Exception ex2)
         {
             Console.WriteLine(ex2.Message);
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex2);
         }
     }
 }
Ejemplo n.º 5
0
 private void ProcessSocket(Socket socket, HttpSocketContext ctx, IPrincipal principal)
 {
     ctx.Reset();
     try
     {
         while (ctx.Parse(socket))
         {
             RouteMatch match;
             var        route = Routes.Find(ctx.HttpMethod, ctx.RawUrl, ctx.AbsolutePath, out match);
             if (route == null)
             {
                 var unknownRoute = "Unknown route " + ctx.RawUrl + " on method " + ctx.HttpMethod;
                 ctx.ReturnError(socket, 404, unknownRoute, false);
                 break;
             }
             var auth = Authentication.TryAuthorize(ctx.GetRequestHeader("authorization"), ctx.RawUrl, route);
             if (auth.Principal != null)
             {
                 ctx.ForRouteWithAuth(match, auth.Principal);
                 ThreadContext.Request  = ctx;
                 ThreadContext.Response = ctx;
                 if (principal != auth.Principal)
                 {
                     Thread.CurrentPrincipal = principal = auth.Principal;
                 }
                 using (var stream = route.Handle(match.OrderedArgs, ctx.Stream))
                 {
                     var keepAlive = ctx.Return(stream, socket);
                     if (keepAlive)
                     {
                         if (ctx.Pipeline)
                         {
                             continue;
                         }
                         else if (socket.Connected)
                         {
                             if (Requests.TryAdd(new RequestInfo(socket, socket.Available > 0)))
                             {
                                 return;
                             }
                         }
                     }
                     socket.Close();
                     return;
                 }
             }
             else if (auth.SendAuthenticate)
             {
                 ctx.AddHeader("WWW-Authenticate", MissingBasicAuth);
                 ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
                 return;
             }
             else
             {
                 ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
                 return;
             }
         }
     }
     catch (SecurityException sex)
     {
         try { ctx.ReturnError(socket, (int)HttpStatusCode.Forbidden, sex.Message, true); }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex);
         }
     }
     catch (ActionNotSupportedException anse)
     {
         try { ctx.ReturnError(socket, 404, anse.Message, true); }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         TraceSource.TraceEvent(TraceEventType.Error, 5403, "{0}", ex);
         try { ctx.ReturnError(socket, 500, ex.Message, false); }
         catch (Exception ex2)
         {
             Console.WriteLine(ex2.Message);
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex2);
         }
     }
 }