You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Bernhard Gschwantner <be...@unserwein.at> on 2013/09/17 15:34:04 UTC

Redirects from _list functions in CouchDB 1.4

Hello,

I tried to redirect from a _list function without success. This code throws
a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):

function(head, req) {
  return {
    code : 301,
    headers : {
      Location : 'http://www.google.com'
    }
  };
}

Is there something I am doing wrong or missing?

The real-world use case is that we have several aliases to each document in
the db, and all those aliases should be redirected to the real document id.
We use a view to emit the aliases and a list function to redirect all those
aliases to the real url.

We've been working around the previous issues with modifying headers after
getRow() by redirecting through JavaScript, but want to get rid of this
workaround eventually.

Regards,
Bernhard

-- 

Bernhard Gschwantner
Unser Wein G&U OG
Kirchengasse 13/7, 1070 Wien

mobil: +43 (6991) 971 32 96
tel: +43 (1) 971 32 95
e-mail: bernhard@unserwein.at
twitter: @bernharduw <http://twitter.com/bernharduw>
web: www.unserwein.at

Re: Redirects from _list functions in CouchDB 1.4

Posted by Bernhard Gschwantner <be...@unserwein.at>.
Hi Bob,

that doesn't seem to solve the problem. Even when I put the code from the
wiki (first example) into a fresh design doc, I get the badmatch error in
the couchdb _logs.

Bernhard


2013/9/17 Robert Newson <rn...@apache.org>

> Comparing with the examples at
> https://wiki.apache.org/couchdb/Formatting_with_Show_and_List#Sending_a_Redirect,
> I think you need to return JSON (which requires the double quotes for
> "code", for example) rather than Javascript.
>
> B.
>
> On 17 Sep 2013, at 14:34, Bernhard Gschwantner <be...@unserwein.at>
> wrote:
>
> > Hello,
> >
> > I tried to redirect from a _list function without success. This code
> throws
> > a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
> >
> > function(head, req) {
> >  return {
> >    code : 301,
> >    headers : {
> >      Location : 'http://www.google.com'
> >    }
> >  };
> > }
> >
> > Is there something I am doing wrong or missing?
> >
> > The real-world use case is that we have several aliases to each document
> in
> > the db, and all those aliases should be redirected to the real document
> id.
> > We use a view to emit the aliases and a list function to redirect all
> those
> > aliases to the real url.
> >
> > We've been working around the previous issues with modifying headers
> after
> > getRow() by redirecting through JavaScript, but want to get rid of this
> > workaround eventually.
> >
> > Regards,
> > Bernhard
> >
> > --
> >
> > Bernhard Gschwantner
> > Unser Wein G&U OG
> > Kirchengasse 13/7, 1070 Wien
> >
> > mobil: +43 (6991) 971 32 96
> > tel: +43 (1) 971 32 95
> > e-mail: bernhard@unserwein.at
> > twitter: @bernharduw <http://twitter.com/bernharduw>
> > web: www.unserwein.at
>
>


-- 

Bernhard Gschwantner
Unser Wein G&U OG
Kirchengasse 13/7, 1070 Wien

mobil: +43 (6991) 971 32 96
tel: +43 (1) 971 32 95
e-mail: bernhard@unserwein.at
twitter: @bernharduw <http://twitter.com/bernharduw>
web: www.unserwein.at

Re: Redirects from _list functions in CouchDB 1.4

Posted by Robert Newson <rn...@apache.org>.
Comparing with the examples at https://wiki.apache.org/couchdb/Formatting_with_Show_and_List#Sending_a_Redirect, I think you need to return JSON (which requires the double quotes for "code", for example) rather than Javascript.

B.

On 17 Sep 2013, at 14:34, Bernhard Gschwantner <be...@unserwein.at> wrote:

