diff options
author | Paul Joseph Davis <davisp@apache.org> | 2009-04-18 18:34:31 +0000 |
---|---|---|
committer | Paul Joseph Davis <davisp@apache.org> | 2009-04-18 18:34:31 +0000 |
commit | daa9d65a53dedaac5aeeb6394d4c0b6f99fa930c (patch) | |
tree | 71a35ed91de64d76b29a44776a0d6d93efc5e75a /src | |
parent | 1df7a4e6a659550cde173381a0e0f9d770417d59 (diff) |
Resolves COUCHDB-306 - Wacky error responses to malformed documents
Mostly adds improvements to the parsing of Json bodies for _bulk_docs and
multi-get queries. Includes tests in basics.js and view_errors.js.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@766373 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r-- | src/couchdb/couch_httpd_db.erl | 25 | ||||
-rw-r--r-- | src/couchdb/couch_httpd_view.erl | 18 |
2 files changed, 36 insertions, 7 deletions
diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl index 209dc75e..bacce4ae 100644 --- a/src/couchdb/couch_httpd_db.erl +++ b/src/couchdb/couch_httpd_db.erl @@ -126,7 +126,13 @@ db_req(#httpd{path_parts=[_,<<"_ensure_full_commit">>]}=Req, _Db) -> db_req(#httpd{method='POST',path_parts=[_,<<"_bulk_docs">>]}=Req, Db) -> couch_stats_collector:increment({httpd, bulk_requests}), - {JsonProps} = couch_httpd:json_body(Req), + JsonProps = + case couch_httpd:json_body(Req) of + {Fields} -> + Fields; + _ -> + throw({bad_request, "Body must be a JSON object"}) + end, DocsArray = proplists:get_value(<<"docs">>, JsonProps), case couch_httpd:header_value(Req, "X-Couch-Full-Commit", "false") of "true" -> @@ -220,9 +226,20 @@ db_req(#httpd{method='GET',path_parts=[_,<<"_all_docs">>]}=Req, Db) -> all_docs_view(Req, Db, nil); db_req(#httpd{method='POST',path_parts=[_,<<"_all_docs">>]}=Req, Db) -> - {Props} = couch_httpd:json_body(Req), - Keys = proplists:get_value(<<"keys">>, Props, nil), - all_docs_view(Req, Db, Keys); + case couch_httpd:json_body(Req) of + {Fields} -> + case proplists:get_value(<<"keys">>, Fields, nil) of + nil -> + ?LOG_DEBUG("POST to _all_docs with no keys member.", []), + all_docs_view(Req, Db, nil); + Keys when is_list(Keys) -> + all_docs_view(Req, Db, Keys); + _ -> + throw({bad_request, "`keys` member must be a array."}) + end; + _ -> + throw({bad_request, "Body must be a JSON object"}) + end; db_req(#httpd{path_parts=[_,<<"_all_docs">>]}=Req, _Db) -> send_method_not_allowed(Req, "GET,HEAD,POST"); diff --git a/src/couchdb/couch_httpd_view.erl b/src/couchdb/couch_httpd_view.erl index 4054eb05..0348cf8b 100644 --- a/src/couchdb/couch_httpd_view.erl +++ b/src/couchdb/couch_httpd_view.erl @@ -56,9 +56,21 @@ handle_view_req(#httpd{method='GET', handle_view_req(#httpd{method='POST', path_parts=[_Db, _Design, DName, _View, ViewName]}=Req, Db) -> - {Props} = couch_httpd:json_body(Req), - Keys = proplists:get_value(<<"keys">>, Props, nil), - design_doc_view(Req, Db, DName, ViewName, Keys); + case couch_httpd:json_body(Req) of + {Fields} -> + case proplists:get_value(<<"keys">>, Fields, nil) of + nil -> + Fmt = "POST to view ~p/~p in database ~p with no keys member.", + ?LOG_DEBUG(Fmt, [DName, ViewName, Db]), + design_doc_view(Req, Db, DName, ViewName, nil); + Keys when is_list(Keys) -> + design_doc_view(Req, Db, DName, ViewName, Keys); + _ -> + throw({bad_request, "`keys` member must be a array."}) + end; + _ -> + throw({bad_request, "Body must be a JSON object"}) + end; handle_view_req(Req, _Db) -> send_method_not_allowed(Req, "GET,POST,HEAD"). |