htmlfile.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var Client = require('../client')
  2. , util = require(process.binding('natives').util ? 'util' : 'sys')
  3. , qs = require('querystring');
  4. var HTMLFile = module.exports = function(){
  5. Client.apply(this, arguments);
  6. };
  7. util.inherits(HTMLFile, Client);
  8. HTMLFile.prototype._onConnect = function(req, res){
  9. var self = this, body = '';
  10. switch (req.method){
  11. case 'GET':
  12. Client.prototype._onConnect.call(this, req, res);
  13. this.response.useChunkedEncodingByDefault = true;
  14. this.response.shouldKeepAlive = true;
  15. this.response.writeHead(200, {
  16. 'Content-Type': 'text/html',
  17. 'Connection': 'keep-alive',
  18. 'Transfer-Encoding': 'chunked'
  19. });
  20. this.response.write('<html><body>' + new Array(245).join(' '));
  21. this._payload();
  22. break;
  23. case 'POST':
  24. req.addListener('data', function(message){
  25. body += message;
  26. });
  27. req.addListener('end', function(){
  28. try {
  29. var msg = qs.parse(body);
  30. self._onMessage(msg.data);
  31. } catch(e){
  32. self.listener.options.log('htmlfile message handler error - ' + e.stack);
  33. }
  34. res.writeHead(200, {'Content-Type': 'text/plain'});
  35. res.write('ok');
  36. res.end();
  37. });
  38. break;
  39. }
  40. };
  41. HTMLFile.prototype._write = function(message){
  42. if (this._open)
  43. this.response.write('<script>parent.s._('+ JSON.stringify(message) +', document);</script>'); //json for escaping
  44. };