- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnodeTransport.js
More file actions
Latest commit
74 lines (68 loc) · 2.47 KB
/
Copy pathnodeTransport.js
File metadata and controls
74 lines (68 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
vario=require('./node_modules/socket.io'),
util=require(process.binding('natives').util ? 'util' : 'sys'),
qs=require('querystring');
varClient=io.Client
varNodeClient=module.exports=function()
{
Client.apply(this,arguments);
};
util.inherits(NodeClient,io.Client);
NodeClient.prototype._onConnect=function(req,res)
{
varself=this,
body='',
headers={};
switch(req.method)
{
case'GET':
Client.prototype._onConnect.apply(this,[req,res]);
headers['Content-Type']='multipart/x-mixed-replace;boundary="socketio"';
headers['Connection']='keep-alive';
this.request.connection.addListener('end',function()
{
self.listener.options.log('[SERVER] _onConnect addListener end called');
self._onClose();
});
// this.response.useChunkedEncodingByDefault = false; //this breaks the node clientRequest, perhaps Browsers are better in handling this
this.response.shouldKeepAlive=true;
this.response.writeHead(200,headers);
this.response.write("--socketio\r\n");
if('flush'inthis.response)this.response.flush();
this._payload();
break;
case'POST':
headers['Content-Type']='text/plain';
req.addListener('data',function(message)
{
body+=message.toString();
});
req.addListener('end',function()
{
try
{
varmsg=qs.parse(body);
self._onMessage(msg.data);
}
catch(e)
{
self.listener.options.log('[SERVER] xhr-multipart message handler error - '+e.stack);
}
res.writeHead(200,headers);
res.write('ok');
res.end();
body='';
});
break;
}
};
NodeClient.prototype._write=function(message)
{
if(this._open)
{
//socket.io's multipart xhr doesn't send CLRF but just RF. This breaks isaacs multipart.js library so fixed this here. Perhaps browser implementations are more tolerant here
this.response.write("Content-Type: text/plain"+(message.length===1&&message.charCodeAt(0)===6 ? "; charset=us-ascii" : "")+"\r\n\r\n");
this.response.write(message+"\r\n");
this.response.write("--socketio");
//this.response.end(); //we use chunked encoding but do boundary definition over mime
}
};