FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge branch 'v0.4' · wave2future/node@59274e8 · GitHub

Repository navigation

Commit 59274e8

Browse files
committed
Merge branch 'v0.4'
Conflicts: lib/crypto.js lib/tls.js
2 parents 7ea19d0 + 9c7f89b commit 59274e8

8 files changed

Lines changed: 248 additions & 40 deletions

File tree

‎doc/api/http.markdown‎

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,59 @@ Options:
483483

484484
### Event: 'upgrade'
485485

486-
`function (request, socket, head)`
486+
`function (response, socket, head)`
487+
488+
Emitted each time a server responds to a request with an upgrade. If this
489+
event isn't being listened for, clients receiving an upgrade header will have
490+
their connections closed.
491+
492+
A client server pair that show you how to listen for the `upgrade` event using `http.getAgent`:
487493

488-
Emitted each time a server responds to a request with an upgrade. If this event
489-
isn't being listened for, clients receiving an upgrade header will have their
490-
connections closed.
494+
var http = require('http');
495+
var net = require('net');
496+
497+
// Create an HTTP server
498+
var srv = http.createServer(function (req, res) {
499+
res.writeHead(200, {'Content-Type': 'text/plain'});
500+
res.end('okay');
501+
});
502+
srv.on('upgrade', function(req, socket, upgradeHead) {
503+
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
504+
'Upgrade: WebSocket\r\n' +
505+
'Connection: Upgrade\r\n' +
506+
'\r\n\r\n');
507+
508+
socket.ondata = function(data, start, end) {
509+
socket.write(data.toString('utf8', start, end), 'utf8'); // echo back
510+
};
511+
});
512+
513+
// now that server is running
514+
srv.listen(1337, '127.0.0.1', function() {
515+
516+
// make a request
517+
var agent = http.getAgent('127.0.0.1', 1337);
518+
519+
var options = {
520+
agent: agent,
521+
port: 1337,
522+
host: '127.0.0.1',
523+
headers: {
524+
'Connection': 'Upgrade',
525+
'Upgrade': 'websocket'
526+
}
527+
};
528+
529+
var req = http.request(options);
530+
req.end();
531+
532+
agent.on('upgrade', function(res, socket, upgradeHead) {
533+
console.log('got upgraded!');
534+
socket.end();
535+
process.exit(0);
536+
});
537+
});
491538

