Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Remove trailing whitespace.

  • Loading branch information...
commit ee22f7595905a1545b0e316257bb2368b07bcef5 1 parent 7b63baa
James Coglan authored December 22, 2012
10  examples/autobahn_client.js
@@ -17,24 +17,24 @@ socket.onclose = function() {
17 17
     if (n > cases) {
18 18
       socket = new WebSocket.Client(host + '/updateReports?agent=' + encodeURIComponent(agent));
19 19
       socket.onclose = process.exit
20  
-      
  20
+
21 21
     } else if (skip.indexOf(n) >= 0) {
22 22
       runCase(n + 1);
23  
-      
  23
+
24 24
     } else {
25 25
       console.log('Running test case #' + n + ' ...');
26 26
       socket = new WebSocket.Client(host + '/runCase?case=' + n + '&agent=' + encodeURIComponent(agent));
27  
-      
  27
+
28 28
       socket.onmessage = function(event) {
29 29
         socket.send(event.data);
30 30
       };
31  
-      
  31
+
32 32
       socket.onclose = function() {
33 33
         runCase(n + 1);
34 34
       };
35 35
     }
36 36
   };
37  
-  
  37
+
38 38
   runCase(1);
39 39
 };
40 40
 
16  examples/server.js
@@ -9,11 +9,11 @@ var port   = process.argv[2] || 7000,
9 9
 var upgradeHandler = function(request, socket, head) {
10 10
   var ws = new WebSocket(request, socket, head, ['irc', 'xmpp'], {ping: 5});
11 11
   console.log('open', ws.url, ws.version, ws.protocol);
12  
-  
  12
+
13 13
   ws.onmessage = function(event) {
14 14
     ws.send(event.data);
15 15
   };
16  
-  
  16
+
17 17
   ws.onclose = function(event) {
18 18
     console.log('close', event.code, event.reason);
19 19
     ws = null;
@@ -23,12 +23,12 @@ var upgradeHandler = function(request, socket, head) {
23 23
 var requestHandler = function(request, response) {
24 24
   if (!WebSocket.EventSource.isEventSource(request))
25 25
     return staticHandler(request, response);
26  
-  
  26
+
27 27
   var es   = new WebSocket.EventSource(request, response),
28 28
       time = parseInt(es.lastEventId, 10) || 0;
29  
-  
  29
+
30 30
   console.log('open', es.url, es.lastEventId);
31  
-  
  31
+
32 32
   var loop = setInterval(function() {
33 33
     time += 1;
34 34
     es.send('Time: ' + time);
@@ -36,9 +36,9 @@ var requestHandler = function(request, response) {
36 36
       if (es) es.send('Update!!', {event: 'update', id: time});
37 37
     }, 1000);
38 38
   }, 2000);
39  
-  
  39
+
40 40
   es.send('Welcome!\n\nThis is an EventSource server.');
41  
-  
  41
+
42 42
   es.onclose = function() {
43 43
     clearInterval(loop);
44 44
     console.log('close', es.url);
@@ -48,7 +48,7 @@ var requestHandler = function(request, response) {
48 48
 
49 49
 var staticHandler = function(request, response) {
50 50
   var path = request.url;
51  
-  
  51
+
52 52
   fs.readFile(__dirname + path, function(err, content) {
53 53
     var status = err ? 404 : 200;
54 54
     response.writeHead(status, {'Content-Type': 'text/html'});
16  examples/sse.html
@@ -5,35 +5,35 @@
5 5
     <title>EventSource test</title>
6 6
   </head>
7 7
   <body>
8  
-    
  8
+
9 9
     <h1>EventSource test</h1>
10 10
     <ul></ul>
11  
-    
  11
+
12 12
     <script type="text/javascript">
13 13
       var logger = document.getElementsByTagName('ul')[0],
14 14
           socket = new EventSource('/');
15  
-      
  15
+
16 16
       var log = function(text) {
17 17
         logger.innerHTML += '<li>' + text + '</li>';
18 18
       };
19  
-      
  19
+
20 20
       socket.onopen = function() {
21 21
         log('OPEN');
22 22
       };
23  
-      
  23
+
24 24
       socket.onmessage = function(event) {
25 25
         log('MESSAGE: ' + event.data);
26 26
       };
27  
-      
  27
+
28 28
       socket.addEventListener('update', function(event) {
29 29
         log('UPDATE(' + event.lastEventId + '): ' + event.data);
30 30
       });
31  
-      
  31
+
32 32
       socket.onerror = function(event) {
33 33
         log('ERROR: ' + event.message);
34 34
       };
35 35
     </script>
36  
-    
  36
+
37 37
   </body>
38 38
 </html>
39 39
 
16  examples/ws.html
@@ -5,40 +5,40 @@
5 5
     <title>WebSocket test</title>
6 6
   </head>
7 7
   <body>
8  
-    
  8
+
9 9
     <h1>WebSocket test</h1>
10 10
     <ul></ul>
11  
-    
  11
+
12 12
     <script type="text/javascript">
13 13
       var logger = document.getElementsByTagName('ul')[0],
14 14
           Socket = window.MozWebSocket || window.WebSocket,
15 15
           protos = ['foo', 'bar', 'xmpp'],
16 16
           socket = new Socket('ws://' + location.hostname + ':' + location.port + '/', protos),
17 17
           index  = 0;
18  
-      
  18
+
19 19
       var log = function(text) {
20 20
         logger.innerHTML += '<li>' + text + '</li>';
21 21
       };
22  
-      
  22
+
23 23
       socket.addEventListener('open', function() {
24 24
         log('OPEN: ' + socket.protocol);
25 25
         socket.send('Hello, world');
26 26
       });
27  
-      
  27
+
28 28
       socket.onerror = function(event) {
29 29
         log('ERROR: ' + event.message);
30 30
       };
31  
-      
  31
+
32 32
       socket.onmessage = function(event) {
33 33
         log('MESSAGE: ' + event.data);
34 34
         setTimeout(function() { socket.send(++index + ' ' + event.data) }, 2000);
35 35
       };
36  
-      
  36
+
37 37
       socket.onclose = function(event) {
38 38
         log('CLOSE: ' + event.code + ', ' + event.reason);
39 39
       };
40 40
     </script>
41  
-    
  41
+
42 42
   </body>
43 43
 </html>
44 44
 
38  lib/faye/eventsource.js
@@ -12,42 +12,42 @@ var isSecureConnection = function(request) {
12 12
 
13 13
 var EventSource = function(request, response, options) {
14 14
   options = options || {};
15  
-  
  15
+
16 16
   this._request  = request;
17 17
   this._response = response;
18 18
   this._stream   = response.socket;
19 19
   this._ping     = options.ping  || this.DEFAULT_PING;
20 20
   this._retry    = options.retry || this.DEFAULT_RETRY;
21  
-  
  21
+
22 22
   var scheme = isSecureConnection(request) ? 'https:' : 'http:';
23 23
   this.url = scheme + '//' + request.headers.host + request.url;
24  
-  
  24
+
25 25
   this.lastEventId = request.headers['last-event-id'] || '';
26  
-  
  26
+
27 27
   var self = this;
28 28
   this.readyState = API.CONNECTING;
29 29
   this._sendBuffer = [];
30 30
   process.nextTick(function() { self._open() });
31  
-  
  31
+
32 32
   var handshake = 'HTTP/1.1 200 OK\r\n' +
33 33
                   'Content-Type: text/event-stream\r\n' +
34 34
                   'Cache-Control: no-cache, no-store\r\n' +
35 35
                   'Connection: close\r\n' +
36 36
                   '\r\n\r\n' +
37 37
                   'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n';
38  
-  
  38
+
39 39
   this.readyState = API.OPEN;
40  
-  
  40
+
41 41
   if (this._ping)
42 42
     this._pingLoop = setInterval(function() { self.ping() }, this._ping * 1000);
43  
-  
  43
+
44 44
   if (!this._stream || !this._stream.writable) return;
45  
-  
  45
+
46 46
   this._stream.setTimeout(0);
47 47
   this._stream.setNoDelay(true);
48  
-  
  48
+
49 49
   try { this._stream.write(handshake, 'utf8') } catch (e) {}
50  
-  
  50
+
51 51
   ['close', 'end', 'error'].forEach(function(event) {
52 52
     self._stream.addListener(event, function() { self.close() });
53 53
   });
@@ -61,18 +61,18 @@ EventSource.isEventSource = function(request) {
61 61
 var instance = {
62 62
   DEFAULT_PING:   10,
63 63
   DEFAULT_RETRY:  5,
64  
-  
  64
+
65 65
   send: function(message, options) {
66 66
     if (this.readyState !== API.OPEN) return false;
67  
-    
  67
+
68 68
     message = String(message).replace(/(\r\n|\r|\n)/g, '$1data: ');
69 69
     options = options || {};
70  
-    
  70
+
71 71
     var frame = '';
72 72
     if (options.event) frame += 'event: ' + options.event + '\r\n';
73 73
     if (options.id)    frame += 'id: '    + options.id    + '\r\n';
74 74
     frame += 'data: ' + message + '\r\n\r\n';
75  
-    
  75
+
76 76
     try {
77 77
       this._stream.write(frame, 'utf8');
78 78
       return true;
@@ -80,7 +80,7 @@ var instance = {
80 80
       return false;
81 81
     }
82 82
   },
83  
-  
  83
+
84 84
   ping: function() {
85 85
     try {
86 86
       this._stream.write(':\r\n\r\n', 'utf8');
@@ -89,15 +89,15 @@ var instance = {
89 89
       return false;
90 90
     }
91 91
   },
92  
-  
  92
+
93 93
   close: function() {
94 94
     if (this.readyState === API.CLOSING || this.readyState === API.CLOSED)
95 95
       return;
96  
-    
  96
+
97 97
     this.readyState = API.CLOSED;
98 98
     clearInterval(this._pingLoop);
99 99
     this._response.end();
100  
-    
  100
+
101 101
     var event = new Event('close');
102 102
     event.initEvent('close', false, false);
103 103
     this.dispatchEvent(event);
20  lib/faye/websocket.js
@@ -36,38 +36,38 @@ var WebSocket = function(request, socket, head, supportedProtos, options) {
36 36
   this._stream = request.socket;
37 37
   this._ping   = options && options.ping;
38 38
   this._pingId = 0;
39  
-  
  39
+
40 40
   var scheme = isSecureConnection(request) ? 'wss:' : 'ws:';
41 41
   this.url = scheme + '//' + request.headers.host + request.url;
42 42
   this.readyState = API.CONNECTING;
43 43
   this.bufferedAmount = 0;
44  
-  
  44
+
45 45
   var Parser = getParser(request);
46 46
   this._parser = new Parser(this, {protocols: supportedProtos});
47  
-  
  47
+
48 48
   var self = this;
49 49
   this._sendBuffer = [];
50 50
   process.nextTick(function() { self._open() });
51  
-  
  51
+
52 52
   var handshake = this._parser.handshakeResponse(head);
53 53
   if (this._parser.isOpen()) this.readyState = API.OPEN;
54  
-  
  54
+
55 55
   if (this._ping)
56 56
     this._pingLoop = setInterval(function() {
57 57
       self._pingId += 1;
58 58
       self.ping(self._pingId.toString());
59 59
     }, this._ping * 1000);
60  
-  
  60
+
61 61
   this.protocol = this._parser.protocol || '';
62 62
   this.version = this._parser.getVersion();
63  
-  
  63
+
64 64
   if (!this._stream || !this._stream.writable) return;
65  
-  
  65
+
66 66
   this._stream.setTimeout(0);
67 67
   this._stream.setNoDelay(true);
68  
-  
  68
+
69 69
   try { this._stream.write(handshake, 'binary') } catch (e) {}
70  
-  
  70
+
71 71
   this._stream.addListener('data', function(data) {
72 72
     var response = self._parser.parse(data);
73 73
     if (!response) return;
20  lib/faye/websocket/api.js
@@ -6,22 +6,22 @@ var API = {
6 6
   OPEN:         1,
7 7
   CLOSING:      2,
8 8
   CLOSED:       3,
9  
-  
  9
+
10 10
   _open: function() {
11 11
     if (this._parser && !this._parser.isOpen()) return;
12 12
     this.readyState = API.OPEN;
13  
-    
  13
+
14 14
     var buffer = this._sendBuffer || [],
15 15
         message;
16  
-    
  16
+
17 17
     while (message = buffer.shift())
18 18
       this.send.apply(this, message);
19  
-    
  19
+
20 20
     var event = new Event('open');
21 21
     event.initEvent('open', false, false);
22 22
     this.dispatchEvent(event);
23 23
   },
24  
-  
  24
+
25 25
   receive: function(data) {
26 26
     if (this.readyState !== API.OPEN) return false;
27 27
     var event = new Event('message');
@@ -29,7 +29,7 @@ var API = {
29 29
     event.data = data;
30 30
     this.dispatchEvent(event);
31 31
   },
32  
-  
  32
+
33 33
   send: function(data, type, errorType) {
34 34
     if (this.readyState === API.CONNECTING) {
35 35
       if (this._sendBuffer) {
@@ -39,12 +39,12 @@ var API = {
39 39
         throw new Error('Cannot call send(), socket is not open yet');
40 40
       }
41 41
     }
42  
-    
  42
+
43 43
     if (this.readyState === API.CLOSED)
44 44
       return false;
45  
-    
  45
+
46 46
     if (!(data instanceof Buffer)) data = String(data);
47  
-    
  47
+
48 48
     var frame = this._parser.frame(data, type, errorType);
49 49
     try {
50 50
       this._stream.write(frame, 'binary');
@@ -53,7 +53,7 @@ var API = {
53 53
       return false;
54 54
     }
55 55
   },
56  
-  
  56
+
57 57
   close: function(code, reason, ack) {
58 58
     if (this.readyState === API.CLOSING ||
59 59
         this.readyState === API.CLOSED) return;
4  lib/faye/websocket/api/event.js
@@ -3,7 +3,7 @@ var Event = function(eventType, options) {
3 3
   for (var key in options)
4 4
     this[key] = options[key];
5 5
 };
6  
-    
  6
+
7 7
 Event.prototype.initEvent = function(eventType, canBubble, cancelable) {
8 8
   this.type       = eventType;
9 9
   this.bubbles    = canBubble;
@@ -12,7 +12,7 @@ Event.prototype.initEvent = function(eventType, canBubble, cancelable) {
12 12
 
13 13
 Event.prototype.stopPropagation = function() {};
14 14
 Event.prototype.preventDefault  = function() {};
15  
-    
  15
+
16 16
 Event.CAPTURING_PHASE = 1;
17 17
 Event.AT_TARGET       = 2;
18 18
 Event.BUBBLING_PHASE  = 3;
16  lib/faye/websocket/api/event_target.js
@@ -5,38 +5,38 @@ var EventTarget = {
5 5
   onmessage:  null,
6 6
   onerror:    null,
7 7
   onclose:    null,
8  
-  
  8
+
9 9
   addEventListener: function(eventType, listener, useCapture) {
10 10
     this._listeners = this._listeners || {};
11 11
     var list = this._listeners[eventType] = this._listeners[eventType] || [];
12 12
     list.push(listener);
13 13
   },
14  
-  
  14
+
15 15
   removeEventListener: function(eventType, listener, useCapture) {
16 16
     if (!this._listeners || !this._listeners[eventType]) return;
17  
-    
  17
+
18 18
     if (!listener) {
19 19
       delete this._listeners[eventType];
20 20
       return;
21 21
     }
22 22
     var list = this._listeners[eventType],
23 23
         i    = list.length;
24  
-    
  24
+
25 25
     while (i--) {
26 26
       if (listener !== list[i]) continue;
27 27
       list.splice(i,1);
28 28
     }
29 29
   },
30  
-  
  30
+
31 31
   dispatchEvent: function(event) {
32 32
     event.target = event.currentTarget = this;
33 33
     event.eventPhase = Event.AT_TARGET;
34  
-    
  34
+
35 35
     if (this['on' + event.type])
36 36
       this['on' + event.type](event);
37  
-    
  37
+
38 38
     if (!this._listeners || !this._listeners[event.type]) return;
39  
-    
  39
+
40 40
     this._listeners[event.type].forEach(function(listener) {
41 41
       listener(event);
42 42
     }, this);
26  lib/faye/websocket/client.js
@@ -8,30 +8,30 @@ var HybiParser = require('./hybi_parser'),
8 8
 var Client = function(url, protocols, options) {
9 9
   this.url  = url;
10 10
   this._uri = require('url').parse(url);
11  
-  
  11
+
12 12
   this.protocol = '';
13 13
   this.readyState = API.CONNECTING;
14 14
   this.bufferedAmount = 0;
15  
-  
  15
+
16 16
   var secure     = (this._uri.protocol === 'wss:'),
17 17
       self       = this,
18 18
       onConnect  = function() { self._onConnect() },
19 19
       tlsOptions = {};
20  
-      
  20
+
21 21
   if (options && options.verify === false) tlsOptions.rejectUnauthorized = false;
22 22
 
23 23
   var connection = secure
24 24
                  ? tls.connect(this._uri.port || 443, this._uri.hostname, tlsOptions, onConnect)
25 25
                  : net.createConnection(this._uri.port || 80, this._uri.hostname);
26  
-  
  26
+
27 27
   this._parser = new HybiParser(this, {masking: true, protocols: protocols});
28 28
   this._stream = connection;
29  
-  
  29
+
30 30
   this._stream.setTimeout(0);
31 31
   this._stream.setNoDelay(true);
32  
-  
  32
+
33 33
   if (!secure) connection.addListener('connect', onConnect);
34  
-  
  34
+
35 35
   connection.addListener('data', function(data) {
36 36
     self._onData(data);
37 37
   });
@@ -39,7 +39,7 @@ var Client = function(url, protocols, options) {
39 39
     connection.addListener(event, function() { self.close(1006, '', false) });
40 40
   });
41 41
 };
42  
-  
  42
+
43 43
 Client.prototype._onConnect = function() {
44 44
   this._handshake = this._parser.createHandshake(this._uri, this._stream);
45 45
   this._message = [];
@@ -54,18 +54,18 @@ Client.prototype._onData = function(data) {
54 54
       var bytes = this._handshake.parse(data);
55 55
       for (var i = 0, n = bytes.length; i < n; i++)
56 56
         this._message.push(bytes[i]);
57  
-      
  57
+
58 58
       if (!this._handshake.isComplete()) return;
59  
-      
  59
+
60 60
       if (this._handshake.isValid()) {
61 61
         this.protocol = this._handshake.protocol || '';
62 62
         this.readyState = API.OPEN;
63 63
         var event = new Event('open');
64 64
         event.initEvent('open', false, false);
65 65
         this.dispatchEvent(event);
66  
-        
  66
+
67 67
         this._parser.parse(this._message);
68  
-        
  68
+
69 69
       } else {
70 70
         this.readyState = API.CLOSED;
71 71
         var event = new Event('close', {code: 1006, reason: ''});
@@ -73,7 +73,7 @@ Client.prototype._onData = function(data) {
73 73
         this.dispatchEvent(event);
74 74
       }
75 75
       break;
76  
-      
  76
+
77 77
     case API.OPEN:
78 78
     case API.CLOSING:
79 79
       this._parser.parse(data);
24  lib/faye/websocket/draft75_parser.js
@@ -7,7 +7,7 @@ var instance = {
7 7
   getVersion: function() {
8 8
     return 'hixie-75';
9 9
   },
10  
-  
  10
+
11 11
   handshakeResponse: function() {
12 12
     return new Buffer('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
13 13
                       'Upgrade: WebSocket\r\n' +
@@ -16,25 +16,25 @@ var instance = {
16 16
                       'WebSocket-Location: ' + this._socket.url + '\r\n\r\n',
17 17
                       'utf8');
18 18
   },
19  
-  
  19
+
20 20
   isOpen: function() {
21 21
     return true;
22 22
   },
23  
-  
  23
+
24 24
   parse: function(buffer) {
25 25
     var data, message, value;
26 26
     for (var i = 0, n = buffer.length; i < n; i++) {
27 27
       data = buffer[i];
28  
-      
  28
+
29 29
       switch (this._stage) {
30 30
         case 0:
31 31
           this._parseLeadingByte(data);
32 32
           break;
33  
-          
  33
+
34 34
         case 1:
35 35
           value = (data & 0x7F);
36 36
           this._length = value + 128 * this._length;
37  
-          
  37
+
38 38
           if (this._closing && this._length === 0) {
39 39
             this._socket.close(null, null, false);
40 40
           }
@@ -49,7 +49,7 @@ var instance = {
49 49
             }
50 50
           }
51 51
           break;
52  
-          
  52
+
53 53
         case 2:
54 54
           if (data === 0xFF) {
55 55
             message = new Buffer(this._buffer);
@@ -65,7 +65,7 @@ var instance = {
65 65
       }
66 66
     }
67 67
   },
68  
-  
  68
+
69 69
   _parseLeadingByte: function(data) {
70 70
     if ((0x80 & data) === 0x80) {
71 71
       this._length = 0;
@@ -76,17 +76,17 @@ var instance = {
76 76
       this._stage = 2;
77 77
     }
78 78
   },
79  
-  
  79
+
80 80
   frame: function(data) {
81 81
     if (Buffer.isBuffer(data)) return data;
82  
-    
  82
+
83 83
     var buffer = new Buffer(data, 'utf8'),
84 84
         frame  = new Buffer(buffer.length + 2);
85  
-    
  85
+
86 86
     frame[0] = 0x00;
87 87
     frame[buffer.length + 1] = 0xFF;
88 88
     buffer.copy(frame, 1);
89  
-    
  89
+
90 90
     return frame;
91 91
   }
92 92
 };
24  lib/faye/websocket/draft76_parser.js
@@ -25,17 +25,17 @@ var bigEndian = function(number) {
25 25
 Draft76Parser.prototype.getVersion = function() {
26 26
   return 'hixie-76';
27 27
 };
28  
-  
  28
+
29 29
 Draft76Parser.prototype.handshakeResponse = function(head) {
30 30
   var request = this._socket.request, tmp;
31  
-  
  31
+
32 32
   var response = new Buffer('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
33 33
                             'Upgrade: WebSocket\r\n' +
34 34
                             'Connection: Upgrade\r\n' +
35 35
                             'Sec-WebSocket-Origin: ' + request.headers.origin + '\r\n' +
36 36
                             'Sec-WebSocket-Location: ' + this._socket.url + '\r\n\r\n',
37 37
                             'binary');
38  
-  
  38
+
39 39
   var signature = this.handshakeSignature(head);
40 40
   if (signature) {
41 41
     tmp = new Buffer(response.length + signature.length);
@@ -43,7 +43,7 @@ Draft76Parser.prototype.handshakeResponse = function(head) {
43 43
     signature.copy(tmp, response.length);
44 44
     response = tmp;
45 45
   }
46  
-  
  46
+
47 47
   return response;
48 48
 };
49 49
 
@@ -53,21 +53,21 @@ Draft76Parser.prototype.isOpen = function() {
53 53
 
54 54
 Draft76Parser.prototype.handshakeSignature = function(head) {
55 55
   if (head.length === 0) return null;
56  
-  
  56
+
57 57
   var request = this._socket.request,
58  
-      
  58
+
59 59
       key1    = request.headers['sec-websocket-key1'],
60 60
       value1  = numberFromKey(key1) / spacesInKey(key1),
61  
-      
  61
+
62 62
       key2    = request.headers['sec-websocket-key2'],
63 63
       value2  = numberFromKey(key2) / spacesInKey(key2),
64  
-      
  64
+
65 65
       MD5     = crypto.createHash('md5');
66  
-  
  66
+
67 67
   MD5.update(bigEndian(value1));
68 68
   MD5.update(bigEndian(value2));
69 69
   MD5.update(head.toString('binary'));
70  
-  
  70
+
71 71
   this._handshakeComplete = true;
72 72
   return new Buffer(MD5.digest('binary'), 'binary');
73 73
 };
@@ -75,14 +75,14 @@ Draft76Parser.prototype.handshakeSignature = function(head) {
75 75
 Draft76Parser.prototype.parse = function(data) {
76 76
   if (this._handshakeComplete)
77 77
     return Draft75Parser.prototype.parse.call(this, data);
78  
-  
  78
+
79 79
   return this.handshakeSignature(data);
80 80
 };
81 81
 
82 82
 Draft76Parser.prototype._parseLeadingByte = function(data) {
83 83
   if (data !== 0xFF)
84 84
     return Draft75Parser.prototype._parseLeadingByte.call(this, data);
85  
-  
  85
+
86 86
   this._closing = true;
87 87
   this._length = 0;
88 88
   this._stage = 1;
104  lib/faye/websocket/hybi_parser.js
@@ -9,9 +9,9 @@ var HybiParser = function(webSocket, options) {
9 9
   this._stage     = 0;
10 10
   this._masking   = options && options.masking;
11 11
   this._protocols = options && options.protocols;
12  
-  
  12
+
13 13
   this._pingCallbacks = {};
14  
-  
  14
+
15 15
   if (typeof this._protocols === 'string')
16 16
     this._protocols = this._protocols.split(/\s*,\s*/);
17 17
 };
@@ -19,7 +19,7 @@ var HybiParser = function(webSocket, options) {
19 19
 HybiParser.mask = function(payload, mask, offset) {
20 20
   if (mask.length === 0) return payload;
21 21
   offset = offset || 0;
22  
-  
  22
+
23 23
   for (var i = 0, n = payload.length - offset; i < n; i++) {
24 24
     payload[offset + i] = payload[offset + i] ^ mask[i % 4];
25 25
   }
@@ -35,7 +35,7 @@ var instance = {
35 35
   RSV3:   16,
36 36
   OPCODE: 15,
37 37
   LENGTH: 127,
38  
-  
  38
+
39 39
   OPCODES: {
40 40
     continuation: 0,
41 41
     text:         1,
@@ -44,7 +44,7 @@ var instance = {
44 44
     ping:         9,
45 45
     pong:         10
46 46
   },
47  
-  
  47
+
48 48
   ERRORS: {
49 49
     normal_closure:       1000,
50 50
     going_away:           1001,
@@ -56,38 +56,38 @@ var instance = {
56 56
     extension_error:      1010,
57 57
     unexpected_condition: 1011
58 58
   },
59  
-  
  59
+
60 60
   FRAGMENTED_OPCODES: [0,1,2],
61 61
   OPENING_OPCODES:    [1,2],
62  
-  
  62
+
63 63
   ERROR_CODES: [1000,1001,1002,1003,1007,1008,1009,1010,1011],
64  
-  
  64
+
65 65
   UTF8_MATCH: /^([\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*$/,
66  
-  
  66
+
67 67
   getVersion: function() {
68 68
     var version = this._socket.request.headers['sec-websocket-version'];
69 69
     return 'hybi-' + version;
70 70
   },
71  
-  
  71
+
72 72
   handshakeResponse: function() {
73 73
     var secKey = this._socket.request.headers['sec-websocket-key'];
74 74
     if (!secKey) return null;
75  
-    
  75
+
76 76
     var SHA1 = crypto.createHash('sha1');
77 77
     SHA1.update(secKey + Handshake.GUID);
78  
-    
  78
+
79 79
     var accept    = SHA1.digest('base64'),
80 80
         protos    = this._socket.request.headers['sec-websocket-protocol'],
81 81
         supported = this._protocols,
82 82
         proto,
83  
-    
  83
+
84 84
         headers = [
85 85
           'HTTP/1.1 101 Switching Protocols',
86 86
           'Upgrade: websocket',
87 87
           'Connection: Upgrade',
88 88
           'Sec-WebSocket-Accept: ' + accept
89 89
         ];
90  
-    
  90
+
91 91
     if (protos !== undefined && supported !== undefined) {
92 92
       if (typeof protos === 'string') protos = protos.split(/\s*,\s*/);
93 93
       proto = protos.filter(function(p) { return supported.indexOf(p) >= 0 })[0];
@@ -96,18 +96,18 @@ var instance = {
96 96
         headers.push('Sec-WebSocket-Protocol: ' + proto);
97 97
       }
98 98
     }
99  
-    
  99
+
100 100
     return new Buffer(headers.concat('','').join('\r\n'), 'utf8');
101 101
   },
102  
-  
  102
+
103 103
   isOpen: function() {
104 104
     return true;
105 105
   },
106  
-  
  106
+
107 107
   createHandshake: function(uri) {
108 108
     return new Handshake(uri, this._protocols);
109 109
   },
110  
-  
  110
+
111 111
   parse: function(data) {
112 112
     this._reader.put(data);
113 113
     var buffer = true;
@@ -117,17 +117,17 @@ var instance = {
117 117
           buffer = this._reader.read(1);
118 118
           if (buffer) this._parseOpcode(buffer[0]);
119 119
           break;
120  
-          
  120
+
121 121
         case 1:
122 122
           buffer = this._reader.read(1);
123 123
           if (buffer) this._parseLength(buffer[0]);
124 124
           break;
125  
-          
  125
+
126 126
         case 2:
127 127
           buffer = this._reader.read(this._lengthSize);
128 128
           if (buffer) this._parseExtendedLength(buffer);
129 129
           break;
130  
-          
  130
+
131 131
         case 3:
132 132
           buffer = this._reader.read(4);
133 133
           if (buffer) {
@@ -135,7 +135,7 @@ var instance = {
135 135
             this._stage = 4;
136 136
           }
137 137
           break;
138  
-          
  138
+
139 139
         case 4:
140 140
           buffer = this._reader.read(this._length);
141 141
           if (buffer) {
@@ -147,40 +147,40 @@ var instance = {
147 147
       }
148 148
     }
149 149
   },
150  
-  
  150
+
151 151
   _parseOpcode: function(data) {
152 152
     var rsvs = [this.RSV1, this.RSV2, this.RSV3].filter(function(rsv) {
153 153
       return (data & rsv) === rsv;
154 154
     }, this);
155  
-    
  155
+
156 156
     if (rsvs.length > 0) return this._socket.close(this.ERRORS.protocol_error, null, false);
157  
-    
  157
+
158 158
     this._final   = (data & this.FIN) === this.FIN;
159 159
     this._opcode  = (data & this.OPCODE);
160 160
     this._mask    = [];
161 161
     this._payload = [];
162  
-    
  162
+
163 163
     var valid = false;
164  
-    
  164
+
165 165
     for (var key in this.OPCODES) {
166 166
       if (this.OPCODES[key] === this._opcode)
167 167
         valid = true;
168 168
     }
169 169
     if (!valid) return this._socket.close(this.ERRORS.protocol_error, null, false);
170  
-    
  170
+
171 171
     if (this.FRAGMENTED_OPCODES.indexOf(this._opcode) < 0 && !this._final)
172 172
       return this._socket.close(this.ERRORS.protocol_error, null, false);
173  
-    
  173
+
174 174
     if (this._mode && this.OPENING_OPCODES.indexOf(this._opcode) >= 0)
175 175
       return this._socket.close(this.ERRORS.protocol_error, null, false);
176  
-    
  176
+
177 177
     this._stage = 1;
178 178
   },
179  
-  
  179
+
180 180
   _parseLength: function(data) {
181 181
     this._masked = (data & this.MASK) === this.MASK;
182 182
     this._length = (data & this.LENGTH);
183  
-    
  183
+
184 184
     if (this._length >= 0 && this._length <= 125) {
185 185
       this._stage = this._masked ? 3 : 4;
186 186
     } else {
@@ -189,15 +189,15 @@ var instance = {
189 189
       this._stage        = 2;
190 190
     }
191 191
   },
192  
-  
  192
+
193 193
   _parseExtendedLength: function(buffer) {
194 194
     this._length = this._getInteger(buffer);
195 195
     this._stage  = this._masked ? 3 : 4;
196 196
   },
197  
-  
  197
+
198 198
   frame: function(data, type, code) {
199 199
     if (this._closed) return null;
200  
-    
  200
+
201 201
     var isText = (typeof data === 'string'),
202 202
         opcode = this.OPCODES[type || (isText ? 'text' : 'binary')],
203 203
         buffer = isText ? new Buffer(data, 'utf8') : data,
@@ -209,9 +209,9 @@ var instance = {
209 209
         frame  = new Buffer(length + offset),
210 210
         BYTE   = this.BYTE,
211 211
         mask, i;
212  
-    
  212
+
213 213
     frame[0] = this.FIN | opcode;
214  
-    
  214
+
215 215
     if (length <= 125) {
216 216
       frame[1] = masked | length;
217 217
     } else if (length <= 65535) {
@@ -229,45 +229,45 @@ var instance = {
229 229
       frame[8] = Math.floor(length / Math.pow(2,8))  & BYTE;
230 230
       frame[9] = length & BYTE;
231 231
     }
232  
-    
  232
+
233 233
     if (code) {
234 234
       frame[offset]   = Math.floor(code / 256) & BYTE;
235 235
       frame[offset+1] = code & BYTE;
236 236
     }
237 237
     buffer.copy(frame, offset + insert);
238  
-    
  238
+
239 239
     if (this._masking) {
240 240
       mask = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256),
241 241
               Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
242 242
       new Buffer(mask).copy(frame, header);
243 243
       HybiParser.mask(frame, mask, offset);
244 244
     }
245  
-    
  245
+
246 246
     return frame;
247 247
   },
248  
-  
  248
+
249 249
   ping: function(message, callback, context) {
250 250
     message = message || '';
251 251
     if (callback) this._pingCallbacks[message] = [callback, context];
252 252
     return this._socket.send(message, 'ping');
253 253
   },
254  
-  
  254
+
255 255
   close: function(code, reason, callback, context) {
256 256
     if (this._closed) return;
257 257
     if (callback) this._closingCallback = [callback, context];
258 258
     this._socket.send(reason || '', 'close', code || this.ERRORS.normal_closure);
259 259
     this._closed = true;
260 260
   },
261  
-  
  261
+
262 262
   buffer: function(fragment) {
263 263
     for (var i = 0, n = fragment.length; i < n; i++)
264 264
       this._buffer.push(fragment[i]);
265 265
   },
266  
-  
  266
+
267 267
   _emitFrame: function() {
268 268
     var payload = HybiParser.mask(this._payload, this._mask),
269 269
         opcode  = this._opcode;
270  
-    
  270
+
271 271
     if (opcode === this.OPCODES.continuation) {
272 272
       if (!this._mode) return this._socket.close(this.ERRORS.protocol_error, null, false);
273 273
       this.buffer(payload);
@@ -300,15 +300,15 @@ var instance = {
300 300
     else if (opcode === this.OPCODES.close) {
301 301
       var code   = (payload.length >= 2) ? 256 * payload[0] + payload[1] : null,
302 302
           reason = (payload.length > 2) ? this._encode(payload.slice(2)) : null;
303  
-      
  303
+
304 304
       if (!(payload.length === 0) &&
305 305
           !(code !== null && code >= 3000 && code < 5000) &&
306 306
           this.ERROR_CODES.indexOf(code) < 0)
307 307
         code = this.ERRORS.protocol_error;
308  
-      
  308
+
309 309
       if (payload.length > 125 || (payload.length > 2 && !reason))
310 310
         code = this.ERRORS.protocol_error;
311  
-      
  311
+
312 312
       this._socket.close(code, (payload.length > 2) ? reason : null, false);
313 313
       if (this._closingCallback)
314 314
         this._closingCallback[0].call(this._closingCallback[1]);
@@ -321,17 +321,17 @@ var instance = {
321 321
       var callbacks = this._pingCallbacks,
322 322
           message   = this._encode(payload),
323 323
           callback  = callbacks[message];
324  
-      
  324
+
325 325
       delete callbacks[message];
326 326
       if (callback) callback[0].call(callback[1]);
327 327
     }
328 328
   },
329  
-  
  329
+
330 330
   _reset: function() {
331 331
     this._mode   = null;
332 332
     this._buffer = [];
333 333
   },
334  
-  
  334
+
335 335
   _encode: function(buffer) {
336 336
     try {
337 337
       var string = buffer.toString('binary', 0, buffer.length);
@@ -339,7 +339,7 @@ var instance = {
339 339
     } catch (e) {}
340 340
     return buffer.toString('utf8', 0, buffer.length);
341 341
   },
342  
-  
  342
+
343 343
   _getInteger: function(bytes) {
344 344
     var number = 0;
345 345
     for (var i = 0, n = bytes.length; i < n; i++)
26  lib/faye/websocket/hybi_parser/handshake.js
@@ -3,25 +3,25 @@ var crypto = require('crypto');
3 3
 var Handshake = function(uri, protocols) {
4 4
   this._uri = uri;
5 5
   this._protocols = protocols;
6  
-  
  6
+
7 7
   var buffer = new Buffer(16), i = 16;
8 8
   while (i--) buffer[i] = Math.floor(Math.random() * 256);
9 9
   this._key = buffer.toString('base64');
10  
-  
  10
+
11 11
   var SHA1 = crypto.createHash('sha1');
12 12
   SHA1.update(this._key + Handshake.GUID);
13 13
   this._accept = SHA1.digest('base64');
14  
-  
  14
+
15 15
   var HTTPParser = process.binding('http_parser').HTTPParser,
16 16
       parser     = new HTTPParser(HTTPParser.RESPONSE || 'response'),
17 17
       current    = null,
18 18
       self       = this;
19  
-  
  19
+
20 20
   this._nodeVersion = HTTPParser.RESPONSE ? 6 : 4;
21 21
   this._complete    = false;
22 22
   this._headers     = {};
23 23
   this._parser      = parser;
24  
-  
  24
+
25 25
   parser.onHeaderField = function(b, start, length) {
26 26
     current = b.toString('utf8', start, start + length);
27 27
   };
@@ -41,10 +41,10 @@ var Handshake = function(uri, protocols) {
41 41
 };
42 42
 
43 43
 Handshake.GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
44  
-    
  44
+
45 45
 Handshake.prototype.requestData = function() {
46 46
   var u = this._uri;
47  
-  
  47
+
48 48
   var headers = [
49 49
     'GET ' + (u.pathname || '/') + (u.search || '') + ' HTTP/1.1',
50 50
     'Host: ' + u.hostname + (u.port ? ':' + u.port : ''),
@@ -53,17 +53,17 @@ Handshake.prototype.requestData = function() {
53 53
     'Sec-WebSocket-Key: ' + this._key,
54 54
     'Sec-WebSocket-Version: 13'
55 55
   ];
56  
-  
  56
+
57 57
   if (this._protocols)
58 58
     headers.push('Sec-WebSocket-Protocol: ' + this._protocols.join(', '));
59  
-  
  59
+
60 60
   return new Buffer(headers.concat('','').join('\r\n'), 'utf8');
61 61
 };
62 62
 
63 63
 Handshake.prototype.parse = function(data) {
64 64
   var consumed = this._parser.execute(data, 0, data.length),
65 65
       offset   = (this._nodeVersion < 6) ? 1 : 0;
66  
-  
  66
+
67 67
   return (consumed === data.length) ? [] : data.slice(consumed + offset);
68 68
 };
69 69
 
@@ -73,15 +73,15 @@ Handshake.prototype.isComplete = function() {
73 73
 
74 74
 Handshake.prototype.isValid = function() {
75 75