@@ -89,61 +89,66 @@ public override async Task ExecuteResultAsync(IWebDavResponse response, Cancella
8989 contentType = MimeTypesMap . DefaultMimeType ;
9090 }
9191
92- HttpContent content ;
9392 if ( _rangeItems . Count == 1 )
9493 {
9594 // No multipart content
9695 var rangeItem = _rangeItems . Single ( ) ;
9796 var streamView = views . Single ( ) ;
98- content = new StreamContent ( streamView ) ;
99- try
97+ using ( var streamContent = new StreamContent ( streamView ) )
10098 {
101- content . Headers . ContentRange = new ContentRangeHeaderValue ( rangeItem . From , rangeItem . To , _document . Length ) ;
102- content . Headers . ContentLength = rangeItem . Length ;
103- }
104- catch
105- {
106- content . Dispose ( ) ;
107- throw ;
108- }
99+ streamContent . Headers . ContentRange = new ContentRangeHeaderValue (
100+ rangeItem . From ,
101+ rangeItem . To ,
102+ _document . Length ) ;
103+ streamContent . Headers . ContentLength = rangeItem . Length ;
104+
105+ streamContent . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
106+
107+ await SetPropertiesToContentHeaderAsync ( streamContent , properties , ct )
108+ . ConfigureAwait ( false ) ;
109109
110- content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
110+ foreach ( var header in streamContent . Headers )
111+ {
112+ response . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
113+ }
114+
115+ // Use the CopyToAsync function of the stream itself, because
116+ // we're able to pass the cancellation token. This is a workaround
117+ // for issue dotnet/corefx#9071 and fixes FubarDevelopment/WebDavServer#47.
118+ await streamView . CopyToAsync ( response . Body , 81920 , ct )
119+ . ConfigureAwait ( false ) ;
120+ }
111121 }
112122 else
113123 {
114124 // Multipart content
115- var multipart = new MultipartContent ( "byteranges" ) ;
116- try
125+ using ( var multipart = new MultipartContent ( "byteranges" ) )
117126 {
118127 var index = 0 ;
119128 foreach ( var rangeItem in _rangeItems )
120129 {
121130 var streamView = views [ index ++ ] ;
122131 var partContent = new StreamContent ( streamView ) ;
123- partContent . Headers . ContentRange = new ContentRangeHeaderValue ( rangeItem . From , rangeItem . To , _document . Length ) ;
132+ partContent . Headers . ContentRange = new ContentRangeHeaderValue (
133+ rangeItem . From ,
134+ rangeItem . To ,
135+ _document . Length ) ;
124136 partContent . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
125137 partContent . Headers . ContentLength = rangeItem . Length ;
126138 multipart . Add ( partContent ) ;
127139 }
128- }
129- catch
130- {
131- multipart . Dispose ( ) ;
132- throw ;
133- }
134140
135- content = multipart ;
136- }
141+ await SetPropertiesToContentHeaderAsync ( multipart , properties , ct )
142+ . ConfigureAwait ( false ) ;
137143
138- using ( content )
139- {
140- await SetPropertiesToContentHeaderAsync ( content , properties , ct )
141- . ConfigureAwait ( false ) ;
142-
143- foreach ( var header in content . Headers )
144- response . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
144+ foreach ( var header in multipart . Headers )
145+ {
146+ response . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
147+ }
145148
146- await content . CopyToAsync ( response . Body ) . ConfigureAwait ( false ) ;
149+ // TODO: Workaround for issue dotnet/corefx#9071
150+ await multipart . CopyToAsync ( response . Body ) . ConfigureAwait ( false ) ;
151+ }
147152 }
148153 }
149154 finally
0 commit comments