public override void Write(byte[] buffer, int offset, int count) { if (_position == HttpStreamPosition.None) throw new InvalidOperationException("リクエストヘッダの情報がセットされていません。"); if (_position != HttpStreamPosition.Entity) _strm.Write(ASCII_CRLF, 0, ASCII_CRLF.Length); _position = HttpStreamPosition.Entity; if (!_isChunked) _strm.Write(buffer, offset, count); else { var bytes = Encoding.ASCII.GetBytes(Convert.ToString(count, 16)); _strm.Write(bytes, 0, bytes.Length); _strm.Write(ASCII_CRLF, 0, ASCII_CRLF.Length); _entityLength += bytes.LongLength + ASCII_CRLF.LongLength; _strm.Write(buffer, offset, count); _strm.Write(ASCII_CRLF, 0, ASCII_CRLF.Length); _entityLength += ASCII_CRLF.LongLength; } _entityLength += count; }
public void WriteStatusLine(Version version, HttpStatusCode status) { if (this._position != HttpStreamPosition.None) throw new InvalidOperationException( "リクエストラインを書き込みは初期化後の何もしていない状態でのみ可能です。"); var s = string.Format("HTTP/{0} {1} {2}\r\n", version.ToString(), (int)status, status.ToString()); var bytes = Encoding.ASCII.GetBytes(s); _position = HttpStreamPosition.StatusLine; _strm.Write(bytes, 0, bytes.Length); }
public override void Close() { switch (_position) { case HttpStreamPosition.None: WriteStatusLine(new Version("1.1"), HttpStatusCode.Found); break; case HttpStreamPosition.StatusLine: _strm.Write(ASCII_CRLF, 0, ASCII_CRLF.Length); _position = HttpStreamPosition.Entity; break; } if (!_isChunked) { var buffer = new byte[1024]; int len; for (var i = _contentLength - Length; i > 0; i -= len) { len = (int)Math.Min(buffer.Length, buffer.Length); Write(buffer, 0, len); } } else { _strm.Write(Encoding.ASCII.GetBytes("0"), 0, 1); _strm.Write(ASCII_CRLF, 0, ASCII_CRLF.Length); _strm.Write(ASCII_CRLF, 0, ASCII_CRLF.Length); } }