-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRoute.cs
More file actions
110 lines (94 loc) · 2.86 KB
/
HttpRoute.cs
File metadata and controls
110 lines (94 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace SimpleHttp
{
public class HttpRoute
{
protected readonly Func<string[], HttpRequest, HttpResponse, bool> callback;
public string MethodPattern { get; }
public string UrlPattern { get; }
public bool IsRegex { get; }
public bool MatchFullUrl { get; }
public static bool InvokeMatched(List<HttpRoute> routes, HttpRequest request, HttpResponse response)
{
foreach (var route in routes)
{
var captures = route.Match(request);
if (captures == null)
continue;
if (!route.Invoke(captures, request, response))
return false;
}
return true;
}
public HttpRoute(
string methodPattern, string urlPattern, bool isRegex, bool matchFullUrl,
Func<string[], HttpRequest, HttpResponse, bool> callback)
{
this.callback = callback;
MethodPattern = methodPattern;
UrlPattern = urlPattern;
IsRegex = isRegex;
MatchFullUrl = matchFullUrl;
}
public HttpRoute(
string methodPattern, string urlPattern, bool isRegex, bool matchFullUrl,
Func<HttpRequest, HttpResponse, bool> callback)
: this(methodPattern, urlPattern, isRegex, matchFullUrl,
(cap, req, res) => callback(req, res))
{ }
public HttpRoute(
string methodPattern, string urlPattern, bool isRegex, bool matchFullUrl,
Action<string[], HttpRequest, HttpResponse> callback)
: this(methodPattern, urlPattern, isRegex, matchFullUrl,
(cap, req, res) => { callback(cap, req, res); return false; })
{ }
public HttpRoute(
string methodPattern, string urlPattern, bool isRegex, bool matchFullUrl,
Action<HttpRequest, HttpResponse> callback)
: this(methodPattern, urlPattern, isRegex, matchFullUrl,
(cap, req, res) => { callback(req, res); return false; })
{ }
public string[] Match(HttpRequest request)
{
var result = new string[0];
if (MethodPattern != null)
{
if (IsRegex)
{
if (!Regex.IsMatch(request.Method, $"\\A(?:{MethodPattern})\\z", RegexOptions.Singleline))
return null;
}
else
{
if (request.Method != MethodPattern)
return null;
}
}
if (UrlPattern != null)
{
string url = MatchFullUrl ? request.Url + request.QueryString : request.Url;
if (IsRegex)
{
var match = Regex.Match(url, $"\\A(?:{UrlPattern})\\z", RegexOptions.Singleline);
if (!match.Success)
return null;
result = match.Groups.Cast<Capture>().Skip(1)
.Select(cap => HttpRequest.UrlDecode(cap.Value)).ToArray();
}
else
{
if (url != UrlPattern)
return null;
}
}
return result;
}
public bool Invoke(string[] captures, HttpRequest request, HttpResponse response) =>
callback(captures, request, response);
public bool Invoke(HttpRequest request, HttpResponse response) =>
Invoke(new string[0], request, response);
}
}