@@ -89,61 +89,66 @@ public override async Task ExecuteResultAsync(IWebDavResponse response, Cancella
89
89
contentType = MimeTypesMap . DefaultMimeType ;
90
90
}
91
91
92
- HttpContent content ;
93
92
if ( _rangeItems . Count == 1 )
94
93
{
95
94
// No multipart content
96
95
var rangeItem = _rangeItems . Single ( ) ;
97
96
var streamView = views . Single ( ) ;
98
- content = new StreamContent ( streamView ) ;
99
- try
97
+ using ( var streamContent = new StreamContent ( streamView ) )
100
98
{
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 ) ;
109
109
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
+ }
111
121
}
112
122
else
113
123
{
114
124
// Multipart content
115
- var multipart = new MultipartContent ( "byteranges" ) ;
116
- try
125
+ using ( var multipart = new MultipartContent ( "byteranges" ) )
117
126
{
118
127
var index = 0 ;
119
128
foreach ( var rangeItem in _rangeItems )
120
129
{
121
130
var streamView = views [ index ++ ] ;
122
131
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 ) ;
124
136
partContent . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
125
137
partContent . Headers . ContentLength = rangeItem . Length ;
126
138
multipart . Add ( partContent ) ;
127
139
}
128
- }
129
- catch
130
- {
131
- multipart . Dispose ( ) ;
132
- throw ;
133
- }
134
140
135
- content = multipart ;
136
- }
141
+ await SetPropertiesToContentHeaderAsync ( multipart , properties , ct )
142
+ . ConfigureAwait ( false ) ;
137
143
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
+ }
145
148
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
+ }
147
152
}
148
153
}
149
154
finally
0 commit comments