Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B4C1310B80 for ; Thu, 4 Dec 2014 20:09:32 +0000 (UTC) Received: (qmail 61464 invoked by uid 500); 4 Dec 2014 20:09:32 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 61274 invoked by uid 500); 4 Dec 2014 20:09:32 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 61252 invoked by uid 99); 4 Dec 2014 20:09:32 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Dec 2014 20:09:32 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 4E2F8A1C89C; Thu, 4 Dec 2014 20:09:32 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davisp@apache.org To: commits@couchdb.apache.org Date: Thu, 04 Dec 2014 20:09:33 -0000 Message-Id: <26682aab73594a77a52aa4194c0b067d@git.apache.org> In-Reply-To: <917542aba84049c581c97078eb27577a@git.apache.org> References: <917542aba84049c581c97078eb27577a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [02/22] couch commit: updated refs/heads/2491-refactor-couch-httpd-auth to 3e8286d Fix header for documents with newlines in the name Properly urlencode the Document-Id in the Location field of the header Based on a patch from Sean Bartell Added some eunit-tests COUCHDB-708 Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/9ee298ff Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/9ee298ff Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/9ee298ff Branch: refs/heads/2491-refactor-couch-httpd-auth Commit: 9ee298ff0af335f7449eca0f839caf0c5646887a Parents: f02a101 Author: Robert Kowalski Authored: Sat Oct 25 03:16:38 2014 +0200 Committer: Robert Kowalski Committed: Thu Oct 30 15:20:32 2014 +0100 ---------------------------------------------------------------------- src/couch_httpd_db.erl | 5 +- test/couchdb_location_header_tests.erl | 78 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/9ee298ff/src/couch_httpd_db.erl ---------------------------------------------------------------------- diff --git a/src/couch_httpd_db.erl b/src/couch_httpd_db.erl index 13e2c44..4ff4d98 100644 --- a/src/couch_httpd_db.erl +++ b/src/couch_httpd_db.erl @@ -721,7 +721,7 @@ update_doc_result_to_json(DocId, Error) -> update_doc(Req, Db, DocId, #doc{deleted=false}=Doc) -> - Loc = absolute_uri(Req, "/" ++ ?b2l(Db#db.name) ++ "/" ++ ?b2l(DocId)), + Loc = absolute_uri(Req, "/" ++ ?b2l(Db#db.name) ++ "/" ++ couch_util:url_encode(DocId)), update_doc(Req, Db, DocId, Doc, [{"Location", Loc}]); update_doc(Req, Db, DocId, Doc) -> update_doc(Req, Db, DocId, Doc, []). @@ -1022,7 +1022,7 @@ db_attachment_req(#httpd{method=Method,mochi_req=MochiReq}=Req, Db, DocId, FileN _ -> [{"Location", absolute_uri(Req, "/" ++ ?b2l(Db#db.name) ++ "/" ++ - ?b2l(DocId) ++ "/" ++ + couch_util:url_encode(DocId) ++ "/" ++ ?b2l(FileName) )}] end, @@ -1233,4 +1233,3 @@ validate_attachment_name(Name) -> true -> Name; false -> throw({bad_request, <<"Attachment name is not UTF-8 encoded">>}) end. - http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/9ee298ff/test/couchdb_location_header_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb_location_header_tests.erl b/test/couchdb_location_header_tests.erl new file mode 100644 index 0000000..74d1281 --- /dev/null +++ b/test/couchdb_location_header_tests.erl @@ -0,0 +1,78 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(couchdb_location_header_tests). + +-include_lib("couch/include/couch_eunit.hrl"). +-include_lib("couch/include/couch_db.hrl"). + +-define(TIMEOUT, 1000). + + +setup() -> + DbName = ?tempdb(), + {ok, Db} = couch_db:create(DbName, [?ADMIN_USER]), + couch_db:close(Db), + + Addr = config:get("httpd", "bind_address", "127.0.0.1"), + Port = integer_to_list(mochiweb_socket_server:get(couch_httpd, port)), + Host = "http://" ++ Addr ++ ":" ++ Port, + {Host, ?b2l(DbName)}. + +teardown({_, DbName}) -> + ok = couch_server:delete(?l2b(DbName), [?ADMIN_USER]), + ok. + + +header_test_() -> + { + "CouchDB Location Header Tests", + { + setup, + fun test_util:start_couch/0, fun test_util:stop_couch/1, + { + foreach, + fun setup/0, fun teardown/1, + [ + fun should_work_with_newlines_in_docs/1, + fun should_work_with_newlines_in_attachments/1 + ] + } + } + }. + +should_work_with_newlines_in_docs({Host, DbName}) -> + Url = Host ++ "/" ++ DbName ++ "/docid%0A", + {"COUCHDB-708", + ?_assertEqual( + Url, + begin + {ok, _, Headers, _} = test_request:put(Url, + [{"Content-Type", "application/json"}], "{}"), + proplists:get_value("Location", Headers) + end)}. + +should_work_with_newlines_in_attachments({Host, DbName}) -> + Url = Host ++ "/" ++ DbName, + AttUrl = Url ++ "/docid%0A/readme.txt", + {"COUCHDB-708", + ?_assertEqual( + AttUrl, + begin + Body = "We all live in a yellow submarine!", + Headers0 = [ + {"Content-Length", "34"}, + {"Content-Type", "text/plain"} + ], + {ok, _, Headers, _} = test_request:put(AttUrl, Headers0, Body), + proplists:get_value("Location", Headers) + end)}.