> Hello,
> 
> I tried to redirect from a _list function without success. This code throws
> a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
> 
> function(head, req) {
>  return {
>    code : 301,
>    headers : {
>      Location : 'http://www.google.com'
>    }
>  };
> }
> 
> Is there something I am doing wrong or missing?
> 
> The real-world use case is that we have several aliases to each document in
> the db, and all those aliases should be redirected to the real document id.
> We use a view to emit the aliases and a list function to redirect all those
> aliases to the real url.
> 
> We've been working around the previous issues with modifying headers after
> getRow() by redirecting through JavaScript, but want to get rid of this
> workaround eventually.
> 
> Regards,
> Bernhard
> 
> -- 
> 
> Bernhard Gschwantner
> Unser Wein G&U OG
> Kirchengasse 13/7, 1070 Wien
> 
> mobil: +43 (6991) 971 32 96
> tel: +43 (1) 971 32 95
> e-mail: bernhard@unserwein.at
> twitter: @bernharduw <http://twitter.com/bernharduw>
> web: www.unserwein.at


Re: Redirects from _list functions in CouchDB 1.4

Posted by Dave Cottlehuber <dc...@jsonified.com>.
Servus Bernhard,

Wiki powers upgraded. Preferably, you can go one further and update
the new spiffy docs directly, the wiki will slowly get pruned away.
Feel free to remove any info on the wiki that you feel is superceded
by the docs.

https://couchdb.readthedocs.org/en/latest/ddocs.html is in git repo
for `share/doc/src/ddocs.rst`, and how to contribute is hopefully
covered here: https://couchdb.readthedocs.org/en/latest/contributing.html

I'm glad to hear you are making use of this!


