Created
May 11, 2010 04:27
-
-
Save taf2/396917 to your computer and use it in GitHub Desktop.
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
var sys = require('sys'), | |
http = require('http'), | |
config = require('./config').config, | |
libxml = require('./libxmljs'); | |
sys.puts(sys.inspect(config)); | |
sys.puts(config.proxy_host); | |
if (config.proxy_host == undefined) { | |
sys.puts("Make sure to configure a valid proxy_host"); | |
sys.exit(1); | |
} | |
if (config.proxy_port == undefined) { | |
sys.puts("Assuming proxy_port 80? Perhaps you meant to define that?"); | |
config.proxy_port = 80; | |
} | |
http.createServer(function (request, response) { | |
var proxy = http.createClient(config.proxy_port, config.proxy_host); | |
var proxy_headers = request.headers; | |
// set the proxy host header | |
if (config.proxy_port == 80) { | |
proxy_headers.host = config.proxy_host; | |
} | |
else { | |
proxy_headers.host = config.proxy_host + ':' + config.proxy_port; | |
} | |
sys.puts(sys.inspect(proxy_headers)); | |
// start the request | |
var proxy_request = proxy.request(request.method, request.url, proxy_headers); | |
// forward to proxy_host | |
proxy_request.addListener('response', function(proxy_response) { | |
response.writeHead(proxy_response.statusCode,proxy_response.headers); | |
sys.puts(sys.inspect(proxy_response.headers)); | |
var parser = null; | |
if (config.esi_content_types[proxy_response.headers['content-type']]) { | |
parser = new libxml.SaxPushParser(function(cb){ | |
cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) { | |
sys.puts(sys.inspect(namespaces)); | |
sys.puts(sys.inspect(elem)); | |
sys.puts(sys.inspect(attrs)); | |
sys.puts(sys.inspect(prefix)); | |
sys.puts(sys.inspect(arguments)); | |
}); | |
cb.onEndElementNS(function(elem, prefix, uri) { | |
sys.puts("close: "+ elem + "\n"); | |
}); | |
cb.onEndDocument(function() { | |
sys.puts("document done\n"); | |
}); | |
cb.onError(function(msg) { | |
sys.puts("error: " + msg + "\n"); | |
}); | |
}); | |
} | |
proxy_response.addListener('data', function(chunk) { | |
if (parser) { | |
try { | |
var buf = new String(chunk); | |
sys.puts("send chunk: " + typeof(buf)); | |
if (parser.push(buf)) { | |
response.write(chunk); | |
} | |
else { | |
// parser error? | |
} | |
} catch(e) { | |
// error exception | |
sys.puts(e.message); | |
response.write(chunk); | |
} | |
} | |
else { | |
response.write(chunk); | |
} | |
}); | |
proxy_response.addListener('end', function() { response.end(''); parser.push('',true); }); | |
}); | |
proxy_request.end(); | |
// response.writeHead(200, {'Content-Type': 'text/plain'}); | |
// response.end('Hello World\n'); | |
}).listen(8000); | |
sys.puts("Server running at http://127.0.0.1:8000/"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment