RejectedSoftware Forums

Sign up

How to do url rewrite ?

Hello !

Where is the best place to do url rewrite?

Example I have a request /prefix/data/list/5 that I want to be rewritten as /data/list/5 or any other thing just in case that url now moved.

I do not want redirection only internal processing.

Cheers !

Re: How to do url rewrite ?

Hello !

Router.prefix is not usable here because I want to apply transformations other than fixed prefix removal.

Cheers !

On Tue, 21 Oct 2014 22:02:36 GMT
"Domingo" mingodad@gmail.com wrote:

Hello !

Where is the best place to do url rewrite?

Example I have a request /prefix/data/list/5 that I want to be rewritten as /data/list/5 or any other thing just in case that url now moved.

I do not want redirection only internal processing.

Cheers !

Domingo Alavarez Duarte mingodad@gmail.com

Re: How to do url rewrite ?

Looking at the code I'm proposing to have this on vibed as a general url rewrite hook:

/// URLRewriter request handler
alias HTTPServerURLRewriter = void function(HTTPServerRequest req);
...
interface HTTPRouter : HTTPServerRequestHandler {

@property string prefix() const;
@property HTTPServerURLRewriter url_rewriter(); //<<<< new
@property void url_rewriter(HTTPServerURLRewriter cb); // <<<< new

...
final class URLRouter : HTTPRouter {

private {
	version (VibeRouterTreeMatch) MatchTree!Route m_routes;
	else Route[] m_routes;
	string m_prefix;
	HTTPServerURLRewriter m_url_rewriter;
}

...

@property string prefix() const { return m_prefix; }

@property void url_rewriter(HTTPServerURLRewriter cb) { m_url_rewriter = cb; }
@property HTTPServerURLRewriter url_rewriter() { return m_url_rewriter; }

...

void handleRequest(HTTPServerRequest req, HTTPServerResponse res)
{
	if(m_url_rewriter !is null)
	{
		m_url_rewriter(req);
	}
	auto method = req.method;

static void url_rewriter(HTTPServerRequest req)
{

const string prefix = "/internal";
if( req.path.indexOf(prefix) == 0 )
{
	req.path = req.path[prefix.length..$];
}

}
...

auto router = new URLRouter;
router.url_rewriter = &url_rewriter;

Re: How to do url rewrite ?

I did it on my fork of vibe.d but I could not find a way to make a pull request with only that commit so here is the link for the commit (on this commit also there is some small changes to other parts that I found usefull):

https://github.com/mingodad/vibe.d/commit/7f0c91b072ccf92420c04ca28567f7cfe06d79bc

Re: How to do url rewrite ?

On Tue, 21 Oct 2014 23:42:12 GMT, Domingo wrote:

I did it on my fork of vibe.d but I could not find a way to make a pull request with only that commit so here is the link for the commit (on this commit also there is some small changes to other parts that I found usefull):

https://github.com/mingodad/vibe.d/commit/7f0c91b072ccf92420c04ca28567f7cfe06d79bc

For doing a pull request, you'd have to create a branch based on upstream master for the desired commit(s) and then, if they are already committed to your local master cherry pick them into the new branch. Then on GitHub, you can select the specific branch to pull.

Regarding the implementation, I'd usually do something like this as a separate "proxy" function. Any functionality that is not strictly needed in the class/interface should ideally be kept separate.

Example:

// library function
alias HTTPServerReqestDelegate withModifiedRequest(alias MODIFIER)(HTTPServerRequestHandler handler, HTTPServerRequest req, HTTPServerResponse res)
{
	return (req, res) { MODIFIER(req); handler(req, res); }
}

// usage
void rewriteURL(HTTPServerRequest req)
{
	if (req.path.startsWith("/something/"))
		req.path = req.path[10 .. $];
}

static this()
{
    listenHTTP(router.withModifiedRequest!rewriteURL);
}

For most other functionality, the router can be used nicely for chaining by prepending a router.any("*", ...); route, but the matching would get less efficient if it had to watch for changed request paths, so that is not really an option here.