private async IAsyncEnumerable <Stream> ReadMultipartResponseAsStreamsAsync(HttpContent httpContent, [EnumeratorCancellation] CancellationToken cancellationToken = default) { EnsureArg.IsNotNull(httpContent, nameof(httpContent)); await using Stream stream = await httpContent.ReadAsStreamAsync() .ConfigureAwait(false); MultipartSection part; var media = MediaTypeHeaderValue.Parse(httpContent.Headers.ContentType.ToString()); var multipartReader = new MultipartReader(HeaderUtilities.RemoveQuotes(media.Boundary).Value, stream, 100); while ((part = await multipartReader.ReadNextSectionAsync(cancellationToken).ConfigureAwait(false)) != null) { MemoryStream memoryStream = GetMemoryStream(); await part.Body.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false); memoryStream.Seek(0, SeekOrigin.Begin); yield return(memoryStream); } }
public async Task GivenInstanceWithFrames_WhenRetrieveRequestForFrameInInstance_ValidateReturnedHeaders(params int[] frames) { (InstanceIdentifier identifier, DicomFile file) = await CreateAndStoreDicomFile(2); var requestUri = new Uri(string.Format(DicomWebConstants.BaseRetrieveFramesUriFormat, identifier.StudyInstanceUid, identifier.SeriesInstanceUid, identifier.SopInstanceUid, string.Join("%2C", frames)), UriKind.Relative); using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) { // Set accept header to multipart/related; type="application/octet-stream" MediaTypeWithQualityHeaderValue multipartHeader = new MediaTypeWithQualityHeaderValue(KnownContentTypes.MultipartRelated); NameValueHeaderValue contentHeader = new NameValueHeaderValue("type", "\"" + KnownContentTypes.ApplicationOctetStream + "\""); multipartHeader.Parameters.Add(contentHeader); string transferSyntaxHeader = ";transfer-syntax=\"*\""; request.Headers.TryAddWithoutValidation("Accept", $"{multipartHeader.ToString()}{transferSyntaxHeader}"); request.Headers.Add("transfer-syntax", "*"); using (HttpResponseMessage response = await _client.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, _defaultCancellationToken)) { Assert.True(response.IsSuccessStatusCode); await using (Stream stream = await response.Content.ReadAsStreamAsync()) { // Open stream in response message's content and read using multipart reader. MultipartSection part; var media = MediaTypeHeaderValue.Parse(response.Content.Headers.ContentType.ToString()); var multipartReader = new MultipartReader(HeaderUtilities.RemoveQuotes(media.Boundary).Value, stream, 100); while ((part = await multipartReader.ReadNextSectionAsync(_defaultCancellationToken)) != null) { // Validate header on individual parts is application/octet-stream. Assert.Equal(KnownContentTypes.ApplicationOctetStream, part.ContentType); } } } } }
private async Task <IEnumerable <Stream> > ReadMultipartResponseAsStreamsAsync(HttpContent httpContent, CancellationToken cancellationToken) { EnsureArg.IsNotNull(httpContent, nameof(httpContent)); var result = new List <Stream>(); await using (Stream stream = await httpContent.ReadAsStreamAsync()) { MultipartSection part; var media = MediaTypeHeaderValue.Parse(httpContent.Headers.ContentType.ToString()); var multipartReader = new MultipartReader(HeaderUtilities.RemoveQuotes(media.Boundary).Value, stream, 100); while ((part = await multipartReader.ReadNextSectionAsync(cancellationToken)) != null) { var memoryStream = GetMemoryStream(); await part.Body.CopyToAsync(memoryStream, cancellationToken); memoryStream.Seek(0, SeekOrigin.Begin); result.Add(memoryStream); } } return(result); }
public CsvOutputFormatter() { SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/csv")); SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); }
private void CheckInvalidParse(string?input) { Assert.Throws <FormatException>(() => MediaTypeHeaderValue.Parse(input)); }
private void CheckValidParse(string?input, MediaTypeHeaderValue expectedResult) { var result = MediaTypeHeaderValue.Parse(input); Assert.Equal(expectedResult, result); }