@@ -91,63 +91,66 @@ public override async Task ExecuteResultAsync(IWebDavResponse response, Cancella
91
91
contentType = MimeTypesMap . DefaultMimeType ;
92
92
}
93
93
94
- HttpContent content ;
95
94
if ( _rangeItems . Count == 1 )
96
95
{
97
96
// No multipart content
98
97
var rangeItem = _rangeItems . Single ( ) ;
99
98
var streamView = views . Single ( ) ;
100
- content = new StreamContent ( streamView ) ;
101
- try
99
+ using ( var streamContent = new StreamContent ( streamView ) )
102
100
{
103
- content . Headers . ContentRange = new ContentRangeHeaderValue ( rangeItem . From , rangeItem . To , _document . Length ) ;
104
- content . Headers . ContentLength = rangeItem . Length ;
105
- }
106
- catch
107
- {
108
- content . Dispose ( ) ;
109
- throw ;
110
- }
101
+ streamContent . Headers . ContentRange = new ContentRangeHeaderValue (
102
+ rangeItem . From ,
103
+ rangeItem . To ,
104
+ _document . Length ) ;
105
+ streamContent . Headers . ContentLength = rangeItem . Length ;
106
+
107
+ streamContent . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
108
+
109
+ await SetPropertiesToContentHeaderAsync ( streamContent , properties , ct )
110
+ . ConfigureAwait ( false ) ;
111
111
112
- content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
112
+ foreach ( var header in streamContent . Headers )
113
+ {
114
+ response . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
115
+ }
116
+
117
+ // Use the CopyToAsync function of the stream itself, because
118
+ // we're able to pass the cancellation token. This is a workaround
119
+ // for issue dotnet/corefx#9071 and fixes FubarDevelopment/WebDavServer#47.
120
+ await streamView . CopyToAsync ( response . Body , 81920 , ct )
121
+ . ConfigureAwait ( false ) ;
122
+ }
113
123
}
114
124
else
115
125
{
116
126
// Multipart content
117
- var multipart = new MultipartContent ( "byteranges" ) ;
118
- try
127
+ using ( var multipart = new MultipartContent ( "byteranges" ) )
119
128
{
120
129
var index = 0 ;
121
130
foreach ( var rangeItem in _rangeItems )
122
131
{
123
132
var streamView = views [ index ++ ] ;
124
133
var partContent = new StreamContent ( streamView ) ;
125
- partContent . Headers . ContentRange = new ContentRangeHeaderValue ( rangeItem . From , rangeItem . To , _document . Length ) ;
134
+ partContent . Headers . ContentRange = new ContentRangeHeaderValue (
135
+ rangeItem . From ,
136
+ rangeItem . To ,
137
+ _document . Length ) ;
126
138
partContent . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
127
139
partContent . Headers . ContentLength = rangeItem . Length ;
128
140
multipart . Add ( partContent ) ;
129
141
}
130
- }
131
- catch
132
- {
133
- multipart . Dispose ( ) ;
134
- throw ;
135
- }
136
142
137
- content = multipart ;
138
- }
143
+ await SetPropertiesToContentHeaderAsync ( multipart , properties , ct )
144
+ . ConfigureAwait ( false ) ;
139
145
140
- using ( content )
141
- {
142
- await SetPropertiesToContentHeaderAsync ( content , properties , ct )
143
- . ConfigureAwait ( false ) ;
146
+ foreach ( var header in multipart . Headers )
147
+ {
148
+ response . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
149
+ }
144
150
145
- foreach ( var header in content . Headers )
146
- {
147
- response . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
151
+ // TODO: Workaround for issue dotnet/corefx#9071
152
+ await multipart . CopyToAsync ( response . Body ) . ConfigureAwait ( false ) ;
148
153
}
149
-
150
- await content . CopyToAsync ( response . Body ) . ConfigureAwait ( false ) ;
151
154
}
152
155
}
153
156
finally
0 commit comments