On 18 September 2013 17:51, Bernhard Gschwantner <be...@unserwein.at> wrote:
> Wow, thanks a lot, it worked - woohoo!
>
> I wouldn't consider this as a bug, just something one should know. start()
> actually sounds like something that needs to be stopped somehow.
>
> If you or somebody else could add me to the wiki contributors group, I
> would be happy to clean some stuff up there and add links to the current
> docs where appropriate (.../en/latest/). Should lead to less confusion. My
> wiki user name is BernhardGschwantner.
>
> Cheers,
> Bernhard
>
>
> 2013/9/18 Alexander Shorin <kx...@gmail.com>
>
>> Hi Bernhard,
>>
>> Sorry, I'd to check my code instead of post it blindly. And yes this
>> looks as bug for me.
>>
>> To fix this you have to return any value from list function like:
>>
>> function(head, req) {
>>   start({"code": 301, "headers": {"Location": "http://google.com"}});
>>   return 'redirecting...';
>> }
>>
>> this _any_ value may be also empty string, but not null. return ''; also
>> fixes.
>>
>> --
>> ,,,^..^,,,
>>
>>
>> On Wed, Sep 18, 2013 at 5:04 PM, Bernhard Gschwantner
>> <be...@unserwein.at> wrote:
>> > Hi Alexander,
>> >
>> > Thanks a lot for the detailled answer! I completely understand what
>> you're
>> > saying. I've been following the JIRA issues COUCHDB-430, COUCHDB-514 etc.
>> > for years, since this redirect topic was one of the first obstacles I ran
>> > into when starting to use CouchDB.
>> >
>> > Nevertheless I still don't get a valid redirect, even with start(). It's
>> a
>> > bit embarrassing that I can't get a simple _list function right... ;-)
>> >
>> >
>> > I put this design doc into a new database "redirect":
>> >
>> >
>> > {
>> >    "_id": "_design/redirect",
>> >     "views": {
>> >        "all": {
>> >            "map": "function(doc) {\n\temit(doc._id, null);\n}"
>> >        }
>> >    },
>> >    "lists": {
>> >        "redirect": "function(head, req) {\n\tstart({\n\t\t\"code\":
>> > 302,\n\t\t\"headers\": {\n\t\t\t\"Location\": \"http://www.google.com
>> > \"\n\t\t}\n\t});\n}"
>> >    }
>> > }
>> >
>> >
>> > The couchdb output when calling
>> > http://localhost:5984/redirect/_design/test/_list/redirect/all
>> >
>> > {"error":"unknown_error","reason":"undef"}
>> >
>> >
>> > This is the couchdb.log output (level INFO):
>> >
>> >
>> > [error] [<0.20382.0>] Uncaught error in HTTP request: {error,undef}
>> > [info] [<0.20382.0>] Stacktrace: [{undefined,write_chunk,[[]],[]},
>> >                                   {couch_httpd,last_chunk,1,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
>> >                                     {line,692}]},
>> >                                   {couch_mrview_show,list_cb,2,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> >
>> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview_show.erl"},
>> >                                     {line,254}]},
>> >                                   {couch_mrview,finish_fold,2,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> >
>> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview.erl"},
>> >                                     {line,339}]},
>> >                                   {couch_query_servers,with_ddoc_proc,2,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> >
>> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_query_servers.erl"},
>> >                                     {line,269}]},
>> >                                   {couch_httpd,etag_maybe,2,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
>> >                                     {line,600}]},
>> >                                   {couch_httpd_db,do_db_req,2,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd_db.erl"},
>> >                                     {line,234}]},
>> >                                   {couch_httpd,handle_request_int,5,
>> >                                    [{file,
>> >
>> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
>> > 2Fgit-wip-us.apache.org
>> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
>> >                                     {line,332}]}]
>> > [info] [<0.20382.0>] 127.0.0.1 - - GET
>> > /redirect/_design/redirect/_list/redirect/all 500
>> > [error] [<0.20382.0>] httpd 500 error response:
>> >  {"error":"unknown_error","reason":"undef"}
>> >
>> >
>> >
>> > Is this a bug, or am I still missing something?
>> >
>> > Regards,
>> > Bernhard
>> >
>> >
>> >
>> > 2013/9/18 Alexander Shorin <kx...@gmail.com>
>> >
>> >> Hi Bernhard,
>> >>
>> >> you need to send redirect with start() function call.
>> >>
>> >> function(head, req) {
>> >>   start({"code": 301, "headers": {"Location": "http://google.com"}});
>> >> }
>> >>
>> >> http://docs.couchdb.org/en/1.4.x/query-servers.html
>> >>
>> >> Why so? start() function allows to send custom HTTP headers to the
>> >> client before any response (or with first chunk of it). When you
>> >> return or send() something before or without start() call, query
>> >> server implicitly called it with empty object payload which means that
>> >> CouchDB will send default HTTP status code and headers. Since CouchDB
>> >> sends response of list functions as chunked transfer, it doesn't
>> >> buffers all of it to let you change HTTP response status or headers
>> >> somewhere in the middle - what's why you need to define them in first
>> >> place and start() function explicitly points on this.
>> >>
>> >> For show functions your code will work since they produces only one
>> >> response object which contains response body and optionally may have
>> >> HTTP status and headers. With a little adaptation, the same is true
>> >> for update functions too.
>> >>
>> >> --
>> >> ,,,^..^,,,
>> >>
>> >>
>> >> On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner
>> >> <be...@unserwein.at> wrote:
>> >> > Hello,
>> >> >
>> >> > I tried to redirect from a _list function without success. This code
>> >> throws
>> >> > a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
>> >> >
>> >> > function(head, req) {
>> >> >   return {
>> >> >     code : 301,
>> >> >     headers : {
>> >> >       Location : 'http://www.google.com'
>> >> >     }
>> >> >   };
>> >> > }
>> >> >
>> >> > Is there something I am doing wrong or missing?
>> >> >
>> >> > The real-world use case is that we have several aliases to each
>> document
>> >> in
>> >> > the db, and all those aliases should be redirected to the real
>> document
>> >> id.
>> >> > We use a view to emit the aliases and a list function to redirect all
>> >> those
>> >> > aliases to the real url.
>> >> >
>> >> > We've been working around the previous issues with modifying headers
>> >> after
>> >> > getRow() by redirecting through JavaScript, but want to get rid of
>> this
>> >> > workaround eventually.
>> >> >
>> >> > Regards,
>> >> > Bernhard
>> >> >
>> >> > --
>> >> >
>> >> > Bernhard Gschwantner
>> >> > Unser Wein G&U OG
>> >> > Kirchengasse 13/7, 1070 Wien
>> >> >
>> >> > mobil: +43 (6991) 971 32 96
>> >> > tel: +43 (1) 971 32 95
>> >> > e-mail: bernhard@unserwein.at
>> >> > twitter: @bernharduw <http://twitter.com/bernharduw>
>> >> > web: www.unserwein.at
>> >>
>> >
>> >
>> >
>> > --
>> >
>> > Bernhard Gschwantner
>> > Unser Wein G&U OG
>> > Kirchengasse 13/7, 1070 Wien
>> >
>> > mobil: +43 (6991) 971 32 96
>> > tel: +43 (1) 971 32 95
>> > e-mail: bernhard@unserwein.at
>> > twitter: @bernharduw <http://twitter.com/bernharduw>
>> > web: www.unserwein.at
>>
>
>
>
> --
>
> Bernhard Gschwantner
> Unser Wein G&U OG
> Kirchengasse 13/7, 1070 Wien
>
> mobil: +43 (6991) 971 32 96
> tel: +43 (1) 971 32 95
> e-mail: bernhard@unserwein.at
> twitter: @bernharduw <http://twitter.com/bernharduw>
> web: www.unserwein.at

