/// <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); }
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); }
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(); }
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)); }
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); }
/// <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)); }
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); } }
public T Get <T>(string key) { return(_response.Get <T>(key)); }