/// <summary> /// Initialize new instance /// </summary> /// <param name="port"></param> /// <param name="ip"></param> public HttpServer(int port, string ip = "*") { this.Port = port; this.Ip = ip; this.AddressFamily = ip != "*" && ValidateHelper.IsIPv6(ip, false) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; this.encoding = Encoding.UTF8; this.AuthorizationCallback = null; this.Backlog = 256; this.ServerName = Dns.GetHostName(); this.websockets = new Dictionary <Int64, HttpServerWebSocketContext>(); this.readHeadTimeout = 10000; this.fileHandler = new HttpServerFileHandler(); }
/// <summary> /// initialize new instance /// </summary> /// <param name="socket"></param> /// <param name="header"></param> /// <param name="server"></param> /// <param name="url"></param> /// <param name="method"></param> /// <param name="protocol"></param> /// <param name="maxRequestContentLength"></param> /// <param name="fileHandler"></param> internal HttpServerContext(Socket socket, NameValueCollection header, HttpServer server, string url, string method, string protocol, int maxRequestContentLength, IHttpServerFileHandler fileHandler) : base(header, server, url, method, protocol) { var version = 0d; try { double.TryParse(protocol.Split('/')[1], out version); } catch { } //Connection: keep-alive var keepAlive = header["Connection"] ?? string.Empty; if ("POST".Equals(method, StringComparison.OrdinalIgnoreCase)) { this.keepAlive = false; } else if ("keep-alive".Equals(keepAlive, StringComparison.OrdinalIgnoreCase)) { this.keepAlive = true; } else if ("close".Equals(keepAlive, StringComparison.OrdinalIgnoreCase)) { this.keepAlive = false; } else if (version > 1.0) { this.keepAlive = true; } //this.ResponseHeader.Add("Date", DateTime.Now.ToString("r")); //this.ResponseHeader.Add("Connection", this.KeepAlive ? "keep-alive" : "Close"); this.ResponseHeader.Add("Content-Type", "text/html"); this.socket = socket; this.contentBuffer = null; this.chunkWriteStatus = HttpServerChunkStatus.NoBegin; base.RequestType = HttpServerRequestType.Http; this.maxRequestContentLength = maxRequestContentLength; this.FileHandler = fileHandler; this.isRequestHead = method.Equals("HEAD", StringComparison.OrdinalIgnoreCase); this.acceptGzip = this.isRequestHead == false && (header["Accept-Encoding"] ?? "").IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1; // var authorization = header["Authorization"]; if (string.IsNullOrEmpty(authorization) == false && authorization.StartsWith("Basic ") && authorization.Length > 6) { this.ParseAuthorize(authorization); } }