Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9CB91D4EB for ; Thu, 25 Oct 2012 16:42:08 +0000 (UTC) Received: (qmail 36184 invoked by uid 500); 25 Oct 2012 16:42:07 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 36070 invoked by uid 500); 25 Oct 2012 16:42:07 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 36062 invoked by uid 99); 25 Oct 2012 16:42:07 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Oct 2012 16:42:07 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of ryan.ramage@gmail.com designates 209.85.212.182 as permitted sender) Received: from [209.85.212.182] (HELO mail-wi0-f182.google.com) (209.85.212.182) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Oct 2012 16:42:01 +0000 Received: by mail-wi0-f182.google.com with SMTP id hm2so1347321wib.5 for ; Thu, 25 Oct 2012 09:41:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=JuHcLr1AhfQRBgScNHwXu5NRf5zDndrmWH0AePqa2fo=; b=KVR0gKlYhfFCC/RO9o3jggyYMcpWd1abr8wV5GRlo8/cpbzTr3AspEIyuMAPEHFkpk hi1y4jKhvZkoNH5rvooSgLq3OV6jEpShI1ynFL4BhGRlS3Nxngdd9+JFxwcrW/JlmqqB vR4mi8Oiai6r5DhkWjO8gQf+uf6acFXVAOvgsODI7Z1rUDJ+/WoKOebouKtc5/SLjq7G HkS5cqZRhMsOadPNPtGC9aEATZeJNaHLp4F6K/m40EtHlNxeqe9hbpJ0gNz8REzBhEhW sVZCT8XGOJMxjXYoaz6zX2Fl7Hm+us0Ox0s9HF/8cjgvmKQPCRADpaWGWPFPSsTaY4gu WD4g== MIME-Version: 1.0 Received: by 10.180.97.35 with SMTP id dx3mr15092627wib.14.1351183300799; Thu, 25 Oct 2012 09:41:40 -0700 (PDT) Received: by 10.216.207.165 with HTTP; Thu, 25 Oct 2012 09:41:40 -0700 (PDT) In-Reply-To: <508968F3.9020304@berjon.com> References: <508968F3.9020304@berjon.com> Date: Thu, 25 Oct 2012 10:41:40 -0600 Message-ID: Subject: Re: Rewrite inside of a path segment From: Ryan Ramage To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Checked: Checked by ClamAV on apache.org Wow, I like the idea of a rewriter function. It does add a lot of power. Some things to consider... I know others had rewriter rewrites in mind, namely benoit and jhs. Would be nice if they thought this was inline with their ideas. Just if we introduce it now like this we want to make sure its compatible with the ideas. Can you add the function as part of an array of rewrites? maybe like this: rewrites : [ {from : "/something", to : "/somethingelse"}, "function(req, path) { ...}" ] If moved ahead, I also would want to just have a check on erica/couchapp tools to make sure could handle this syntax. No one likes writing js functions inside of strings in a json file :) On Thu, Oct 25, 2012 at 10:29 AM, Robin Berjon wrote: > Hi Nathan, > > > On 19/09/2012 22:24 , Nathan Vander Wilt wrote: >> >> On Sep 18, 2012, at 1:16 AM, Robin Berjon wrote: >>> >>> The problem is that I need a rewrite that invariably accepts >>> parameters that don't fall at the / boundary. Typically: >>> >>> { from: "/blog/*" , to: "../../blog.*" , method: >>> "DELETE" } >>> >>> And that doesn't work. It just invariably encodes the * or :id, or >>> whatnot. Have I missed something? I of course could simply expose >>> another ID for DELETE or ugly IDs everywhere, but that completely >>> defeats the point of having rewrites in the first place. >> >> >> You have not missed anything. The problem is that you're wanting a >> rewrite doesn't fall on / boundaries, and the simplistic rewrite >> handler does not support this. I wish that rewrites were implemented >> in JavaScript instead of this one-off metalanguage, but my dreams >> aren't much practical value to you right now ;-) > > > As a matter of fact, they are :) > > It took me a little while to get back to you because I liked your idea of > having the rewrite work from JS. So I foolishly picked up an Erlang tutorial > and went on to implement it. You can find the pull request at: > > https://github.com/apache/couchdb/pull/38 > > Here's a quick description of how it works: > > """ > Alternatively, the rewriting can be performed by a function. It is specified > as follows: > > { > .... > "rewrites": "function (req, path) { > // process the request, and return the rewrite > }" > } > > The function is called with the request object and a path string. The latter > contains whatever path is left after removing everything up to _rewrite/. > > Which rewrite takes place depends on what the function returns, which can be > one of three things: > > - false, or a falsy value: indicates that the rewrite could not be > performed. This translates to a 500 error. > - "a/path": causes the rewrite to that path to take place, with the > original method being kept. > - { path: "a/path", method: "FOO" }: rewrites to that path and changes > the method to the one provided. > > Rewrite functions are meant to be devoid of side-effects and one should > write under the assumption that they are being cached. > """ > > There's a bit more information given in the pull request, and in the tests. > > I've only given it a cursory run yet but so far it works pretty well. I'd > appreciate comments, screams, jokes, etc. > > > -- > Robin Berjon - http://berjon.com/ - @robinberjon