Skip to content

Commit ea7ec05

Browse files
Fixed #51
1 parent 46bc7b6 commit ea7ec05

File tree

21 files changed

+159
-199
lines changed

21 files changed

+159
-199
lines changed

SAPI/API/Utilities/Auth.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Net;
2+
using System.Text;
23

34
namespace SAPI.API.Utilities
45
{
@@ -16,11 +17,11 @@ public static class Auth
1617
/// </summary>
1718
/// <param name="keys">List of all API keys authorized</param>
1819
/// <param name="packet">Packet ref you got from server</param>
19-
public static bool CheckForApiKey(List<string> keys, ref Packet packet)
20+
public static bool CheckForApiKey(List<string> keys, HttpListenerContext context)
2021
{
2122
try
2223
{
23-
if (GetApiKey(out string? _key, ref packet))
24+
if (GetApiKey(out string? _key, context))
2425
foreach (var key in keys)
2526
{
2627
if (_key == key)
@@ -41,11 +42,11 @@ public static bool CheckForApiKey(List<string> keys, ref Packet packet)
4142
/// <param name="credentialsList">List of all usernames and passwords authorized</param>
4243
/// <param name="hashingFunction">Password hashing algorithm. Takes un-hashed password as parameter, returns hashed password</param>
4344
/// <param name="packet">Packet ref you got from server</param>
44-
public static bool CheckForBasicCredentials(List<BasicCredentials> credentialsList, Func<string, string> hashingFunction, ref Packet packet)
45+
public static bool CheckForBasicCredentials(List<BasicCredentials> credentialsList, Func<string, string> hashingFunction, HttpListenerContext context)
4546
{
4647
try
4748
{
48-
if (GetBasicCredentials(out BasicCredentials? credentials, ref packet))
49+
if (GetBasicCredentials(out BasicCredentials? credentials, context))
4950
{
5051
credentials.HashedPassword = hashingFunction(credentials.Password);
5152
foreach (BasicCredentials _credentials in credentialsList)
@@ -63,9 +64,9 @@ public static bool CheckForBasicCredentials(List<BasicCredentials> credentialsLi
6364
return false;
6465
}
6566

66-
public static bool GetApiKey(out string? key, ref Packet packet)
67+
public static bool GetApiKey(out string? key, HttpListenerContext context)
6768
{
68-
key = packet.Request.Headers.Get("x-api-key");
69+
key = context.Request.Headers.Get("x-api-key");
6970

7071
if (key is null)
7172
return false;
@@ -78,14 +79,14 @@ public static bool GetApiKey(out string? key, ref Packet packet)
7879
/// </summary>
7980
/// <param name="credentials">Variable contains passed user credentials</param>
8081
/// <param name="packet">Packet ref you got from server</param>
81-
public static bool GetBasicCredentials(out BasicCredentials? credentials, ref Packet packet)
82+
public static bool GetBasicCredentials(out BasicCredentials? credentials, HttpListenerContext context)
8283
{
8384
credentials = null;
8485
try
8586
{
86-
if (packet.Request.Headers.Get("Authorization").Contains("Basic "))
87+
if (context.Request.Headers.Get("Authorization").Contains("Basic "))
8788
{
88-
string authData = packet.Request.Headers.GetValues("Authorization").GetValue(0).ToString().Substring(6);
89+
string authData = context.Request.Headers.GetValues("Authorization").GetValue(0).ToString().Substring(6);
8990

9091
byte[] decodedBase64 = Convert.FromBase64String(authData);
9192

SAPI/API/Utilities/Cookies.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class Cookies
1111
/// <param name="cookie">Return Cookie</param>
1212
/// <param name="request">Pass from Task()</param>
1313
/// <returns>Does cookie exist?</returns>
14-
public static bool CheckForCookie(string cookieName, out Cookie? cookie, ref Packet packet)
14+
public static bool CheckForCookie(string cookieName, out Cookie? cookie, HttpListenerContext context)
1515
{
16-
cookie = packet.Request.Cookies[cookieName];
16+
cookie = context.Request.Cookies[cookieName];
1717

1818
return cookie != null;
1919
}
@@ -24,9 +24,9 @@ public static bool CheckForCookie(string cookieName, out Cookie? cookie, ref Pac
2424
/// <param name="cookieName">Name of cookie to be saved</param>
2525
/// <param name="cookieValue">Value of cookie to be saved</param>
2626
/// <param name="response">Pass from Task()</param>
27-
public static void GiveCookie(Cookie cookie, ref Packet packet)
27+
public static void GiveCookie(Cookie cookie, HttpListenerContext context)
2828
{
29-
packet.Response.AppendCookie(cookie);
29+
context.Response.AppendCookie(cookie);
3030
}
3131
}
3232
}

SAPI/API/Utilities/Error.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static class Error
5151
/// </summary>
5252
/// <param name="httpStatus">It's the status code send to client</param>
5353
/// <param name="packet">Response ref you got from server - argument in Task()</param>
54-
public static void Page(HttpStatus httpStatus, ref Packet packet)
54+
public static void Page(HttpStatus httpStatus, HttpListenerContext context)
5555
{
5656
string statusName = httpStatusNames[httpStatus];
5757
int statusCode = httpStatusCodes[httpStatus];
@@ -76,7 +76,7 @@ public static void Page(HttpStatus httpStatus, ref Packet packet)
7676
" </body>" +
7777
"</html>";
7878

79-
Html.HtmlResponse(page, ref packet, statusCode);
79+
Html.HtmlResponse(page, context, statusCode);
8080
}
8181
}
8282
}

SAPI/API/Utilities/FileIO.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3333
switch (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

7878
fileName = 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
{
159159
if (File.Exists(path))
160160
try
@@ -163,46 +163,46 @@ public static void ServeFile(string path, ref Packet packet)
163163
{
164164
string 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

174174
var buffer = new byte[1024 * 32];
175175
int nbytes;
176176
while ((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

184184
input.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
}
190190
catch
191191
{
192-
packet.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
192+
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
193193
}
194194
else
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
{
207207
try
208208
{
@@ -212,17 +212,17 @@ public static void ServeDirectory(string path, ref Packet packet)
212212
{
213213
string 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);
218218
break;
219219
}
220220
}
221221
}
222222
catch (Exception e)
223223
{
224224
Debug.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
{
236236
try
237237
{
238238
path = path.Replace('\\', '/');
239-
string recursivePath = packet.Request.Url.AbsolutePath.Substring(url.LastIndexOf('{') + 1);
239+
string recursivePath = context.Request.Url.AbsolutePath.Substring(url.LastIndexOf('{') + 1);
240240
List<string> filesInDir = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).ToList();
241241
List<string> absoluteFiles = new();
242242

@@ -248,7 +248,7 @@ public static void ServeDirectoryRecursively(string path, string url, ref Packet
248248
foreach ((string rel, string abs) file in zip)
249249
if (recursivePath == file.abs)
250250
{
251-
ServeFile(file.rel, ref packet);
251+
ServeFile(file.rel, context);
252252
break;
253253
}
254254
}
@@ -259,7 +259,7 @@ public static void ServeDirectoryRecursively(string path, string url, ref Packet
259259
catch (Exception e)
260260
{
261261
SentryWrapper.CaptureException(e);
262-
Error.Page(HttpStatus.InternalServerError, ref packet);
262+
Error.Page(HttpStatus.InternalServerError, context);
263263
}
264264
}
265265

SAPI/API/Utilities/Json.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Net;
2+
using System.Text;
23
using Newtonsoft.Json;
34

45
namespace SAPI.API.Utilities
@@ -10,18 +11,18 @@ public static class Json
1011
/// </summary>
1112
/// <param name="json">Add here your object to send with json</param>
1213
/// <param name="response">Response ref you got from server</param>
13-
public static void Response<T>(T json, ref Packet packet)
14+
public static void Response<T>(T json, HttpListenerContext context)
1415
{
1516
var serializedJson = JsonConvert.SerializeObject(json);
1617
var data = Encoding.UTF8.GetBytes(serializedJson);
17-
packet.Response.ContentEncoding = Encoding.UTF8;
18-
packet.Response.ContentType = "application/json";
19-
packet.Response.ContentLength64 = data.LongLength;
20-
packet.Response.StatusCode = 200;
18+
context.Response.ContentEncoding = Encoding.UTF8;
19+
context.Response.ContentType = "application/json";
20+
context.Response.ContentLength64 = data.LongLength;
21+
context.Response.StatusCode = 200;
2122

22-
if (packet.Request.HttpMethod != "HEAD")
23+
if (context.Request.HttpMethod != "HEAD")
2324
{
24-
packet.Response.OutputStream.Write(data, 0, data.Length);
25+
context.Response.OutputStream.Write(data, 0, data.Length);
2526
}
2627
}
2728

@@ -30,9 +31,9 @@ public static void Response<T>(T json, ref Packet packet)
3031
/// </summary>
3132
/// <param name="json">Add here your instance of an object to be serialized</param>
3233
/// <param name="request">Request ref you got from server - argument in Task()</param>
33-
public static void Fetch<T>(out T json, ref Packet packet)
34+
public static void Fetch<T>(out T json, HttpListenerContext context)
3435
{
35-
StreamReader reader = new(packet.Request.InputStream, packet.Request.ContentEncoding);
36+
StreamReader reader = new(context.Request.InputStream, context.Request.ContentEncoding);
3637

3738
var data = reader.ReadToEnd();
3839
json = JsonConvert.DeserializeObject<T>(data);

0 commit comments

Comments
 (0)