You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2011/05/06 17:39:46 UTC

svn commit: r1100254 - in /couchdb/branches/1.1.x: share/www/script/test/attachment_names.js src/couchdb/couch_httpd_db.erl

Author: rnewson
Date: Fri May  6 15:39:46 2011
New Revision: 1100254

URL: http://svn.apache.org/viewvc?rev=1100254&view=rev
Log:
COUCHDB-760 - allow utf-8 in attachment names (patch by davisp/benoitc)

Modified:
    couchdb/branches/1.1.x/share/www/script/test/attachment_names.js
    couchdb/branches/1.1.x/src/couchdb/couch_httpd_db.erl

Modified: couchdb/branches/1.1.x/share/www/script/test/attachment_names.js
URL: http://svn.apache.org/viewvc/couchdb/branches/1.1.x/share/www/script/test/attachment_names.js?rev=1100254&r1=1100253&r2=1100254&view=diff
==============================================================================
--- couchdb/branches/1.1.x/share/www/script/test/attachment_names.js (original)
+++ couchdb/branches/1.1.x/share/www/script/test/attachment_names.js Fri May  6 15:39:46 2011
@@ -16,6 +16,24 @@ couchTests.attachment_names = function(d
   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(d
   };
 
   // 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(d
     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

Modified: couchdb/branches/1.1.x/src/couchdb/couch_httpd_db.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/1.1.x/src/couchdb/couch_httpd_db.erl?rev=1100254&r1=1100253&r2=1100254&view=diff
==============================================================================
--- couchdb/branches/1.1.x/src/couchdb/couch_httpd_db.erl (original)
+++ couchdb/branches/1.1.x/src/couchdb/couch_httpd_db.erl Fri May  6 15:39:46 2011
@@ -1276,34 +1276,8 @@ validate_attachment_name(Name) when is_l
 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(<<C, Rest/binary>>) ->
-    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.