Parcourir la source

HTTP-header-safe JSON for dropbox

address #1370
Yonah Forst il y a 6 ans
Parent
commit
abb719da2f
1 fichiers modifiés avec 16 ajouts et 2 suppressions
  1. 16 2
      packages/@uppy/companion/src/server/provider/dropbox/index.js

+ 16 - 2
packages/@uppy/companion/src/server/provider/dropbox/index.js

@@ -3,6 +3,20 @@ const purest = require('purest')({ request })
 const logger = require('../../logger')
 const adapter = require('./adapter')
 
+
+// From https://www.dropbox.com/developers/reference/json-encoding:
+//
+// This function is simple and has OK performance compared to more
+// complicated ones: http://jsperf.com/json-escape-unicode/4
+const charsToEncode = /[\u007f-\uffff]/g;
+function http_header_safe_json(v) {
+  return JSON.stringify(v).replace(charsToEncode,
+    function(c) {
+      return '\\u'+('000'+c.charCodeAt(0).toString(16)).slice(-4);
+    }
+  );
+}
+
 class DropBox {
   constructor (options) {
     this.authProvider = options.provider = DropBox.authProvider
@@ -83,7 +97,7 @@ class DropBox {
       .options({
         version: '2',
         headers: {
-          'Dropbox-API-Arg': JSON.stringify({path: `${id}`})
+          'Dropbox-API-Arg': http_header_safe_json({path: `${id}`})
         }
       })
       .auth(token)
@@ -101,7 +115,7 @@ class DropBox {
       .options({
         version: '2',
         headers: {
-          'Dropbox-API-Arg': JSON.stringify({path: `${id}`})
+          'Dropbox-API-Arg': http_header_safe_json({path: `${id}`})
         }
       })
       .auth(token)