public override void Write(byte[] buffer, int offset, int count) { #if DEBUG if (HttpTraceHelper.Api.TraceVerbose) { HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::Write() count:" + count.ToString()); } #endif if (m_ContentLength != -1 && m_SentContentLength + count > m_ContentLength) { Exception exception = new ProtocolViolationException(); #if DEBUG if (HttpTraceHelper.ExceptionThrown.TraceVerbose) { HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::Write() throwing: " + exception.ToString()); } #endif throw exception; } Socket checkSocket; if (!m_HttpListenerWebResponse.SentHeaders) { // // we didn't send the headers yet, do so now. // // we null out the Socket when we cleanup // make a local copy to avoid null reference exceptions checkSocket = m_HttpListenerWebResponse.Request.ConnectionState.ConnectionSocket; if (checkSocket != null) { #if DEBUG if (HttpTraceHelper.Socket.TraceVerbose) { HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::GetResponseStream() calling Socket.Send() Length:" + m_HttpListenerWebResponse.HeadersBuffer.Length.ToString()); } #endif checkSocket.Send(m_HttpListenerWebResponse.HeadersBuffer); m_HttpListenerWebResponse.SentHeaders = true; } } int DataToWrite = count; if (m_WriteChunked) { string ChunkHeader = "0x" + Convert.ToString( count, 16 ); DataToWrite += ChunkHeader.Length + 4; byte[] newBuffer = new byte[DataToWrite]; for (int index=0; index<ChunkHeader.Length; index++) { newBuffer[index] = (byte)ChunkHeader[index]; } newBuffer[ChunkHeader.Length] = 0x0D; newBuffer[ChunkHeader.Length + 1] = 0x0A; Buffer.BlockCopy(buffer, offset, newBuffer, ChunkHeader.Length + 2, count); newBuffer[DataToWrite - 2] = 0x0D; newBuffer[DataToWrite - 1] = 0x0A; buffer = newBuffer; offset = 0; } #if DEBUG if (HttpTraceHelper.Socket.TraceVerbose) { HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::GetResponseStream() calling Socket.Send() Length:" + DataToWrite.ToString()); } #endif // we null out the Socket when we cleanup // make a local copy to avoid null reference exceptions checkSocket = m_HttpListenerWebResponse.Request.ConnectionState.ConnectionSocket; if (checkSocket != null) { checkSocket.Send(buffer, offset, DataToWrite, SocketFlags.None); } if (m_ContentLength != -1) { // // keep track of the data transferred // m_SentContentLength -= count; } }