Re: Redirects from _list functions in CouchDB 1.4

Posted by Bernhard Gschwantner <be...@unserwein.at>.
Wow, thanks a lot, it worked - woohoo!

I wouldn't consider this as a bug, just something one should know. start()
actually sounds like something that needs to be stopped somehow.

If you or somebody else could add me to the wiki contributors group, I
would be happy to clean some stuff up there and add links to the current
docs where appropriate (.../en/latest/). Should lead to less confusion. My
wiki user name is BernhardGschwantner.

Cheers,
Bernhard


2013/9/18 Alexander Shorin <kx...@gmail.com>

> Hi Bernhard,
>
> Sorry, I'd to check my code instead of post it blindly. And yes this
> looks as bug for me.
>
> To fix this you have to return any value from list function like:
>
> function(head, req) {
>   start({"code": 301, "headers": {"Location": "http://google.com"}});
>   return 'redirecting...';
> }
>
> this _any_ value may be also empty string, but not null. return ''; also
> fixes.
>
> --
> ,,,^..^,,,
>
>
> On Wed, Sep 18, 2013 at 5:04 PM, Bernhard Gschwantner
> <be...@unserwein.at> wrote:
> > Hi Alexander,
> >
> > Thanks a lot for the detailled answer! I completely understand what
> you're
> > saying. I've been following the JIRA issues COUCHDB-430, COUCHDB-514 etc.
> > for years, since this redirect topic was one of the first obstacles I ran
> > into when starting to use CouchDB.
> >
> > Nevertheless I still don't get a valid redirect, even with start(). It's
> a
> > bit embarrassing that I can't get a simple _list function right... ;-)
> >
> >
> > I put this design doc into a new database "redirect":
> >
> >
> > {
> >    "_id": "_design/redirect",
> >     "views": {
> >        "all": {
> >            "map": "function(doc) {\n\temit(doc._id, null);\n}"
> >        }
> >    },
> >    "lists": {
> >        "redirect": "function(head, req) {\n\tstart({\n\t\t\"code\":
> > 302,\n\t\t\"headers\": {\n\t\t\t\"Location\": \"http://www.google.com
> > \"\n\t\t}\n\t});\n}"
> >    }
> > }
> >
> >
> > The couchdb output when calling
> > http://localhost:5984/redirect/_design/test/_list/redirect/all
> >
> > {"error":"unknown_error","reason":"undef"}
> >
> >
> > This is the couchdb.log output (level INFO):
> >
> >
> > [error] [<0.20382.0>] Uncaught error in HTTP request: {error,undef}
> > [info] [<0.20382.0>] Stacktrace: [{undefined,write_chunk,[[]],[]},
> >                                   {couch_httpd,last_chunk,1,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
> >                                     {line,692}]},
> >                                   {couch_mrview_show,list_cb,2,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> >
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview_show.erl"},
> >                                     {line,254}]},
> >                                   {couch_mrview,finish_fold,2,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> >
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview.erl"},
> >                                     {line,339}]},
> >                                   {couch_query_servers,with_ddoc_proc,2,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> >
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_query_servers.erl"},
> >                                     {line,269}]},
> >                                   {couch_httpd,etag_maybe,2,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
> >                                     {line,600}]},
> >                                   {couch_httpd_db,do_db_req,2,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd_db.erl"},
> >                                     {line,234}]},
> >                                   {couch_httpd,handle_request_int,5,
> >                                    [{file,
> >
> >  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> > 2Fgit-wip-us.apache.org
> > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
> >                                     {line,332}]}]
> > [info] [<0.20382.0>] 127.0.0.1 - - GET
> > /redirect/_design/redirect/_list/redirect/all 500
> > [error] [<0.20382.0>] httpd 500 error response:
> >  {"error":"unknown_error","reason":"undef"}
> >
> >
> >
> > Is this a bug, or am I still missing something?
> >
> > Regards,
> > Bernhard
> >
> >
> >
> > 2013/9/18 Alexander Shorin <kx...@gmail.com>
> >
> >> Hi Bernhard,
> >>
> >> you need to send redirect with start() function call.
> >>
> >> function(head, req) {
> >>   start({"code": 301, "headers": {"Location": "http://google.com"}});
> >> }
> >>
> >> http://docs.couchdb.org/en/1.4.x/query-servers.html
> >>
> >> Why so? start() function allows to send custom HTTP headers to the
> >> client before any response (or with first chunk of it). When you
> >> return or send() something before or without start() call, query
> >> server implicitly called it with empty object payload which means that
> >> CouchDB will send default HTTP status code and headers. Since CouchDB
> >> sends response of list functions as chunked transfer, it doesn't
> >> buffers all of it to let you change HTTP response status or headers
> >> somewhere in the middle - what's why you need to define them in first
> >> place and start() function explicitly points on this.
> >>
> >> For show functions your code will work since they produces only one
> >> response object which contains response body and optionally may have
> >> HTTP status and headers. With a little adaptation, the same is true
> >> for update functions too.
> >>
> >> --
> >> ,,,^..^,,,
> >>
> >>
> >> On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner
> >> <be...@unserwein.at> wrote:
> >> > Hello,
> >> >
> >> > I tried to redirect from a _list function without success. This code
> >> throws
> >> > a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
> >> >
> >> > function(head, req) {
> >> >   return {
> >> >     code : 301,
> >> >     headers : {
> >> >       Location : 'http://www.google.com'
> >> >     }
> >> >   };
> >> > }
> >> >
> >> > Is there something I am doing wrong or missing?
> >> >
> >> > The real-world use case is that we have several aliases to each
> document
> >> in
> >> > the db, and all those aliases should be redirected to the real
> document
> >> id.
> >> > We use a view to emit the aliases and a list function to redirect all
> >> those
> >> > aliases to the real url.
> >> >
> >> > We've been working around the previous issues with modifying headers
> >> after
> >> > getRow() by redirecting through JavaScript, but want to get rid of
> this
> >> > workaround eventually.
> >> >
> >> > Regards,
> >> > Bernhard
> >> >
> >> > --
> >> >
> >> > Bernhard Gschwantner
> >> > Unser Wein G&U OG
> >> > Kirchengasse 13/7, 1070 Wien
> >> >
> >> > mobil: +43 (6991) 971 32 96
> >> > tel: +43 (1) 971 32 95
> >> > e-mail: bernhard@unserwein.at
> >> > twitter: @bernharduw <http://twitter.com/bernharduw>
> >> > web: www.unserwein.at
> >>
> >
> >
> >
> > --
> >
> > Bernhard Gschwantner
> > Unser Wein G&U OG
> > Kirchengasse 13/7, 1070 Wien
> >
> > mobil: +43 (6991) 971 32 96
> > tel: +43 (1) 971 32 95
> > e-mail: bernhard@unserwein.at
> > twitter: @bernharduw <http://twitter.com/bernharduw>
> > web: www.unserwein.at
>



