From 595b5974d7fcb7ee415e8e4ce6ec182a07bc9dc1 Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Wed, 23 Jan 2013 15:17:04 -0800 Subject: [PATCH] Add bytesWritten to tls.CryptoStream This adds a proxy for bytesWritten to the tls.CryptoStream. This change makes the connection object more similar between HTTP and HTTPS requests in an effort to avoid confusion. See issue #4650 for more background information. --- doc/api/tls.markdown | 9 ++++++ lib/tls.js | 4 ++- test/simple/test-http-byteswritten.js | 43 ++++++++++++++++++++++++++ test/simple/test-https-byteswritten.js | 56 ++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/simple/test-http-byteswritten.js create mode 100644 test/simple/test-https-byteswritten.js diff --git a/doc/api/tls.markdown b/doc/api/tls.markdown index ac9d642..28bc056 100644 --- a/doc/api/tls.markdown +++ b/doc/api/tls.markdown @@ -449,6 +449,15 @@ gets high. The number of concurrent connections on the server. +## Class: CryptoStream + +This is an encrypted stream. + +### cryptoStream.bytesWritten + +A proxy to the underlying socket's bytesWritten accessor, this will return +the total bytes written to the socket, *including the TLS overhead*. + ## Class: tls.CleartextStream This is a stream on top of the *Encrypted* stream that makes it possible to diff --git a/lib/tls.js b/lib/tls.js index f20dd7f..3efa3e5 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -336,6 +336,9 @@ CryptoStream.prototype.setKeepAlive = function(enable, initialDelay) { if (this.socket) this.socket.setKeepAlive(enable, initialDelay); }; +CryptoStream.prototype.__defineGetter__('bytesWritten', function() { + return this.socket ? this.socket.bytesWritten : 0; +}); CryptoStream.prototype.setEncoding = function(encoding) { var StringDecoder = require('string_decoder').StringDecoder; // lazy load @@ -689,7 +692,6 @@ CleartextStream.prototype.__defineGetter__('remotePort', function() { return this.socket && this.socket.remotePort; }); - function EncryptedStream(pair) { CryptoStream.call(this, pair); } diff --git a/test/simple/test-http-byteswritten.js b/test/simple/test-http-byteswritten.js new file mode 100644 index 0000000..b5e8e57 --- /dev/null +++ b/test/simple/test-http-byteswritten.js @@ -0,0 +1,43 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var fs = require('fs'); +var http = require('http'); + +var body = 'hello world\n'; + +var httpServer = http.createServer(function(req, res) { + res.on('finish', function() { + assert(typeof(req.connection.bytesWritten) === 'number'); + assert(req.connection.bytesWritten > 0); + httpServer.close(); + console.log('ok'); + }); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end(body); +}); + +httpServer.listen(common.PORT, function() { + http.get({ port: common.PORT }); +}); + diff --git a/test/simple/test-https-byteswritten.js b/test/simple/test-https-byteswritten.js new file mode 100644 index 0000000..21c300e --- /dev/null +++ b/test/simple/test-https-byteswritten.js @@ -0,0 +1,56 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +if (!process.versions.openssl) { + console.error('Skipping because node compiled without OpenSSL.'); + process.exit(0); +} + +var common = require('../common'); +var assert = require('assert'); +var fs = require('fs'); +var http = require('http'); +var https = require('https'); + +var options = { + key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), + cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') +}; + +var body = 'hello world\n'; + +var httpsServer = https.createServer(options, function(req, res) { + res.on('finish', function() { + assert(typeof(req.connection.bytesWritten) === 'number'); + assert(req.connection.bytesWritten > 0); + httpsServer.close(); + console.log('ok'); + }); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end(body); +}); + +httpsServer.listen(common.PORT, function() { + https.get({ + port: common.PORT, + rejectUnauthorized: false + }); +}); -- 2.7.4