Esempio n. 1
0
        private void WriteTo(Stream responseStream, int paddingLength)
        {
            var response = RequestContext != null ? RequestContext.Response : null;

            if (this.FileInfo != null)
            {
                if (response != null)
                {
                    response.SetContentLength(FileInfo.Length + paddingLength);
                }

                using (var fs = this.FileInfo.OpenRead())
                {
                    fs.WriteTo(responseStream);
                    return;
                }
            }

            if (this.ResponseStream != null)
            {
                if (response != null)
                {
                    var ms = ResponseStream as MemoryStream;
                    if (ms != null)
                    {
                        var bytes = ms.ToArray();
                        response.SetContentLength(bytes.LongLength + paddingLength);

                        responseStream.Write(bytes, 0, bytes.Length);
                        return;
                    }
                }

                this.ResponseStream.WriteTo(responseStream);
                return;
            }

            if (this.ResponseText != null)
            {
                var bytes = Encoding.UTF8.GetBytes(this.ResponseText);
                if (response != null)
                {
                    response.SetContentLength(bytes.LongLength + paddingLength);
                }

                responseStream.Write(bytes, 0, bytes.Length);
                return;
            }

            if (this.ResponseFilter == null)
            {
                throw new ArgumentNullException("ResponseFilter");
            }
            if (this.RequestContext == null)
            {
                throw new ArgumentNullException("RequestContext");
            }

            var bytesResponse = this.Response as byte[];

            if (bytesResponse != null)
            {
                if (response != null)
                {
                    response.SetContentLength(bytesResponse.LongLength + paddingLength);
                }

                responseStream.Write(bytesResponse, 0, bytesResponse.Length);
                return;
            }

            if (View != null)
            {
                RequestContext.SetItem("View", View);
            }
            if (Template != null)
            {
                RequestContext.SetItem("Template", Template);
            }

            RequestContext.SetItem("HttpResult", this);

            ResponseFilter.SerializeToStream(this.RequestContext, this.Response, responseStream);
        }