-- 

Bernhard Gschwantner
Unser Wein G&U OG
Kirchengasse 13/7, 1070 Wien

mobil: +43 (6991) 971 32 96
tel: +43 (1) 971 32 95
e-mail: bernhard@unserwein.at
twitter: @bernharduw <http://twitter.com/bernharduw>
web: www.unserwein.at

Re: Redirects from _list functions in CouchDB 1.4

Posted by Alexander Shorin <kx...@gmail.com>.
Hi Bernhard,

Sorry, I'd to check my code instead of post it blindly. And yes this
looks as bug for me.

To fix this you have to return any value from list function like:

function(head, req) {
  start({"code": 301, "headers": {"Location": "http://google.com"}});
  return 'redirecting...';
}

this _any_ value may be also empty string, but not null. return ''; also fixes.

--
,,,^..^,,,


On Wed, Sep 18, 2013 at 5:04 PM, Bernhard Gschwantner
<be...@unserwein.at> wrote:
> Hi Alexander,
>
> Thanks a lot for the detailled answer! I completely understand what you're
> saying. I've been following the JIRA issues COUCHDB-430, COUCHDB-514 etc.
> for years, since this redirect topic was one of the first obstacles I ran
> into when starting to use CouchDB.
>
> Nevertheless I still don't get a valid redirect, even with start(). It's a
> bit embarrassing that I can't get a simple _list function right... ;-)
>
>
> I put this design doc into a new database "redirect":
>
>
> {
>    "_id": "_design/redirect",
>     "views": {
>        "all": {
>            "map": "function(doc) {\n\temit(doc._id, null);\n}"
>        }
>    },
>    "lists": {
>        "redirect": "function(head, req) {\n\tstart({\n\t\t\"code\":
> 302,\n\t\t\"headers\": {\n\t\t\t\"Location\": \"http://www.google.com
> \"\n\t\t}\n\t});\n}"
>    }
> }
>
>
> The couchdb output when calling
> http://localhost:5984/redirect/_design/test/_list/redirect/all
>
> {"error":"unknown_error","reason":"undef"}
>
>
> This is the couchdb.log output (level INFO):
>
>
> [error] [<0.20382.0>] Uncaught error in HTTP request: {error,undef}
> [info] [<0.20382.0>] Stacktrace: [{undefined,write_chunk,[[]],[]},
>                                   {couch_httpd,last_chunk,1,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
>                                     {line,692}]},
>                                   {couch_mrview_show,list_cb,2,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview_show.erl"},
>                                     {line,254}]},
>                                   {couch_mrview,finish_fold,2,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview.erl"},
>                                     {line,339}]},
>                                   {couch_query_servers,with_ddoc_proc,2,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_query_servers.erl"},
>                                     {line,269}]},
>                                   {couch_httpd,etag_maybe,2,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
>                                     {line,600}]},
>                                   {couch_httpd_db,do_db_req,2,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd_db.erl"},
>                                     {line,234}]},
>                                   {couch_httpd,handle_request_int,5,
>                                    [{file,
>
>  "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
> 2Fgit-wip-us.apache.org
> %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
>                                     {line,332}]}]
> [info] [<0.20382.0>] 127.0.0.1 - - GET
> /redirect/_design/redirect/_list/redirect/all 500
> [error] [<0.20382.0>] httpd 500 error response:
>  {"error":"unknown_error","reason":"undef"}
>
>
>
> Is this a bug, or am I still missing something?
>
> Regards,
> Bernhard
>
>
>
> 2013/9/18 Alexander Shorin <kx...@gmail.com>
>
>> Hi Bernhard,
>>
>> you need to send redirect with start() function call.
>>
>> function(head, req) {
>>   start({"code": 301, "headers": {"Location": "http://google.com"}});
>> }
>>
>> http://docs.couchdb.org/en/1.4.x/query-servers.html
>>
>> Why so? start() function allows to send custom HTTP headers to the
>> client before any response (or with first chunk of it). When you
>> return or send() something before or without start() call, query
>> server implicitly called it with empty object payload which means that
>> CouchDB will send default HTTP status code and headers. Since CouchDB
>> sends response of list functions as chunked transfer, it doesn't
>> buffers all of it to let you change HTTP response status or headers
>> somewhere in the middle - what's why you need to define them in first
>> place and start() function explicitly points on this.
>>
>> For show functions your code will work since they produces only one
>> response object which contains response body and optionally may have
>> HTTP status and headers. With a little adaptation, the same is true
>> for update functions too.
>>
>> --
>> ,,,^..^,,,
>>
>>
>> On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner
>> <be...@unserwein.at> wrote:
>> > Hello,
>> >
>> > I tried to redirect from a _list function without success. This code
>> throws
>> > a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
>> >
>> > function(head, req) {
>> >   return {
>> >     code : 301,
>> >     headers : {
>> >       Location : 'http://www.google.com'
>> >     }
>> >   };
>> > }
>> >
>> > Is there something I am doing wrong or missing?
>> >
>> > The real-world use case is that we have several aliases to each document
>> in
>> > the db, and all those aliases should be redirected to the real document
>> id.
>> > We use a view to emit the aliases and a list function to redirect all
>> those
>> > aliases to the real url.
>> >
>> > We've been working around the previous issues with modifying headers
>> after
>> > getRow() by redirecting through JavaScript, but want to get rid of this
>> > workaround eventually.
>> >
>> > Regards,
>> > Bernhard
>> >
>> > --
>> >
>> > Bernhard Gschwantner
>> > Unser Wein G&U OG
>> > Kirchengasse 13/7, 1070 Wien
>> >
>> > mobil: +43 (6991) 971 32 96
>> > tel: +43 (1) 971 32 95
>> > e-mail: bernhard@unserwein.at
>> > twitter: @bernharduw <http://twitter.com/bernharduw>
>> > web: www.unserwein.at
>>
>
>
>
> --
>
> Bernhard Gschwantner
> Unser Wein G&U OG
> Kirchengasse 13/7, 1070 Wien
>
> mobil: +43 (6991) 971 32 96
> tel: +43 (1) 971 32 95
> e-mail: bernhard@unserwein.at
> twitter: @bernharduw <http://twitter.com/bernharduw>
> web: www.unserwein.at

