diff --git a/TShockAPI/Rest.cs b/TShockAPI/Rest.cs
index 03a58a21..51e36d8d 100644
--- a/TShockAPI/Rest.cs
+++ b/TShockAPI/Rest.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
+using System.Text.RegularExpressions;
using HttpServer;
using HttpServer.Headers;
using Newtonsoft.Json;
@@ -16,10 +17,10 @@ namespace TShockAPI
/// Parameters in the url
/// Http request
/// Response object or null to not handle request
- public delegate object RestCommandD(IParameterCollection parameters, RequestEventArgs request);
+ public delegate object RestCommandD(Dictionary verbs, IParameterCollection parameters, RequestEventArgs request);
public class Rest : IDisposable
{
- List commands = new List();
+ readonly List commands = new List();
HttpListener listener;
public Rest(IPAddress ip, int port)
@@ -61,12 +62,19 @@ namespace TShockAPI
protected virtual object Process(object sender, RequestEventArgs e)
{
- var coms = commands.Where(r => r.Path.ToLower().Equals(e.Request.Uri.AbsolutePath.ToLower()));
- foreach (var com in coms)
+ foreach (var com in commands)
{
- var obj = com.Callback(e.Request.Parameters, e);
- if (obj != null)
- return obj;
+ var matches = Regex.Matches(e.Request.Uri.AbsolutePath, com.UriMatch);
+ if (matches.Count == com.UriNames.Length)
+ {
+ var verbs = new Dictionary();
+ for (int i = 0; i < matches.Count; i++)
+ verbs.Add(com.UriNames[i], matches[i].Groups[1].Value);
+
+ var obj = com.Callback(verbs, e.Request.Parameters, e);
+ if (obj != null)
+ return obj;
+ }
}
return new Dictionary { { "Error", "Invalid request" } };
}
@@ -98,12 +106,17 @@ namespace TShockAPI
public class RestCommand
{
- public string Path { get; protected set; }
+ public string UriTemplate { get; protected set; }
+ public string UriMatch { get; protected set; }
+ public string[] UriNames { get; protected set; }
public RestCommandD Callback { get; protected set; }
- public RestCommand(string path, RestCommandD callback)
+ public RestCommand(string uritemplate, RestCommandD callback)
{
- Path = path;
+ UriTemplate = uritemplate;
+ UriMatch = string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}"));
+ var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}");
+ UriNames = (from Match match in matches select match.Groups[1].Value).ToArray();
Callback = callback;
}
}
diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs
index d914a988..ef3effc3 100644
--- a/TShockAPI/TShock.cs
+++ b/TShockAPI/TShock.cs
@@ -34,6 +34,7 @@ using System.IO;
using System.Net;
using System.Reflection;
using System.Linq;
+using System.Text.RegularExpressions;
using System.Threading;
using Community.CsharpSqlite.SQLiteClient;
using HttpServer;
@@ -204,7 +205,7 @@ namespace TShockAPI
if (Initialized != null)
Initialized();
- RestApi.Register(new RestCommand("/users", usertest));
+ RestApi.Register(new RestCommand("/HelloWorld/name/{username}", usertest));
}
catch (Exception ex)
{
@@ -235,8 +236,8 @@ namespace TShockAPI
//RconHandler.ShutdownAllThreads();
}
- //http://127.0.0.1:8080/users?type=status
- object usertest(IParameterCollection parameters, RequestEventArgs request)
+ //http://127.0.0.1:8080/HelloWorld/name/{username}?type=status
+ object usertest(Dictionary verbs, IParameterCollection parameters, RequestEventArgs request)
{
var ret = new Dictionary();
var type = parameters["type"];