Bläddra i källkod

Add check to XMLHttpRequest.send()

Related to #89 , when the request body is a Blob instance, we should
check if the Blob is created before the request actually send.
Ben Hsieh 8 år sedan
förälder
incheckning
0f43d0d6b3
2 ändrade filer med 19 tillägg och 10 borttagningar
  1. 2
    2
      src/polyfill/Blob.js
  2. 17
    8
      src/polyfill/XMLHttpRequest.js

+ 2
- 2
src/polyfill/Blob.js Visa fil

11
 const log = new Log('Blob')
11
 const log = new Log('Blob')
12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
13
 
13
 
14
-// log.disable()
15
-log.level(3)
14
+log.disable()
15
+// log.level(3)
16
 
16
 
17
 /**
17
 /**
18
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
18
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to

+ 17
- 8
src/polyfill/XMLHttpRequest.js Visa fil

130
    * @param  {any} body Body in RNfetchblob flavor
130
    * @param  {any} body Body in RNfetchblob flavor
131
    */
131
    */
132
   send(body) {
132
   send(body) {
133
-
133
+    this._body = body
134
     if(this._readyState !== XMLHttpRequest.OPENED)
134
     if(this._readyState !== XMLHttpRequest.OPENED)
135
       throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
135
       throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
136
     let promise = Promise.resolve()
136
     let promise = Promise.resolve()
141
     log.verbose(typeof body, body instanceof FormData)
141
     log.verbose(typeof body, body instanceof FormData)
142
 
142
 
143
     if(body instanceof Blob) {
143
     if(body instanceof Blob) {
144
+      log.debug('sending blob body', body._blobCreated)
144
       promise = new Promise((resolve, reject) => {
145
       promise = new Promise((resolve, reject) => {
145
           body.onCreated((blob) => {
146
           body.onCreated((blob) => {
146
-            body = RNFetchBlob.wrap(body.getRNFetchBlobRef())
147
-            resolve()
147
+              log.debug('body created send request')
148
+              body = RNFetchBlob.wrap(blob.getRNFetchBlobRef())
149
+              resolve()
148
           })
150
           })
149
         })
151
         })
150
     }
152
     }
158
     }
160
     }
159
 
161
 
160
     promise.then(() => {
162
     promise.then(() => {
163
+      log.debug('send request invoke', body)
164
+      for(let h in _headers) {
165
+        _headers[h] = _headers[h].toString()
166
+      }
161
       this._task = RNFetchBlob
167
       this._task = RNFetchBlob
162
                     .config({
168
                     .config({
163
                       auto: true,
169
                       auto: true,
224
   }
230
   }
225
 
231
 
226
   getResponseHeader(field:string):string | null {
232
   getResponseHeader(field:string):string | null {
227
-    log.verbose('XMLHttpRequest get header', field)
233
+    log.verbose('XMLHttpRequest get header', field, this._responseHeaders)
228
     if(!this._responseHeaders)
234
     if(!this._responseHeaders)
229
       return null
235
       return null
230
-    return this.responseHeaders[field] || null
236
+    return (this._responseHeaders[field] || this._responseHeaders[field.toLowerCase()]) || null
231
 
237
 
232
   }
238
   }
233
 
239
 
238
     let result = ''
244
     let result = ''
239
     let respHeaders = this.responseHeaders
245
     let respHeaders = this.responseHeaders
240
     for(let i in respHeaders) {
246
     for(let i in respHeaders) {
241
-      result += `${i}:${respHeaders[i]}\r\n`
247
+      result += `${i}: ${respHeaders[i]}${String.fromCharCode(0x0D,0x0A)}`
242
     }
248
     }
243
-    return result
249
+    console.log('###', result.substr(0, result.length-2))
250
+    return result.substr(0, result.length-2)
244
   }
251
   }
245
 
252
 
246
   _headerReceived(e) {
253
   _headerReceived(e) {
310
       let info = resp.respInfo || {}
317
       let info = resp.respInfo || {}
311
       switch(info.respType) {
318
       switch(info.respType) {
312
         case 'json' :
319
         case 'json' :
320
+        try{
313
           this._responseText = resp.text()
321
           this._responseText = resp.text()
314
           this._response = resp.json()
322
           this._response = resp.json()
315
           responseDataReady()
323
           responseDataReady()
324
+        } catch(err) {
325
+        }
316
         break;
326
         break;
317
         case 'blob' :
327
         case 'blob' :
318
           resp.blob().then((b) => {
328
           resp.blob().then((b) => {
322
           })
332
           })
323
         break;
333
         break;
324
         default :
334
         default :
325
-        console.log(resp, resp.text())
326
           this._responseText = resp.text()
335
           this._responseText = resp.text()
327
           this._response = this.responseText
336
           this._response = this.responseText
328
           responseDataReady()
337
           responseDataReady()