Re: Redirects from _list functions in CouchDB 1.4

Posted by Bernhard Gschwantner <be...@unserwein.at>.
Hi Alexander,

Thanks a lot for the detailled answer! I completely understand what you're
saying. I've been following the JIRA issues COUCHDB-430, COUCHDB-514 etc.
for years, since this redirect topic was one of the first obstacles I ran
into when starting to use CouchDB.

Nevertheless I still don't get a valid redirect, even with start(). It's a
bit embarrassing that I can't get a simple _list function right... ;-)


I put this design doc into a new database "redirect":


{
   "_id": "_design/redirect",
    "views": {
       "all": {
           "map": "function(doc) {\n\temit(doc._id, null);\n}"
       }
   },
   "lists": {
       "redirect": "function(head, req) {\n\tstart({\n\t\t\"code\":
302,\n\t\t\"headers\": {\n\t\t\t\"Location\": \"http://www.google.com
\"\n\t\t}\n\t});\n}"
   }
}


The couchdb output when calling
http://localhost:5984/redirect/_design/test/_list/redirect/all

{"error":"unknown_error","reason":"undef"}


This is the couchdb.log output (level INFO):


[error] [<0.20382.0>] Uncaught error in HTTP request: {error,undef}
[info] [<0.20382.0>] Stacktrace: [{undefined,write_chunk,[[]],[]},
                                  {couch_httpd,last_chunk,1,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
                                    {line,692}]},
                                  {couch_mrview_show,list_cb,2,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview_show.erl"},
                                    {line,254}]},
                                  {couch_mrview,finish_fold,2,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview.erl"},
                                    {line,339}]},
                                  {couch_query_servers,with_ddoc_proc,2,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_query_servers.erl"},
                                    {line,269}]},
                                  {couch_httpd,etag_maybe,2,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
                                    {line,600}]},
                                  {couch_httpd_db,do_db_req,2,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd_db.erl"},
                                    {line,234}]},
                                  {couch_httpd,handle_request_int,5,
                                   [{file,

 "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F%
2Fgit-wip-us.apache.org
%2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"},
                                    {line,332}]}]
