Skip to content

Commit 3d44cf3

Browse files
committed
Server: Verify HTTP method
1 parent 6f8dea4 commit 3d44cf3

8 files changed

Lines changed: 35 additions & 11 deletions

File tree

RhythmDoctor.CafeLink/RhythmDoctor.CafeLink.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AssemblyName>io.github.nonperforming.cafelink</AssemblyName>
55
<Product>RhythmDoctor.CafeLink</Product>
66
<Description>Additional support for rhythm.cafe</Description>
7-
<Version>1.0.0</Version>
7+
<Version>1.0.1</Version>
88
<LangVersion>latest</LangVersion>
99
<RestoreAdditionalProjectSources>
1010
https://api.nuget.org/v3/index.json;

RhythmDoctor.CafeLink/Server/Handlers/ImportHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace RhythmDoctor.CafeLink.Server.Handlers;
1+
using System.Net.Http;
2+
3+
namespace RhythmDoctor.CafeLink.Server.Handlers;
24

35
internal class ImportHandler : IHandler
46
{
57
public string Endpoint => "import";
8+
public HttpMethod AcceptedMethod => HttpMethod.Post;
69

710
public IResponse HandleRequest(NameValueCollection parameters)
811
{

RhythmDoctor.CafeLink/Server/Handlers/PlayHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace RhythmDoctor.CafeLink.Server.Handlers;
1+
using System.Net.Http;
2+
3+
namespace RhythmDoctor.CafeLink.Server.Handlers;
24

35
internal class PlayHandler : IHandler
46
{
57
public string Endpoint => "play";
8+
public HttpMethod AcceptedMethod => HttpMethod.Post;
69

710
public IResponse HandleRequest(NameValueCollection parameters)
811
{

RhythmDoctor.CafeLink/Server/Handlers/StatusHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System.Text;
1+
using System.Net.Http;
2+
using System.Text;
23

34
namespace RhythmDoctor.CafeLink.Server.Handlers;
45

56
internal class StatusHandler : IHandler
67
{
78
public string Endpoint => "status";
9+
public HttpMethod AcceptedMethod => HttpMethod.Get;
810

911
public IResponse HandleRequest(NameValueCollection parameters)
1012
{
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
namespace RhythmDoctor.CafeLink.Server.Interfaces;
1+
using System.Net.Http;
2+
3+
namespace RhythmDoctor.CafeLink.Server.Interfaces;
24

35
internal interface IHandler
46
{
57
internal string Endpoint { get; }
68

9+
HttpMethod AcceptedMethod { get; }
10+
711
internal IResponse HandleRequest(NameValueCollection parameters);
812
}

RhythmDoctor.CafeLink/Server/Interfaces/IResponse.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace RhythmDoctor.CafeLink.Server.Interfaces;
1+
using System.Net.Http;
2+
3+
namespace RhythmDoctor.CafeLink.Server.Interfaces;
24

35
public interface IResponse
46
{

RhythmDoctor.CafeLink/Server/Responses/ErrorResponse.cs

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

34
namespace RhythmDoctor.CafeLink.Server.Responses;
45

RhythmDoctor.CafeLink/Server/Server.cs

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

34
namespace RhythmDoctor.CafeLink.Server;
45

@@ -98,9 +99,17 @@ private async Task HandleRequest()
9899
}
99100

100101
Plugin.Logger.LogInfo($"[{nameof(Server)}] Handling '{endpoint}' request...");
101-
response = _handlers.TryGetValue(endpoint, out IHandler handler)
102-
? handler.HandleRequest(httpContext.Request.QueryString)
103-
: new ErrorResponse(HttpStatusCode.NotFound, "Unknown endpoint.");
102+
if (_handlers.TryGetValue(endpoint, out IHandler handler))
103+
{
104+
response =
105+
handler.AcceptedMethod.Method != httpRequest.HttpMethod
106+
? new ErrorResponse(HttpStatusCode.MethodNotAllowed, $"Expected {handler.AcceptedMethod.Method} method.")
107+
: handler.HandleRequest(httpContext.Request.QueryString);
108+
}
109+
else
110+
{
111+
response = new ErrorResponse(HttpStatusCode.NotFound, "Unknown endpoint.");
112+
}
104113

105114
SendResponseWithDataAndGetReadyForNextRequest:
106115
IReadOnlyCollection<byte> data = response.Data;

0 commit comments

Comments
 (0)