private void OnEncode(ISession session, object data, System.IO.Stream stream) { PipeStream pstream = stream.ToPipeStream(); MemoryBlockCollection msgsize = pstream.Allocate(4); int length = (int)pstream.CacheLength; OnWrite(session, data, pstream); if (SizeType == FixedSizeType.INT) { msgsize.Full((int)pstream.CacheLength - length); } else { msgsize.Full((Int16)pstream.CacheLength - length); } }
public void Encode(object data, IClient client, Stream stream) { PipeStream pstream = stream.ToPipeStream(); MemoryBlockCollection msgsize = pstream.Allocate(4); int length = (int)pstream.CacheLength; OnWrite(data, client, pstream); if (SizeType == FixedSizeType.INT) { msgsize.Full((int)pstream.CacheLength - length); } else { msgsize.Full((Int16)pstream.CacheLength - length); } }
protected override void OnWrite(ISession session, object data, PipeStream writer) { TypeHeader.WriteType(data, writer); MemoryBlockCollection bodysize = writer.Allocate(4); int bodyStartlegnth = (int)writer.CacheLength; ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(writer, data); bodysize.Full((int)writer.CacheLength - bodyStartlegnth); }
public void Execute(PipeStream stream) { stream.Write(Method + " "); stream.Write(Url); if (QuestryString != null && QuestryString.Count > 0) { int i = 0; foreach (var item in this.QuestryString) { if (i == 0) { stream.Write("?"); } else { stream.Write("&"); } stream.Write(item.Key + "="); stream.Write(System.Net.WebUtility.UrlEncode(item.Value)); i++; } } stream.Write(HeaderTypeFactory.SPACE_BYTES, 0, 1); stream.Write(this.HttpProtocol); stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); if (Header != null) { Header.Write(stream); } if (Method == POST || Method == PUT) { if (Body != null) { stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, 16); MemoryBlockCollection contentLength = stream.Allocate(10); stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4); int len = stream.CacheLength; Formater.Serialization(Body, stream); int count = stream.CacheLength - len; 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.LINE_BYTES, 0, 2); } }
internal void Write(PipeStream stream) { string stateLine = string.Concat(HttpVersion, " ", mCode, " ", mCodeMsg); stream.WriteLine(stateLine); Header.Write(stream); MemoryBlockCollection contentLength = stream.Allocate(28); stream.WriteLine(""); int count = Serializer.Serialize(stream, mBody); contentLength.Full("Content-Length: " + count.ToString().PadRight(10) + "\r\n", stream.Encoding); }
internal void Write(PipeStream stream) { stream.Write(HttpVersion); stream.Write(HeaderType.SPACE_BYTES[0]); stream.Write(mCode); stream.Write(HeaderType.SPACE_BYTES[0]); stream.Write(CodeMsg); stream.Write(HeaderType.LINE_BYTES); Header.Write(stream); for (int i = 0; i < mSetCookies.Count; i++) { HeaderType.Write(HeaderType.SET_COOKIE, stream); stream.Write(mSetCookies[i]); stream.Write(HeaderType.LINE_BYTES); } if (mBody != null) { StaticResurce.FileBlock fb = mBody as StaticResurce.FileBlock; if (fb != null) { stream.Write(HeaderType.LINE_BYTES); fb.Write(stream); } else { MemoryBlockCollection contentLength = stream.Allocate(28); stream.Write(HeaderType.LINE_BYTES); int count = Serializer.Serialize(stream, mBody); contentLength.Full("Content-Length: " + count.ToString().PadRight(10) + "\r\n", stream.Encoding); } } else { stream.Write(HeaderType.NULL_CONTENT_LENGTH_BYTES); stream.Write(HeaderType.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); } }
internal void Write(PipeStream stream) { stream.Write(HttpVersion); stream.Write(HeaderType.SPACE_BYTES[0]); stream.Write(mCode); stream.Write(HeaderType.SPACE_BYTES[0]); stream.Write(CodeMsg); stream.Write(HeaderType.LINE_BYTES); Header.Write(stream); for (int i = 0; i < mSetCookies.Count; i++) { HeaderType.Write(HeaderType.SET_COOKIE, stream); stream.Write(mSetCookies[i]); stream.Write(HeaderType.LINE_BYTES); } if (mBody != null) { StaticResurce.FileBlock fb = mBody as StaticResurce.FileBlock; if (fb != null) { stream.Write(HeaderType.LINE_BYTES); fb.Write(stream); } else { MemoryBlockCollection contentLength = stream.Allocate(28); stream.Write(HeaderType.LINE_BYTES); int count = Serializer.Serialize(stream, mBody); contentLength.Full("Content-Length: " + count.ToString().PadRight(10) + "\r\n", stream.Encoding); } } else { stream.Write(HeaderType.NULL_CONTENT_LENGTH_BYTES); stream.Write(HeaderType.LINE_BYTES); } }
internal void Execute(PipeStream stream) { var buffer = HttpParse.GetByteBuffer(); int offset = 0; offset += Encoding.ASCII.GetBytes(Method, 0, Method.Length, buffer, offset); buffer[offset] = HeaderTypeFactory._SPACE_BYTE; offset++; offset += Encoding.ASCII.GetBytes(Url, 0, Url.Length, buffer, offset); if (QuestryString != null && QuestryString.Count > 0) { int i = 0; foreach (var item in this.QuestryString) { string key = item.Key; string value = item.Value; if (string.IsNullOrEmpty(value)) { continue; } value = System.Net.WebUtility.UrlEncode(value); if (i == 0) { buffer[offset] = HeaderTypeFactory._QMARK; offset++; } else { buffer[offset] = HeaderTypeFactory._AND; offset++; } offset += Encoding.ASCII.GetBytes(key, 0, key.Length, buffer, offset); buffer[offset] = HeaderTypeFactory._EQ; offset++; offset += Encoding.ASCII.GetBytes(value, 0, value.Length, buffer, offset); i++; } } buffer[offset] = HeaderTypeFactory._SPACE_BYTE; offset++; offset += Encoding.ASCII.GetBytes(HttpProtocol, 0, HttpProtocol.Length, buffer, offset); buffer[offset] = HeaderTypeFactory._LINE_R; offset++; buffer[offset] = HeaderTypeFactory._LINE_N; offset++; stream.Write(buffer, 0, offset); if (Header != null) { Header.Write(stream); } if (Method == POST || Method == PUT) { if (Body != null) { stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, 16); MemoryBlockCollection contentLength = stream.Allocate(10); stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4); int len = stream.CacheLength; Formater.Serialization(Body, stream); int count = stream.CacheLength - len; 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.LINE_BYTES, 0, 2); } }
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); } }
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); } }