a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. var Stream = require('stream').Stream,
  3. url = require('url'),
  4. util = require('util'),
  5. Base = require('./base'),
  6. Headers = require('./headers'),
  7. HttpParser = require('../http_parser');
  8. var PORTS = {'ws:': 80, 'wss:': 443};
  9. var Proxy = function(client, origin, options) {
  10. this._client = client;
  11. this._http = new HttpParser('response');
  12. this._origin = (typeof client.url === 'object') ? client.url : url.parse(client.url);
  13. this._url = (typeof origin === 'object') ? origin : url.parse(origin);
  14. this._options = options || {};
  15. this._state = 0;
  16. this.readable = this.writable = true;
  17. this._paused = false;
  18. this._headers = new Headers();
  19. this._headers.set('Host', this._origin.host);
  20. this._headers.set('Connection', 'keep-alive');
  21. this._headers.set('Proxy-Connection', 'keep-alive');
  22. var auth = this._url.auth && new Buffer(this._url.auth, 'utf8').toString('base64');
  23. if (auth) this._headers.set('Proxy-Authorization', 'Basic ' + auth);
  24. };
  25. util.inherits(Proxy, Stream);
  26. var instance = {
  27. setHeader: function(name, value) {
  28. if (this._state !== 0) return false;
  29. this._headers.set(name, value);
  30. return true;
  31. },
  32. start: function() {
  33. if (this._state !== 0) return false;
  34. this._state = 1;
  35. var origin = this._origin,
  36. port = origin.port || PORTS[origin.protocol],
  37. start = 'CONNECT ' + origin.hostname + ':' + port + ' HTTP/1.1';
  38. var headers = [start, this._headers.toString(), ''];
  39. this.emit('data', new Buffer(headers.join('\r\n'), 'utf8'));
  40. return true;
  41. },
  42. pause: function() {
  43. this._paused = true;
  44. },
  45. resume: function() {
  46. this._paused = false;
  47. this.emit('drain');
  48. },
  49. write: function(chunk) {
  50. if (!this.writable) return false;
  51. this._http.parse(chunk);
  52. if (!this._http.isComplete()) return !this._paused;
  53. this.statusCode = this._http.statusCode;
  54. this.headers = this._http.headers;
  55. if (this.statusCode === 200) {
  56. this.emit('connect', new Base.ConnectEvent());
  57. } else {
  58. var message = "Can't establish a connection to the server at " + this._origin.href;
  59. this.emit('error', new Error(message));
  60. }
  61. this.end();
  62. return !this._paused;
  63. },
  64. end: function(chunk) {
  65. if (!this.writable) return;
  66. if (chunk !== undefined) this.write(chunk);
  67. this.readable = this.writable = false;
  68. this.emit('close');
  69. this.emit('end');
  70. },
  71. destroy: function() {
  72. this.end();
  73. }
  74. };
  75. for (var key in instance)
  76. Proxy.prototype[key] = instance[key];
  77. module.exports = Proxy;