示例#1
0
 public HttpServer(ILogFactory logFactory, IServiceLocator locator)
 {
     Logger = logFactory.Create("Http server");
     Listener = new HttpListener();
     Listener.IgnoreWriteExceptions = true;
     foreach (string key in ConfigurationManager.AppSettings.Keys)
     {
         if (key.StartsWith("HttpAddress", StringComparison.InvariantCultureIgnoreCase))
             Listener.Prefixes.Add(ConfigurationManager.AppSettings[key]);
     }
     if (Listener.Prefixes.Count == 0)
     {
         Listener.Prefixes.Add("http://*:80/");
         Listener.Prefixes.Add("https://*:443/");
     }
     Routes = new Routes(locator);
     var customAuth = ConfigurationManager.AppSettings["CustomAuth"];
     if (!string.IsNullOrEmpty(customAuth))
     {
         var authType = Type.GetType(customAuth);
         if (!typeof(HttpAuth).IsAssignableFrom(authType))
             throw new ConfigurationErrorsException("Custom auth does not inherit from HttpAuth. Please inherit from " + typeof(HttpAuth).FullName);
         Authentication = locator.Resolve<HttpAuth>(authType);
     }
     else Authentication = locator.Resolve<HttpAuth>();
 }
示例#2
0
        public HttpListenerServer(IServiceProvider locator)
        {
            Listener = new HttpListener();
            Listener.IgnoreWriteExceptions = true;
            foreach (string key in ConfigurationManager.AppSettings.Keys)
            {
                if (key.StartsWith("HttpAddress", StringComparison.InvariantCultureIgnoreCase))
                {
                    Listener.Prefixes.Add(ConfigurationManager.AppSettings[key]);
                }
            }
            if (Listener.Prefixes.Count == 0)
            {
                Listener.Prefixes.Add("http://*:8999/");
            }
            Routes = locator.Resolve <Routes>();
            var customAuth = ConfigurationManager.AppSettings["CustomAuth"];

            if (!string.IsNullOrEmpty(customAuth))
            {
                var authType = Type.GetType(customAuth);
                if (!typeof(HttpAuth).IsAssignableFrom(authType))
                {
                    throw new ConfigurationErrorsException("Custom auth does not inherit from HttpAuth. Please inherit from " + typeof(HttpAuth).FullName);
                }
                Authentication = locator.Resolve <HttpAuth>(authType);
            }
            else
            {
                Authentication = locator.Resolve <HttpAuth>();
            }
        }
