Comments (17)
@uaply, regex support seems a good idea? :)
from brookframework.
Yeah, regexps are frequently used for routing, for example in Django under Python. They are powerful, and sometimes too much powerful :)
The only problem is how to implement it. We should find good RegExp library under Pascal. The one which present in FPC seems doesn't support named groups. Without named groups it will be very hard to use, harder than with current syntax.
from brookframework.
http://www.regular-expressions.info/download/TPerlRegEx.zip is a wrapper over PCRE and is licensed under MPL. Maybe only requires some cleanup to make it platform independent. I prefer a pure Pascal solution so I guess it's the time somebody needs to extend Sorokin's TRegExpr to support named group. The code is quite neat so I guess it won't be too difficult to support.
from brookframework.
We can use Sorokin's solution (it was moved to FPC after this looong thread: http://lists.lazarus.freepascal.org/pipermail/lazarus/2011-December/069381.html), and with an advantage: it's a code that comes with the Free Pascal.
I think something like:
protected
function DoMatchDirectly: Boolean; virtual;
function DoMatchRegEx: Boolean; virtual;
With DoMatchDirectly
using the current implementation and DoMatchRegEx
using new implementation with regex. So if someone wants to use a different solution of Sorokin, overrides DoMatchRegEx
.
What do you think?
I'll test all uaply changes and merge it to the master branch, so we'll be ready for this new implementation.
from brookframework.
How would be router path look like with this regexpr library? Also we need to pass variables to action. Now they are passed as JSON dictionary and accessed by string names.
Library regexpr.pas
has only subexpr
indexed by integers. Another library regex.pp
not allow getting sub-groups at all.
from brookframework.
You can see how to Slim implement it:
Brook router is based on Slim framework.
from brookframework.
PHP has full-fledged support of regexp, so routes in Slim implementation internally are converted in such way:
/user/:name/post/:id → /user/(?P<name>[^/]+)/post/(?P<id>.+)
But we don't have named group functionality now in pascal, the best we can do is
/user/([^/]+)/post/(.+)
And loose variable names, because groups addressed by index only. Values[1] and Values[2] instead of Values['name'] and Values['id'] is not very pleasing.
from brookframework.
Hm... We can test if the chars "(" and ")" (the sintax will be: /path/(<regex>)
) are present, so:
/user/:name/post/:id([0-9])
Two variables will created (name
, user
) and the second variable will tested with a regex, so:
/user/foo/post/1
OK!
/user/foo/post/a
Error!
Seems a good idea? :/
from brookframework.
...Or, we can send a patch to improve regex lib of Free Pascal.
from brookframework.
I was also thinking about how we can pre-process initial route path to memorize names of variables. But things are complicated by the fact groups could be nested: '(?(?[^/]+?).?(?[^/]*))'. The better solution is to modify regexp lib, but not to do patches over top of it.
As you see RegExp are much more cryptic :) It is obvious drawback, so we should leave old syntax too.
from brookframework.
I was also thinking about how we can pre-process initial route path to memorize names of variables. But things are > complicated by the fact groups could be nested: '(?(?[^/]+?).?(?[^/]*))'. The better > solution is to modify regexp lib, but not to do patches over top of it.
Hm.. we can implement a basic regex support.
I thought of another possibility, something like:
TAction.Register('/user/:name/:id', ['^[a-z]$', '^[0-9]$'], ['Invalid name: %s.', 'Invalid ID: %d.']);
Usage:
http://host/script/user/1/1
- 404 - Msg: Invalid name: 1.
http://host/script/user/a/a
- 404 - Msg: Invalid ID: a.
http://host/script/user/1/a
- 404 - Msg: Invalid name: 1.
http://host/script/user/a/1
- OK
So regex will be used to validate paths in URL.
Other demo:
TAction.Register('/home/*path/download', ['^[a-z]$']);
- It validates *path
variable.
TAction.Register('/download/*path/:fileid', ['', '^[0-9]$']);
- It validates :fileid
variable.
What do you think?
As you see RegExp are much more cryptic :) It is obvious drawback, so we should leave old syntax too.
Hehehe... we keep the old code, and the new feature using current implementation of RegExpr available on Free Pascal. :)
from brookframework.
Oops, I forgot ...
I'm planning show a new idea about middleware class support. I'll create a new issue talking about ...
Imagine it:
TUserAction.Register('/user/:id', TMyMiddlewareClass);
...
procedure TMyMiddlewareClass.Execute;
var
Vid: Integer;
begin
Vid := Action.Fields['id'].AsInteger;
if not VId in [0..9] then
Error('Invalid ID: %d', [VId]);
end;
And reuse TMyMiddlewareClass
in other class:
TDownloadAction.Register('/download/:id', TMyMiddlewareClass);
from brookframework.
... Or:
initialization
TDownloadAction.Register('/download/:id');
TDownloadAction.RegisterMiddleware(TMyMiddlewareClass);
from brookframework.
I'm planning show a new idea about middleware class support. I'll create a new issue talking about ...
Done. (#54)
from brookframework.
TAction.Register('/user/:name/:id', ['^[a-z]$', '^[0-9]$'], ['Invalid name: %s.', 'Invalid ID: %d.']);
Good idea – and it has very clear meaning. We validate values with regexp before matching. Error messages probably will not be needed (maybe only for debug).
from brookframework.
Hello @uaply, you want to implement this feature?
from brookframework.
Done, you can use middleware
+ constraints
to do it! :)
from brookframework.
Related Issues (20)
- Why plugins projects on github to brookframework is closing? HOT 4
- Telegram plugin for brookframework HOT 15
- i18n HOT 4
- Version 4 ? HOT 14
- libbrook.pas in Source HOT 3
- tardigrade demos throwing errors in libbrook HOT 4
- Server requirements? HOT 4
- dopf & LastInsertID HOT 4
- httprouter.lpr throwing error HOT 2
- How am I supposed to free worker thread in brook daemon? HOT 9
- Use legacy application with tardigrade HOT 19
- CPU Activity is high for tardigrade projects BROOK4 HOT 4
- More documentation HOT 1
- Can't get POST request data HOT 13
- Brookframework with sagui (legacy) HOT 11
- HTTP Client HOT 1
- Where did the captcha plugin for 4.0 go? HOT 1
- Some suggestion about JTemplate? HOT 3
- [IDEA] New repository HOT 1
- EBrookHTTPServer: Failed to send data in request for HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from brookframework.