492-
See the description of the [upgrade event](http.html#event_upgrade_) for `http.Server` for further details.
493539

494540
### Event: 'continue'
495541

‎lib/crypto.js‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ try {
3737
}
3838

3939

40-
function Credentials(secureProtocol, flags) {
40+
function Credentials(secureProtocol, flags, context) {
4141
if (!(this instanceof Credentials)) {
4242
return new Credentials(secureProtocol);
4343
}
@@ -46,24 +46,32 @@ function Credentials(secureProtocol, flags) {
4646
throw new Error('node.js not compiled with openssl crypto support.');
4747
}
4848

49-
this.context = new SecureContext();
50-
51-
if (secureProtocol) {
52-
this.context.init(secureProtocol);
49+
if (context) {
50+
this.context = context;
5351
} else {
54-
this.context.init();
55-
}
52+
this.context = new SecureContext();
5653

57-
if(flags) this.context.setOptions(flags);
54+
if (secureProtocol) {
55+
this.context.init(secureProtocol);
56+
} else {
57+
this.context.init();
58+
}
59+
}
5860

61+
if (flags) this.context.setOptions(flags);
5962
}
6063

6164
exports.Credentials = Credentials;
6265

6366

64-
exports.createCredentials = function(options) {
67+
exports.createCredentials = function(options, context) {
6568
if (!options) options = {};
66-
var c = new Credentials(options.secureProtocol, options.secureOptions);
69+
70+
var c = new Credentials(options.secureProtocol,
71+
options.secureOptions,
72+
context);
73+
74+
if (context) return c;
6775

6876
if (options.key) c.context.setKey(options.key);
6977

‎lib/tls.js‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,23 @@ CryptoStream.prototype._done = function() {
244244
};
245245

246246

247-
CryptoStream.prototype.fd = -1;
248-
CryptoStream.prototype.__defineGetter__('readyState',
249-
net.Socket.prototype.__lookupGetter__('readyState'));
247+
// readyState is deprecated. Don't use it.
248+
Object.defineProperty(CryptoStream.prototype, 'readyState', {
249+
get: function() {
250+
if (this._connecting) {
251+
return 'opening';
252+
} else if (this.readable && this.writable) {
253+
return 'open';
254+
} else if (this.readable && !this.writable) {
255+
return 'readOnly';
256+
} else if (!this.readable && this.writable) {
257+
return 'writeOnly';
258+
} else {
259+
return 'closed';
260+
}
261+
}
262+
});
263+
250264

251265
// Move decrypted, clear data out into the application.
252266
// From the user's perspective this occurs as a 'data' event
@@ -750,18 +764,24 @@ function Server(/* [options], listener */) {
750764

751765
var self = this;
752766

767+
// Handle option defaults:
768+
this.setOptions(options);
769+
770+
var sharedCreds = crypto.createCredentials({
771+
key: self.key,
772+
cert: self.cert,
773+
ca: self.ca,
774+
ciphers: self.ciphers,
775+
secureProtocol: self.secureProtocol,
776+
secureOptions: self.secureOptions,
777+
crl: self.crl
778+
});
779+
780+
sharedCreds.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
781+
753782
// constructor call
754783
net.Server.call(this, function(socket) {
755-
var creds = crypto.createCredentials({
756-
key: self.key,
757-
cert: self.cert,
758-
ca: self.ca,
759-
ciphers: self.ciphers,
760-
secureProtocol: self.secureProtocol,
761-
secureOptions: self.secureOptions,
762-
crl: self.crl
763-
});
764-
creds.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
784+
var creds = crypto.createCredentials(null, sharedCreds.context);
765785

766786
var pair = new SecurePair(creds,
767787
true,

‎src/node_buffer.cc‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@ Handle<Value> Buffer::ByteLength(const Arguments &args) {
702702
Handle<Value> Buffer::MakeFastBuffer(const Arguments &args) {
703703
HandleScope scope;
704704

705+
if (!Buffer::HasInstance(args[0])) {
706+
return ThrowException(Exception::TypeError(String::New(
707+
"First argument must be a Buffer")));
708+
}
709+
705710
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
706711
Local<Object> fast_buffer = args[1]->ToObject();;
707712
uint32_t offset = args[2]->Uint32Value();

‎src/platform_sunos.cc‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
168168
*cpus = Array::New();
169169

170170
lookup_instance = 0;
171-
while (ksp = kstat_lookup(kc, "cpu_info", lookup_instance, NULL)){
171+
while (ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL)){
172172
cpuinfo = Object::New();
173173

174174
if (kstat_read(kc, ksp, NULL) == -1) {
@@ -183,9 +183,9 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
183183
cpuinfo->Set(String::New("error"), String::New(strerror(errno)));
184184
(*cpus)->Set(lookup_instance, cpuinfo);
185185
} else {
186-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "clock_MHz");
186+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clock_MHz");
187187
cpuinfo->Set(String::New("speed"), data_named(knp));
188-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "brand");
188+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"brand");
189189
cpuinfo->Set(String::New("model"), data_named(knp));
190190
(*cpus)->Set(lookup_instance, cpuinfo);
191191
}
@@ -194,21 +194,21 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
194194
}
195195

196196
lookup_instance = 0;
197-
while (ksp = kstat_lookup(kc, "cpu", lookup_instance, "sys")){
197+
while (ksp = kstat_lookup(kc, (char *)"cpu", lookup_instance, (char *)"sys")){
198198
cpuinfo = (*cpus)->Get(lookup_instance)->ToObject();
199199
cputimes = Object::New();
200200

201201
if (kstat_read(kc, ksp, NULL) == -1) {
202202
cputimes->Set(String::New("error"), String::New(strerror(errno)));
203203
cpuinfo->Set(String::New("times"), cpuinfo);
204204
} else {
205-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_kernel");
205+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_kernel");
206206
cputimes->Set(String::New("system"), data_named(knp));
207-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_user");
207+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_user");
208208
cputimes->Set(String::New("user"), data_named(knp));
209-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "cpu_ticks_idle");
209+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_idle");
210210
cputimes->Set(String::New("idle"), data_named(knp));
211-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "intr");
211+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"intr");
212212
cputimes->Set(String::New("irq"), data_named(knp));
213213

214214
cpuinfo->Set(String::New("times"), cputimes);
@@ -224,12 +224,37 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
224224

225225

226226
double Platform::GetFreeMemory() {
227-
return 0.0;
227+
kstat_ctl_t *kc;
228+
kstat_t *ksp;
229+
kstat_named_t *knp;
230+
231+
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
232+
ulong_t freemem;
233+
234+
if((kc = kstat_open()) == NULL)
235+
throw "could not open kstat";
236+
237+
ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_pages");
238+
239+
if(kstat_read(kc, ksp, NULL) == -1){
240+
throw "could not read kstat";
241+
}
242+
else {
243+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"freemem");
244+
freemem = knp->value.ul;
245+
}
246+
247+
kstat_close(kc);
248+
249+
return static_cast<double>(freemem)*pagesize;
228250
}
229251

230252

231253
double Platform::GetTotalMemory() {
232-
return 0.0;
254+
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
255+
double pages = static_cast<double>(sysconf(_SC_PHYS_PAGES));
256+
257+
return pagesize*pages;
233258
}
234259

235260
double Platform::GetUptimeImpl() {
@@ -243,12 +268,12 @@ double Platform::GetUptimeImpl() {
243268
if ((kc = kstat_open()) == NULL)
244269
throw "could not open kstat";
245270

246-
ksp = kstat_lookup(kc, "unix", 0, "system_misc");
271+
ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_misc");
247272

248273
if (kstat_read(kc, ksp, NULL) == -1) {
249274
throw "unable to read kstat";
250275
} else {
251-
knp = (kstat_named_t *) kstat_data_lookup(ksp, "clk_intr");
276+
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clk_intr");
252277
clk_intr = knp->value.ul;
253278
}
254279

‎test/simple/test-buffer.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,9 @@ assert.equal(0xee, b[0]);
577577
assert.equal(0xad, b[1]);
578578
assert.equal(0xbe, b[2]);
579579
assert.equal(0xef, b[3]);
580+
581+
582+
// This should not segfault the program.
583+
assert.throws(function() {
584+
new Buffer('"pong"', 0, 6, 8031, '127.0.0.1')
585+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
// Verify that the 'upgrade' header causes an 'upgrade' event to be emitted to
23+
// the HTTP client. This test uses a raw TCP server to better control server
24+
// behavior.
25+
26+
var common = require('../common');
27+
var assert = require('assert');
28+
29+
var http = require('http');
30+
var net = require('net');
31+
32+
// Create a TCP server
33+
var srv = net.createServer(function(c) {
34+
var data = '';
35+
c.addListener('data', function(d) {
36+
data += d.toString('utf8');
37+
38+
c.write('HTTP/1.1 101\r\n');
39+
c.write('hello: world\r\n');
40+
c.write('connection: upgrade\r\n');
41+
c.write('upgrade: websocket\r\n');
42+
c.write('\r\n');
43+
c.write('nurtzo');
44+
});
45+
46+
c.addListener('end', function() {
47+
c.end();
48+
});
49+
});
50+
51+
var gotUpgrade = false;
52+
53+
srv.listen(common.PORT, '127.0.0.1', function() {
54+
55+
var agent = http.getAgent({
56+
host: '127.0.0.1',
57+
port: common.PORT
58+
});
59+
assert.ok(agent);
60+
61+
var options = {
62+
port: common.PORT,
63+
host: '127.0.0.1',
64+
headers: {
65+
'upgrade': 'websocket'
66+
}
67+
};
68+
69+
var req = http.request(options);
70+
req.end();
71+
72+
agent.on('upgrade', function(res, socket, upgradeHead) {
73+
// XXX: This test isn't fantastic, as it assumes that the entire response
74+
// from the server will arrive in a single data callback
75+
assert.equal(upgradeHead, 'nurtzo');
76+
77+
console.log(res.headers);
78+
var expectedHeaders = { 'hello': 'world',
79+
'connection': 'upgrade',
80+
'upgrade': 'websocket' };
81+
assert.deepEqual(expectedHeaders, res.headers);
82+
83+
socket.end();
84+
srv.close();
85+
86+
gotUpgrade = true;
87+
});
88+
89+
});
90+
91+
process.addListener('exit', function() {
92+
assert.ok(gotUpgrade);
93+
});

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL