vibe.http.router 205/454(45%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
6912
700
7112
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
129124
130124
131124
132124
133124
1340
1350
1360
1370
1380
13937
1400
1410
1420
1430
1440
1453
1460
1470
1480
1490
1501
1510
1520
1530
1540
1551
1560
1570
1580
1590
1601
1610
1620
1630
1640
1651
1660
1670
1680
1690
1700
1714
172360
173116
1740
1754
1760
1770
1781
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
19625
1970
19825
1990
2000
2010
2020
2030
2040
2050
20625
20750
20825
2090
21025
21125
212218
213202
2140
21516
21648
21716
21816
21916
2200
22138
2220
22312
22412
2250
2260
22712
2280
2290
2300
2310
2320
2331
2347
2351
2361
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
27337
27490
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4830
4840
4850
4860
4870
4880
4890
4900
4910
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5881
5890
5900
5910
592124
593124
5940
5950
5960
5970
5980
59937
6000
60125
6020
6030
6040
605110
6060
6070
6080
6090
6100
6110
6120
6130
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6470
6480
6490
6500
6510
6520
6530
65425
6550
6560
6570
6580
65925
66025
6610
6620
663376
664109
665109
666109
667109
668122
6690
67012
6710
6720
6730
6740
67525
6760
67725
6780
6790
680175
68125
68225
68325
6840
6850
6860
68725
68825
68925
69025
6910
6920
6930
6940
695109
696109
697109
6980
699109
7000
7010
702763
703218
7040
7050
706109
7070
7080
7090
7100
7110
712109
7130
7140
7150
7160
717109
718109
7190
7200
7210
722218
723109
7240
7250
7260
7270
7280
7290
73012
73112
7320
73312
73412
7350
73612
7370
73812
739532
740124
7410
74212
7430
74412
74512
7460
74712
74812
7490
75012
7510
7520
7530
7546144
75524
75624
75724
7580
75924
76024
761816
762496
7633496
764248
765248
7660
7670
76824
76924648
77027648
7713072
7720
77324
7740
77524
7760
77712
77812
7790
78012
7810
78212
7830
7840
7850
7860
7870
7880
7890
7900
7910
7920
7930
7940
7950
7960
7970
7980
7990
8000
8010
8020
8030
8040
8050
8060
8070
8080
8090
8100
8110
8120
8130
8140
8150
8160
8170
8180
8190
8200
8210
8220
8230
8240
8250
8260
8270
8280
8290
8300
8310
8320
8330
8340
8350
8360
8370
8380
8390
8400
8410
8420
8430
8440
8450
8460
8470
8480
8490
8500
8510
8520
8530
8540
8550
8560
8570
8580
8590
8600
8610
8620
8630
8640
8650
8660
8670
8680
8690
8700
8710
8720
8730
8743472
8750
8760
8770
8780
8790
8800
8810
8820
8830
8840
8850
8860
8870
888124
889124
890136
8910
8920
893124
894124
8950
896248
897124
898124
899124
900124
9010
90295604
90331868
90431620
9050
9060
9070
9080
9090
9100
9110
9120
9130
9140
9150
9160
9170
9180
9190
9200
9210
9220
9230
9240
925124
926124
9270
9280
9290
9300
9310
9320
9330
9340
93512
9360
9370
93824
93912
94012
94112
94212
9433108
9443096
9450
9463096
9476156
9480
94936
9500
95127756
9529216
9539216
9540
9550
95617404
9570
9580
9594096
9600
9610
9628
9638
9640
9650
9668
967720
968232
969178872
97059392
9710
9728
9730
9740
9758
9766168
9772048
978184320
97959392
98059392
98159392
9820
9832048
9840
9850
9860
987952
988696
989224
9900
9918
9920
9930
9940
99527756
9969216
9970
99812
9990
10000
10010
10020
10030
10040
10050
10060
10070
10080
10090
10100
10110
10120
10130
10140
10150
10160
10170
10180
10190
10200
10210
10220
10230
10240
10250
10260
102731868
102863624
1029112
103031868
10310
10320
10330
10340
10350
10360
1037124
1038124
1039124
1040124
10410
10420
10430
10440
1045127224
104631620
104731620
104831620
104931620
10500
10510
1052248
10530
10540
10550
10560
1057268
1058268
1059268
10600
10610
10620
10630
106432784
10650
10660
10671624
10680
10690
10700
1071464
10720
10730
10740
10750
10760
10770
10780
10790
10800
10810
10820
10836204
10840
10850
10860
10870
10880
10899240
10909240
109136
1092108
109336
10940
10950
10960
10970
10980
109912
110012
110124
11020
11030
11040
11050
11069216
110736900
11083084
110912276
11100
11110
11120
11130
11143096
11153096
11160
11170
/** Pattern based URL router for HTTP request. See `URLRouter` for more details. Copyright: © 2012-2015 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.http.router; public import vibe.http.server; import vibe.core.log; import std.functional; /** Routes HTTP requests based on the request method and URL. Routes are matched using a special URL match string that supports two forms of placeholders. See the sections below for more details. Registered routes are matched according to the same sequence as initially specified using `match`, `get`, `post` etc. Matching ends as soon as a route handler writes a response using `res.writeBody()` or similar means. If no route matches or if no route handler writes a response, the router will simply not handle the request and the HTTP server will automatically generate a 404 error. Match_patterns: Match patterns are character sequences that can optionally contain placeholders or raw wildcards ("*"). Raw wild cards match any character sequence, while placeholders match only sequences containing no slash ("/") characters. Placeholders are started using a colon (":") and are directly followed by their name. The first "/" character (or the end of the match string) denotes the end of the placeholder name. The part of the string that matches a placeholder will be stored in the `HTTPServerRequest.params` map using the placeholder name as the key. Match strings are subject to the following rules: $(UL $(LI A raw wildcard ("*") may only occur at the end of the match string) $(LI At least one character must be placed between any two placeholders or wildcards) $(LI The maximum allowed number of placeholders in a single match string is 64) ) Match_String_Examples: $(UL $(LI `"/foo/bar"` matches only `"/foo/bar"` itself) $(LI `"/foo/*"` matches `"/foo/"`, `"/foo/bar"`, `"/foo/bar/baz"` or _any other string beginning with `"/foo/"`) $(LI `"/:x/"` matches `"/foo/"`, `"/bar/"` and similar strings (and stores `"foo"`/`"bar"` in `req.params["x"]`), but not `"/foo/bar/"`) $(LI Matching partial path entries with wildcards is possible: `"/foo:x"` matches `"/foo"`, `"/foobar"`, but not `"/foo/bar"`) $(LI Multiple placeholders and raw wildcards can be combined: `"/:x/:y/*"`) ) */ final class URLRouter : HTTPServerRequestHandler { @safe: private { MatchTree!Route m_routes; string m_prefix; bool m_computeBasePath; } this(string prefix = null) { m_prefix = prefix; } /** Sets a common prefix for all registered routes. All routes will implicitly have this prefix prepended before being matched against incoming requests. */ @property string prefix() const { return m_prefix; } /** Controls the computation of the "routerRootDir" parameter. This parameter is available as `req.params["routerRootDir"]` and contains the relative path to the base path of the router. The base path is determined by the `prefix` property. Note that this feature currently is requires dynamic memory allocations and is opt-in for this reason. */ @property void enableRootDir(bool enable) { m_computeBasePath = enable; } /// Returns a single route handle to conveniently register multiple methods. URLRoute route(string path) in { assert(path.length, "Cannot register null or empty path!"); } body { return URLRoute(this, path); } /// unittest { void getFoo(scope HTTPServerRequest req, scope HTTPServerResponse res) { /* ... */ } void postFoo(scope HTTPServerRequest req, scope HTTPServerResponse res) { /* ... */ } void deleteFoo(scope HTTPServerRequest req, scope HTTPServerResponse res) { /* ... */ } auto r = new URLRouter; // using 'with' statement with (r.route("/foo")) { get(&getFoo); post(&postFoo); delete_(&deleteFoo); } // using method chaining r.route("/foo") .get(&getFoo) .post(&postFoo) .delete_(&deleteFoo); // without using route() r.get("/foo", &getFoo); r.post("/foo", &postFoo); r.delete_("/foo", &deleteFoo); } /// Adds a new route for requests matching the specified HTTP method and pattern. URLRouter match(Handler)(HTTPMethod method, string path, Handler handler) if (isValidHandler!Handler) { import std.algorithm; assert(path.length, "Cannot register null or empty path!"); assert(count(path, ':') <= maxRouteParameters, "Too many route parameters"); logDebug("add route %s %s", method, path); m_routes.addTerminal(path, Route(method, path, handlerDelegate(handler))); return this; } /// ditto URLRouter match(HTTPMethod method, string path, HTTPServerRequestDelegate handler) { return match!HTTPServerRequestDelegate(method, path, handler); } /// Adds a new route for GET requests matching the specified pattern. URLRouter get(Handler)(string url_match, Handler handler) if (isValidHandler!Handler) { return match(HTTPMethod.GET, url_match, handler); } /// ditto URLRouter get(string url_match, HTTPServerRequestDelegate handler) { return match(HTTPMethod.GET, url_match, handler); } /// Adds a new route for POST requests matching the specified pattern. URLRouter post(Handler)(string url_match, Handler handler) if (isValidHandler!Handler) { return match(HTTPMethod.POST, url_match, handler); } /// ditto URLRouter post(string url_match, HTTPServerRequestDelegate handler) { return match(HTTPMethod.POST, url_match, handler); } /// Adds a new route for PUT requests matching the specified pattern. URLRouter put(Handler)(string url_match, Handler handler) if (isValidHandler!Handler) { return match(HTTPMethod.PUT, url_match, handler); } /// ditto URLRouter put(string url_match, HTTPServerRequestDelegate handler) { return match(HTTPMethod.PUT, url_match, handler); } /// Adds a new route for DELETE requests matching the specified pattern. URLRouter delete_(Handler)(string url_match, Handler handler) if (isValidHandler!Handler) { return match(HTTPMethod.DELETE, url_match, handler); } /// ditto URLRouter delete_(string url_match, HTTPServerRequestDelegate handler) { return match(HTTPMethod.DELETE, url_match, handler); } /// Adds a new route for PATCH requests matching the specified pattern. URLRouter patch(Handler)(string url_match, Handler handler) if (isValidHandler!Handler) { return match(HTTPMethod.PATCH, url_match, handler); } /// ditto URLRouter patch(string url_match, HTTPServerRequestDelegate handler) { return match(HTTPMethod.PATCH, url_match, handler); } /// Adds a new route for requests matching the specified pattern, regardless of their HTTP verb. URLRouter any(Handler)(string url_match, Handler handler) { import std.traits; static HTTPMethod[] all_methods = [EnumMembers!HTTPMethod]; foreach(immutable method; all_methods) match(method, url_match, handler); return this; } /// ditto URLRouter any(string url_match, HTTPServerRequestDelegate handler) { return any!HTTPServerRequestDelegate(url_match, handler); } /** Rebuilds the internal matching structures to account for newly added routes. This should be used after a lot of routes have been added to the router, to force eager computation of the match structures. The alternative is to let the router lazily compute the structures when the first request happens, which can delay this request. */ void rebuild() { m_routes.rebuildGraph(); } /// Handles a HTTP request by dispatching it to the registered route handlers. void handleRequest(HTTPServerRequest req, HTTPServerResponse res) { auto method = req.method; string calcBasePath() @safe { import vibe.inet.path; auto p = InetPath(prefix.length ? prefix : "/"); p.endsWithSlash = true; return p.relativeToWeb(InetPath(req.path)).toString(); } auto path = req.path; if (path.length < m_prefix.length || path[0 .. m_prefix.length] != m_prefix) return; path = path[m_prefix.length .. $]; while (true) { bool done = m_routes.match(path, (ridx, scope values) @safe { auto r = () @trusted { return &m_routes.getTerminalData(ridx); } (); if (r.method != method) return false; logDebugV("route match: %s -> %s %s %s", req.path, r.method, r.pattern, values); foreach (i, v; values) req.params[m_routes.getTerminalVarNames(ridx)[i]] = v; if (m_computeBasePath) req.params["routerRootDir"] = calcBasePath(); r.cb(req, res); return res.headerWritten; }); if (done) return; if (method == HTTPMethod.HEAD) method = HTTPMethod.GET; else break; } logDebug("no route match: %s %s", req.method, req.requestURL); } /// Returns all registered routes as const AA const(Route)[] getAllRoutes() { auto routes = new Route[m_routes.terminalCount]; foreach (i, ref r; routes) r = m_routes.getTerminalData(i); return routes; } template isValidHandler(Handler) { @system { alias USDel = void delegate(HTTPServerRequest, HTTPServerResponse) @system; alias USFun = void function(HTTPServerRequest, HTTPServerResponse) @system; alias USDelS = void delegate(scope HTTPServerRequest, scope HTTPServerResponse) @system; alias USFunS = void function(scope HTTPServerRequest, scope HTTPServerResponse) @system; } static if ( is(Handler : HTTPServerRequestDelegate) || is(Handler : HTTPServerRequestFunction) || is(Handler : HTTPServerRequestHandler) || is(Handler : HTTPServerRequestDelegateS) || is(Handler : HTTPServerRequestFunctionS) || is(Handler : HTTPServerRequestHandlerS) ) { enum isValidHandler = true; } else static if ( is(Handler : USDel) || is(Handler : USFun) || is(Handler : USDelS) || is(Handler : USFunS) ) { enum isValidHandler = true; } else { enum isValidHandler = false; } } static void delegate(HTTPServerRequest, HTTPServerResponse) @safe handlerDelegate(Handler)(Handler handler) { import std.traits : isFunctionPointer; static if (isFunctionPointer!Handler) return handlerDelegate(() @trusted { return toDelegate(handler); } ()); else static if (is(Handler == class) || is(Handler == interface)) return &handler.handleRequest; else static if (__traits(compiles, () @safe { handler(HTTPServerRequest.init, HTTPServerResponse.init); } ())) return handler; else return (req, res) @trusted { handler(req, res); }; } unittest { static assert(isValidHandler!HTTPServerRequestFunction); static assert(isValidHandler!HTTPServerRequestDelegate); static assert(isValidHandler!HTTPServerRequestHandler); static assert(isValidHandler!HTTPServerRequestFunctionS); static assert(isValidHandler!HTTPServerRequestDelegateS); static assert(isValidHandler!HTTPServerRequestHandlerS); static assert(isValidHandler!(void delegate(HTTPServerRequest req, HTTPServerResponse res) @system)); static assert(isValidHandler!(void function(HTTPServerRequest req, HTTPServerResponse res) @system)); static assert(isValidHandler!(void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @system)); static assert(isValidHandler!(void function(scope HTTPServerRequest req, scope HTTPServerResponse res) @system)); static assert(!isValidHandler!(int delegate(HTTPServerRequest req, HTTPServerResponse res) @system)); static assert(!isValidHandler!(int delegate(HTTPServerRequest req, HTTPServerResponse res) @safe)); void test(H)(H h) { static assert(isValidHandler!H); } test((HTTPServerRequest req, HTTPServerResponse res) {}); } } /// @safe unittest { import vibe.http.fileserver; void addGroup(HTTPServerRequest req, HTTPServerResponse res) { // Route variables are accessible via the params map logInfo("Getting group %s for user %s.", req.params["groupname"], req.params["username"]); } void deleteUser(HTTPServerRequest req, HTTPServerResponse res) { // ... } void auth(HTTPServerRequest req, HTTPServerResponse res) { // TODO: check req.session to see if a user is logged in and // write an error page or throw an exception instead. } void setup() { auto router = new URLRouter; // Matches all GET requests for /users/*/groups/* and places // the place holders in req.params as 'username' and 'groupname'. router.get("/users/:username/groups/:groupname", &addGroup); // Matches all requests. This can be useful for authorization and // similar tasks. The auth method will only write a response if the // user is _not_ authorized. Otherwise, the router will fall through // and continue with the following routes. router.any("*", &auth); // Matches a POST request router.post("/users/:username/delete", &deleteUser); // Matches all GET requests in /static/ such as /static/img.png or // /static/styles/sty.css router.get("/static/*", serveStaticFiles("public/")); // Setup a HTTP server... auto settings = new HTTPServerSettings; // ... // The router can be directly passed to the listenHTTP function as // the main request handler. listenHTTP(settings, router); } } /** Using nested routers to map components to different sub paths. A component could for example be an embedded blog engine. */ @safe unittest { // some embedded component: void showComponentHome(HTTPServerRequest req, HTTPServerResponse res) { // ... } void showComponentUser(HTTPServerRequest req, HTTPServerResponse res) { // ... } void registerComponent(URLRouter router) { router.get("/", &showComponentHome); router.get("/users/:user", &showComponentUser); } // main application: void showHome(HTTPServerRequest req, HTTPServerResponse res) { // ... } void setup() { auto c1router = new URLRouter("/component1"); registerComponent(c1router); auto mainrouter = new URLRouter; mainrouter.get("/", &showHome); // forward all unprocessed requests to the component router mainrouter.any("*", c1router); // now the following routes will be matched: // / -> showHome // /component1/ -> showComponentHome // /component1/users/:user -> showComponentUser // Start the HTTP server auto settings = new HTTPServerSettings; // ... listenHTTP(settings, mainrouter); } } @safe unittest { // issue #1668 auto r = new URLRouter; r.get("/", (req, res) { if ("foo" in req.headers) res.writeBody("bar"); }); r.get("/", (scope req, scope res) { if ("foo" in req.headers) res.writeBody("bar"); }); r.get("/", (req, res) {}); r.post("/", (req, res) {}); r.put("/", (req, res) {}); r.delete_("/", (req, res) {}); r.patch("/", (req, res) {}); r.any("/", (req, res) {}); } @safe unittest { // issue #1866 auto r = new URLRouter; r.match(HTTPMethod.HEAD, "/foo", (scope req, scope res) { res.writeVoidBody; }); r.match(HTTPMethod.HEAD, "/foo", (req, res) { res.writeVoidBody; }); r.match(HTTPMethod.HEAD, "/foo", (scope HTTPServerRequest req, scope HTTPServerResponse res) { res.writeVoidBody; }); r.match(HTTPMethod.HEAD, "/foo", (HTTPServerRequest req, HTTPServerResponse res) { res.writeVoidBody; }); auto r2 = new URLRouter; r.match(HTTPMethod.HEAD, "/foo", r2); } @safe unittest { import vibe.inet.url; auto router = new URLRouter; string result; void a(HTTPServerRequest req, HTTPServerResponse) { result ~= "A"; } void b(HTTPServerRequest req, HTTPServerResponse) { result ~= "B"; } void c(HTTPServerRequest req, HTTPServerResponse) { assert(req.params["test"] == "x", "Wrong variable contents: "~req.params["test"]); result ~= "C"; } void d(HTTPServerRequest req, HTTPServerResponse) { assert(req.params["test"] == "y", "Wrong variable contents: "~req.params["test"]); result ~= "D"; } router.get("/test", &a); router.post("/test", &b); router.get("/a/:test", &c); router.get("/a/:test/", &d); auto res = createTestHTTPServerResponse(); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/")), res); assert(result == "", "Matched for non-existent / path"); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/test")), res); assert(result == "A", "Didn't match a simple GET request"); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/test"), HTTPMethod.POST), res); assert(result == "AB", "Didn't match a simple POST request"); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/a/"), HTTPMethod.GET), res); assert(result == "AB", "Matched empty variable. "~result); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/a/x"), HTTPMethod.GET), res); assert(result == "ABC", "Didn't match a trailing 1-character var."); // currently fails due to Path not accepting "//" //router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/a//"), HTTPMethod.GET), res); //assert(result == "ABC", "Matched empty string or slash as var. "~result); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/a/y/"), HTTPMethod.GET), res); assert(result == "ABCD", "Didn't match 1-character infix variable."); } @safe unittest { import vibe.inet.url; auto router = new URLRouter("/test"); string result; void a(HTTPServerRequest req, HTTPServerResponse) { result ~= "A"; } void b(HTTPServerRequest req, HTTPServerResponse) { result ~= "B"; } router.get("/x", &a); router.get("/y", &b); auto res = createTestHTTPServerResponse(); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/test")), res); assert(result == ""); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/test/x")), res); assert(result == "A"); router.handleRequest(createTestHTTPServerRequest(URL("http://localhost/test/y")), res); assert(result == "AB"); } @safe unittest { string ensureMatch(string pattern, string local_uri, string[string] expected_params = null) { import vibe.inet.url : URL; string ret = local_uri ~ " did not match " ~ pattern; auto r = new URLRouter; r.get(pattern, (req, res) { ret = null; foreach (k, v; expected_params) { if (k !in req.params) { ret = "Parameter "~k~" was not matched."; return; } if (req.params[k] != v) { ret = "Parameter "~k~" is '"~req.params[k]~"' instead of '"~v~"'."; return; } } }); auto req = createTestHTTPServerRequest(URL("http://localhost"~local_uri)); auto res = createTestHTTPServerResponse(); r.handleRequest(req, res); return ret; } assert(ensureMatch("/foo bar/", "/foo%20bar/") is null); // normalized pattern: "/foo%20bar/" //assert(ensureMatch("/foo%20bar/", "/foo%20bar/") is null); // normalized pattern: "/foo%20bar/" assert(ensureMatch("/foo/bar/", "/foo/bar/") is null); // normalized pattern: "/foo/bar/" //assert(ensureMatch("/foo/bar/", "/foo%2fbar/") !is null); //assert(ensureMatch("/foo%2fbar/", "/foo%2fbar/") is null); // normalized pattern: "/foo%2Fbar/" //assert(ensureMatch("/foo%2Fbar/", "/foo%2fbar/") is null); // normalized pattern: "/foo%2Fbar/" //assert(ensureMatch("/foo%2fbar/", "/foo%2Fbar/") is null); //assert(ensureMatch("/foo%2fbar/", "/foo/bar/") !is null); //assert(ensureMatch("/:foo/", "/foo%2Fbar/", ["foo": "foo/bar"]) is null); assert(ensureMatch("/:foo/", "/foo/bar/") !is null); } /** Convenience abstraction for a single `URLRouter` route. See `URLRouter.route` for a usage example. */ struct URLRoute { @safe: URLRouter router; string path; ref URLRoute get(Handler)(Handler h) { router.get(path, h); return this; } ref URLRoute post(Handler)(Handler h) { router.post(path, h); return this; } ref URLRoute put(Handler)(Handler h) { router.put(path, h); return this; } ref URLRoute delete_(Handler)(Handler h) { router.delete_(path, h); return this; } ref URLRoute patch(Handler)(Handler h) { router.patch(path, h); return this; } ref URLRoute any(Handler)(Handler h) { router.any(path, h); return this; } ref URLRoute match(Handler)(HTTPMethod method, Handler h) { router.match(method, path, h); return this; } } private enum maxRouteParameters = 64; private struct Route { HTTPMethod method; string pattern; HTTPServerRequestDelegate cb; } private string skipPathNode(string str, ref size_t idx) @safe { size_t start = idx; while( idx < str.length && str[idx] != '/' ) idx++; return str[start .. idx]; } private string skipPathNode(ref string str) @safe { size_t idx = 0; auto ret = skipPathNode(str, idx); str = str[idx .. $]; return ret; } private struct MatchTree(T) { @safe: import std.algorithm : countUntil; import std.array : array; private { struct Node { size_t terminalsStart; // slice into m_terminalTags size_t terminalsEnd; uint[ubyte.max+1] edges = uint.max; // character -> index into m_nodes } struct TerminalTag { size_t index; // index into m_terminals array size_t var; // index into Terminal.varNames/varValues or size_t.max } struct Terminal { string pattern; T data; string[] varNames; size_t[size_t] varMap; // maps node index to variable index } Node[] m_nodes; // all nodes as a single array TerminalTag[] m_terminalTags; Terminal[] m_terminals; enum TerminalChar = 0; bool m_upToDate = false; } @property size_t terminalCount() const { return m_terminals.length; } void addTerminal(string pattern, T data) { m_terminals ~= Terminal(pattern, data, null, null); m_upToDate = false; } bool match(string text, scope bool delegate(size_t terminal, scope string[] vars) @safe del) { // lazily update the match graph if (!m_upToDate) rebuildGraph(); return doMatch(text, del); } const(string)[] getTerminalVarNames(size_t terminal) const { return m_terminals[terminal].varNames; } ref inout(T) getTerminalData(size_t terminal) inout { return m_terminals[terminal].data; } void print() const { import std.algorithm : map; import std.array : join; import std.conv : to; import std.range : iota; import std.string : format; logInfo("Nodes:"); foreach (i, n; m_nodes) { logInfo(" %s %s", i, m_terminalTags[n.terminalsStart .. n.terminalsEnd] .map!(t => format("T%s%s", t.index, t.var != size_t.max ? "("~m_terminals[t.index].varNames[t.var]~")" : "")).join(" ")); //logInfo(" %s %s-%s", i, n.terminalsStart, n.terminalsEnd); static string mapChar(ubyte ch) { if (ch == TerminalChar) return "$"; if (ch >= '0' && ch <= '9') return to!string(cast(dchar)ch); if (ch >= 'a' && ch <= 'z') return to!string(cast(dchar)ch); if (ch >= 'A' && ch <= 'Z') return to!string(cast(dchar)ch); if (ch == '/') return "/"; if (ch == '^') return "^"; return ch.to!string; } void printRange(uint node, ubyte from, ubyte to) { if (to - from <= 10) logInfo(" %s -> %s", iota(from, cast(uint)to+1).map!(ch => mapChar(cast(ubyte)ch)).join("|"), node); else logInfo(" %s-%s -> %s", mapChar(from), mapChar(to), node); } uint last_to = uint.max; ubyte last_ch = 0; foreach (ch, e; n.edges) if (e != last_to) { if (last_to != uint.max) printRange(last_to, last_ch, cast(ubyte)(ch-1)); last_ch = cast(ubyte)ch; last_to = e; } if (last_to != uint.max) printRange(last_to, last_ch, ubyte.max); } } private bool doMatch(string text, scope bool delegate(size_t terminal, scope string[] vars) @safe del) const { string[maxRouteParameters] vars_buf;// = void; import std.algorithm : canFind; // first, determine the end node, if any auto n = matchTerminals(text); if (!n) return false; // then, go through the terminals and match their variables foreach (ref t; m_terminalTags[n.terminalsStart .. n.terminalsEnd]) { auto term = &m_terminals[t.index]; auto vars = vars_buf[0 .. term.varNames.length]; matchVars(vars, term, text); if (vars.canFind!(v => v.length == 0)) continue; // all variables must be non-empty to match if (del(t.index, vars)) return true; } return false; } private inout(Node)* matchTerminals(string text) inout { if (!m_nodes.length) return null; auto n = &m_nodes[0]; // follow the path through the match graph foreach (i, char ch; text) { auto nidx = n.edges[cast(size_t)ch]; if (nidx == uint.max) return null; n = &m_nodes[nidx]; } // finally, find a matching terminal node auto nidx = n.edges[TerminalChar]; if (nidx == uint.max) return null; n = &m_nodes[nidx]; return n; } private void matchVars(string[] dst, in Terminal* term, string text) const { auto nidx = 0; size_t activevar = size_t.max; size_t activevarstart; dst[] = null; // folow the path throgh the match graph foreach (i, char ch; text) { auto var = term.varMap.get(nidx, size_t.max); // detect end of variable if (var != activevar && activevar != size_t.max) { dst[activevar] = text[activevarstart .. i-1]; activevar = size_t.max; } // detect beginning of variable if (var != size_t.max && activevar == size_t.max) { activevar = var; activevarstart = i; } nidx = m_nodes[nidx].edges[cast(ubyte)ch]; assert(nidx != uint.max); } // terminate any active varible with the end of the input string or with the last character auto var = term.varMap.get(nidx, size_t.max); if (activevar != size_t.max) dst[activevar] = text[activevarstart .. (var == activevar ? $ : $-1)]; } private void rebuildGraph() { import std.array : appender; if (m_upToDate) return; m_upToDate = true; m_nodes = null; m_terminalTags = null; if (!m_terminals.length) return; MatchGraphBuilder builder; foreach (i, ref t; m_terminals) t.varNames = builder.insert(t.pattern, i); //builder.print(); builder.disambiguate(); auto nodemap = new uint[builder.m_nodes.length]; nodemap[] = uint.max; auto nodes = appender!(Node[]); nodes.reserve(1024); uint process(size_t n) { import std.algorithm : canFind; if (nodemap[n] != uint.max) return nodemap[n]; auto nmidx = cast(uint)nodes.data.length; nodemap[n] = nmidx; nodes.put(Node.init); Node nn; nn.terminalsStart = m_terminalTags.length; foreach (t; builder.m_nodes[n].terminals) { auto var = t.var.length ? m_terminals[t.index].varNames.countUntil(t.var) : size_t.max; assert(!m_terminalTags[nn.terminalsStart .. $].canFind!(u => u.index == t.index && u.var == var)); m_terminalTags ~= TerminalTag(t.index, var); if (var != size_t.max) m_terminals[t.index].varMap[nmidx] = var; } nn.terminalsEnd = m_terminalTags.length; foreach (ch, targets; builder.m_nodes[n].edges) foreach (to; targets) nn.edges[ch] = process(to); nodes.data[nmidx] = nn; return nmidx; } assert(builder.m_nodes[0].edges['^'].length == 1, "Graph must be disambiguated before purging."); process(builder.m_nodes[0].edges['^'][0]); m_nodes = nodes.data; logDebug("Match tree has %s(%s) nodes, %s terminals", m_nodes.length, builder.m_nodes.length, m_terminals.length); } } unittest { import std.string : format; MatchTree!int m; void testMatch(string str, size_t[] terms, string[] vars) { size_t[] mterms; string[] mvars; m.match(str, (t, scope vals) { mterms ~= t; mvars ~= vals; return false; }); assert(mterms == terms, format("Mismatched terminals: %s (expected %s)", mterms, terms)); assert(mvars == vars, format("Mismatched variables; %s (expected %s)", mvars, vars)); } m.addTerminal("a", 0); m.addTerminal("b", 0); m.addTerminal("ab", 0); m.rebuildGraph(); assert(m.getTerminalVarNames(0) == []); assert(m.getTerminalVarNames(1) == []); assert(m.getTerminalVarNames(2) == []); testMatch("a", [0], []); testMatch("ab", [2], []); testMatch("abc", [], []); testMatch("b", [1], []); m = MatchTree!int.init; m.addTerminal("ab", 0); m.addTerminal("a*", 0); m.rebuildGraph(); assert(m.getTerminalVarNames(0) == []); assert(m.getTerminalVarNames(1) == []); testMatch("a", [1], []); testMatch("ab", [0, 1], []); testMatch("abc", [1], []); m = MatchTree!int.init; m.addTerminal("ab", 0); m.addTerminal("a:var", 0); m.rebuildGraph(); assert(m.getTerminalVarNames(0) == []); assert(m.getTerminalVarNames(1) == ["var"], format("%s", m.getTerminalVarNames(1))); testMatch("a", [], []); // vars may not be empty testMatch("ab", [0, 1], ["b"]); testMatch("abc", [1], ["bc"]); m = MatchTree!int.init; m.addTerminal(":var1/:var2", 0); m.addTerminal("a/:var3", 0); m.addTerminal(":var4/b", 0); m.rebuildGraph(); assert(m.getTerminalVarNames(0) == ["var1", "var2"]); assert(m.getTerminalVarNames(1) == ["var3"]); assert(m.getTerminalVarNames(2) == ["var4"]); testMatch("a", [], []); testMatch("a/a", [0, 1], ["a", "a", "a"]); testMatch("a/b", [0, 1, 2], ["a", "b", "b", "a"]); testMatch("a/bc", [0, 1], ["a", "bc", "bc"]); testMatch("ab/b", [0, 2], ["ab", "b", "ab"]); testMatch("ab/bc", [0], ["ab", "bc"]); m = MatchTree!int.init; m.addTerminal(":var1/", 0); m.rebuildGraph(); assert(m.getTerminalVarNames(0) == ["var1"]); testMatch("ab/", [0], ["ab"]); testMatch("ab", [], []); testMatch("/ab", [], []); testMatch("a/b", [], []); testMatch("ab//", [], []); } private struct MatchGraphBuilder { @safe: import std.array : array; import std.algorithm : filter; import std.string : format; private { enum TerminalChar = 0; struct TerminalTag { size_t index; string var; bool opEquals(in ref TerminalTag other) const { return index == other.index && var == other.var; } } struct Node { size_t idx; TerminalTag[] terminals; size_t[][ubyte.max+1] edges; } Node[] m_nodes; } string[] insert(string pattern, size_t terminal) { import std.algorithm : canFind; auto full_pattern = pattern; string[] vars; if (!m_nodes.length) addNode(); // create start node and connect to zero node auto nidx = addNode(); addEdge(0, nidx, '^', terminal, null); while (pattern.length) { auto ch = pattern[0]; if (ch == '*') { assert(pattern.length == 1, "Asterisk is only allowed at the end of a pattern: "~full_pattern); pattern = null; foreach (v; ubyte.min .. ubyte.max+1) { if (v == TerminalChar) continue; addEdge(nidx, nidx, cast(ubyte)v, terminal, null); } } else if (ch == ':') { pattern = pattern[1 .. $]; auto name = skipPathNode(pattern); assert(name.length > 0, "Missing placeholder name: "~full_pattern); assert(!vars.canFind(name), "Duplicate placeholder name ':"~name~"': '"~full_pattern~"'"); vars ~= name; assert(!pattern.length || (pattern[0] != '*' && pattern[0] != ':'), "Cannot have two placeholders directly follow each other."); foreach (v; ubyte.min .. ubyte.max+1) { if (v == TerminalChar || v == '/') continue; addEdge(nidx, nidx, cast(ubyte)v, terminal, name); } } else { nidx = addEdge(nidx, ch, terminal, null); pattern = pattern[1 .. $]; } } addEdge(nidx, TerminalChar, terminal, null); return vars; } void disambiguate() { import std.algorithm : map, sum; import std.array : appender, join; //logInfo("Disambiguate"); if (!m_nodes.length) return; import vibe.utils.hashmap; HashMap!(size_t[], size_t) combined_nodes; auto visited = new bool[m_nodes.length * 2]; Stack!size_t node_stack; node_stack.reserve(m_nodes.length); node_stack.push(0); while (!node_stack.empty) { auto n = node_stack.pop(); while (n >= visited.length) visited.length = visited.length * 2; if (visited[n]) continue; //logInfo("Disambiguate %s", n); visited[n] = true; foreach (ch_; ubyte.min .. ubyte.max+1) { ubyte ch = cast(ubyte)ch_; auto chnodes = m_nodes[n].edges[ch_]; // handle trivial cases if (chnodes.length <= 1) continue; // generate combined state for ambiguous edges if (auto pn = () @trusted { return chnodes in combined_nodes; } ()) { m_nodes[n].edges[ch] = singleNodeArray(*pn); continue; } // for new combinations, create a new node size_t ncomb = addNode(); combined_nodes[chnodes] = ncomb; // allocate memory for all edges combined size_t total_edge_count = 0; foreach (chn; chnodes) { Node* cn = &m_nodes[chn]; foreach (edges; cn.edges) total_edge_count +=edges.length; } auto mem = new size_t[total_edge_count]; // write all edges size_t idx = 0; foreach (to_ch; ubyte.min .. ubyte.max+1) { size_t start = idx; foreach (chn; chnodes) { auto edges = m_nodes[chn].edges[to_ch]; mem[idx .. idx + edges.length] = edges; idx += edges.length; } m_nodes[ncomb].edges[to_ch] = mem[start .. idx]; } // add terminal indices foreach (chn; chnodes) addToArray(m_nodes[ncomb].terminals, m_nodes[chn].terminals); foreach (i; 1 .. m_nodes[ncomb].terminals.length) assert(m_nodes[ncomb].terminals[0] != m_nodes[ncomb].terminals[i]); m_nodes[n].edges[ch] = singleNodeArray(ncomb); } // process nodes recursively foreach (ch; ubyte.min .. ubyte.max+1) node_stack.push(m_nodes[n].edges[ch]); } debug logDebug("Disambiguate done: %s nodes, %s max stack size", m_nodes.length, node_stack.maxSize); } void print() const { import std.algorithm : map; import std.array : join; import std.conv : to; import std.string : format; logInfo("Nodes:"); foreach (i, n; m_nodes) { string mapChar(ubyte ch) { if (ch == TerminalChar) return "$"; if (ch >= '0' && ch <= '9') return to!string(cast(dchar)ch); if (ch >= 'a' && ch <= 'z') return to!string(cast(dchar)ch); if (ch >= 'A' && ch <= 'Z') return to!string(cast(dchar)ch); if (ch == '/') return "/"; return ch.to!string; } logInfo(" %s %s", i, n.terminals.map!(t => format("T%s%s", t.index, t.var.length ? "("~t.var~")" : "")).join(" ")); foreach (ch, tnodes; n.edges) foreach (tn; tnodes) logInfo(" %s -> %s", mapChar(cast(ubyte)ch), tn); } } private void addEdge(size_t from, size_t to, ubyte ch, size_t terminal, string var) { auto e = &m_nodes[from].edges[ch]; if (!(*e).length) *e = singleNodeArray(to); else *e ~= to; addTerminal(to, terminal, var); } private size_t addEdge(size_t from, ubyte ch, size_t terminal, string var) { import std.algorithm : canFind; import std.string : format; assert(!m_nodes[from].edges[ch].length > 0, format("%s is in %s", ch, m_nodes[from].edges)); auto nidx = addNode(); addEdge(from, nidx, ch, terminal, var); return nidx; } private void addTerminal(size_t node, size_t terminal, string var) { foreach (ref t; m_nodes[node].terminals) { if (t.index == terminal) { assert(t.var.length == 0 || t.var == var, "Ambiguous route var match!? '"~t.var~"' vs. '"~var~"'"); t.var = var; return; } } m_nodes[node].terminals ~= TerminalTag(terminal, var); } private size_t addNode() { auto idx = m_nodes.length; m_nodes ~= Node(idx, null, null); return idx; } private size_t[] singleNodeArray(size_t node) @trusted { return (&m_nodes[node].idx)[0 .. 1]; } private static addToArray(T)(ref T[] arr, T[] elems) { foreach (e; elems) addToArray(arr, e); } private static addToArray(T)(ref T[] arr, T elem) { import std.algorithm : canFind; if (!arr.canFind(elem)) arr ~= elem; } } private struct Stack(E) { private { E[] m_storage; size_t m_fill; debug size_t m_maxFill; } @property bool empty() const { return m_fill == 0; } debug @property size_t maxSize() const { return m_maxFill; } void reserve(size_t amt) { auto minsz = m_fill + amt; if (m_storage.length < minsz) { auto newlength = 64; while (newlength < minsz) newlength *= 2; m_storage.length = newlength; } } void push(E el) { reserve(1); m_storage[m_fill++] = el; debug if (m_fill > m_maxFill) m_maxFill = m_fill; } void push(E[] els) { reserve(els.length); foreach (el; els) m_storage[m_fill++] = el; debug if (m_fill > m_maxFill) m_maxFill = m_fill; } E pop() { assert(!empty, "Stack underflow."); return m_storage[--m_fill]; } }