示例#3
0
        public HttpSocketServer(IServiceProvider locator)
        {
            this.Locator = locator;
            var endpoints   = new List <IPEndPoint>();
            var networkType = AddressFamily.InterNetworkV6;

            foreach (string key in ConfigurationManager.AppSettings.Keys)
            {
                if (key.StartsWith("HttpAddress", StringComparison.InvariantCultureIgnoreCase))
                {
                    var       addr = new Uri(ConfigurationManager.AppSettings[key]);
                    IPAddress ip;
                    if (!IPAddress.TryParse(addr.Host, out ip))
                    {
                        var ips = Dns.GetHostAddresses(addr.Host);
                        foreach (var i in ips)
                        {
                            if (i.AddressFamily == networkType)
                            {
                                endpoints.Add(new IPEndPoint(i, addr.Port));
                            }
                        }
                        if (endpoints.Count == 0 && ips.Length > 0)
                        {
                            if (ips[0].AddressFamily == AddressFamily.InterNetwork)
                            {
                                networkType = AddressFamily.InterNetwork;
                                foreach (var i in ips)
                                {
                                    if (i.AddressFamily == networkType)
                                    {
                                        endpoints.Add(new IPEndPoint(i, addr.Port));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        endpoints.Add(new IPEndPoint(ip, addr.Port));
                    }
                }
            }
            if (endpoints.Count == 0)
            {
                Console.WriteLine("Http address not found in config. Starting IPv6 on all interfaces");
                endpoints.Add(new IPEndPoint(Socket.OSSupportsIPv6 ? IPAddress.IPv6Any : IPAddress.Any, 8999));
            }
            else if (endpoints.FindAll(it => it.AddressFamily == AddressFamily.InterNetwork).Count == endpoints.Count)
            {
                networkType = AddressFamily.InterNetwork;
            }
            else if (endpoints.FindAll(it => it.AddressFamily == AddressFamily.InterNetworkV6).Count != endpoints.Count)
            {
                throw new ConfigurationErrorsException(@"Unable to setup configuration for both IPv4 and IPv6. Use either only IPv4 or IPv6. 
Please check settings: " + string.Join(", ", endpoints));
            }
            Socket = new Socket(networkType, SocketType.Stream, ProtocolType.Tcp);
            //Socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0);
            foreach (var ep in endpoints)
            {
                Socket.Bind(ep);
                Console.WriteLine("Bound to: " + ep);
            }
            var customAuth = ConfigurationManager.AppSettings["CustomAuth"];

            if (!string.IsNullOrEmpty(customAuth))
            {
                var authType = Type.GetType(customAuth);
                if (!typeof(HttpAuth).IsAssignableFrom(authType))
                {
                    throw new ConfigurationErrorsException("Custom auth does not inherit from HttpAuth. Please inherit from " + typeof(HttpAuth).FullName);
                }
                Authentication = locator.Resolve <HttpAuth>(authType);
            }
            else
            {
                Authentication = locator.Resolve <HttpAuth>();
            }
            var routes = locator.Resolve <Routes>();

            Context = new ThreadLocal <HttpSocketContext>(() => new HttpSocketContext("http://127.0.0.1/", MessageSizeLimit, routes));
            var ca = ConfigurationManager.AppSettings["Revenj.HttpCapacity"];

            Requests = !string.IsNullOrEmpty(ca)
                                ? new BlockingCollection <RequestInfo>((int)Math.Log(int.Parse(ca), 2))
                                : new BlockingCollection <RequestInfo>();
        }
示例#4
0
 public HttpSocketServer(IServiceProvider locator)
 {
     this.Locator = locator;
     var endpoints = new List<IPEndPoint>();
     var networkType = AddressFamily.InterNetworkV6;
     foreach (string key in ConfigurationManager.AppSettings.Keys)
     {
         if (key.StartsWith("HttpAddress", StringComparison.InvariantCultureIgnoreCase))
         {
             var addr = new Uri(ConfigurationManager.AppSettings[key]);
             IPAddress ip;
             if (!IPAddress.TryParse(addr.Host, out ip))
             {
                 var ips = Dns.GetHostAddresses(addr.Host);
                 foreach (var i in ips)
                     if (i.AddressFamily == networkType)
                         endpoints.Add(new IPEndPoint(i, addr.Port));
                 if (endpoints.Count == 0 && ips.Length > 0)
                 {
                     if (ips[0].AddressFamily == AddressFamily.InterNetwork)
                     {
                         networkType = AddressFamily.InterNetwork;
                         foreach (var i in ips)
                             if (i.AddressFamily == networkType)
                                 endpoints.Add(new IPEndPoint(i, addr.Port));
                     }
                 }
             }
             else endpoints.Add(new IPEndPoint(ip, addr.Port));
         }
     }
     if (endpoints.Count == 0)
     {
         Console.WriteLine("Http address not found in config. Starting IPv6 on all interfaces");
         endpoints.Add(new IPEndPoint(Socket.OSSupportsIPv6 ? IPAddress.IPv6Any : IPAddress.Any, 8999));
     }
     else if (endpoints.FindAll(it => it.AddressFamily == AddressFamily.InterNetwork).Count == endpoints.Count)
     {
         networkType = AddressFamily.InterNetwork;
     }
     else if (endpoints.FindAll(it => it.AddressFamily == AddressFamily.InterNetworkV6).Count != endpoints.Count)
     {
         throw new ConfigurationErrorsException(@"Unable to setup configuration for both IPv4 and IPv6. Use either only IPv4 or IPv6.
     Please check settings: " + string.Join(", ", endpoints));
     }
     Socket = new Socket(networkType, SocketType.Stream, ProtocolType.Tcp);
     //Socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0);
     foreach (var ep in endpoints)
     {
         Socket.Bind(ep);
         Console.WriteLine("Bound to: " + ep);
     }
     var customAuth = ConfigurationManager.AppSettings["CustomAuth"];
     if (!string.IsNullOrEmpty(customAuth))
     {
         var authType = Type.GetType(customAuth);
         if (!typeof(HttpAuth).IsAssignableFrom(authType))
             throw new ConfigurationErrorsException("Custom auth does not inherit from HttpAuth. Please inherit from " + typeof(HttpAuth).FullName);
         Authentication = locator.Resolve<HttpAuth>(authType);
     }
     else Authentication = locator.Resolve<HttpAuth>();
     var routes = locator.Resolve<Routes>();
     Context = new ThreadLocal<HttpSocketContext>(() => new HttpSocketContext("http://127.0.0.1/", MessageSizeLimit, routes));
     var ca = ConfigurationManager.AppSettings["Revenj.HttpCapacity"];
     Requests = !string.IsNullOrEmpty(ca)
         ? new BlockingCollection<RequestInfo>((int)Math.Log(int.Parse(ca), 2))
         : new BlockingCollection<RequestInfo>();
 }
示例#5
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;
 }
示例#6
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);
     }
 }