private HeaderValue Find(string name) { HeaderType type = HeaderTypeFactory.Find(name); HeaderValue value; if (mValues.TryGetValue(type.ID, out value)) { return(value); } value = new HeaderValue(type, null); mValues[type.ID] = value; return(value); }
private HeaderValue Find(string name) { HeaderValue result; for (int i = 0; i < mValues.Count; i++) { result = mValues[i]; if (result.Type.Compare(name)) { return(result); } } HeaderType type = HeaderTypeFactory.Find(name); if (type == null) { type = new HeaderType(name); } result = new HeaderValue(type, null); mValues.Add(result); return(result); }
public bool Read(PipeStream stream, Cookies cookies) { IndexOfResult index = stream.IndexOf(HeaderTypeFactory.LINE_BYTES); while (index.End != null) { if (index.Length == 2) { stream.ReadFree(2); return(true); } else { ReadOnlySpan <Char> line = HttpParse.ReadCharLine(index); stream.ReadFree(index.Length); if (line[0] == 'C' && line[5] == 'e' && line[1] == 'o' && line[2] == 'o' && line[3] == 'k' && line[4] == 'i') { HttpParse.AnalyzeCookie(line.Slice(8, line.Length - 8), cookies); } else { Tuple <string, string> result = HttpParse.AnalyzeHeader(line); HeaderType type = HeaderTypeFactory.Find(result.Item1); if (type == null) { Add(result.Item1, result.Item2); } else { Add(type.Name, result.Item2); } } } index = stream.IndexOf(HeaderTypeFactory.LINE_BYTES); } return(false); }
public void Open() { NetConfig config = new NetConfig(); config.Host = ServerConfig.Host; config.Port = ServerConfig.Port; config.CertificateFile = ServerConfig.CertificateFile; config.CertificatePassword = ServerConfig.CertificatePassword; config.BufferSize = ServerConfig.BufferSize; config.LogLevel = ServerConfig.LogLevel; config.Combined = ServerConfig.PacketCombined; config.SessionTimeOut = ServerConfig.SessionTimeOut; config.UseIPv6 = ServerConfig.UseIPv6; config.BufferPoolMaxMemory = ServerConfig.BufferPoolMaxMemory; if (!string.IsNullOrEmpty(config.CertificateFile)) { config.SSL = true; } config.LittleEndian = false; AppDomain.CurrentDomain.AssemblyResolve += ResolveHandler; HttpPacket hp = new HttpPacket(this, this); mServer = SocketFactory.CreateTcpServer(config, this, hp); Name = "BeetleX Http Server"; if (mAssemblies != null) { foreach (System.Reflection.Assembly assembly in mAssemblies) { mResourceCenter.LoadManifestResource(assembly); } } mResourceCenter.LoadManifestResource(typeof(HttpApiServer).Assembly); mResourceCenter.Path = ServerConfig.StaticResourcePath; mResourceCenter.Debug = ServerConfig.Debug; mResourceCenter.Load(); ModuleManage.Load(); StartTime = DateTime.Now; mServer.Open(); mServerCounter = new ServerCounter(this); mUrlRewrite.UrlIgnoreCase = ServerConfig.UrlIgnoreCase; mUrlRewrite.AddRegion(this.ServerConfig.Routes); HeaderTypeFactory.Find(HeaderTypeFactory.HOST); AppDomain.CurrentDomain.UnhandledException += (s, e) => { using (System.IO.StreamWriter writer = new StreamWriter("__UnhandledException.txt")) { Exception error = e.ExceptionObject as Exception; writer.WriteLine(DateTime.Now); if (error != null) { writer.WriteLine(error.Message); writer.WriteLine(error.StackTrace); if (error.InnerException != null) { writer.WriteLine(error.InnerException.Message); writer.WriteLine(error.InnerException.StackTrace); } } else { writer.WriteLine("Unhandled Exception:" + e.ExceptionObject.ToString()); } writer.Flush(); } }; mServer.Log(LogType.Info, null, $"BeetleX FastHttpApi start@[v:{typeof(HttpApiServer).Assembly.GetName().Version}]"); }
private void OnWrite(PipeStream stream) { IResult result = mBody as IResult; if (result != null) { result.Setting(this); } byte[] buffer = HttpParse.GetByteBuffer(); int hlen = 0; hlen = hlen + Encoding.ASCII.GetBytes(HttpVersion, 0, HttpVersion.Length, buffer, hlen); buffer[hlen] = HeaderTypeFactory._SPACE_BYTE; hlen++; hlen = hlen + Encoding.ASCII.GetBytes(Code, 0, Code.Length, buffer, hlen); buffer[hlen] = HeaderTypeFactory._SPACE_BYTE; hlen++; hlen = hlen + Encoding.ASCII.GetBytes(CodeMsg, 0, CodeMsg.Length, buffer, hlen); buffer[hlen] = HeaderTypeFactory._LINE_R; hlen++; buffer[hlen] = HeaderTypeFactory._LINE_N; hlen++; stream.Write(buffer, 0, hlen); stream.Write(HeaderTypeFactory.SERVAR_HEADER_BYTES, 0, HeaderTypeFactory.SERVAR_HEADER_BYTES.Length); Header.Write(stream); if (result != null) { result.ContentType.Write(stream); } var datebuffer = GMTDate.Default.DATE; stream.Write(datebuffer.Array, 0, datebuffer.Count); for (int i = 0; i < mSetCookies.Count; i++) { HeaderTypeFactory.Write(HeaderTypeFactory.SET_COOKIE, stream); stream.Write(mSetCookies[i]); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); } if (mBody != null) { if (mBody is IDataResponse dataResponse) { stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); dataResponse.Write(stream); } else { if (result.HasBody) { if (result.Length > 0) { stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.CONTENT_LENGTH_BYTES.Length); stream.Write(result.Length.ToString()); stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4); result.Write(stream, this); } else { stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.CONTENT_LENGTH_BYTES.Length); MemoryBlockCollection contentLength = stream.Allocate(10); stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4); int len = stream.CacheLength; result.Write(stream, this); int count = stream.CacheLength - len; // contentLength.Full(count.ToString().PadRight(10), stream.Encoding); contentLength.Full(GetLengthBuffer(count.ToString())); } } else { stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES.Length); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); } } } else { stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES.Length); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); } if (Session.Server.EnableLog(EventArgs.LogType.Debug)) { Session.Server.Log(EventArgs.LogType.Debug, Session, "{0} {1}", Request.RemoteIPAddress, this.ToString()); } if (Session.Server.EnableLog(EventArgs.LogType.Info)) { Session.Server.Log(EventArgs.LogType.Info, Session, "{4} {0} {1} response {2} {3}", Request.Method, Request.Url, Code, CodeMsg, Request.RemoteIPAddress); } }
public void Open() { AppDomain.CurrentDomain.AssemblyResolve += ResolveHandler; HttpPacket hp = new HttpPacket(this, this); mServer = SocketFactory.CreateTcpServer(this, hp) .Setting(o => { o.DefaultListen.Host = Options.Host; o.IOQueueEnabled = Options.IOQueueEnabled; o.DefaultListen.Port = Options.Port; o.BufferSize = Options.BufferSize; o.LogLevel = Options.LogLevel; o.Combined = Options.PacketCombined; o.SessionTimeOut = Options.SessionTimeOut; o.UseIPv6 = Options.UseIPv6; o.BufferPoolMaxMemory = Options.BufferPoolMaxMemory; o.LittleEndian = false; o.Statistical = Options.Statistical; }); if (Options.SSL) { mServer.Setting(o => { o.AddListenSSL(Options.CertificateFile, Options.CertificatePassword, o.DefaultListen.Host, Options.SSLPort); }); } Name = "BeetleX Http Server"; if (mAssemblies != null) { foreach (System.Reflection.Assembly assembly in mAssemblies) { mResourceCenter.LoadManifestResource(assembly); } } mResourceCenter.LoadManifestResource(typeof(HttpApiServer).Assembly); mResourceCenter.Path = Options.StaticResourcePath; mResourceCenter.Debug = Options.Debug; mResourceCenter.Load(); ModuleManager.Load(); if (Options.ManageApiEnabled) { ServerController serverStatusController = new ServerController(); mActionFactory.Register(serverStatusController); } StartTime = DateTime.Now; mServer.Open(); mServerCounter = new ServerCounter(this); mUrlRewrite.UrlIgnoreCase = Options.UrlIgnoreCase; mUrlRewrite.AddRegion(this.Options.Routes); HeaderTypeFactory.Find(HeaderTypeFactory.HOST); AppDomain.CurrentDomain.UnhandledException += (s, e) => { using (System.IO.StreamWriter writer = new StreamWriter("__UnhandledException.txt")) { Exception error = e.ExceptionObject as Exception; writer.WriteLine(DateTime.Now); if (error != null) { writer.WriteLine(error.Message); writer.WriteLine(error.StackTrace); if (error.InnerException != null) { writer.WriteLine(error.InnerException.Message); writer.WriteLine(error.InnerException.StackTrace); } } else { writer.WriteLine("Unhandled Exception:" + e.ExceptionObject.ToString()); } writer.Flush(); } }; mServer.Log(LogType.Info, null, $"BeetleX FastHttpApi start@[v:{typeof(HttpApiServer).Assembly.GetName().Version}]"); OnOptionLoad(new EventOptionsReloadArgs { HttpApiServer = this, HttpOptions = this.Options }); OnStrated(new EventHttpServerStartedArgs { HttpApiServer = this }); }
public void Open() { var date = GMTDate.Default.DATE; var ct = ContentTypes.TEXT_UTF8; var a = HeaderTypeFactory.Find("Content-Length"); AppDomain.CurrentDomain.AssemblyResolve += ResolveHandler; HttpPacket hp = new HttpPacket(this, this); var gtmdate = GMTDate.Default; string serverInfo = $"Server: BeetleX[{typeof(BeetleX.BXException).Assembly.GetName().Version}]/FastHttpApi[{typeof(HttpApiServer).Assembly.GetName().Version}]\r\n"; HeaderTypeFactory.SERVAR_HEADER_BYTES = Encoding.ASCII.GetBytes(serverInfo); mServer = SocketFactory.CreateTcpServer(this, hp) .Setting(o => { o.SyncAccept = Options.SyncAccept; // o.IOQueues = Options.IOQueues; o.DefaultListen.Host = Options.Host; // o.IOQueueEnabled = Options.IOQueueEnabled; o.DefaultListen.Port = Options.Port; o.BufferSize = Options.BufferSize; o.LogLevel = Options.LogLevel; o.Combined = Options.PacketCombined; o.SessionTimeOut = Options.SessionTimeOut; o.UseIPv6 = Options.UseIPv6; o.BufferPoolMaxMemory = Options.BufferPoolMaxMemory; o.LittleEndian = false; o.Statistical = Options.Statistical; o.BufferPoolGroups = Options.BufferPoolGroups; o.BufferPoolSize = Options.BufferPoolSize; o.PrivateBufferPool = Options.PrivateBufferPool; o.PrivateBufferPoolSize = Options.MaxBodyLength; o.MaxWaitMessages = Options.MaxWaitQueue; }); if (Options.IOQueueEnabled) { mRequestIOQueues = new DispatchCenter <IOQueueProcessArgs>(OnIOQueueProcess, Options.IOQueues); } if (Options.SSL) { mServer.Setting(o => { o.AddListenSSL(Options.CertificateFile, Options.CertificatePassword, o.DefaultListen.Host, Options.SSLPort); }); } Name = "BeetleX Http Server"; if (mAssemblies != null) { foreach (System.Reflection.Assembly assembly in mAssemblies) { mResourceCenter.LoadManifestResource(assembly); } } mResourceCenter.LoadManifestResource(typeof(HttpApiServer).Assembly); mResourceCenter.Path = Options.StaticResourcePath; mResourceCenter.Debug = Options.Debug; mResourceCenter.Load(); ModuleManager.Load(); if (Options.ManageApiEnabled) { ServerController serverStatusController = new ServerController(); mActionFactory.Register(serverStatusController); } StartTime = DateTime.Now; mServer.Open(); mServerCounter = new ServerCounter(this); mUrlRewrite.UrlIgnoreCase = Options.UrlIgnoreCase; mUrlRewrite.Load(); //mUrlRewrite.AddRegion(this.Options.Routes); HeaderTypeFactory.Find(HeaderTypeFactory.HOST); AppDomain.CurrentDomain.UnhandledException += (s, e) => { using (System.IO.StreamWriter writer = new StreamWriter("__UnhandledException.txt")) { Exception error = e.ExceptionObject as Exception; writer.WriteLine(DateTime.Now); if (error != null) { writer.WriteLine(error.Message); writer.WriteLine(error.StackTrace); if (error.InnerException != null) { writer.WriteLine(error.InnerException.Message); writer.WriteLine(error.InnerException.StackTrace); } } else { writer.WriteLine("Unhandled Exception:" + e.ExceptionObject.ToString()); } writer.Flush(); } }; mServer.Log(LogType.Info, null, $"BeetleX FastHttpApi [IOQueue:{Options.IOQueueEnabled}|Threads:{Options.IOQueues}] [V:{typeof(HttpApiServer).Assembly.GetName().Version}]"); OnOptionLoad(new EventOptionsReloadArgs { HttpApiServer = this, HttpOptions = this.Options }); OnStrated(new EventHttpServerStartedArgs { HttpApiServer = this }); }
public void Open() { NetConfig config = new NetConfig(); config.Host = ServerConfig.Host; config.Port = ServerConfig.Port; config.CertificateFile = ServerConfig.CertificateFile; config.CertificatePassword = ServerConfig.CertificatePassword; config.BufferSize = ServerConfig.BufferSize; config.LogLevel = ServerConfig.LogLevel; if (!string.IsNullOrEmpty(config.CertificateFile)) { config.SSL = true; } config.LittleEndian = false; HttpPacket hp = new HttpPacket(this.ServerConfig, this); mServer = SocketFactory.CreateTcpServer(config, this, hp); Name = "FastHttpApi Http Server"; if (mAssemblies != null) { foreach (System.Reflection.Assembly assembly in mAssemblies) { mResourceCenter.LoadManifestResource(assembly); } } mResourceCenter.LoadManifestResource(typeof(HttpApiServer).Assembly); mResourceCenter.Path = ServerConfig.StaticResourcePath; mResourceCenter.Debug = ServerConfig.Debug; mResourceCenter.Load(); StartTime = DateTime.Now; mServer.Open(); mServerCounter = new ServerCounter(this); HeaderTypeFactory.Find(HeaderTypeFactory.HOST); AppDomain.CurrentDomain.UnhandledException += (s, e) => { using (System.IO.StreamWriter writer = new StreamWriter("__UnhandledException.txt")) { Exception error = e.ExceptionObject as Exception; writer.WriteLine(DateTime.Now); if (error != null) { writer.WriteLine(error.Message); writer.WriteLine(error.StackTrace); if (error.InnerException != null) { writer.WriteLine(error.InnerException.Message); writer.WriteLine(error.InnerException.StackTrace); } } else { writer.WriteLine("Unhandled Exception:" + e.ExceptionObject.ToString()); } writer.Flush(); } }; if (EnableLog(LogType.Info)) { mServer.Log(LogType.Info, null, "FastHttpApi Server started!"); } }
internal void Write(PipeStream stream) { IResult result = mBody as IResult; if (result != null) { this.Header[HeaderTypeFactory.CONTENT_TYPE] = result.ContentType; result.Setting(this); } byte[] buffer = HttpParse.GetByteBuffer(); //string line = string.Concat(HttpVersion, " ", mCode, " ", CodeMsg, "\r\n"); //var hlen = Encoding.ASCII.GetBytes(line, 0, line.Length, buffer, 0); int hlen = 0; hlen = hlen + Encoding.ASCII.GetBytes(HttpVersion, 0, HttpVersion.Length, buffer, hlen); buffer[hlen] = HeaderTypeFactory._SPACE_BYTE; hlen++; hlen = hlen + Encoding.ASCII.GetBytes(mCode, 0, mCode.Length, buffer, hlen); buffer[hlen] = HeaderTypeFactory._SPACE_BYTE; hlen++; hlen = hlen + Encoding.ASCII.GetBytes(CodeMsg, 0, CodeMsg.Length, buffer, hlen); buffer[hlen] = HeaderTypeFactory._LINE_R; hlen++; buffer[hlen] = HeaderTypeFactory._LINE_N; hlen++; stream.Write(buffer, 0, hlen); stream.Write(HeaderTypeFactory.SERVAR_HEADER_BYTES, 0, HeaderTypeFactory.SERVAR_HEADER_BYTES.Length); Header.Write(stream); for (int i = 0; i < mSetCookies.Count; i++) { HeaderTypeFactory.Write(HeaderTypeFactory.SET_COOKIE, stream); stream.Write(mSetCookies[i]); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); } if (mBody != null) { StaticResurce.FileBlock fb = mBody as StaticResurce.FileBlock; if (fb != null) { stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); fb.Write(stream); } else { if (result.HasBody) { if (result.Length > 0) { stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.CONTENT_LENGTH_BYTES.Length); stream.Write(result.Length.ToString()); stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4); result.Write(stream, this); } else { stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.CONTENT_LENGTH_BYTES.Length); MemoryBlockCollection contentLength = stream.Allocate(10); stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4); int len = stream.CacheLength; result.Write(stream, this); int count = stream.CacheLength - len; //contentLength.Full("Content-Length: " + count.ToString().PadRight(10) + "\r\n\r\n", stream.Encoding); contentLength.Full(count.ToString().PadRight(10), stream.Encoding); } } else { stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES.Length); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); } } } else { stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES.Length); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); } if (Session.Server.EnableLog(EventArgs.LogType.Debug)) { Session.Server.Log(EventArgs.LogType.Debug, Session, "{0} {1}", Request.ClientIPAddress, this.ToString()); } if (Session.Server.EnableLog(EventArgs.LogType.Info)) { Session.Server.Log(EventArgs.LogType.Info, Session, "{4} {0} {1} response {2} {3}", Request.Method, Request.Url, Code, CodeMsg, Request.ClientIPAddress); } }
internal void Write(PipeStream stream) { IResult result = mBody as IResult; if (result != null) { this.Header[HeaderTypeFactory.CONTENT_TYPE] = result.ContentType; result.Setting(this); } stream.Write(HttpVersion); stream.Write(HeaderTypeFactory.SPACE_BYTES[0]); stream.Write(mCode); stream.Write(HeaderTypeFactory.SPACE_BYTES[0]); stream.Write(CodeMsg); stream.Write(HeaderTypeFactory.LINE_BYTES); Header.Write(stream); for (int i = 0; i < mSetCookies.Count; i++) { HeaderTypeFactory.Write(HeaderTypeFactory.SET_COOKIE, stream); stream.Write(mSetCookies[i]); stream.Write(HeaderTypeFactory.LINE_BYTES); } if (mBody != null) { StaticResurce.FileBlock fb = mBody as StaticResurce.FileBlock; if (fb != null) { stream.Write(HeaderTypeFactory.LINE_BYTES); fb.Write(stream); } else { if (result.HasBody) { MemoryBlockCollection contentLength = stream.Allocate(28); stream.Write(HeaderTypeFactory.LINE_BYTES); int len = stream.CacheLength; result.Write(stream, this); int count = stream.CacheLength - len; contentLength.Full("Content-Length: " + count.ToString().PadRight(10) + "\r\n", stream.Encoding); } else { stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES); stream.Write(HeaderTypeFactory.LINE_BYTES); } } } else { stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES); stream.Write(HeaderTypeFactory.LINE_BYTES); } if (Session.Server.EnableLog(EventArgs.LogType.Debug)) { Session.Server.Log(EventArgs.LogType.Debug, Session, "{0} {1}", Request.ClientIPAddress, this.ToString()); } if (Session.Server.EnableLog(EventArgs.LogType.Info)) { Session.Server.Log(EventArgs.LogType.Info, Session, "{4} {0} {1} response {2} {3}", Request.Method, Request.Url, Code, CodeMsg, Request.ClientIPAddress); } }