예제 #1
0
 /// <summary>
 /// Checks if the SendFile extension is supported.
 /// </summary>
 /// <param name="response"></param>
 /// <returns>True if sendfile.SendAsync is defined in the environment.</returns>
 public static bool SupportsSendFile(this IOwinResponse response)
 {
     if (response == null)
     {
         throw new ArgumentNullException("response");
     }
     return(response.Get <SendFileFunc>(Constants.SendFileAsyncKey) != null);
 }
예제 #2
0
        public Task SendAsync()
        {
            ApplyResponseHeaders(Constants.Status200Ok);

            string       physicalPath = _fileInfo.PhysicalPath;
            SendFileFunc sendFile     = _response.Get <SendFileFunc>(Constants.SendFileAsyncKey);

            if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
            {
                return(sendFile(physicalPath, 0, _length, _request.CallCancelled));
            }

            Stream readStream    = _fileInfo.CreateReadStream();
            var    copyOperation = new StreamCopyOperation(readStream, _response.Body, _length, _request.CallCancelled);
            Task   task          = copyOperation.Start();

            task.ContinueWith(resultTask => readStream.Close(), TaskContinuationOptions.ExecuteSynchronously);
            return(task);
        }
예제 #3
0
        public async Task SendAsync()
        {
            ApplyResponseHeaders(Constants.Status200Ok);

            string       physicalPath = _fileInfo.PhysicalPath;
            SendFileFunc sendFile     = _response.Get <SendFileFunc>(Constants.SendFileAsyncKey);

            if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
            {
                await sendFile(physicalPath, 0, _length, _request.CallCancelled);
            }

            Stream readStream    = _fileInfo.CreateReadStream();
            var    copyOperation = new StreamCopyOperation(readStream, _response.Body, _length, _request.CallCancelled);

            await copyOperation.Start();

            readStream.Close();
        }
예제 #4
0
        private string HandleResponseMessage(IOwinResponse response)
        {
            if (response.IsSuccessStatusCode())
            {
                return(ReadBodyStreamAsString(response.Body));
            }

            var requestEx = response.Get <Exception>("Exception");

            if (!requestEx.IsNull())
            {
                if (!response.Get <bool>("IsHandledError"))
                {
                    response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }

                return(response.Get <Exception>("Exception").TrimMessage());
            }

            return(HandleHttpError(response));
        }
예제 #5
0
        public void Attach()
        {
            // TODO: look to see if this Vary is already added?
            _response.Headers.Append("Vary", "Accept-Encoding");

            _originalIfNoneMatch = CleanRequestHeader("If-None-Match");
            _originalIfMatch     = CleanRequestHeader("If-Match");

            _originalResponseBody = _response.Body;
            _response.Body        = new SwitchingStream(this, _originalResponseBody);

            _originalSendFileAsyncDelegate = _response.Get <SendFileFunc>("sendfile.SendAsync");
            _response.Set <SendFileFunc>("sendfile.SendAsync", SendFileAsync);
        }
예제 #6
0
        /// <summary>
        /// Sends the given file using the SendFile extension.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="fileName">The full or relative path to the file.</param>
        /// <param name="offset">The offset in the file.</param>
        /// <param name="count">The number of types to send, or null to send the remainder of the file.</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task SendFileAsync(this IOwinResponse response, string fileName, long offset, long?count, CancellationToken cancellationToken)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            SendFileFunc sendFileFunc = response.Get <SendFileFunc>(Constants.SendFileAsyncKey);

            if (sendFileFunc == null)
            {
                throw new NotSupportedException(Resources.Exception_SendFileNotSupported);
            }

            return(sendFileFunc(fileName, offset, count, cancellationToken));
        }
예제 #7
0
        public async Task SendAsync()
        {
            ApplyResponseHeaders(Constants.Status200Ok);

            var physicalPath = fileInfo.PhysicalPath;
            var sendFile     = response.Get <SendFileFunc>(Constants.SendFileAsyncKey);

            if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
            {
                await sendFile(physicalPath, 0, length, request.CallCancelled)
                .ConfigureAwait(false);
            }

            using (var readStream = fileInfo.CreateReadStream())
            {
                await new StreamCopyOperation(readStream, response.Body, length, request.CallCancelled).Start()
                .ConfigureAwait(false);
            }
        }
예제 #8
0
 public T Get <T>(string key)
 {
     return(_response.Get <T>(key));
 }