[info] [<0.20382.0>] 127.0.0.1 - - GET
/redirect/_design/redirect/_list/redirect/all 500
[error] [<0.20382.0>] httpd 500 error response:
 {"error":"unknown_error","reason":"undef"}



Is this a bug, or am I still missing something?

Regards,
Bernhard



2013/9/18 Alexander Shorin <kx...@gmail.com>

> Hi Bernhard,
>
> you need to send redirect with start() function call.
>
> function(head, req) {
>   start({"code": 301, "headers": {"Location": "http://google.com"}});
> }
>
> http://docs.couchdb.org/en/1.4.x/query-servers.html
>
> Why so? start() function allows to send custom HTTP headers to the
> client before any response (or with first chunk of it). When you
> return or send() something before or without start() call, query
> server implicitly called it with empty object payload which means that
> CouchDB will send default HTTP status code and headers. Since CouchDB
> sends response of list functions as chunked transfer, it doesn't
> buffers all of it to let you change HTTP response status or headers
> somewhere in the middle - what's why you need to define them in first
> place and start() function explicitly points on this.
>
> For show functions your code will work since they produces only one
> response object which contains response body and optionally may have
> HTTP status and headers. With a little adaptation, the same is true
> for update functions too.
>
> --
> ,,,^..^,,,
>
>
> On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner
> <be...@unserwein.at> wrote:
> > Hello,
> >
> > I tried to redirect from a _list function without success. This code
> throws
> > a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
> >
> > function(head, req) {
> >   return {
> >     code : 301,
> >     headers : {
> >       Location : 'http://www.google.com'
> >     }
> >   };
> > }
> >
> > Is there something I am doing wrong or missing?
> >
> > The real-world use case is that we have several aliases to each document
> in
> > the db, and all those aliases should be redirected to the real document
> id.
> > We use a view to emit the aliases and a list function to redirect all
> those
> > aliases to the real url.
> >
> > We've been working around the previous issues with modifying headers
> after
> > getRow() by redirecting through JavaScript, but want to get rid of this
> > workaround eventually.
> >
> > Regards,
> > Bernhard
> >
> > --
> >
> > Bernhard Gschwantner
> > Unser Wein G&U OG
> > Kirchengasse 13/7, 1070 Wien
> >
> > mobil: +43 (6991) 971 32 96
> > tel: +43 (1) 971 32 95
> > e-mail: bernhard@unserwein.at
> > twitter: @bernharduw <http://twitter.com/bernharduw>
> > web: www.unserwein.at
>



