Inheritance: IRequestContext, IResponseContext
Exemple #1
0
 public void CopyFrom(HttpSocketContext other)
 {
     RequestHeadersLength  = other.RequestHeadersLength;
     ResponseHeadersLength = 0;
     if (RequestHeaders.Length < RequestHeadersLength)
     {
         var newHeaders = new HeaderPair[other.RequestHeaders.Length];
         Array.Copy(other.RequestHeaders, newHeaders, RequestHeadersLength);
         RequestHeaders = newHeaders;
     }
     else
     {
         Array.Copy(other.RequestHeaders, RequestHeaders, RequestHeadersLength);
     }
     RawUrl        = other.RawUrl;
     positionInTmp = other.positionInTmp;
     Pipeline      = other.Pipeline;
     IsHttp10      = other.IsHttp10;
     totalBytes    = other.totalBytes;
     Buffer.BlockCopy(other.InputTemp, 0, InputTemp, 0, positionInTmp);
     other.InputStream.CopyTo(InputStream);
     InputStream.Position = other.InputStream.Position;
     InputStream.SetLength(other.InputStream.Length);
     HttpMethod               = other.HttpMethod;
     HttpProtocolVersion      = other.HttpProtocolVersion;
     TemplateMatch            = other.TemplateMatch;
     ResponseStatus           = HttpStatusCode.OK;
     ResponseLength           = null;
     ResponseContentType      = null;
     ResponseIsJson           = false;
     ContentTypeResponseIndex = -1;
     offsetInOutput           = 0;
 }
Exemple #2
0
        private void LogError(Socket socket, HttpSocketContext ctx, Exception ex)
        {
            var sex = ex as SecurityException;
            var ans = ex as ActionNotSupportedException;

            if (sex != null)
            {
                try { ctx.ReturnError(socket, (int)HttpStatusCode.Forbidden, sex.Message, true); }
                catch (Exception e)
                {
                    TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", e);
                }
            }
            else if (ans != null)
            {
                try { ctx.ReturnError(socket, 404, ans.Message, true); }
                catch (Exception e)
                {
                    TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", e);
                }
            }
            else
            {
                TraceSource.TraceEvent(TraceEventType.Error, 5403, "{0}", ex);
                try { ctx.ReturnError(socket, 500, ex.Message, false); }
                catch (Exception ex2)
                {
                    TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex2);
                }
            }
        }
Exemple #3
0
 public ThreadArgs(RequestInfo request, HttpSocketContext context, ManualResetEvent resetEvent, HttpAuth.AuthorizeOrError auth, RouteHandler route, RouteMatch match)
 {
     this.Request    = request;;
     this.Context    = context;
     this.ResetEvent = resetEvent;
     this.Auth       = auth;
     this.Route      = route;
     this.Match      = match;
 }
Exemple #4
0
 public ThreadArgs(Socket socket, HttpSocketContext context, ManualResetEventSlim resetEvent, HttpAuth.AuthorizeOrError auth, RouteHandler route, RouteMatch match)
 {
     this.Socket     = socket;
     this.Context    = context;
     this.ResetEvent = resetEvent;
     this.Auth       = auth;
     this.Route      = route;
     this.Match      = match;
 }
Exemple #5
0
 private void CheckAuth(Socket socket, HttpAuth.AuthorizeOrError auth, HttpSocketContext ctx)
 {
     if (auth.SendAuthenticate)
     {
         ctx.AddHeader("WWW-Authenticate", MissingBasicAuth);
         ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
     }
     else
     {
         ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
     }
 }
Exemple #6
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;
                }
            }
        }
Exemple #7
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;
                }
            }
        }
Exemple #8
0
 public ThreadArgs(RequestInfo request, HttpSocketContext context, ManualResetEvent resetEvent, HttpAuth.AuthorizeOrError auth, RouteHandler route, RouteMatch match)
 {
     this.Request = request; ;
     this.Context = context;
     this.ResetEvent = resetEvent;
     this.Auth = auth;
     this.Route = route;
     this.Match = match;
 }
Exemple #9
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;
         }
     }
 }
Exemple #10
0
 private void LogError(Socket socket, HttpSocketContext ctx, Exception ex)
 {
     var sex = ex as SecurityException;
     var ans = ex as ActionNotSupportedException;
     if (sex != null)
     {
         try { ctx.ReturnError(socket, (int)HttpStatusCode.Forbidden, sex.Message, true); }
         catch (Exception e)
         {
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", e);
         }
     }
     else if (ans != null)
     {
         try { ctx.ReturnError(socket, 404, ans.Message, true); }
         catch (Exception e)
         {
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", e);
         }
     }
     else
     {
         TraceSource.TraceEvent(TraceEventType.Error, 5403, "{0}", ex);
         try { ctx.ReturnError(socket, 500, ex.Message, false); }
         catch (Exception ex2)
         {
             TraceSource.TraceEvent(TraceEventType.Error, 5404, "{0}", ex2);
         }
     }
 }
Exemple #11
0
 private void CheckAuth(Socket socket, HttpAuth.AuthorizeOrError auth, HttpSocketContext ctx)
 {
     if (auth.SendAuthenticate)
     {
         ctx.AddHeader("WWW-Authenticate", MissingBasicAuth);
         ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
     }
     else
     {
         ctx.ReturnError(socket, (int)auth.ResponseCode, auth.Error, true);
     }
 }
Exemple #12
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);
         }
     }
 }
Exemple #13
0
 public void CopyFrom(HttpSocketContext other)
 {
     RequestHeadersLength = other.RequestHeadersLength;
     ResponseHeadersLength = 0;
     if (RequestHeaders.Length < RequestHeadersLength)
     {
         var newHeaders = new HeaderPair[other.RequestHeaders.Length];
         Array.Copy(other.RequestHeaders, newHeaders, RequestHeadersLength);
         RequestHeaders = newHeaders;
     }
     else Array.Copy(other.RequestHeaders, RequestHeaders, RequestHeadersLength);
     RawUrl = other.RawUrl;
     positionInTmp = other.positionInTmp;
     Pipeline = other.Pipeline;
     IsHttp10 = other.IsHttp10;
     totalBytes = other.totalBytes;
     Buffer.BlockCopy(other.InputTemp, 0, InputTemp, 0, positionInTmp);
     other.InputStream.CopyTo(InputStream);
     InputStream.Position = other.InputStream.Position;
     InputStream.SetLength(other.InputStream.Length);
     HttpMethod = other.HttpMethod;
     HttpProtocolVersion = other.HttpProtocolVersion;
     TemplateMatch = other.TemplateMatch;
     ResponseStatus = HttpStatusCode.OK;
     ResponseLength = null;
     ResponseContentType = null;
     ResponseIsJson = false;
     ContentTypeResponseIndex = -1;
     offsetInOutput = 0;
 }
Exemple #14
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);
         }
     }
 }