@@ -26,9 +26,9 @@ public enum FileNamingScheme
2626/// <param name="namingScheme">A naming scheme that the file will follow</param>
2727/// <param name="packet">Packet from HTTP method</param>
2828/// <returns>Path to the file</returns>
29- public static string SaveFile ( string path , FileNamingScheme namingScheme , ref Packet packet )
29+ public static string SaveFile ( string path , FileNamingScheme namingScheme , HttpListenerContext context )
3030{
31- string tempFile = LowLevelAPI . SaveFile ( packet . Request . ContentEncoding , LowLevelAPI . GetBoundary ( packet . Request . ContentType ) , packet . Request . InputStream ) ;
31+ string tempFile = LowLevelAPI . SaveFile ( context . Request . ContentEncoding , LowLevelAPI . GetBoundary ( context . Request . ContentType ) , context . Request . InputStream ) ;
3232
3333switch ( namingScheme )
3434{
@@ -71,9 +71,9 @@ public static string SaveFile(string path, FileNamingScheme namingScheme, ref Pa
7171/// <param name="customFileNameHandler">Custom method for naming files: param -> temp file location; return -> new file name(with extension)</param>
7272/// <param name="packet">Packet from HTTP method</param>
7373/// <returns>Path to file</returns>
74- public static string SaveFile ( string path , Func < string , string > customFileNameHandler , ref Packet packet )
74+ public static string SaveFile ( string path , Func < string , string > customFileNameHandler , HttpListenerContext context )
7575{
76- string tempFile = LowLevelAPI . SaveFile ( packet . Request . ContentEncoding , LowLevelAPI . GetBoundary ( packet . Request . ContentType ) , packet . Request . InputStream ) ;
76+ string tempFile = LowLevelAPI . SaveFile ( context . Request . ContentEncoding , LowLevelAPI . GetBoundary ( context . Request . ContentType ) , context . Request . InputStream ) ;
7777
7878fileName = customFileNameHandler ( tempFile ) ;
7979
@@ -154,7 +154,7 @@ public static string DetermineFileExtension(string file)
154154/// </summary>
155155/// <param name="path">Path to file</param>
156156/// <param name="packet">Packet from HTTP method</param>
157- public static void ServeFile ( string path , ref Packet packet )
157+ public static void ServeFile ( string path , HttpListenerContext context )
158158{
159159if ( File . Exists ( path ) )
160160try
@@ -163,46 +163,46 @@ public static void ServeFile(string path, ref Packet packet)
163163{
164164string file = Path . GetFileName ( path ) ;
165165
166- //Adding permanent http response headers
167- packet . Response . ContentType = mimeTypeMappings . TryGetValue ( Path . GetExtension ( path ) , out string mime ) ? mime : "application/octet-stream" ;
166+ //Adding permanent httpcontextresponse headers
167+ context . Response . ContentType = mimeTypeMappings . TryGetValue ( Path . GetExtension ( path ) , out string mime ) ? mime : "application/octet-stream" ;
168168
169- packet . Response . ContentLength64 = input . Length ;
170- packet . Response . AddHeader ( "Date" , DateTime . Now . ToString ( "r" ) ) ;
171- packet . Response . AddHeader ( "Last-Modified" , File . GetLastWriteTime ( path ) . ToString ( "r" ) ) ;
172- packet . Response . AddHeader ( "Content-Disposition" , $ "filename={ file } ") ;
169+ context . Response . ContentLength64 = input . Length ;
170+ context . Response . AddHeader ( "Date" , DateTime . Now . ToString ( "r" ) ) ;
171+ context . Response . AddHeader ( "Last-Modified" , File . GetLastWriteTime ( path ) . ToString ( "r" ) ) ;
172+ context . Response . AddHeader ( "Content-Disposition" , $ "filename={ file } ") ;
173173
174174var buffer = new byte [ 1024 * 32 ] ;
175175int nbytes ;
176176while ( ( nbytes = input . Read ( buffer , 0 , buffer . Length ) ) > 0 )
177177{
178- if ( packet . Request . HttpMethod != "HEAD" )
178+ if ( context . Request . HttpMethod != "HEAD" )
179179{
180- packet . Response . OutputStream . Write ( buffer , 0 , nbytes ) ;
180+ context . Response . OutputStream . Write ( buffer , 0 , nbytes ) ;
181181}
182182}
183183
184184input . Close ( ) ;
185- packet . Response . OutputStream . Flush ( ) ;
185+ context . Response . OutputStream . Flush ( ) ;
186186}
187187
188- packet . Response . StatusCode = ( int ) HttpStatusCode . OK ;
188+ context . Response . StatusCode = ( int ) HttpStatusCode . OK ;
189189}
190190catch
191191{
192- packet . Response . StatusCode = ( int ) HttpStatusCode . InternalServerError ;
192+ context . Response . StatusCode = ( int ) HttpStatusCode . InternalServerError ;
193193}
194194else
195- packet . Response . StatusCode = ( int ) HttpStatusCode . NotFound ;
195+ context . Response . StatusCode = ( int ) HttpStatusCode . NotFound ;
196196
197- packet . Response . OutputStream . Close ( ) ;
197+ context . Response . OutputStream . Close ( ) ;
198198}
199199
200200/// <summary>
201201/// Exposes directory contents to be accessed by clients
202202/// </summary>
203203/// <param name="path">Path to directory</param>
204204/// <param name="packet">Packet from HTTP method</param>
205- public static void ServeDirectory ( string path , ref Packet packet )
205+ public static void ServeDirectory ( string path , HttpListenerContext context , Dictionary < string , string > parameters )
206206{
207207try
208208{
@@ -212,17 +212,17 @@ public static void ServeDirectory(string path, ref Packet packet)
212212{
213213string fileName = Path . GetFileName ( fileInDir ) ;
214214
215- if ( fileName == packet . Parameters [ "file" ] )
215+ if ( fileName == parameters [ "file" ] )
216216{
217- ServeFile ( fileInDir , ref packet ) ;
217+ ServeFile ( fileInDir , context ) ;
218218break ;
219219}
220220}
221221}
222222catch ( Exception e )
223223{
224224Debug . Log ( $ "Error: { e } ") ;
225- Error . Page ( HttpStatus . NotFound , ref packet ) ;
225+ Error . Page ( HttpStatus . NotFound , context ) ;
226226}
227227}
228228
@@ -231,12 +231,12 @@ public static void ServeDirectory(string path, ref Packet packet)
231231/// </summary>
232232/// <param name="path">Path to directory</param>
233233/// <param name="packet">Packet from HTTP method</param>
234- public static void ServeDirectoryRecursively ( string path , string url , ref Packet packet )
234+ public static void ServeDirectoryRecursively ( string path , string url , HttpListenerContext context )
235235{
236236try
237237{
238238path = path . Replace ( '\\ ' , '/' ) ;
239- string recursivePath = packet . Request . Url . AbsolutePath . Substring ( url . LastIndexOf ( '{' ) + 1 ) ;
239+ string recursivePath = context . Request . Url . AbsolutePath . Substring ( url . LastIndexOf ( '{' ) + 1 ) ;
240240List < string > filesInDir = Directory . GetFiles ( path , "*.*" , SearchOption . AllDirectories ) . ToList ( ) ;
241241List < string > absoluteFiles = new ( ) ;
242242
@@ -248,7 +248,7 @@ public static void ServeDirectoryRecursively(string path, string url, ref Packet
248248foreach ( ( string rel , string abs ) file in zip )
249249if ( recursivePath == file . abs )
250250{
251- ServeFile ( file . rel , ref packet ) ;
251+ ServeFile ( file . rel , context ) ;
252252break ;
253253}
254254}
@@ -259,7 +259,7 @@ public static void ServeDirectoryRecursively(string path, string url, ref Packet
259259catch ( Exception e )
260260{
261261SentryWrapper . CaptureException ( e ) ;
262- Error . Page ( HttpStatus . InternalServerError , ref packet ) ;
262+ Error . Page ( HttpStatus . InternalServerError , context ) ;
263263}
264264}
265265
0 commit comments