file_loadinfo_redirectchain.sjs (3714B)
1 /* 2 * Redirect handler specifically for the needs of: 3 * Bug 1194052 - Append Principal to RedirectChain within LoadInfo before the channel is succesfully openend 4 */ 5 6 function createIframeContent(aQuery) { 7 var content = ` 8 <!DOCTYPE HTML> 9 <html> 10 <head><meta charset="utf-8"> 11 <title>Bug 1194052 - LoadInfo redirect chain subtest</title> 12 </head> 13 <body> 14 <script type="text/javascript"> 15 var myXHR = new XMLHttpRequest(); 16 myXHR.open("GET", "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?${aQuery}"); 17 myXHR.onload = function() { 18 var loadinfo = SpecialPowers.wrap(myXHR).channel.loadInfo; 19 var redirectChain = loadinfo.redirectChain; 20 var redirectChainIncludingInternalRedirects = loadinfo.redirectChainIncludingInternalRedirects; 21 var resultOBJ = { redirectChain : [], redirectChainIncludingInternalRedirects : [] }; 22 for (var i = 0; i < redirectChain.length; i++) { 23 resultOBJ.redirectChain.push(redirectChain[i].principal.spec); 24 } 25 for (var i = 0; i < redirectChainIncludingInternalRedirects.length; i++) { 26 resultOBJ.redirectChainIncludingInternalRedirects.push(redirectChainIncludingInternalRedirects[i].principal.spec); 27 } 28 var loadinfoJSON = JSON.stringify(resultOBJ); 29 window.parent.postMessage({ loadinfo: loadinfoJSON }, "*"); 30 } 31 myXHR.onerror = function() { 32 var resultOBJ = { redirectChain : [], redirectChainIncludingInternalRedirects : [] }; 33 var loadinfoJSON = JSON.stringify(resultOBJ); 34 window.parent.postMessage({ loadinfo: loadinfoJSON }, "*"); 35 } 36 myXHR.send(); 37 </script> 38 </body> 39 </html>`; 40 41 return content; 42 } 43 44 function handleRequest(request, response) { 45 response.setHeader("Cache-Control", "no-cache", false); 46 var queryString = request.queryString; 47 48 if ( 49 queryString == "iframe-redir-https-2" || 50 queryString == "iframe-redir-err-2" 51 ) { 52 var query = queryString.replace("iframe-", ""); 53 // send upgrade-insecure-requests CSP header 54 response.setHeader("Content-Type", "text/html", false); 55 response.setHeader( 56 "Content-Security-Policy", 57 "upgrade-insecure-requests", 58 false 59 ); 60 response.write(createIframeContent(query)); 61 return; 62 } 63 64 // at the end of the redirectchain we return some text 65 // for sanity checking 66 if (queryString == "redir-0" || queryString == "redir-https-0") { 67 response.setHeader("Content-Type", "text/html", false); 68 response.write("checking redirectchain"); 69 return; 70 } 71 72 // special case redir-err-1 and return an error to trigger the fallback 73 if (queryString == "redir-err-1") { 74 response.setStatusLine("1.1", 404, "Bad request"); 75 return; 76 } 77 78 // must be a redirect 79 var newLocation = ""; 80 switch (queryString) { 81 case "redir-err-2": 82 newLocation = 83 "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-err-1"; 84 break; 85 86 case "redir-https-2": 87 newLocation = 88 "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-https-1"; 89 break; 90 91 case "redir-https-1": 92 newLocation = 93 "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-https-0"; 94 break; 95 96 case "redir-2": 97 newLocation = 98 "http://mochi.test:8888/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-1"; 99 break; 100 101 case "redir-1": 102 newLocation = 103 "http://mochi.test:8888/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-0"; 104 break; 105 } 106 107 response.setStatusLine("1.1", 302, "Found"); 108 response.setHeader("Location", newLocation, false); 109 }