Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Support the keys parameter in GET requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Aug 2, 2012
1 parent 859c23c commit dba3edd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/chttpd.erl
Expand Up @@ -16,7 +16,7 @@
-export([start_link/0, start_link/1, start_link/2,
stop/0, handle_request/1, config_change/2,
primary_header_value/2, header_value/2, header_value/3, qs_value/2,
qs_value/3, qs/1, path/1, absolute_uri/2, body_length/1,
qs_value/3, qs/1, qs_json_value/3, path/1, absolute_uri/2, body_length/1,
verify_is_server_admin/1, unquote/1, quote/1, recv/2, recv_chunked/4,
error_info/1, parse_form/1, json_body/1, json_body_obj/1, body/1,
doc_etag/1, make_etag/1, etag_respond/3, partition/1, serve_file/3,
Expand Down Expand Up @@ -382,6 +382,14 @@ qs_value(Req, Key) ->
qs_value(Req, Key, Default) ->
couch_util:get_value(Key, qs(Req), Default).

qs_json_value(Req, Key, Default) ->
case qs_value(Req, Key, Default) of
Default ->
Default;
Result ->
?JSON_DECODE(Result)
end.

qs(#httpd{mochi_req=MochiReq}) ->
MochiReq:parse_qs().

Expand Down
9 changes: 8 additions & 1 deletion src/chttpd_db.erl
Expand Up @@ -355,7 +355,14 @@ db_req(#httpd{path_parts=[_,<<"_purge">>]}=Req, _Db) ->
send_method_not_allowed(Req, "POST");

db_req(#httpd{method='GET',path_parts=[_,<<"_all_docs">>]}=Req, Db) ->
all_docs_view(Req, Db, nil);
case chttpd:qs_json_value(Req, "keys", nil) of
Keys when is_list(Keys) ->
all_docs_view(Req, Db, Keys);
nil ->
all_docs_view(Req, Db, nil);
_ ->
throw({bad_request, "`keys` parameter must be an array."})
end;

db_req(#httpd{method='POST',path_parts=[_,<<"_all_docs">>]}=Req, Db) ->
{Fields} = chttpd:json_body_obj(Req),
Expand Down
6 changes: 4 additions & 2 deletions src/chttpd_show.erl
Expand Up @@ -148,12 +148,14 @@ send_doc_update_response(Req, Db, DDoc, UpdateName, Doc, DocId) ->
% view-list request with view and list from same design doc.
handle_view_list_req(#httpd{method='GET',
path_parts=[_, _, DesignName, _, ListName, ViewName]}=Req, Db, DDoc) ->
handle_view_list(Req, Db, DDoc, ListName, {DesignName, ViewName}, nil);
Keys = chttpd:qs_json_value(Req, "keys", nil),
handle_view_list(Req, Db, DDoc, ListName, {DesignName, ViewName}, Keys);

% view-list request with view and list from different design docs.
handle_view_list_req(#httpd{method='GET',
path_parts=[_, _, _, _, ListName, DesignName, ViewName]}=Req, Db, DDoc) ->
handle_view_list(Req, Db, DDoc, ListName, {DesignName, ViewName}, nil);
Keys = chttpd:qs_json_value(Req, "keys", nil),
handle_view_list(Req, Db, DDoc, ListName, {DesignName, ViewName}, Keys);

handle_view_list_req(#httpd{method='GET'}=Req, _Db, _DDoc) ->
chttpd:send_error(Req, 404, <<"list_error">>, <<"Invalid path.">>);
Expand Down
3 changes: 2 additions & 1 deletion src/chttpd_view.erl
Expand Up @@ -101,7 +101,8 @@ extract_view_type(ViewName, [View|Rest], IsReduce) ->

handle_view_req(#httpd{method='GET',
path_parts=[_, _, _, _, ViewName]}=Req, Db, DDoc) ->
design_doc_view(Req, Db, DDoc, ViewName, nil);
Keys = couch_httpd:qs_json_value(Req, "keys", nil),
design_doc_view(Req, Db, DDoc, ViewName, Keys);

handle_view_req(#httpd{method='POST',
path_parts=[_, _, _, _, ViewName]}=Req, Db, DDoc) ->
Expand Down

0 comments on commit dba3edd

Please sign in to comment.