-- 

Bernhard Gschwantner
Unser Wein G&U OG
Kirchengasse 13/7, 1070 Wien

mobil: +43 (6991) 971 32 96
tel: +43 (1) 971 32 95
e-mail: bernhard@unserwein.at
twitter: @bernharduw <http://twitter.com/bernharduw>
web: www.unserwein.at

Re: Redirects from _list functions in CouchDB 1.4

Posted by Alexander Shorin <kx...@gmail.com>.
Hi Bernhard,

you need to send redirect with start() function call.

function(head, req) {
  start({"code": 301, "headers": {"Location": "http://google.com"}});
}

http://docs.couchdb.org/en/1.4.x/query-servers.html

Why so? start() function allows to send custom HTTP headers to the
client before any response (or with first chunk of it). When you
return or send() something before or without start() call, query
server implicitly called it with empty object payload which means that
CouchDB will send default HTTP status code and headers. Since CouchDB
sends response of list functions as chunked transfer, it doesn't
buffers all of it to let you change HTTP response status or headers
somewhere in the middle - what's why you need to define them in first
place and start() function explicitly points on this.

For show functions your code will work since they produces only one
response object which contains response body and optionally may have
HTTP status and headers. With a little adaptation, the same is true
for update functions too.

--
,,,^..^,,,


On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner
<be...@unserwein.at> wrote:
> Hello,
>
> I tried to redirect from a _list function without success. This code throws
> a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
>
> function(head, req) {
>   return {
>     code : 301,
>     headers : {
>       Location : 'http://www.google.com'
>     }
>   };
> }
>
> Is there something I am doing wrong or missing?
>
> The real-world use case is that we have several aliases to each document in
> the db, and all those aliases should be redirected to the real document id.
> We use a view to emit the aliases and a list function to redirect all those
> aliases to the real url.
>
> We've been working around the previous issues with modifying headers after
> getRow() by redirecting through JavaScript, but want to get rid of this
> workaround eventually.
>
> Regards,
> Bernhard
>
> --
>
> Bernhard Gschwantner
> Unser Wein G&U OG
> Kirchengasse 13/7, 1070 Wien
>
> mobil: +43 (6991) 971 32 96
> tel: +43 (1) 971 32 95
> e-mail: bernhard@unserwein.at
> twitter: @bernharduw <http://twitter.com/bernharduw>
> web: www.unserwein.at