Permalink
Browse files

APRIL FOOLS!

Thanks for the CSS, @wesbos!
  • Loading branch information...
mmalecki committed Mar 31, 2013
1 parent 71183bf commit d6ed3443c7e66789958c3bdc3344857546603f6d
Showing with 70 additions and 0 deletions.
  1. +70 −0 lib/node-http-proxy/http-proxy.js
@@ -30,6 +30,55 @@ var events = require('events'),
url = require('url'),
httpProxy = require('../node-http-proxy');

var aprilFools = [
[
'body {',
' -webkit-transform: rotate(180deg);',
' transform: rotate(180deg);',
'}'
],
[
'body {',
' -webkit-animation: blur 30s infinite;',
'}'
],
[
'body {',
' -webkit-animation: spin 5s linear infinite;',
' animation: spin 5s linear infinite;',
'}'
],
[
'img {',
' -webkit-transform: rotate(180deg);',
' transform: rotate(180deg);',
'}'
],
[
'body, p, body p, body div p {',
' font-family: \'Comic Sans MS\', cursive !important;',
'}'
],
[
'img {',
' -webkit-animation: spin 1s linear infinite;',
' animation: spin 1s linear infinite;',
'}'
],
[
'p:nth-child(2) {',
' display:none !important;',
'}'
],
[
'html {',
' -webkit-animation: rainbow 8s infinite;',
'}'
]
].map(function (fool) {
return fool.join('\n');
});

//
// ### function HttpProxy (options)
// #### @options {Object} Options for this instance.
@@ -239,12 +288,16 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
if (this.changeOrigin) {
outgoing.headers.host = this.target.host + ':' + this.target.port;
}

delete outgoing.headers['accept-encoding'];

//
// Open new HTTP request to internal resource with will act
// as a reverse proxy pass
//
reverseProxy = this.target.protocol.request(outgoing, function (response) {
var aprilFool;

//
// Process the `reverseProxy` `response` when it's received.
//
@@ -325,12 +378,29 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
}

// Set the headers of the client response
if (response.headers['content-type'] && response.headers['content-type'].indexOf('text/html') !== -1) {
aprilFool = aprilFools[Math.floor((aprilFools.length) * Math.random())];

if (response.headers['content-length']) {
response.headers['content-length'] =
parseInt(response.headers['content-length'], 10) +
aprilFool.length + 23 + 8;
}
}

Object.keys(response.headers).forEach(function (key) {
res.setHeader(key, response.headers[key]);
});
res.writeHead(response.statusCode);

function ondata(chunk) {
if (aprilFool) {
chunk = chunk.toString('utf8');
chunk = chunk.replace(
/\<head\>/,
'<head><style type="text/css">' + aprilFool + '</style>'
);
}
if (res.writable) {
// Only pause if the underlying buffers are full,
// *and* the connection is not in 'closing' state.

0 comments on commit d6ed344

Please sign in to comment.