From fc90ac341c154eac58780d8040263cb3df62512d Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Fri, 6 May 2011 15:39:46 +0000 Subject: COUCHDB-760 - allow utf-8 in attachment names (patch by davisp/benoitc) git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1100254 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/test/attachment_names.js | 41 ++++++++++++++++++++----------- src/couchdb/couch_httpd_db.erl | 28 +-------------------- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/share/www/script/test/attachment_names.js b/share/www/script/test/attachment_names.js index 988dd2d2..777b5ece 100644 --- a/share/www/script/test/attachment_names.js +++ b/share/www/script/test/attachment_names.js @@ -16,6 +16,24 @@ couchTests.attachment_names = function(debug) { db.createDb(); if (debug) debugger; + var goodDoc = { + _id: "good_doc", + _attachments: { + "Колян.txt": { + content_type:"text/plain", + data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=" + } + } + }; + + var save_response = db.save(goodDoc); + T(save_response.ok); + + var xhr = CouchDB.request("GET", "/test_suite_db/good_doc/Колян.txt"); + T(xhr.responseText == "This is a base64 encoded text"); + T(xhr.getResponseHeader("Content-Type") == "text/plain"); + T(xhr.getResponseHeader("Etag") == '"' + save_response.rev + '"'); + var binAttDoc = { _id: "bin_doc", _attachments:{ @@ -27,28 +45,23 @@ couchTests.attachment_names = function(debug) { }; // inline attachments - try { - db.save(binAttDoc); - TEquals(1, 2, "Attachment name with non UTF-8 encoding saved. Should never show!"); - } catch (e) { - TEquals("bad_request", e.error, "attachment_name: inline attachments"); - TEquals("Attachment name is not UTF-8 encoded", e.reason, "attachment_name: inline attachments"); - } + resp = db.save(binAttDoc); + TEquals(true, resp.ok, "attachment_name: inline attachment"); // standalone docs var bin_data = "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])} ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np"; + var xhr = (CouchDB.request("PUT", "/test_suite_db/bin_doc3/attachment\x80txt", { headers:{"Content-Type":"text/plain;charset=utf-8"}, body:bin_data })); var resp = JSON.parse(xhr.responseText); - TEquals(400, xhr.status, "attachment_name: standalone API"); - TEquals("bad_request", resp.error, "attachment_name: standalone API"); - TEquals("Attachment name is not UTF-8 encoded", resp.reason, "attachment_name: standalone API"); - + TEquals(201, xhr.status, "attachment_name: standalone API"); + TEquals("Created", xhr.statusText, "attachment_name: standalone API"); + TEquals(true, resp.ok, "attachment_name: standalone API"); // bulk docs var docs = { docs: [binAttDoc] }; @@ -57,10 +70,8 @@ couchTests.attachment_names = function(debug) { body: JSON.stringify(docs) }); - var resp = JSON.parse(xhr.responseText); - TEquals(400, xhr.status, "attachment_name: bulk docs"); - TEquals("bad_request", resp.error, "attachment_name: bulk docs"); - TEquals("Attachment name is not UTF-8 encoded", resp.reason, "attachment_name: bulk docs"); + TEquals(201, xhr.status, "attachment_name: bulk docs"); + TEquals("Created", xhr.statusText, "attachment_name: bulk docs"); // leading underscores diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl index 0b8f891b..0dbebb6e 100644 --- a/src/couchdb/couch_httpd_db.erl +++ b/src/couchdb/couch_httpd_db.erl @@ -1276,34 +1276,8 @@ validate_attachment_name(Name) when is_list(Name) -> validate_attachment_name(<<"_",_/binary>>) -> throw({bad_request, <<"Attachment name can't start with '_'">>}); validate_attachment_name(Name) -> - case is_valid_utf8(Name) of + case couch_util:validate_utf8(Name) of true -> Name; false -> throw({bad_request, <<"Attachment name is not UTF-8 encoded">>}) end. -%% borrowed from mochijson2:json_bin_is_safe() -is_valid_utf8(<<>>) -> - true; -is_valid_utf8(<>) -> - case C of - $\" -> - false; - $\\ -> - false; - $\b -> - false; - $\f -> - false; - $\n -> - false; - $\r -> - false; - $\t -> - false; - C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF -> - false; - C when C < 16#7f -> - is_valid_utf8(Rest); - _ -> - false - end. -- cgit v1.2.3