Exemple #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>();
 }
Exemple #2
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 maxLen = ConfigurationManager.AppSettings["Revenj.ContentLengthLimit"];
     if (!string.IsNullOrEmpty(maxLen)) MessageSizeLimit = int.Parse(maxLen);
     var ka = ConfigurationManager.AppSettings["Revenj.KeepAliveLimit"];
     if (!string.IsNullOrEmpty(ka)) KeepAliveTimeout = int.Parse(ka);
     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>();
     Context = new ThreadLocal<HttpSocketContext>(() => new HttpSocketContext("http://127.0.0.1/", MessageSizeLimit));
     var ca = ConfigurationManager.AppSettings["Revenj.HttpCapacity"];
     if (!string.IsNullOrEmpty(ca))
         Requests = new BlockingCollection<RequestInfo>(new ConcurrentQueue<RequestInfo>(), int.Parse(ca));
     else
         Requests = new BlockingCollection<RequestInfo>(new ConcurrentQueue<RequestInfo>());
 }
Exemple #3
0
 public HttpSocketContext(string prefix, int limit, Routes routes)
 {
     this.Prefix = prefix;
     this.Limit = limit;
     this.Routes = routes;
     InputStream = ChunkedMemoryStream.Static();
     OutputStream = ChunkedMemoryStream.Static();
 }