22using System . IO ;
33using System . Linq ;
44using System . Net ;
5+ using System . Threading ;
56using System . Threading . Tasks ;
67using Microsoft . AspNetCore . Http ;
78using Red . Interfaces ;
@@ -29,6 +30,11 @@ internal Response(Context context) : base(context)
2930 /// </summary>
3031 public IHeaderDictionary Headers => AspNetResponse . Headers ;
3132
33+ /// <summary>
34+ /// The cancellation token the request being aborted
35+ /// </summary>
36+ public CancellationToken Aborted => AspNetResponse . HttpContext . RequestAborted ;
37+
3238 /// <summary>
3339 /// Obsolete. Please used Headers property instead.
3440 /// This method Will be removed in a later version
@@ -63,14 +69,14 @@ public Task<HandlerType> Redirect(string redirectPath, bool permanent = false)
6369 public Task < HandlerType > SendString ( string data , string contentType = "text/plain" , string fileName = "" ,
6470 bool attachment = false , HttpStatusCode status = HttpStatusCode . OK )
6571 {
66- return SendString ( AspNetResponse , data , contentType , fileName , attachment , status ) ;
72+ return SendString ( AspNetResponse , data , contentType , fileName , attachment , status , Aborted ) ;
6773 }
6874
6975 /// <summary>
7076 /// Static helper for use in middleware
7177 /// </summary>
7278 public static async Task < HandlerType > SendString ( HttpResponse response , string data , string contentType ,
73- string fileName , bool attachment , HttpStatusCode status )
79+ string fileName , bool attachment , HttpStatusCode status , CancellationToken cancellationToken )
7480 {
7581 response . StatusCode = ( int ) status ;
7682 response . ContentType = contentType ;
@@ -80,7 +86,7 @@ public static async Task<HandlerType> SendString(HttpResponse response, string d
8086 response . Headers . Add ( "Content-disposition" , contentDisposition ) ;
8187 }
8288
83- await response . WriteAsync ( data ) ;
89+ await response . WriteAsync ( data , cancellationToken ) ;
8490 return HandlerType . Final ;
8591 }
8692
@@ -89,9 +95,9 @@ public static async Task<HandlerType> SendString(HttpResponse response, string d
8995 /// </summary>
9096 /// <param name="response">The HttpResponse object</param>
9197 /// <param name="status">The status code for the response</param>
92- public static Task < HandlerType > SendStatus ( HttpResponse response , HttpStatusCode status )
98+ public static Task < HandlerType > SendStatus ( HttpResponse response , HttpStatusCode status , CancellationToken cancellationToken )
9399 {
94- return SendString ( response , status . ToString ( ) , "text/plain" , "" , false , status ) ;
100+ return SendString ( response , status . ToString ( ) , "text/plain" , "" , false , status , cancellationToken ) ;
95101 }
96102
97103 /// <summary>
@@ -112,7 +118,7 @@ public async Task<HandlerType> SendJson<T>(T data, HttpStatusCode status = HttpS
112118 {
113119 AspNetResponse . StatusCode = ( int ) status ;
114120 AspNetResponse . ContentType = "application/json" ;
115- await Context . Plugins . Get < IJsonConverter > ( ) . SerializeAsync ( data , AspNetResponse . Body ) ;
121+ await Context . Plugins . Get < IJsonConverter > ( ) . SerializeAsync ( data , AspNetResponse . Body , Aborted ) ;
116122 return HandlerType . Final ;
117123 }
118124
@@ -125,7 +131,7 @@ public async Task<HandlerType> SendXml<T>(T data, HttpStatusCode status = HttpSt
125131 {
126132 AspNetResponse . StatusCode = ( int ) status ;
127133 AspNetResponse . ContentType = "application/xml" ;
128- await Context . Plugins . Get < IXmlConverter > ( ) . SerializeAsync ( data , AspNetResponse . Body ) ;
134+ await Context . Plugins . Get < IXmlConverter > ( ) . SerializeAsync ( data , AspNetResponse . Body , Aborted ) ;
129135 return HandlerType . Final ;
130136 }
131137
@@ -146,7 +152,7 @@ public async Task<HandlerType> SendStream(Stream dataStream, string contentType,
146152 if ( ! string . IsNullOrEmpty ( fileName ) )
147153 Headers [ "Content-disposition" ] = $ "{ ( attachment ? "attachment" : "inline" ) } ; filename=\" { fileName } \" ";
148154
149- await dataStream . CopyToAsync ( AspNetResponse . Body ) ;
155+ await dataStream . CopyToAsync ( AspNetResponse . Body , Aborted ) ;
150156 if ( dispose ) dataStream . Dispose ( ) ;
151157
152158 return HandlerType . Final ;
@@ -192,14 +198,14 @@ public async Task<HandlerType> SendFile(string filePath, string? contentType = n
192198 AspNetResponse . ContentType = Handlers . GetMimeType ( contentType , filePath ) ;
193199 AspNetResponse . ContentLength = length ;
194200 Headers [ "Content-Range" ] = $ "bytes { offset } -{ offset + length - 1 } /{ fileSize } ";
195- await AspNetResponse . SendFileAsync ( filePath , offset , length ) ;
201+ await AspNetResponse . SendFileAsync ( filePath , offset , length , Aborted ) ;
196202 }
197203 else
198204 {
199205 AspNetResponse . StatusCode = ( int ) status ;
200206 AspNetResponse . ContentType = Handlers . GetMimeType ( contentType , filePath ) ;
201207 AspNetResponse . ContentLength = fileSize ;
202- await AspNetResponse . SendFileAsync ( filePath ) ;
208+ await AspNetResponse . SendFileAsync ( filePath , Aborted ) ;
203209 }
204210
205211 return HandlerType . Final ;
@@ -222,7 +228,7 @@ public async Task<HandlerType> Download(string filePath, string? fileName = "",
222228 AspNetResponse . ContentType = Handlers . GetMimeType ( contentType , filePath ) ;
223229 var name = string . IsNullOrEmpty ( fileName ) ? Path . GetFileName ( filePath ) : fileName ;
224230 Headers [ "Content-disposition" ] = $ "attachment; filename=\" { name } \" ";
225- await AspNetResponse . SendFileAsync ( filePath ) ;
231+ await AspNetResponse . SendFileAsync ( filePath , Aborted ) ;
226232 return HandlerType . Final ;
227233 }
228234 }
0 commit comments