| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -483,13 +483,59 @@ Options: | |||
| 483 | 483 | ||
| 484 | 484 | ### Event: 'upgrade' | |
| 485 | 485 | ||
| 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`: | ||
| 487 | 493 | ||
| 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 | + }); | ||
| 491 | 538 | ||
| 492 | - See the description of the [upgrade event](http.html#event_upgrade_) for `http.Server` for further details. | ||
| 493 | 539 | ||
| 494 | 540 | ### Event: 'continue' | |
| 495 | 541 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ try { | |||
| 37 | 37 | } | |
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | - function Credentials(secureProtocol, flags) { | ||
| 40 | + function Credentials(secureProtocol, flags, context) { | ||
| 41 | 41 | if (!(this instanceof Credentials)) { | |
| 42 | 42 | return new Credentials(secureProtocol); | |
| 43 | 43 | } | |
@@ -46,24 +46,32 @@ function Credentials(secureProtocol, flags) { | |||
| 46 | 46 | throw new Error('node.js not compiled with openssl crypto support.'); | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | - this.context = new SecureContext(); | ||
| 50 | - | ||
| 51 | - if (secureProtocol) { | ||
| 52 | - this.context.init(secureProtocol); | ||
| 49 | + if (context) { | ||
| 50 | + this.context = context; | ||
| 53 | 51 | } else { | |
| 54 | - this.context.init(); | ||
| 55 | - } | ||
| 52 | + this.context = new SecureContext(); | ||
| 56 | 53 | ||
| 57 | - if(flags) this.context.setOptions(flags); | ||
| 54 | + if (secureProtocol) { | ||
| 55 | + this.context.init(secureProtocol); | ||
| 56 | + } else { | ||
| 57 | + this.context.init(); | ||
| 58 | + } | ||
| 59 | + } | ||
| 58 | 60 | ||
| 61 | + if (flags) this.context.setOptions(flags); | ||
| 59 | 62 | } | |
| 60 | 63 | ||
| 61 | 64 | exports.Credentials = Credentials; | |
| 62 | 65 | ||
| 63 | 66 | ||
| 64 | - exports.createCredentials = function(options) { | ||
| 67 | + exports.createCredentials = function(options, context) { | ||
| 65 | 68 | 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; | ||
| 67 | 75 | ||
| 68 | 76 | if (options.key) c.context.setKey(options.key); | |
| 69 | 77 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -244,9 +244,23 @@ CryptoStream.prototype._done = function() { | |||
| 244 | 244 | }; | |
| 245 | 245 | ||
| 246 | 246 | ||
| 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 | + | ||
| 250 | 264 | ||
| 251 | 265 | // Move decrypted, clear data out into the application. | |
| 252 | 266 | // From the user's perspective this occurs as a 'data' event | |
@@ -750,18 +764,24 @@ function Server(/* [options], listener */) { | |||
| 750 | 764 | ||
| 751 | 765 | var self = this; | |
| 752 | 766 | ||
| 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 | + | ||
| 753 | 782 | // constructor call | |
| 754 | 783 | 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); | ||
| 765 | 785 | ||
| 766 | 786 | var pair = new SecurePair(creds, | |
| 767 | 787 | true, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -702,6 +702,11 @@ Handle<Value> Buffer::ByteLength(const Arguments &args) { | |||
| 702 | 702 | Handle<Value> Buffer::MakeFastBuffer(const Arguments &args) { | |
| 703 | 703 | HandleScope scope; | |
| 704 | 704 | ||
| 705 | + if (!Buffer::HasInstance(args[0])) { | ||
| 706 | + return ThrowException(Exception::TypeError(String::New( | ||
| 707 | + "First argument must be a Buffer"))); | ||
| 708 | + } | ||
| 709 | + | ||
| 705 | 710 | Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject()); | |
| 706 | 711 | Local<Object> fast_buffer = args[1]->ToObject();; | |
| 707 | 712 | uint32_t offset = args[2]->Uint32Value(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -168,7 +168,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) { | |||
| 168 | 168 | *cpus = Array::New(); | |
| 169 | 169 | ||
| 170 | 170 | 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)){ | ||
| 172 | 172 | cpuinfo = Object::New(); | |
| 173 | 173 | ||
| 174 | 174 | if (kstat_read(kc, ksp, NULL) == -1) { | |
@@ -183,9 +183,9 @@ int Platform::GetCPUInfo(Local<Array> *cpus) { | |||
| 183 | 183 | cpuinfo->Set(String::New("error"), String::New(strerror(errno))); | |
| 184 | 184 | (*cpus)->Set(lookup_instance, cpuinfo); | |
| 185 | 185 | } else { | |
| 186 | - knp = (kstat_named_t *) kstat_data_lookup(ksp, "clock_MHz"); | ||
| 186 | + knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clock_MHz"); | ||
| 187 | 187 | 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"); | ||
| 189 | 189 | cpuinfo->Set(String::New("model"), data_named(knp)); | |
| 190 | 190 | (*cpus)->Set(lookup_instance, cpuinfo); | |
| 191 | 191 | } | |
@@ -194,21 +194,21 @@ int Platform::GetCPUInfo(Local<Array> *cpus) { | |||
| 194 | 194 | } | |
| 195 | 195 | ||
| 196 | 196 | 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")){ | ||
| 198 | 198 | cpuinfo = (*cpus)->Get(lookup_instance)->ToObject(); | |
| 199 | 199 | cputimes = Object::New(); | |
| 200 | 200 | ||
| 201 | 201 | if (kstat_read(kc, ksp, NULL) == -1) { | |
| 202 | 202 | cputimes->Set(String::New("error"), String::New(strerror(errno))); | |
| 203 | 203 | cpuinfo->Set(String::New("times"), cpuinfo); | |
| 204 | 204 | } 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"); | ||
| 206 | 206 | 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"); | ||
| 208 | 208 | 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"); | ||
| 210 | 210 | 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"); | ||
| 212 | 212 | cputimes->Set(String::New("irq"), data_named(knp)); | |
| 213 | 213 | ||
| 214 | 214 | cpuinfo->Set(String::New("times"), cputimes); | |
@@ -224,12 +224,37 @@ int Platform::GetCPUInfo(Local<Array> *cpus) { | |||
| 224 | 224 | ||
| 225 | 225 | ||
| 226 | 226 | 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; | ||
| 228 | 250 | } | |
| 229 | 251 | ||
| 230 | 252 | ||
| 231 | 253 | 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; | ||
| 233 | 258 | } | |
| 234 | 259 | ||
| 235 | 260 | double Platform::GetUptimeImpl() { | |
@@ -243,12 +268,12 @@ double Platform::GetUptimeImpl() { | |||
| 243 | 268 | if ((kc = kstat_open()) == NULL) | |
| 244 | 269 | throw "could not open kstat"; | |
| 245 | 270 | ||
| 246 | - ksp = kstat_lookup(kc, "unix", 0, "system_misc"); | ||
| 271 | + ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_misc"); | ||
| 247 | 272 | ||
| 248 | 273 | if (kstat_read(kc, ksp, NULL) == -1) { | |
| 249 | 274 | throw "unable to read kstat"; | |
| 250 | 275 | } else { | |
| 251 | - knp = (kstat_named_t *) kstat_data_lookup(ksp, "clk_intr"); | ||
| 276 | + knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clk_intr"); | ||
| 252 | 277 | clk_intr = knp->value.ul; | |
| 253 | 278 | } | |
| 254 | 279 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -577,3 +577,9 @@ assert.equal(0xee, b[0]); | |||
| 577 | 577 | assert.equal(0xad, b[1]); | |
| 578 | 578 | assert.equal(0xbe, b[2]); | |
| 579 | 579 | 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 | + }); | ||
| Original file line number | Diff line number | Diff 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 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments