Mercurial > hg > nodescore
comparison node_modules/socket.io/lib/transports/http.js @ 69:333afcfd3f3a
added node_modules to project and fixed path to chronometer
also added deps to installer script
author | tzara <rc-web@kiben.net> |
---|---|
date | Sat, 26 Oct 2013 14:12:50 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
68:b076cd17638c | 69:333afcfd3f3a |
---|---|
1 | |
2 /*! | |
3 * socket.io-node | |
4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com> | |
5 * MIT Licensed | |
6 */ | |
7 | |
8 /** | |
9 * Module requirements. | |
10 */ | |
11 | |
12 var Transport = require('../transport') | |
13 , parser = require('../parser') | |
14 , qs = require('querystring'); | |
15 | |
16 /** | |
17 * Export the constructor. | |
18 */ | |
19 | |
20 exports = module.exports = HTTPTransport; | |
21 | |
22 /** | |
23 * HTTP interface constructor. For all non-websocket transports. | |
24 * | |
25 * @api public | |
26 */ | |
27 | |
28 function HTTPTransport (mng, data, req) { | |
29 Transport.call(this, mng, data, req); | |
30 }; | |
31 | |
32 /** | |
33 * Inherits from Transport. | |
34 */ | |
35 | |
36 HTTPTransport.prototype.__proto__ = Transport.prototype; | |
37 | |
38 /** | |
39 * Handles a request. | |
40 * | |
41 * @api private | |
42 */ | |
43 | |
44 HTTPTransport.prototype.handleRequest = function (req) { | |
45 | |
46 // Always set the response in case an error is returned to the client | |
47 this.response = req.res; | |
48 | |
49 if (req.method == 'POST') { | |
50 var buffer = '' | |
51 , res = req.res | |
52 , origin = req.headers.origin | |
53 , headers = { 'Content-Length': 1, 'Content-Type': 'text/plain; charset=UTF-8' } | |
54 , self = this; | |
55 | |
56 req.on('data', function (data) { | |
57 buffer += data; | |
58 | |
59 if (Buffer.byteLength(buffer) >= self.manager.get('destroy buffer size')) { | |
60 buffer = ''; | |
61 req.connection.destroy(); | |
62 } | |
63 }); | |
64 | |
65 req.on('end', function () { | |
66 res.writeHead(200, headers); | |
67 res.end('1'); | |
68 | |
69 self.onData(self.postEncoded ? qs.parse(buffer).d : buffer); | |
70 }); | |
71 | |
72 // prevent memory leaks for uncompleted requests | |
73 req.on('close', function () { | |
74 buffer = ''; | |
75 self.onClose(); | |
76 }); | |
77 | |
78 if (origin) { | |
79 // https://developer.mozilla.org/En/HTTP_Access_Control | |
80 headers['Access-Control-Allow-Origin'] = origin; | |
81 headers['Access-Control-Allow-Credentials'] = 'true'; | |
82 } | |
83 } else { | |
84 Transport.prototype.handleRequest.call(this, req); | |
85 } | |
86 }; | |
87 | |
88 /** | |
89 * Handles data payload. | |
90 * | |
91 * @api private | |
92 */ | |
93 | |
94 HTTPTransport.prototype.onData = function (data) { | |
95 var messages = parser.decodePayload(data); | |
96 this.log.debug(this.name + ' received data packet', data); | |
97 | |
98 for (var i = 0, l = messages.length; i < l; i++) { | |
99 this.onMessage(messages[i]); | |
100 } | |
101 }; | |
102 | |
103 /** | |
104 * Closes the request-response cycle | |
105 * | |
106 * @api private | |
107 */ | |
108 | |
109 HTTPTransport.prototype.doClose = function () { | |
110 this.response.end(); | |
111 }; | |
112 | |
113 /** | |
114 * Writes a payload of messages | |
115 * | |
116 * @api private | |
117 */ | |
118 | |
119 HTTPTransport.prototype.payload = function (msgs) { | |
120 this.write(parser.encodePayload(msgs)); | |
121 }; |