Browse Source

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 years ago
parent
commit
0f43d0d6b3
2 changed files with 19 additions and 10 deletions
  1. 2
    2
      src/polyfill/Blob.js
  2. 17
    8
      src/polyfill/XMLHttpRequest.js

+ 2
- 2
src/polyfill/Blob.js View File

@@ -11,8 +11,8 @@ import EventTarget from './EventTarget'
11 11
 const log = new Log('Blob')
12 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 18
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to

+ 17
- 8
src/polyfill/XMLHttpRequest.js View File

@@ -130,7 +130,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
130 130
    * @param  {any} body Body in RNfetchblob flavor
131 131
    */
132 132
   send(body) {
133
-
133
+    this._body = body
134 134
     if(this._readyState !== XMLHttpRequest.OPENED)
135 135
       throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
136 136
     let promise = Promise.resolve()
@@ -141,10 +141,12 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
141 141
     log.verbose(typeof body, body instanceof FormData)
142 142
 
143 143
     if(body instanceof Blob) {
144
+      log.debug('sending blob body', body._blobCreated)
144 145
       promise = new Promise((resolve, reject) => {
145 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,6 +160,10 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
158 160
     }
159 161
 
160 162
     promise.then(() => {
163
+      log.debug('send request invoke', body)
164
+      for(let h in _headers) {
165
+        _headers[h] = _headers[h].toString()
166
+      }
161 167
       this._task = RNFetchBlob
162 168
                     .config({
163 169
                       auto: true,
@@ -224,10 +230,10 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
224 230
   }
225 231
 
226 232
   getResponseHeader(field:string):string | null {
227
-    log.verbose('XMLHttpRequest get header', field)
233
+    log.verbose('XMLHttpRequest get header', field, this._responseHeaders)
228 234
     if(!this._responseHeaders)
229 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,9 +244,10 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
238 244
     let result = ''
239 245
     let respHeaders = this.responseHeaders
240 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 253
   _headerReceived(e) {
@@ -310,9 +317,12 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
310 317
       let info = resp.respInfo || {}
311 318
       switch(info.respType) {
312 319
         case 'json' :
320
+        try{
313 321
           this._responseText = resp.text()
314 322
           this._response = resp.json()
315 323
           responseDataReady()
324
+        } catch(err) {
325
+        }
316 326
         break;
317 327
         case 'blob' :
318 328
           resp.blob().then((b) => {
@@ -322,7 +332,6 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
322 332
           })
323 333
         break;
324 334
         default :
325
-        console.log(resp, resp.text())
326 335
           this._responseText = resp.text()
327 336
           this._response = this.responseText
328 337
           responseDataReady()