You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Robert Newson <ro...@gmail.com> on 2010/03/11 04:47:07 UTC

Content-Type Negotiation/Programmatic URL rewriting

All,

I want to retrieve the JSON document or a specific binary attachment
of it from the same URL, conditional on the Accept header. That is,
"/db/doc" is either JSON metadata or a binary stream, depending on
content-type negotiation. The below rewrite enhancement would achieve
it, I think, but perhaps there's a cleaner way? Right now, I'm adding
another Erlang module to achieve this but I would really like to do it
entirely from a design document.

I'm thinking of writing a new kind of rewrite handler. I have a case
where it would be very useful to rewrite a URL based on the contents
of a request header. That is, the presence of a particular header will
alter the path, but not otherwise;

{
   "_id":"_design/foo",
  "rewrites": {
    "bar": {
      "function":"function(req){ if (req.headers["Accept"] == blah) {
return req.path + "extra";} return req.path"

that is, the rewrite handler would get a full request object and
return the rewritten path, and could use anything from the request to
do so.

Thoughts?
B.

Re: Content-Type Negotiation/Programmatic URL rewriting

Posted by Robert Newson <ro...@gmail.com>.
adding "headers" to pattern matching achieves my goal and seems simple
enough. I'll work on patch.

In other news, I'm at the 10gen nosql conference right now. :)

B.

On Thu, Mar 11, 2010 at 12:17 AM, Jan Lehnardt <ja...@apache.org> wrote:
>
> On 10 Mar 2010, at 21:07, J Chris Anderson wrote:
>
>>
>> On Mar 10, 2010, at 7:47 PM, Robert Newson wrote:
>>
>>> All,
>>>
>>> I want to retrieve the JSON document or a specific binary attachment
>>> of it from the same URL, conditional on the Accept header. That is,
>>> "/db/doc" is either JSON metadata or a binary stream, depending on
>>> content-type negotiation. The below rewrite enhancement would achieve
>>> it, I think, but perhaps there's a cleaner way? Right now, I'm adding
>>> another Erlang module to achieve this but I would really like to do it
>>> entirely from a design document.
>>>
>>> I'm thinking of writing a new kind of rewrite handler. I have a case
>>> where it would be very useful to rewrite a URL based on the contents
>>> of a request header. That is, the presence of a particular header will
>>> alter the path, but not otherwise;
>>>
>>> {
>>>  "_id":"_design/foo",
>>> "rewrites": {
>>>   "bar": {
>>>     "function":"function(req){ if (req.headers["Accept"] == blah) {
>>> return req.path + "extra";} return req.path"
>>>
>>> that is, the rewrite handler would get a full request object and
>>> return the rewritten path, and could use anything from the request to
>>> do so.
>>>
>>
>> we originally decided against an executable rewrite handler for reasons of performance.
>
> One could argue that optional executable rewrites are okay. Especially when written as
> Erlang functions.
>
>
>> is there a way to add to the pattern matching api to achieve this?
>
> "from": {
>  "path":"/db/:doc",
>  "headers":{"Accept":"application/octet-stream"}
> },
> "to":"/db/doc/attachment"
>
> "from": {
>  "path":"/db/:doc",
>  "headers":{"Accept":"application/json"}
> },
> "to":"/db/doc"
>
> Could do the trick, but I don't want to be the one implementing that :)
>
> Cheers
> Jan
> --
>
>

Re: Content-Type Negotiation/Programmatic URL rewriting

Posted by Jan Lehnardt <ja...@apache.org>.
On 10 Mar 2010, at 21:07, J Chris Anderson wrote:

> 
> On Mar 10, 2010, at 7:47 PM, Robert Newson wrote:
> 
>> All,
>> 
>> I want to retrieve the JSON document or a specific binary attachment
>> of it from the same URL, conditional on the Accept header. That is,
>> "/db/doc" is either JSON metadata or a binary stream, depending on
>> content-type negotiation. The below rewrite enhancement would achieve
>> it, I think, but perhaps there's a cleaner way? Right now, I'm adding
>> another Erlang module to achieve this but I would really like to do it
>> entirely from a design document.
>> 
>> I'm thinking of writing a new kind of rewrite handler. I have a case
>> where it would be very useful to rewrite a URL based on the contents
>> of a request header. That is, the presence of a particular header will
>> alter the path, but not otherwise;
>> 
>> {
>>  "_id":"_design/foo",
>> "rewrites": {
>>   "bar": {
>>     "function":"function(req){ if (req.headers["Accept"] == blah) {
>> return req.path + "extra";} return req.path"
>> 
>> that is, the rewrite handler would get a full request object and
>> return the rewritten path, and could use anything from the request to
>> do so.
>> 
> 
> we originally decided against an executable rewrite handler for reasons of performance.

One could argue that optional executable rewrites are okay. Especially when written as
Erlang functions.


> is there a way to add to the pattern matching api to achieve this?

"from": {
  "path":"/db/:doc",
  "headers":{"Accept":"application/octet-stream"}
},
"to":"/db/doc/attachment"

"from": {
  "path":"/db/:doc",
  "headers":{"Accept":"application/json"}
},
"to":"/db/doc"

Could do the trick, but I don't want to be the one implementing that :)

Cheers
Jan
--


Re: Content-Type Negotiation/Programmatic URL rewriting

Posted by J Chris Anderson <jc...@couch.io>.
On Mar 10, 2010, at 7:47 PM, Robert Newson wrote:

> All,
> 
> I want to retrieve the JSON document or a specific binary attachment
> of it from the same URL, conditional on the Accept header. That is,
> "/db/doc" is either JSON metadata or a binary stream, depending on
> content-type negotiation. The below rewrite enhancement would achieve
> it, I think, but perhaps there's a cleaner way? Right now, I'm adding
> another Erlang module to achieve this but I would really like to do it
> entirely from a design document.
> 
> I'm thinking of writing a new kind of rewrite handler. I have a case
> where it would be very useful to rewrite a URL based on the contents
> of a request header. That is, the presence of a particular header will
> alter the path, but not otherwise;
> 
> {
>   "_id":"_design/foo",
>  "rewrites": {
>    "bar": {
>      "function":"function(req){ if (req.headers["Accept"] == blah) {
> return req.path + "extra";} return req.path"
> 
> that is, the rewrite handler would get a full request object and
> return the rewritten path, and could use anything from the request to
> do so.
> 

we originally decided against an executable rewrite handler for reasons of performance.

is there a way to add to the pattern matching api to achieve this?

Chris

> Thoughts?
> B.