@@ -276,7 +276,7 @@ function __emscripten_fetch_cache_data(db, fetch, data, onsuccess, onerror) {
276
276
}
277
277
#endif // ~FETCH_SUPPORT_INDEXEDDB
278
278
279
- function __emscripten_fetch_xhr ( fetch , onsuccess , onerror , onprogress ) {
279
+ function __emscripten_fetch_xhr ( fetch , onsuccess , onerror , onprogress , onreadystatechange ) {
280
280
var url = HEAPU32 [ fetch + { { { C_STRUCTS . emscripten_fetch_t . url } } } >> 2 ] ;
281
281
if ( ! url ) {
282
282
#if FETCH_DEBUG
@@ -438,6 +438,13 @@ function __emscripten_fetch_xhr(fetch, onsuccess, onerror, onprogress) {
438
438
if ( xhr . statusText ) stringToUTF8 ( xhr . statusText , fetch + { { { C_STRUCTS . emscripten_fetch_t . statusText } } } , 64 ) ;
439
439
if ( onprogress ) onprogress ( fetch , xhr , e ) ;
440
440
}
441
+ xhr . onreadystatechange = function ( e ) {
442
+ HEAPU16 [ fetch + { { { C_STRUCTS . emscripten_fetch_t . readyState } } } >> 1 ] = xhr . readyState ;
443
+ if ( xhr . readyState >= 2 ) {
444
+ HEAPU16 [ fetch + { { { C_STRUCTS . emscripten_fetch_t . status } } } >> 1 ] = xhr . status ;
445
+ }
446
+ if ( onreadystatechange ) onreadystatechange ( fetch , xhr , e ) ;
447
+ }
441
448
#if FETCH_DEBUG
442
449
console . log ( 'fetch: xhr.send(data=' + data + ')' ) ;
443
450
#endif
@@ -451,14 +458,15 @@ function __emscripten_fetch_xhr(fetch, onsuccess, onerror, onprogress) {
451
458
}
452
459
}
453
460
454
- function emscripten_start_fetch ( fetch , successcb , errorcb , progresscb ) {
461
+ function emscripten_start_fetch ( fetch , successcb , errorcb , progresscb , readystatechangecb ) {
455
462
if ( typeof Module !== 'undefined' ) Module [ 'noExitRuntime' ] = true ; // If we are the main Emscripten runtime, we should not be closing down.
456
463
457
464
var fetch_attr = fetch + { { { C_STRUCTS . emscripten_fetch_t . __attributes } } } ;
458
465
var requestMethod = UTF8ToString ( fetch_attr ) ;
459
466
var onsuccess = HEAPU32 [ fetch_attr + { { { C_STRUCTS . emscripten_fetch_attr_t . onsuccess } } } >> 2 ] ;
460
467
var onerror = HEAPU32 [ fetch_attr + { { { C_STRUCTS . emscripten_fetch_attr_t . onerror } } } >> 2 ] ;
461
468
var onprogress = HEAPU32 [ fetch_attr + { { { C_STRUCTS . emscripten_fetch_attr_t . onprogress } } } >> 2 ] ;
469
+ var onreadystatechange = HEAPU32 [ fetch_attr + { { { C_STRUCTS . emscripten_fetch_attr_t . onreadystatechange } } } >> 2 ] ;
462
470
var fetchAttributes = HEAPU32 [ fetch_attr + { { { C_STRUCTS . emscripten_fetch_attr_t . attributes } } } >> 2 ] ;
463
471
var fetchAttrLoadToMemory = ! ! ( fetchAttributes & { { { cDefine ( 'EMSCRIPTEN_FETCH_LOAD_TO_MEMORY' ) } } } ) ;
464
472
var fetchAttrStreamData = ! ! ( fetchAttributes & { { { cDefine ( 'EMSCRIPTEN_FETCH_STREAM_DATA' ) } } } ) ;
@@ -490,11 +498,19 @@ function emscripten_start_fetch(fetch, successcb, errorcb, progresscb) {
490
498
else if ( errorcb ) errorcb ( fetch ) ;
491
499
} ;
492
500
501
+ var reportReadyStateChange = function ( fetch , xhr , e ) {
502
+ #if FETCH_DEBUG
503
+ console . log ( 'fetch: ready state change. e: ' + e ) ;
504
+ #endif
505
+ if ( onreadystatechange ) { { { makeDynCall ( 'vi' ) } } } ( onreadystatechange , fetch ) ;
506
+ else if ( readystatechangecb ) readystatechangecb ( fetch ) ;
507
+ }
508
+
493
509
var performUncachedXhr = function ( fetch , xhr , e ) {
494
510
#if FETCH_DEBUG
495
511
console . error ( 'fetch: starting (uncached) XHR: ' + e ) ;
496
512
#endif
497
- __emscripten_fetch_xhr ( fetch , reportSuccess , reportError , reportProgress ) ;
513
+ __emscripten_fetch_xhr ( fetch , reportSuccess , reportError , reportProgress , reportReadyStateChange ) ;
498
514
} ;
499
515
500
516
#if FETCH_SUPPORT_INDEXEDDB
@@ -523,7 +539,7 @@ function emscripten_start_fetch(fetch, successcb, errorcb, progresscb) {
523
539
#if FETCH_DEBUG
524
540
console . error ( 'fetch: starting (cached) XHR: ' + e ) ;
525
541
#endif
526
- __emscripten_fetch_xhr ( fetch , cacheResultAndReportSuccess , reportError , reportProgress ) ;
542
+ __emscripten_fetch_xhr ( fetch , cacheResultAndReportSuccess , reportError , reportProgress , reportReadyStateChange ) ;
527
543
} ;
528
544
529
545
// Should we try IndexedDB first?
@@ -545,7 +561,7 @@ function emscripten_start_fetch(fetch, successcb, errorcb, progresscb) {
545
561
} else if ( ! fetchAttrReplace ) {
546
562
__emscripten_fetch_load_cached_data ( Fetch . dbInstance , fetch , reportSuccess , fetchAttrNoDownload ? reportError : ( fetchAttrPersistFile ? performCachedXhr : performUncachedXhr ) ) ;
547
563
} else if ( ! fetchAttrNoDownload ) {
548
- __emscripten_fetch_xhr ( fetch , fetchAttrPersistFile ? cacheResultAndReportSuccess : reportSuccess , reportError , reportProgress ) ;
564
+ __emscripten_fetch_xhr ( fetch , fetchAttrPersistFile ? cacheResultAndReportSuccess : reportSuccess , reportError , reportProgress , reportReadyStateChange ) ;
549
565
} else {
550
566
#if FETCH_DEBUG
551
567
console . error ( 'fetch: Invalid combination of flags passed.' ) ;
@@ -554,7 +570,18 @@ function emscripten_start_fetch(fetch, successcb, errorcb, progresscb) {
554
570
}
555
571
return fetch ;
556
572
#else // !FETCH_SUPPORT_INDEXEDDB
557
- __emscripten_fetch_xhr ( fetch , reportSuccess , reportError , reportProgress ) ;
573
+ __emscripten_fetch_xhr ( fetch , reportSuccess , reportError , reportProgress , reportReadyStateChange ) ;
558
574
return fetch ;
559
575
#endif // ~FETCH_SUPPORT_INDEXEDDB
560
576
}
577
+
578
+ function _fetch_get_response_headers_length ( id ) {
579
+ return lengthBytesUTF8 ( Fetch . xhrs [ id - 1 ] . getAllResponseHeaders ( ) ) + 1 ;
580
+ }
581
+
582
+ function _fetch_get_response_headers ( id , dst , dstSizeBytes ) {
583
+ var responseHeaders = Fetch . xhrs [ id - 1 ] . getAllResponseHeaders ( ) ;
584
+ var lengthBytes = lengthBytesUTF8 ( responseHeaders ) + 1 ;
585
+ stringToUTF8 ( responseHeaders , dst , dstSizeBytes ) ;
586
+ return Math . min ( lengthBytes , dstSizeBytes ) ;
587
+ }
0 commit comments