summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_httpd_show.erl
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2009-07-10 00:59:36 +0000
committerJohn Christopher Anderson <jchris@apache.org>2009-07-10 00:59:36 +0000
commit9ccb235a2d58d6b7caf406952f18ca13d9889f3e (patch)
tree9a0f6ac06d91d6bd70aee5d6f921665423139653 /src/couchdb/couch_httpd_show.erl
parent2f18f60fea5a1de6a221c6124038399c47d42aa2 (diff)
Apply patch from Benoit Chesneau's COUCHDB-404
Restores 0.8-style /db/_view view urls and adds an option to render views and documents as other formats like: /db/docid?show=blog/post /db/_view/blog/posts?list=index We're retaining the longer _design/appname paths as well because that resource is valuable for reverse proxies and rewriters. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@792771 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_httpd_show.erl')
-rw-r--r--src/couchdb/couch_httpd_show.erl56
1 files changed, 29 insertions, 27 deletions
diff --git a/src/couchdb/couch_httpd_show.erl b/src/couchdb/couch_httpd_show.erl
index c29d89c5..9c873e8f 100644
--- a/src/couchdb/couch_httpd_show.erl
+++ b/src/couchdb/couch_httpd_show.erl
@@ -12,7 +12,8 @@
-module(couch_httpd_show).
--export([handle_doc_show_req/2, handle_view_list_req/2]).
+-export([handle_doc_show_req/2, handle_view_list_req/2,
+ handle_doc_show/5, handle_view_list/6]).
-include("couch_db.hrl").
@@ -26,58 +27,59 @@ handle_doc_show_req(#httpd{
method='GET',
path_parts=[_DbName, _Design, DesignName, _Show, ShowName, DocId]
}=Req, Db) ->
- DesignId = <<"_design/", DesignName/binary>>,
- #doc{body={Props}} = couch_httpd_db:couch_doc_open(Db, DesignId, nil, []),
- Lang = proplists:get_value(<<"language">>, Props, <<"javascript">>),
- ShowSrc = get_nested_json_value({Props}, [<<"shows">>, ShowName]),
- Doc = try couch_httpd_db:couch_doc_open(Db, DocId, nil, [conflicts]) of
- FoundDoc -> FoundDoc
- catch
- _ -> nil
- end,
- send_doc_show_response(Lang, ShowSrc, DocId, Doc, Req, Db);
-
+ handle_doc_show(Req, DesignName, ShowName, DocId, Db);
+
handle_doc_show_req(#httpd{
method='GET',
path_parts=[_DbName, _Design, DesignName, _Show, ShowName]
}=Req, Db) ->
- DesignId = <<"_design/", DesignName/binary>>,
- #doc{body={Props}} = couch_httpd_db:couch_doc_open(Db, DesignId, nil, []),
- Lang = proplists:get_value(<<"language">>, Props, <<"javascript">>),
- ShowSrc = get_nested_json_value({Props}, [<<"shows">>, ShowName]),
- send_doc_show_response(Lang, ShowSrc, nil, nil, Req, Db);
+ handle_doc_show(Req, DesignName, ShowName, nil, Db);
handle_doc_show_req(#httpd{method='GET'}=Req, _Db) ->
send_error(Req, 404, <<"show_error">>, <<"Invalid path.">>);
handle_doc_show_req(Req, _Db) ->
send_method_not_allowed(Req, "GET,HEAD").
-
-handle_view_list_req(#httpd{method='GET',
- path_parts=[_DbName, _Design, DesignName, _List, ListName, ViewName]}=Req, Db) ->
+
+handle_doc_show(Req, DesignName, ShowName, DocId, Db) ->
DesignId = <<"_design/", DesignName/binary>>,
#doc{body={Props}} = couch_httpd_db:couch_doc_open(Db, DesignId, nil, []),
Lang = proplists:get_value(<<"language">>, Props, <<"javascript">>),
- ListSrc = get_nested_json_value({Props}, [<<"lists">>, ListName]),
- send_view_list_response(Lang, ListSrc, ViewName, DesignId, Req, Db, nil);
+ ShowSrc = get_nested_json_value({Props}, [<<"shows">>, ShowName]),
+ Doc = case DocId of
+ nil -> nil;
+ _ ->
+ try couch_httpd_db:couch_doc_open(Db, DocId, nil, [conflicts]) of
+ FoundDoc -> FoundDoc
+ catch
+ _ -> nil
+ end
+ end,
+ send_doc_show_response(Lang, ShowSrc, DocId, Doc, Req, Db).
+
+handle_view_list_req(#httpd{method='GET',
+ path_parts=[_DbName, _Design, DesignName, _List, ListName, ViewName]}=Req, Db) ->
+ handle_view_list(Req, DesignName, ListName, ViewName, Db, nil);
handle_view_list_req(#httpd{method='GET'}=Req, _Db) ->
send_error(Req, 404, <<"list_error">>, <<"Invalid path.">>);
handle_view_list_req(#httpd{method='POST',
path_parts=[_DbName, _Design, DesignName, _List, ListName, ViewName]}=Req, Db) ->
- DesignId = <<"_design/", DesignName/binary>>,
- #doc{body={Props}} = couch_httpd_db:couch_doc_open(Db, DesignId, nil, []),
- Lang = proplists:get_value(<<"language">>, Props, <<"javascript">>),
- ListSrc = get_nested_json_value({Props}, [<<"lists">>, ListName]),
ReqBody = couch_httpd:body(Req),
{Props2} = ?JSON_DECODE(ReqBody),
Keys = proplists:get_value(<<"keys">>, Props2, nil),
- send_view_list_response(Lang, ListSrc, ViewName, DesignId, Req#httpd{req_body=ReqBody}, Db, Keys);
+ handle_view_list(Req#httpd{req_body=ReqBody}, DesignName, ListName, ViewName, Db, Keys);
handle_view_list_req(Req, _Db) ->
send_method_not_allowed(Req, "GET,POST,HEAD").
+handle_view_list(Req, DesignName, ListName, ViewName, Db, Keys) ->
+ DesignId = <<"_design/", DesignName/binary>>,
+ #doc{body={Props}} = couch_httpd_db:couch_doc_open(Db, DesignId, nil, []),
+ Lang = proplists:get_value(<<"language">>, Props, <<"javascript">>),
+ ListSrc = get_nested_json_value({Props}, [<<"lists">>, ListName]),
+ send_view_list_response(Lang, ListSrc, ViewName, DesignId, Req, Db, Keys).
get_nested_json_value({Props}, [Key|Keys]) ->
case proplists:get_value(Key, Props, nil) of