Last active
March 5, 2017 11:33
-
-
Save UKNC/887059b4bf6359ced5d1d982399dd32e to your computer and use it in GitHub Desktop.
Showcase for unsupported gzip decompression in phantom-nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "phantomgzip", | |
"version": "1.0.0", | |
"description": "Showcase of inability to support gzip", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "UKNC", | |
"license": "ISC", | |
"dependencies": { | |
"bluebird": "^3.5.0", | |
"phantom": "^4.0.0" | |
} | |
} | |
~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Run as | |
> node server.js 0 | |
to see that pages are plain HTML | |
and | |
> node server.js 1 | |
to see that GZIPPed htmls are not decompressed. | |
*/ | |
const phantom = require('phantom'); | |
/* jslint ignore: start */ | |
const Promise = require('bluebird'); | |
/* jslink ignore: end */ | |
let url = "http://www.google.com"; | |
let gzip = process.argv[2] || 0; | |
let phInst, page; | |
phantom.create() | |
.then( inst => { | |
phInst = inst; | |
return phInst.createPage(); | |
}) | |
.then( p=>{ | |
page = p; | |
return page.property("customHeaders", { "Accept-Encoding": `identity;q=1.0, gzip;q=${gzip}` }); | |
}) | |
.then( ()=>{ | |
return page.property("onResourceRequested", function( req, net ) { | |
console.log( "REQ: " + req.method + " " + req.url + " HEADERS: " + JSON.stringify( req.headers) ); | |
}); | |
}) | |
.then( ()=>{ | |
return page.property("onResourceReceived", function( res ) { | |
console.log("RES: " + "HEADERS: " + JSON.stringify( res.headers ) ) ; | |
}); | |
}) | |
.then( ()=>{ | |
return page.property("onResourceError", function( resourceError ) { | |
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString); | |
}); | |
}) | |
.then( ()=>{ | |
return page.property("onConsoleMessage", function( msg ) { | |
console.log('onConsoleMessage:' + msg ); | |
}); | |
}) | |
.then( ()=>{ | |
return page.open( url, function(status) { | |
console.log("Page load status: " + status ); | |
}); | |
}) | |
.then( ()=>{ | |
return Promise.delay(1000); | |
}) | |
.then( ()=>{ | |
return page.property("content"); | |
}) | |
.then( content=>{ | |
console.log(content); | |
}) | |
.catch( err=>{ | |
console.log('Error:' + err ); | |
phInst.exit(); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment