Skip to content

Commit

Permalink
Add support for cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciejowski2006 committed Apr 25, 2023
1 parent ab921d1 commit 0e199a5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SAPI/SAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Copyright>Copyright © Maciejowski 2022</Copyright>
<PackageLicenseUrl>https://github.com/Maciejowski2006/SAPI/blob/master/LICENSE.md</PackageLicenseUrl>
<RepositoryUrl>https://github.com/Maciejowski2006/SAPI</RepositoryUrl>
<PackageVersion>6.0.0</PackageVersion>
<PackageVersion>7.0.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 19 additions & 0 deletions SAPI/Utilities/Cookies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Net;

namespace SAPI.Utilities.Cookies
{
public static class Cookies
{
public static bool CheckForCookie(string cookieName, out Cookie? cookie, ref HttpListenerRequest request)
{
cookie = request.Cookies[cookieName];

return cookie != null;
}
public static void GiveCookie(string cookieName, string cookieValue, ref HttpListenerResponse response)
{
Cookie cookie = new(cookieName, cookieValue);
response.AppendCookie(cookie);
}
}
}
26 changes: 26 additions & 0 deletions SAPI_Testing/Endpoints/Cookies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Net;
using SAPI.Endpoints;
using SAPI.Utilities;
using SAPI.Utilities.Cookies;

namespace Testing.Endpoints
{
public class Cookies : IEndpoint
{
public string url { get; set; } = "cookies";
public Method method { get; set; } = Method.GET;
public void Task(ref HttpListenerRequest request, ref HttpListenerResponse response, Dictionary<string, string> parameters)
{
if (SAPI.Utilities.Cookies.Cookies.CheckForCookie("visit", out Cookie cookie, ref request))
{
SAPI.Utilities.Cookies.Cookies.GiveCookie("visit", "false", ref response);
Utilities.Error(HttpStatus.OK, ref response);
}
else
{
SAPI.Utilities.Cookies.Cookies.GiveCookie("visit", "true", ref response);
Utilities.Error(HttpStatus.Forbidden, ref response);
}
}
}
}
1 change: 1 addition & 0 deletions SAPI_Testing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void Main()
sapi.MountEndpoint(new ApiAuth());
sapi.MountEndpoint(new Static());
sapi.MountEndpoint(new Dir());
sapi.MountEndpoint(new Cookies());

sapi.Start();
}
Expand Down

0 comments on commit 0e199a5

Please sign in to comment.