diff options
Diffstat (limited to 'testing/web-platform/tests/tools/pywebsocket/src/example')
27 files changed, 0 insertions, 3853 deletions
diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/abort_handshake_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/abort_handshake_wsh.py deleted file mode 100644 index 008023a1f..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/abort_handshake_wsh.py +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright 2012, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -from mod_pywebsocket import handshake - - -def web_socket_do_extra_handshake(request): - raise handshake.AbortedByUserException( - "Aborted in web_socket_do_extra_handshake") - - -def web_socket_transfer_data(request): - pass - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/abort_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/abort_wsh.py deleted file mode 100644 index 2bbf005f6..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/abort_wsh.py +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright 2012, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -from mod_pywebsocket import handshake - - -def web_socket_do_extra_handshake(request): - pass - - -def web_socket_transfer_data(request): - raise handshake.AbortedByUserException( - "Aborted in web_socket_transfer_data") - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/arraybuffer_benchmark.html b/testing/web-platform/tests/tools/pywebsocket/src/example/arraybuffer_benchmark.html deleted file mode 100644 index 869cd7e1e..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/arraybuffer_benchmark.html +++ /dev/null @@ -1,134 +0,0 @@ -<!-- -Copyright 2013, Google Inc. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---> - -<html> -<head> -<title>ArrayBuffer benchmark</title> -<script src="util.js"></script> -<script> -var PRINT_SIZE = true; - -// Initial size of arrays. -var START_SIZE = 10 * 1024; -// Stops benchmark when the size of an array exceeds this threshold. -var STOP_THRESHOLD = 100000 * 1024; -// If the size of each array is small, write/read the array multiple times -// until the sum of sizes reaches this threshold. -var MIN_TOTAL = 100000 * 1024; -var MULTIPLIERS = [5, 2]; - -// Repeat benchmark for several times to measure performance of optimized -// (such as JIT) run. -var REPEAT_FOR_WARMUP = 3; - -function writeBenchmark(size, minTotal) { - var totalSize = 0; - while (totalSize < minTotal) { - var arrayBuffer = new ArrayBuffer(size); - - // Write 'a's. - fillArrayBuffer(arrayBuffer, 0x61); - - totalSize += size; - } - return totalSize; -} - -function readBenchmark(size, minTotal) { - var totalSize = 0; - while (totalSize < minTotal) { - var arrayBuffer = new ArrayBuffer(size); - - if (!verifyArrayBuffer(arrayBuffer, 0x00)) { - queueLog('Verification failed'); - return -1; - } - - totalSize += size; - } - return totalSize; -} - -function runBenchmark(benchmarkFunction, - size, - stopThreshold, - minTotal, - multipliers, - multiplierIndex) { - while (size <= stopThreshold) { - var maxSpeed = 0; - - for (var i = 0; i < REPEAT_FOR_WARMUP; ++i) { - var startTimeInMs = getTimeStamp(); - - var totalSize = benchmarkFunction(size, minTotal); - - maxSpeed = Math.max(maxSpeed, - calculateSpeedInKB(totalSize, startTimeInMs)); - } - queueLog(formatResultInKiB(size, maxSpeed, PRINT_SIZE)); - - size *= multipliers[multiplierIndex]; - multiplierIndex = (multiplierIndex + 1) % multipliers.length; - } -} - -function runBenchmarks() { - queueLog('Message size in KiB, Speed in kB/s'); - - queueLog('Write benchmark'); - runBenchmark( - writeBenchmark, START_SIZE, STOP_THRESHOLD, MIN_TOTAL, MULTIPLIERS, 0); - queueLog('Finished'); - - queueLog('Read benchmark'); - runBenchmark( - readBenchmark, START_SIZE, STOP_THRESHOLD, MIN_TOTAL, MULTIPLIERS, 0); - addToLog('Finished'); -} - -function init() { - logBox = document.getElementById('log'); - - queueLog(window.navigator.userAgent.toLowerCase()); - - addToLog('Started...'); - - setTimeout(runBenchmarks, 0); -} - -</script> -</head> -<body onload="init()"> -<textarea - id="log" rows="50" style="width: 100%" readonly></textarea> -</body> -</html> diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/bench_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/bench_wsh.py deleted file mode 100644 index 5067ca7d8..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/bench_wsh.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright 2011, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -"""A simple load tester for WebSocket clients. - -A client program sends a message formatted as "<time> <count> <message>" to -this handler. This handler starts sending total <count> WebSocket messages -containing <message> every <time> seconds. <time> can be a floating point -value. <count> must be an integer value. -""" - - -import time - - -def web_socket_do_extra_handshake(request): - pass # Always accept. - - -def web_socket_transfer_data(request): - line = request.ws_stream.receive_message() - parts = line.split(' ') - if len(parts) != 3: - raise ValueError('Bad parameter format') - wait = float(parts[0]) - count = int(parts[1]) - message = parts[2] - for i in xrange(count): - request.ws_stream.send_message(message) - time.sleep(wait) - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark.html b/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark.html deleted file mode 100644 index 3a218173a..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark.html +++ /dev/null @@ -1,203 +0,0 @@ -<!-- -Copyright 2013, Google Inc. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---> - -<html> -<head> -<title>WebSocket benchmark</title> -<script src="util_main.js"></script> -<script src="util.js"></script> -<script src="benchmark.js"></script> -<script> -var addressBox = null; - -function getConfig() { - return { - prefixUrl: addressBox.value, - printSize: getBoolFromCheckBox('printsize'), - numSockets: getIntFromInput('numsockets'), - // Initial size of messages. - numIterations: getIntFromInput('numiterations'), - numWarmUpIterations: getIntFromInput('numwarmupiterations'), - startSize: getIntFromInput('startsize'), - // Stops benchmark when the size of message exceeds this threshold. - stopThreshold: getIntFromInput('stopthreshold'), - // If the size of each message is small, send/receive multiple messages - // until the sum of sizes reaches this threshold. - minTotal: getIntFromInput('mintotal'), - multipliers: getIntArrayFromInput('multipliers'), - verifyData: getBoolFromCheckBox('verifydata') - }; -} - -var worker = new Worker('benchmark.js'); -worker.onmessage = onMessage; - -function onSendBenchmark() { - var config = getConfig(); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'sendBenchmark', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - sendBenchmark(config); - } -} - -function onReceiveBenchmark() { - var config = getConfig(); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'receiveBenchmark', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - receiveBenchmark(config); - } -} - -function onBatchBenchmark() { - var config = getConfig(); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'batchBenchmark', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - batchBenchmark(config); - } -} - -function onStop() { - var config = getConfig(); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'stop', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - stop(config); - } -} -function init() { - addressBox = document.getElementById('address'); - logBox = document.getElementById('log'); - - summaryBox = document.getElementById('summary'); - - var scheme = window.location.protocol == 'https:' ? 'wss://' : 'ws://'; - var defaultAddress = scheme + window.location.host + '/benchmark_helper'; - - addressBox.value = defaultAddress; - - addToLog(window.navigator.userAgent.toLowerCase()); - addToSummary(window.navigator.userAgent.toLowerCase()); - - if (!('WebSocket' in window)) { - addToLog('WebSocket is not available'); - } -} -</script> -</head> -<body onload="init()"> - -<div id="benchmark_div"> - url <input type="text" id="address" size="40"> - <input type="button" value="send" onclick="onSendBenchmark()"> - <input type="button" value="receive" onclick="onReceiveBenchmark()"> - <input type="button" value="batch" onclick="onBatchBenchmark()"> - <input type="button" value="stop" onclick="onStop()"> - - <br/> - - <input type="checkbox" id="printsize" checked> - <label for="printsize">Print size and time per message</label> - <input type="checkbox" id="verifydata" checked> - <label for="verifydata">Verify data</label> - <input type="checkbox" id="worker"> - <label for="worker">Run on worker</label> - - <br/> - - Parameters: - - <br/> - - <table> - <tr> - <td>Num sockets</td> - <td><input type="text" id="numsockets" value="1"></td> - </tr> - <tr> - <td>Number of iterations</td> - <td><input type="text" id="numiterations" value="1"></td> - </tr> - <tr> - <td>Number of warm-up iterations</td> - <td><input type="text" id="numwarmupiterations" value="0"></td> - </tr> - <tr> - <td>Start size</td> - <td><input type="text" id="startsize" value="10240"></td> - </tr> - <tr> - <td>Stop threshold</td> - <td><input type="text" id="stopthreshold" value="102400000"></td> - </tr> - <tr> - <td>Minimum total</td> - <td><input type="text" id="mintotal" value="102400000"></td> - </tr> - <tr> - <td>Multipliers</td> - <td><input type="text" id="multipliers" value="5, 2"></td> - </tr> - </table> -</div> - -<div id="log_div"> - <textarea - id="log" rows="20" style="width: 100%" readonly></textarea> -</div> -<div id="summary_div"> - Summary - <textarea - id="summary" rows="20" style="width: 100%" readonly></textarea> -</div> - -Note: Effect of RTT is not eliminated. - -</body> -</html> diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark.js b/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark.js deleted file mode 100644 index d347ae9e1..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark.js +++ /dev/null @@ -1,309 +0,0 @@ -// Copyright 2014 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the COPYING file or at -// https://developers.google.com/open-source/licenses/bsd - -if (typeof importScripts !== "undefined") { - // Running on a worker - importScripts('util.js', 'util_worker.js'); -} - -// Namespace for holding globals. -var benchmark = {startTimeInMs: 0}; - -var sockets = []; -var numEstablishedSockets = 0; - -var timerID = null; - -function destroySocket(socket) { - socket.onopen = null; - socket.onmessage = null; - socket.onerror = null; - socket.onclose = null; - socket.close(); -} - -function destroyAllSockets() { - for (var i = 0; i < sockets.length; ++i) { - destroySocket(sockets[i]); - } - sockets = []; -} - -function sendBenchmarkStep(size, config) { - timerID = null; - - var totalSize = 0; - var totalReplied = 0; - - var onMessageHandler = function(event) { - if (!verifyAcknowledgement(config, event.data, size)) { - destroyAllSockets(); - return; - } - - totalReplied += size; - - if (totalReplied < totalSize) { - return; - } - - calculateAndLogResult(config, size, benchmark.startTimeInMs, totalSize); - - runNextTask(config); - }; - - for (var i = 0; i < sockets.length; ++i) { - var socket = sockets[i]; - socket.onmessage = onMessageHandler; - } - - var dataArray = []; - - while (totalSize < config.minTotal) { - var buffer = new ArrayBuffer(size); - - fillArrayBuffer(buffer, 0x61); - - dataArray.push(buffer); - totalSize += size; - } - - benchmark.startTimeInMs = getTimeStamp(); - - totalSize = 0; - - var socketIndex = 0; - var dataIndex = 0; - while (totalSize < config.minTotal) { - var command = ['send']; - command.push(config.verifyData ? '1' : '0'); - sockets[socketIndex].send(command.join(' ')); - sockets[socketIndex].send(dataArray[dataIndex]); - socketIndex = (socketIndex + 1) % sockets.length; - - totalSize += size; - ++dataIndex; - } -} - -function receiveBenchmarkStep(size, config) { - timerID = null; - - var totalSize = 0; - var totalReplied = 0; - - var onMessageHandler = function(event) { - var bytesReceived = event.data.byteLength; - if (bytesReceived != size) { - config.addToLog('Expected ' + size + 'B but received ' + - bytesReceived + 'B'); - destroyAllSockets(); - return; - } - - if (config.verifyData && !verifyArrayBuffer(event.data, 0x61)) { - config.addToLog('Response verification failed'); - destroyAllSockets(); - return; - } - - totalReplied += bytesReceived; - - if (totalReplied < totalSize) { - return; - } - - calculateAndLogResult(config, size, benchmark.startTimeInMs, totalSize); - - runNextTask(config); - }; - - for (var i = 0; i < sockets.length; ++i) { - var socket = sockets[i]; - socket.binaryType = 'arraybuffer'; - socket.onmessage = onMessageHandler; - } - - benchmark.startTimeInMs = getTimeStamp(); - - var socketIndex = 0; - while (totalSize < config.minTotal) { - sockets[socketIndex].send('receive ' + size); - socketIndex = (socketIndex + 1) % sockets.length; - - totalSize += size; - } -} - -function createSocket(config) { - // TODO(tyoshino): Add TCP warm up. - var url = config.prefixUrl; - - config.addToLog('Connect ' + url); - - var socket = new WebSocket(url); - socket.onmessage = function(event) { - config.addToLog('Unexpected message received. Aborting.'); - }; - socket.onerror = function() { - config.addToLog('Error'); - }; - socket.onclose = function(event) { - config.addToLog('Closed'); - }; - return socket; -} - -var tasks = []; - -function startBenchmark(config) { - clearTimeout(timerID); - destroyAllSockets(); - - numEstablishedSockets = 0; - - for (var i = 0; i < config.numSockets; ++i) { - var socket = createSocket(config); - socket.onopen = function() { - config.addToLog('Opened'); - - ++numEstablishedSockets; - - if (numEstablishedSockets == sockets.length) { - runNextTask(config); - } - }; - sockets.push(socket); - } -} - -function runNextTask(config) { - var task = tasks.shift(); - if (task == undefined) { - config.addToLog('Finished'); - destroyAllSockets(); - return; - } - timerID = setTimeout(task, 0); -} - -function buildLegendString(config) { - var legend = '' - if (config.printSize) - legend = 'Message size in KiB, Time/message in ms, '; - legend += 'Speed in kB/s'; - return legend; -} - -function getConfigString(config) { - return '(WebSocket' + - ', ' + (typeof importScripts !== "undefined" ? 'Worker' : 'Main') + - ', numSockets=' + config.numSockets + - ', numIterations=' + config.numIterations + - ', verifyData=' + config.verifyData + - ', minTotal=' + config.minTotal + - ', numWarmUpIterations=' + config.numWarmUpIterations + - ')'; -} - -function addTasks(config, stepFunc) { - for (var i = 0; - i < config.numWarmUpIterations + config.numIterations; ++i) { - // Ignore the first |config.numWarmUpIterations| iterations. - if (i == config.numWarmUpIterations) - addResultClearingTask(config); - - var multiplierIndex = 0; - for (var size = config.startSize; - size <= config.stopThreshold; - ++multiplierIndex) { - var task = stepFunc.bind( - null, - size, - config); - tasks.push(task); - size *= config.multipliers[ - multiplierIndex % config.multipliers.length]; - } - } -} - -function addResultReportingTask(config, title) { - tasks.push(function(){ - timerID = null; - config.addToSummary(title); - reportAverageData(config); - clearAverageData(); - runNextTask(config); - }); -} - -function addResultClearingTask(config) { - tasks.push(function(){ - timerID = null; - clearAverageData(); - runNextTask(config); - }); -} - -function sendBenchmark(config) { - config.addToLog('Send benchmark'); - config.addToLog(buildLegendString(config)); - - tasks = []; - clearAverageData(); - addTasks(config, sendBenchmarkStep); - addResultReportingTask(config, 'Send Benchmark ' + getConfigString(config)); - startBenchmark(config); -} - -function receiveBenchmark(config) { - config.addToLog('Receive benchmark'); - config.addToLog(buildLegendString(config)); - - tasks = []; - clearAverageData(); - addTasks(config, receiveBenchmarkStep); - addResultReportingTask(config, - 'Receive Benchmark ' + getConfigString(config)); - startBenchmark(config); -} - -function batchBenchmark(config) { - config.addToLog('Batch benchmark'); - config.addToLog(buildLegendString(config)); - - tasks = []; - clearAverageData(); - addTasks(config, sendBenchmarkStep); - addResultReportingTask(config, 'Send Benchmark ' + getConfigString(config)); - addTasks(config, receiveBenchmarkStep); - addResultReportingTask(config, 'Receive Benchmark ' + - getConfigString(config)); - startBenchmark(config); -} - -function stop(config) { - clearTimeout(timerID); - timerID = null; - config.addToLog('Stopped'); - destroyAllSockets(); -} - -onmessage = function (message) { - var config = message.data.config; - config.addToLog = workerAddToLog; - config.addToSummary = workerAddToSummary; - config.measureValue = workerMeasureValue; - if (message.data.type === 'sendBenchmark') - sendBenchmark(config); - else if (message.data.type === 'receiveBenchmark') - receiveBenchmark(config); - else if (message.data.type === 'batchBenchmark') - batchBenchmark(config); - else if (message.data.type === 'stop') - stop(config); -}; diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark_helper_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark_helper_wsh.py deleted file mode 100644 index 44ad0bfee..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/benchmark_helper_wsh.py +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright 2013, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -"""Handler for benchmark.html.""" - - -def web_socket_do_extra_handshake(request): - # Turn off compression. - request.ws_extension_processors = [] - - -def web_socket_transfer_data(request): - data = '' - - while True: - command = request.ws_stream.receive_message() - if command is None: - return - - if not isinstance(command, unicode): - raise ValueError('Invalid command data:' + command) - commands = command.split(' ') - if len(commands) == 0: - raise ValueError('Invalid command data: ' + command) - - if commands[0] == 'receive': - if len(commands) != 2: - raise ValueError( - 'Illegal number of arguments for send command' + - command) - size = int(commands[1]) - - # Reuse data if possible. - if len(data) != size: - data = 'a' * size - request.ws_stream.send_message(data, binary=True) - elif commands[0] == 'send': - if len(commands) != 2: - raise ValueError( - 'Illegal number of arguments for receive command' + - command) - verify_data = commands[1] == '1' - - data = request.ws_stream.receive_message() - if data is None: - raise ValueError('Payload not received') - size = len(data) - - if verify_data: - if data != 'a' * size: - raise ValueError('Payload verification failed') - - request.ws_stream.send_message(str(size)) - else: - raise ValueError('Invalid command: ' + commands[0]) - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/close_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/close_wsh.py deleted file mode 100644 index 26b083840..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/close_wsh.py +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright 2012, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -import struct - -from mod_pywebsocket import common -from mod_pywebsocket import stream - - -def web_socket_do_extra_handshake(request): - pass - - -def web_socket_transfer_data(request): - while True: - line = request.ws_stream.receive_message() - if line is None: - return - code, reason = line.split(' ', 1) - if code is None or reason is None: - return - request.ws_stream.close_connection(int(code), reason) - # close_connection() initiates closing handshake. It validates code - # and reason. If you want to send a broken close frame for a test, - # following code will be useful. - # > data = struct.pack('!H', int(code)) + reason.encode('UTF-8') - # > request.connection.write(stream.create_close_frame(data)) - # > # Suppress to re-respond client responding close frame. - # > raise Exception("customized server initiated closing handshake") - - -def web_socket_passive_closing_handshake(request): - # Simply echo a close status code - code, reason = request.ws_close_code, request.ws_close_reason - - # pywebsocket sets pseudo code for receiving an empty body close frame. - if code == common.STATUS_NO_STATUS_RECEIVED: - code = None - reason = '' - return code, reason - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/console.html b/testing/web-platform/tests/tools/pywebsocket/src/example/console.html deleted file mode 100644 index ccd6d8f80..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/console.html +++ /dev/null @@ -1,317 +0,0 @@ -<!-- -Copyright 2011, Google Inc. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---> - -<!-- -A simple console for testing WebSocket server. - -Type an address into the top text input and click connect to establish -WebSocket. Then, type some message into the bottom text input and click send -to send the message. Received/sent messages and connection state will be shown -on the middle textarea. ---> - -<html> -<head> -<title>WebSocket console</title> -<script> -var socket = null; - -var showTimeStamp = false; - -var addressBox = null; -var protocolsBox = null; -var logBox = null; -var messageBox = null; -var fileBox = null; -var codeBox = null; -var reasonBox = null; - -function getTimeStamp() { - return new Date().getTime(); -} - -function addToLog(log) { - if (showTimeStamp) { - logBox.value += '[' + getTimeStamp() + '] '; - } - logBox.value += log + '\n' - // Large enough to keep showing the latest message. - logBox.scrollTop = 1000000; -} - -function setbinarytype(binaryType) { - if (!socket) { - addToLog('Not connected'); - return; - } - - socket.binaryType = binaryType; - addToLog('Set binaryType to ' + binaryType); -} - -function send() { - if (!socket) { - addToLog('Not connected'); - return; - } - - socket.send(messageBox.value); - addToLog('> ' + messageBox.value); - messageBox.value = ''; -} - -function sendfile() { - if (!socket) { - addToLog('Not connected'); - return; - } - - var files = fileBox.files; - - if (files.length == 0) { - addToLog('File not selected'); - return; - } - - socket.send(files[0]); - addToLog('> Send ' + files[0].name); -} - -function parseProtocols(protocolsText) { - var protocols = protocolsText.split(','); - for (var i = 0; i < protocols.length; ++i) { - protocols[i] = protocols[i].trim(); - } - - if (protocols.length == 0) { - // Don't pass. - protocols = null; - } else if (protocols.length == 1) { - if (protocols[0].length == 0) { - // Don't pass. - protocols = null; - } else { - // Pass as a string. - protocols = protocols[0]; - } - } - - return protocols; -} - -function connect() { - var url = addressBox.value; - var protocols = parseProtocols(protocolsBox.value); - - if ('WebSocket' in window) { - if (protocols) { - socket = new WebSocket(url, protocols); - } else { - socket = new WebSocket(url); - } - } else { - return; - } - - socket.onopen = function () { - var extraInfo = []; - if (('protocol' in socket) && socket.protocol) { - extraInfo.push('protocol = ' + socket.protocol); - } - if (('extensions' in socket) && socket.extensions) { - extraInfo.push('extensions = ' + socket.extensions); - } - - var logMessage = 'Opened'; - if (extraInfo.length > 0) { - logMessage += ' (' + extraInfo.join(', ') + ')'; - } - addToLog(logMessage); - }; - socket.onmessage = function (event) { - if (('ArrayBuffer' in window) && (event.data instanceof ArrayBuffer)) { - addToLog('< Received an ArrayBuffer of ' + event.data.byteLength + - ' bytes') - } else if (('Blob' in window) && (event.data instanceof Blob)) { - addToLog('< Received a Blob of ' + event.data.size + ' bytes') - } else { - addToLog('< ' + event.data); - } - }; - socket.onerror = function () { - addToLog('Error'); - }; - socket.onclose = function (event) { - var logMessage = 'Closed ('; - if ((arguments.length == 1) && ('CloseEvent' in window) && - (event instanceof CloseEvent)) { - logMessage += 'wasClean = ' + event.wasClean; - // code and reason are present only for - // draft-ietf-hybi-thewebsocketprotocol-06 and later - if ('code' in event) { - logMessage += ', code = ' + event.code; - } - if ('reason' in event) { - logMessage += ', reason = ' + event.reason; - } - } else { - logMessage += 'CloseEvent is not available'; - } - addToLog(logMessage + ')'); - }; - - if (protocols) { - addToLog('Connect ' + url + ' (protocols = ' + protocols + ')'); - } else { - addToLog('Connect ' + url); - } -} - -function closeSocket() { - if (!socket) { - addToLog('Not connected'); - return; - } - - if (codeBox.value || reasonBox.value) { - socket.close(codeBox.value, reasonBox.value); - } else { - socket.close(); - } -} - -function printState() { - if (!socket) { - addToLog('Not connected'); - return; - } - - addToLog( - 'url = ' + socket.url + - ', readyState = ' + socket.readyState + - ', bufferedAmount = ' + socket.bufferedAmount); -} - -function init() { - var scheme = window.location.protocol == 'https:' ? 'wss://' : 'ws://'; - var defaultAddress = scheme + window.location.host + '/echo'; - - addressBox = document.getElementById('address'); - protocolsBox = document.getElementById('protocols'); - logBox = document.getElementById('log'); - messageBox = document.getElementById('message'); - fileBox = document.getElementById('file'); - codeBox = document.getElementById('code'); - reasonBox = document.getElementById('reason'); - - addressBox.value = defaultAddress; - - if (!('WebSocket' in window)) { - addToLog('WebSocket is not available'); - } -} -</script> -<style type="text/css"> -form { - margin: 0px; -} - -#connect_div, #log_div, #send_div, #sendfile_div, #close_div, #printstate_div { - padding: 5px; - margin: 5px; - border-width: 0px 0px 0px 10px; - border-style: solid; - border-color: silver; -} -</style> -</head> -<body onload="init()"> - -<div> - -<div id="connect_div"> - <form action="#" onsubmit="connect(); return false;"> - url <input type="text" id="address" size="40"> - <input type="submit" value="connect"> - <br/> - protocols <input type="text" id="protocols" size="20"> - </form> -</div> - -<div id="log_div"> - <textarea id="log" rows="10" cols="40" readonly></textarea> - <br/> - <input type="checkbox" - name="showtimestamp" - value="showtimestamp" - onclick="showTimeStamp = this.checked">Show time stamp -</div> - -<div id="send_div"> - <form action="#" onsubmit="send(); return false;"> - data <input type="text" id="message" size="40"> - <input type="submit" value="send"> - </form> -</div> - -<div id="sendfile_div"> - <form action="#" onsubmit="sendfile(); return false;"> - <input type="file" id="file" size="40"> - <input type="submit" value="send file"> - </form> - - Set binaryType - <input type="radio" - name="binarytype" - value="blob" - onclick="setbinarytype('blob')" checked>blob - <input type="radio" - name="binarytype" - value="arraybuffer" - onclick="setbinarytype('arraybuffer')">arraybuffer -</div> - -<div id="close_div"> - <form action="#" onsubmit="closeSocket(); return false;"> - code <input type="text" id="code" size="10"> - reason <input type="text" id="reason" size="20"> - <input type="submit" value="close"> - </form> -</div> - -<div id="printstate_div"> - <input type="button" value="print state" onclick="printState();"> -</div> - -</div> - -</body> -</html> diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/cookie_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/cookie_wsh.py deleted file mode 100644 index 8b327152e..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/cookie_wsh.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2014 Google Inc. All rights reserved. -# -# Use of this source code is governed by a BSD-style -# license that can be found in the COPYING file or at -# https://developers.google.com/open-source/licenses/bsd - - -import urlparse - - -def _add_set_cookie(request, value): - request.extra_headers.append(('Set-Cookie', value)) - - -def web_socket_do_extra_handshake(request): - components = urlparse.urlparse(request.uri) - command = components[4] - - ONE_DAY_LIFE = 'Max-Age=86400' - - if command == 'set': - _add_set_cookie(request, '; '.join(['foo=bar', ONE_DAY_LIFE])) - elif command == 'set_httponly': - _add_set_cookie(request, - '; '.join(['httpOnlyFoo=bar', ONE_DAY_LIFE, 'httpOnly'])) - elif command == 'clear': - _add_set_cookie(request, 'foo=0; Max-Age=0') - _add_set_cookie(request, 'httpOnlyFoo=0; Max-Age=0') - - -def web_socket_transfer_data(request): - pass diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/echo_client.py b/testing/web-platform/tests/tools/pywebsocket/src/example/echo_client.py deleted file mode 100755 index 943ce64e8..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/echo_client.py +++ /dev/null @@ -1,1128 +0,0 @@ -#!/usr/bin/env python -# -# Copyright 2011, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -"""Simple WebSocket client named echo_client just because of historical reason. - -mod_pywebsocket directory must be in PYTHONPATH. - -Example Usage: - -# server setup - % cd $pywebsocket - % PYTHONPATH=$cwd/src python ./mod_pywebsocket/standalone.py -p 8880 \ - -d $cwd/src/example - -# run client - % PYTHONPATH=$cwd/src python ./src/example/echo_client.py -p 8880 \ - -s localhost \ - -o http://localhost -r /echo -m test - -or - -# run echo client to test IETF HyBi 00 protocol - run with --protocol-version=hybi00 -""" - - -import base64 -import codecs -import logging -from optparse import OptionParser -import os -import random -import re -import socket -import struct -import sys - -from mod_pywebsocket import common -from mod_pywebsocket.extensions import DeflateFrameExtensionProcessor -from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor -from mod_pywebsocket.extensions import _PerMessageDeflateFramer -from mod_pywebsocket.extensions import _parse_window_bits -from mod_pywebsocket.stream import Stream -from mod_pywebsocket.stream import StreamHixie75 -from mod_pywebsocket.stream import StreamOptions -from mod_pywebsocket import util - - -_TIMEOUT_SEC = 10 -_UNDEFINED_PORT = -1 - -_UPGRADE_HEADER = 'Upgrade: websocket\r\n' -_UPGRADE_HEADER_HIXIE75 = 'Upgrade: WebSocket\r\n' -_CONNECTION_HEADER = 'Connection: Upgrade\r\n' - -# Special message that tells the echo server to start closing handshake -_GOODBYE_MESSAGE = 'Goodbye' - -_PROTOCOL_VERSION_HYBI13 = 'hybi13' -_PROTOCOL_VERSION_HYBI08 = 'hybi08' -_PROTOCOL_VERSION_HYBI00 = 'hybi00' -_PROTOCOL_VERSION_HIXIE75 = 'hixie75' - -# Constants for the --tls_module flag. -_TLS_BY_STANDARD_MODULE = 'ssl' -_TLS_BY_PYOPENSSL = 'pyopenssl' - -# Values used by the --tls-version flag. -_TLS_VERSION_SSL23 = 'ssl23' -_TLS_VERSION_SSL3 = 'ssl3' -_TLS_VERSION_TLS1 = 'tls1' - - -class ClientHandshakeError(Exception): - pass - - -def _build_method_line(resource): - return 'GET %s HTTP/1.1\r\n' % resource - - -def _origin_header(header, origin): - # 4.1 13. concatenation of the string "Origin:", a U+0020 SPACE character, - # and the /origin/ value, converted to ASCII lowercase, to /fields/. - return '%s: %s\r\n' % (header, origin.lower()) - - -def _format_host_header(host, port, secure): - # 4.1 9. Let /hostport/ be an empty string. - # 4.1 10. Append the /host/ value, converted to ASCII lowercase, to - # /hostport/ - hostport = host.lower() - # 4.1 11. If /secure/ is false, and /port/ is not 80, or if /secure/ - # is true, and /port/ is not 443, then append a U+003A COLON character - # (:) followed by the value of /port/, expressed as a base-ten integer, - # to /hostport/ - if ((not secure and port != common.DEFAULT_WEB_SOCKET_PORT) or - (secure and port != common.DEFAULT_WEB_SOCKET_SECURE_PORT)): - hostport += ':' + str(port) - # 4.1 12. concatenation of the string "Host:", a U+0020 SPACE - # character, and /hostport/, to /fields/. - return '%s: %s\r\n' % (common.HOST_HEADER, hostport) - - -def _receive_bytes(socket, length): - bytes = [] - remaining = length - while remaining > 0: - received_bytes = socket.recv(remaining) - if not received_bytes: - raise IOError( - 'Connection closed before receiving requested length ' - '(requested %d bytes but received only %d bytes)' % - (length, length - remaining)) - bytes.append(received_bytes) - remaining -= len(received_bytes) - return ''.join(bytes) - - -def _get_mandatory_header(fields, name): - """Gets the value of the header specified by name from fields. - - This function expects that there's only one header with the specified name - in fields. Otherwise, raises an ClientHandshakeError. - """ - - values = fields.get(name.lower()) - if values is None or len(values) == 0: - raise ClientHandshakeError( - '%s header not found: %r' % (name, values)) - if len(values) > 1: - raise ClientHandshakeError( - 'Multiple %s headers found: %r' % (name, values)) - return values[0] - - -def _validate_mandatory_header(fields, name, - expected_value, case_sensitive=False): - """Gets and validates the value of the header specified by name from - fields. - - If expected_value is specified, compares expected value and actual value - and raises an ClientHandshakeError on failure. You can specify case - sensitiveness in this comparison by case_sensitive parameter. This function - expects that there's only one header with the specified name in fields. - Otherwise, raises an ClientHandshakeError. - """ - - value = _get_mandatory_header(fields, name) - - if ((case_sensitive and value != expected_value) or - (not case_sensitive and value.lower() != expected_value.lower())): - raise ClientHandshakeError( - 'Illegal value for header %s: %r (expected) vs %r (actual)' % - (name, expected_value, value)) - - -class _TLSSocket(object): - """Wrapper for a TLS connection.""" - - def __init__(self, - raw_socket, tls_module, tls_version, disable_tls_compression): - self._logger = util.get_class_logger(self) - - if tls_module == _TLS_BY_STANDARD_MODULE: - if tls_version == _TLS_VERSION_SSL23: - version = ssl.PROTOCOL_SSLv23 - elif tls_version == _TLS_VERSION_SSL3: - version = ssl.PROTOCOL_SSLv3 - elif tls_version == _TLS_VERSION_TLS1: - version = ssl.PROTOCOL_TLSv1 - else: - raise ValueError( - 'Invalid --tls-version flag: %r' % tls_version) - - if disable_tls_compression: - raise ValueError( - '--disable-tls-compression is not available for ssl ' - 'module') - - self._tls_socket = ssl.wrap_socket(raw_socket, ssl_version=version) - - # Print cipher in use. Handshake is done on wrap_socket call. - self._logger.info("Cipher: %s", self._tls_socket.cipher()) - elif tls_module == _TLS_BY_PYOPENSSL: - if tls_version == _TLS_VERSION_SSL23: - version = OpenSSL.SSL.SSLv23_METHOD - elif tls_version == _TLS_VERSION_SSL3: - version = OpenSSL.SSL.SSLv3_METHOD - elif tls_version == _TLS_VERSION_TLS1: - version = OpenSSL.SSL.TLSv1_METHOD - else: - raise ValueError( - 'Invalid --tls-version flag: %r' % tls_version) - - context = OpenSSL.SSL.Context(version) - - if disable_tls_compression: - # OP_NO_COMPRESSION is not defined in OpenSSL module. - context.set_options(0x00020000) - - self._tls_socket = OpenSSL.SSL.Connection(context, raw_socket) - # Client mode. - self._tls_socket.set_connect_state() - self._tls_socket.setblocking(True) - - # Do handshake now (not necessary). - self._tls_socket.do_handshake() - else: - raise ValueError('No TLS support module is available') - - def send(self, data): - return self._tls_socket.write(data) - - def sendall(self, data): - return self._tls_socket.sendall(data) - - def recv(self, size=-1): - return self._tls_socket.read(size) - - def close(self): - return self._tls_socket.close() - - def getpeername(self): - return self._tls_socket.getpeername() - - -class ClientHandshakeBase(object): - """A base class for WebSocket opening handshake processors for each - protocol version. - """ - - def __init__(self): - self._logger = util.get_class_logger(self) - - def _read_fields(self): - # 4.1 32. let /fields/ be a list of name-value pairs, initially empty. - fields = {} - while True: # "Field" - # 4.1 33. let /name/ and /value/ be empty byte arrays - name = '' - value = '' - # 4.1 34. read /name/ - name = self._read_name() - if name is None: - break - # 4.1 35. read spaces - # TODO(tyoshino): Skip only one space as described in the spec. - ch = self._skip_spaces() - # 4.1 36. read /value/ - value = self._read_value(ch) - # 4.1 37. read a byte from the server - ch = _receive_bytes(self._socket, 1) - if ch != '\n': # 0x0A - raise ClientHandshakeError( - 'Expected LF but found %r while reading value %r for ' - 'header %r' % (ch, value, name)) - self._logger.debug('Received %r header', name) - # 4.1 38. append an entry to the /fields/ list that has the name - # given by the string obtained by interpreting the /name/ byte - # array as a UTF-8 stream and the value given by the string - # obtained by interpreting the /value/ byte array as a UTF-8 byte - # stream. - fields.setdefault(name, []).append(value) - # 4.1 39. return to the "Field" step above - return fields - - def _read_name(self): - # 4.1 33. let /name/ be empty byte arrays - name = '' - while True: - # 4.1 34. read a byte from the server - ch = _receive_bytes(self._socket, 1) - if ch == '\r': # 0x0D - return None - elif ch == '\n': # 0x0A - raise ClientHandshakeError( - 'Unexpected LF when reading header name %r' % name) - elif ch == ':': # 0x3A - return name - elif ch >= 'A' and ch <= 'Z': # Range 0x31 to 0x5A - ch = chr(ord(ch) + 0x20) - name += ch - else: - name += ch - - def _skip_spaces(self): - # 4.1 35. read a byte from the server - while True: - ch = _receive_bytes(self._socket, 1) - if ch == ' ': # 0x20 - continue - return ch - - def _read_value(self, ch): - # 4.1 33. let /value/ be empty byte arrays - value = '' - # 4.1 36. read a byte from server. - while True: - if ch == '\r': # 0x0D - return value - elif ch == '\n': # 0x0A - raise ClientHandshakeError( - 'Unexpected LF when reading header value %r' % value) - else: - value += ch - ch = _receive_bytes(self._socket, 1) - - -def _get_permessage_deflate_framer(extension_response): - """Validate the response and return a framer object using the parameters in - the response. This method doesn't accept the server_.* parameters. - """ - - client_max_window_bits = None - client_no_context_takeover = None - - client_max_window_bits_name = ( - PerMessageDeflateExtensionProcessor. - _CLIENT_MAX_WINDOW_BITS_PARAM) - client_no_context_takeover_name = ( - PerMessageDeflateExtensionProcessor. - _CLIENT_NO_CONTEXT_TAKEOVER_PARAM) - - # We didn't send any server_.* parameter. - # Handle those parameters as invalid if found in the response. - - for param_name, param_value in extension_response.get_parameters(): - if param_name == client_max_window_bits_name: - if client_max_window_bits is not None: - raise ClientHandshakeError( - 'Multiple %s found' % client_max_window_bits_name) - - parsed_value = _parse_window_bits(param_value) - if parsed_value is None: - raise ClientHandshakeError( - 'Bad %s: %r' % - (client_max_window_bits_name, param_value)) - client_max_window_bits = parsed_value - elif param_name == client_no_context_takeover_name: - if client_no_context_takeover is not None: - raise ClientHandshakeError( - 'Multiple %s found' % client_no_context_takeover_name) - - if param_value is not None: - raise ClientHandshakeError( - 'Bad %s: Has value %r' % - (client_no_context_takeover_name, param_value)) - client_no_context_takeover = True - - if client_no_context_takeover is None: - client_no_context_takeover = False - - return _PerMessageDeflateFramer(client_max_window_bits, - client_no_context_takeover) - - -class ClientHandshakeProcessor(ClientHandshakeBase): - """WebSocket opening handshake processor for - draft-ietf-hybi-thewebsocketprotocol-06 and later. - """ - - def __init__(self, socket, options): - super(ClientHandshakeProcessor, self).__init__() - - self._socket = socket - self._options = options - - self._logger = util.get_class_logger(self) - - def handshake(self): - """Performs opening handshake on the specified socket. - - Raises: - ClientHandshakeError: handshake failed. - """ - - request_line = _build_method_line(self._options.resource) - self._logger.debug('Client\'s opening handshake Request-Line: %r', - request_line) - self._socket.sendall(request_line) - - fields = [] - fields.append(_format_host_header( - self._options.server_host, - self._options.server_port, - self._options.use_tls)) - fields.append(_UPGRADE_HEADER) - fields.append(_CONNECTION_HEADER) - if self._options.origin is not None: - if self._options.protocol_version == _PROTOCOL_VERSION_HYBI08: - fields.append(_origin_header( - common.SEC_WEBSOCKET_ORIGIN_HEADER, - self._options.origin)) - else: - fields.append(_origin_header(common.ORIGIN_HEADER, - self._options.origin)) - - original_key = os.urandom(16) - self._key = base64.b64encode(original_key) - self._logger.debug( - '%s: %r (%s)', - common.SEC_WEBSOCKET_KEY_HEADER, - self._key, - util.hexify(original_key)) - fields.append( - '%s: %s\r\n' % (common.SEC_WEBSOCKET_KEY_HEADER, self._key)) - - if self._options.version_header > 0: - fields.append('%s: %d\r\n' % (common.SEC_WEBSOCKET_VERSION_HEADER, - self._options.version_header)) - elif self._options.protocol_version == _PROTOCOL_VERSION_HYBI08: - fields.append('%s: %d\r\n' % (common.SEC_WEBSOCKET_VERSION_HEADER, - common.VERSION_HYBI08)) - else: - fields.append('%s: %d\r\n' % (common.SEC_WEBSOCKET_VERSION_HEADER, - common.VERSION_HYBI_LATEST)) - - extensions_to_request = [] - - if self._options.deflate_frame: - extensions_to_request.append( - common.ExtensionParameter(common.DEFLATE_FRAME_EXTENSION)) - - if self._options.use_permessage_deflate: - extension = common.ExtensionParameter( - common.PERMESSAGE_DEFLATE_EXTENSION) - # Accept the client_max_window_bits extension parameter by default. - extension.add_parameter( - PerMessageDeflateExtensionProcessor. - _CLIENT_MAX_WINDOW_BITS_PARAM, - None) - extensions_to_request.append(extension) - - if len(extensions_to_request) != 0: - fields.append( - '%s: %s\r\n' % - (common.SEC_WEBSOCKET_EXTENSIONS_HEADER, - common.format_extensions(extensions_to_request))) - - for field in fields: - self._socket.sendall(field) - - self._socket.sendall('\r\n') - - self._logger.debug('Sent client\'s opening handshake headers: %r', - fields) - self._logger.debug('Start reading Status-Line') - - status_line = '' - while True: - ch = _receive_bytes(self._socket, 1) - status_line += ch - if ch == '\n': - break - - m = re.match('HTTP/\\d+\.\\d+ (\\d\\d\\d) .*\r\n', status_line) - if m is None: - raise ClientHandshakeError( - 'Wrong status line format: %r' % status_line) - status_code = m.group(1) - if status_code != '101': - self._logger.debug('Unexpected status code %s with following ' - 'headers: %r', status_code, self._read_fields()) - raise ClientHandshakeError( - 'Expected HTTP status code 101 but found %r' % status_code) - - self._logger.debug('Received valid Status-Line') - self._logger.debug('Start reading headers until we see an empty line') - - fields = self._read_fields() - - ch = _receive_bytes(self._socket, 1) - if ch != '\n': # 0x0A - raise ClientHandshakeError( - 'Expected LF but found %r while reading value %r for header ' - 'name %r' % (ch, value, name)) - - self._logger.debug('Received an empty line') - self._logger.debug('Server\'s opening handshake headers: %r', fields) - - _validate_mandatory_header( - fields, - common.UPGRADE_HEADER, - common.WEBSOCKET_UPGRADE_TYPE, - False) - - _validate_mandatory_header( - fields, - common.CONNECTION_HEADER, - common.UPGRADE_CONNECTION_TYPE, - False) - - accept = _get_mandatory_header( - fields, common.SEC_WEBSOCKET_ACCEPT_HEADER) - - # Validate - try: - binary_accept = base64.b64decode(accept) - except TypeError, e: - raise HandshakeError( - 'Illegal value for header %s: %r' % - (common.SEC_WEBSOCKET_ACCEPT_HEADER, accept)) - - if len(binary_accept) != 20: - raise ClientHandshakeError( - 'Decoded value of %s is not 20-byte long' % - common.SEC_WEBSOCKET_ACCEPT_HEADER) - - self._logger.debug( - 'Response for challenge : %r (%s)', - accept, util.hexify(binary_accept)) - - binary_expected_accept = util.sha1_hash( - self._key + common.WEBSOCKET_ACCEPT_UUID).digest() - expected_accept = base64.b64encode(binary_expected_accept) - - self._logger.debug( - 'Expected response for challenge: %r (%s)', - expected_accept, util.hexify(binary_expected_accept)) - - if accept != expected_accept: - raise ClientHandshakeError( - 'Invalid %s header: %r (expected: %s)' % - (common.SEC_WEBSOCKET_ACCEPT_HEADER, accept, expected_accept)) - - deflate_frame_accepted = False - permessage_deflate_accepted = False - - extensions_header = fields.get( - common.SEC_WEBSOCKET_EXTENSIONS_HEADER.lower()) - accepted_extensions = [] - if extensions_header is not None and len(extensions_header) != 0: - accepted_extensions = common.parse_extensions(extensions_header[0]) - - # TODO(bashi): Support the new style perframe compression extension. - for extension in accepted_extensions: - extension_name = extension.name() - if (extension_name == common.DEFLATE_FRAME_EXTENSION and - self._options.deflate_frame): - deflate_frame_accepted = True - processor = DeflateFrameExtensionProcessor(extension) - unused_extension_response = processor.get_extension_response() - self._options.deflate_frame = processor - continue - elif (extension_name == common.PERMESSAGE_DEFLATE_EXTENSION and - self._options.use_permessage_deflate): - permessage_deflate_accepted = True - - framer = _get_permessage_deflate_framer(extension) - framer.set_compress_outgoing_enabled(True) - self._options.use_permessage_deflate = framer - continue - - raise ClientHandshakeError( - 'Unexpected extension %r' % extension_name) - - if (self._options.deflate_frame and not deflate_frame_accepted): - raise ClientHandshakeError( - 'Requested %s, but the server rejected it' % - common.DEFLATE_FRAME_EXTENSION) - - if (self._options.use_permessage_deflate and - not permessage_deflate_accepted): - raise ClientHandshakeError( - 'Requested %s, but the server rejected it' % - common.PERMESSAGE_DEFLATE_EXTENSION) - - # TODO(tyoshino): Handle Sec-WebSocket-Protocol - # TODO(tyoshino): Handle Cookie, etc. - - -class ClientHandshakeProcessorHybi00(ClientHandshakeBase): - """WebSocket opening handshake processor for - draft-ietf-hybi-thewebsocketprotocol-00 (equivalent to - draft-hixie-thewebsocketprotocol-76). - """ - - def __init__(self, socket, options): - super(ClientHandshakeProcessorHybi00, self).__init__() - - self._socket = socket - self._options = options - - self._logger = util.get_class_logger(self) - - if (self._options.deflate_frame or - self._options.use_permessage_deflate): - logging.critical('HyBi 00 doesn\'t support extensions.') - sys.exit(1) - - def handshake(self): - """Performs opening handshake on the specified socket. - - Raises: - ClientHandshakeError: handshake failed. - """ - - # 4.1 5. send request line. - self._socket.sendall(_build_method_line(self._options.resource)) - # 4.1 6. Let /fields/ be an empty list of strings. - fields = [] - # 4.1 7. Add the string "Upgrade: WebSocket" to /fields/. - fields.append(_UPGRADE_HEADER_HIXIE75) - # 4.1 8. Add the string "Connection: Upgrade" to /fields/. - fields.append(_CONNECTION_HEADER) - # 4.1 9-12. Add Host: field to /fields/. - fields.append(_format_host_header( - self._options.server_host, - self._options.server_port, - self._options.use_tls)) - # 4.1 13. Add Origin: field to /fields/. - if not self._options.origin: - raise ClientHandshakeError( - 'Specify the origin of the connection by --origin flag') - fields.append(_origin_header(common.ORIGIN_HEADER, - self._options.origin)) - # TODO: 4.1 14 Add Sec-WebSocket-Protocol: field to /fields/. - # TODO: 4.1 15 Add cookie headers to /fields/. - - # 4.1 16-23. Add Sec-WebSocket-Key<n> to /fields/. - self._number1, key1 = self._generate_sec_websocket_key() - self._logger.debug('Number1: %d', self._number1) - fields.append('%s: %s\r\n' % (common.SEC_WEBSOCKET_KEY1_HEADER, key1)) - self._number2, key2 = self._generate_sec_websocket_key() - self._logger.debug('Number2: %d', self._number2) - fields.append('%s: %s\r\n' % (common.SEC_WEBSOCKET_KEY2_HEADER, key2)) - - fields.append('%s: 0\r\n' % common.SEC_WEBSOCKET_DRAFT_HEADER) - - # 4.1 24. For each string in /fields/, in a random order: send the - # string, encoded as UTF-8, followed by a UTF-8 encoded U+000D CARRIAGE - # RETURN U+000A LINE FEED character pair (CRLF). - random.shuffle(fields) - for field in fields: - self._socket.sendall(field) - # 4.1 25. send a UTF-8-encoded U+000D CARRIAGE RETURN U+000A LINE FEED - # character pair (CRLF). - self._socket.sendall('\r\n') - # 4.1 26. let /key3/ be a string consisting of eight random bytes (or - # equivalently, a random 64 bit integer encoded in a big-endian order). - self._key3 = self._generate_key3() - # 4.1 27. send /key3/ to the server. - self._socket.sendall(self._key3) - self._logger.debug( - 'Key3: %r (%s)', self._key3, util.hexify(self._key3)) - - self._logger.info('Sent handshake') - - # 4.1 28. Read bytes from the server until either the connection - # closes, or a 0x0A byte is read. let /field/ be these bytes, including - # the 0x0A bytes. - field = '' - while True: - ch = _receive_bytes(self._socket, 1) - field += ch - if ch == '\n': - break - # if /field/ is not at least seven bytes long, or if the last - # two bytes aren't 0x0D and 0x0A respectively, or if it does not - # contain at least two 0x20 bytes, then fail the WebSocket connection - # and abort these steps. - if len(field) < 7 or not field.endswith('\r\n'): - raise ClientHandshakeError('Wrong status line: %r' % field) - m = re.match('[^ ]* ([^ ]*) .*', field) - if m is None: - raise ClientHandshakeError( - 'No HTTP status code found in status line: %r' % field) - # 4.1 29. let /code/ be the substring of /field/ that starts from the - # byte after the first 0x20 byte, and ends with the byte before the - # second 0x20 byte. - code = m.group(1) - # 4.1 30. if /code/ is not three bytes long, or if any of the bytes in - # /code/ are not in the range 0x30 to 0x90, then fail the WebSocket - # connection and abort these steps. - if not re.match('[0-9][0-9][0-9]', code): - raise ClientHandshakeError( - 'HTTP status code %r is not three digit in status line: %r' % - (code, field)) - # 4.1 31. if /code/, interpreted as UTF-8, is "101", then move to the - # next step. - if code != '101': - raise ClientHandshakeError( - 'Expected HTTP status code 101 but found %r in status line: ' - '%r' % (code, field)) - # 4.1 32-39. read fields into /fields/ - fields = self._read_fields() - # 4.1 40. _Fields processing_ - # read a byte from server - ch = _receive_bytes(self._socket, 1) - if ch != '\n': # 0x0A - raise ClientHandshakeError('Expected LF but found %r' % ch) - # 4.1 41. check /fields/ - # TODO(ukai): protocol - # if the entry's name is "upgrade" - # if the value is not exactly equal to the string "WebSocket", - # then fail the WebSocket connection and abort these steps. - _validate_mandatory_header( - fields, - common.UPGRADE_HEADER, - common.WEBSOCKET_UPGRADE_TYPE_HIXIE75, - True) - # if the entry's name is "connection" - # if the value, converted to ASCII lowercase, is not exactly equal - # to the string "upgrade", then fail the WebSocket connection and - # abort these steps. - _validate_mandatory_header( - fields, - common.CONNECTION_HEADER, - common.UPGRADE_CONNECTION_TYPE, - False) - - origin = _get_mandatory_header( - fields, common.SEC_WEBSOCKET_ORIGIN_HEADER) - - location = _get_mandatory_header( - fields, common.SEC_WEBSOCKET_LOCATION_HEADER) - - # TODO(ukai): check origin, location, cookie, .. - - # 4.1 42. let /challenge/ be the concatenation of /number_1/, - # expressed as a big endian 32 bit integer, /number_2/, expressed - # as big endian 32 bit integer, and the eight bytes of /key_3/ in the - # order they were sent on the wire. - challenge = struct.pack('!I', self._number1) - challenge += struct.pack('!I', self._number2) - challenge += self._key3 - - self._logger.debug( - 'Challenge: %r (%s)', challenge, util.hexify(challenge)) - - # 4.1 43. let /expected/ be the MD5 fingerprint of /challenge/ as a - # big-endian 128 bit string. - expected = util.md5_hash(challenge).digest() - self._logger.debug( - 'Expected challenge response: %r (%s)', - expected, util.hexify(expected)) - - # 4.1 44. read sixteen bytes from the server. - # let /reply/ be those bytes. - reply = _receive_bytes(self._socket, 16) - self._logger.debug( - 'Actual challenge response: %r (%s)', reply, util.hexify(reply)) - - # 4.1 45. if /reply/ does not exactly equal /expected/, then fail - # the WebSocket connection and abort these steps. - if expected != reply: - raise ClientHandshakeError( - 'Bad challenge response: %r (expected) != %r (actual)' % - (expected, reply)) - # 4.1 46. The *WebSocket connection is established*. - - def _generate_sec_websocket_key(self): - # 4.1 16. let /spaces_n/ be a random integer from 1 to 12 inclusive. - spaces = random.randint(1, 12) - # 4.1 17. let /max_n/ be the largest integer not greater than - # 4,294,967,295 divided by /spaces_n/. - maxnum = 4294967295 / spaces - # 4.1 18. let /number_n/ be a random integer from 0 to /max_n/ - # inclusive. - number = random.randint(0, maxnum) - # 4.1 19. let /product_n/ be the result of multiplying /number_n/ and - # /spaces_n/ together. - product = number * spaces - # 4.1 20. let /key_n/ be a string consisting of /product_n/, expressed - # in base ten using the numerals in the range U+0030 DIGIT ZERO (0) to - # U+0039 DIGIT NINE (9). - key = str(product) - # 4.1 21. insert between one and twelve random characters from the - # range U+0021 to U+002F and U+003A to U+007E into /key_n/ at random - # positions. - available_chars = range(0x21, 0x2f + 1) + range(0x3a, 0x7e + 1) - n = random.randint(1, 12) - for _ in xrange(n): - ch = random.choice(available_chars) - pos = random.randint(0, len(key)) - key = key[0:pos] + chr(ch) + key[pos:] - # 4.1 22. insert /spaces_n/ U+0020 SPACE characters into /key_n/ at - # random positions other than start or end of the string. - for _ in xrange(spaces): - pos = random.randint(1, len(key) - 1) - key = key[0:pos] + ' ' + key[pos:] - return number, key - - def _generate_key3(self): - # 4.1 26. let /key3/ be a string consisting of eight random bytes (or - # equivalently, a random 64 bit integer encoded in a big-endian order). - return ''.join([chr(random.randint(0, 255)) for _ in xrange(8)]) - - -class ClientConnection(object): - """A wrapper for socket object to provide the mp_conn interface. - mod_pywebsocket library is designed to be working on Apache mod_python's - mp_conn object. - """ - - def __init__(self, socket): - self._socket = socket - - def write(self, data): - self._socket.sendall(data) - - def read(self, n): - return self._socket.recv(n) - - def get_remote_addr(self): - return self._socket.getpeername() - remote_addr = property(get_remote_addr) - - -class ClientRequest(object): - """A wrapper class just to make it able to pass a socket object to - functions that expect a mp_request object. - """ - - def __init__(self, socket): - self._logger = util.get_class_logger(self) - - self._socket = socket - self.connection = ClientConnection(socket) - - -def _import_ssl(): - global ssl - try: - import ssl - return True - except ImportError: - return False - - -def _import_pyopenssl(): - global OpenSSL - try: - import OpenSSL.SSL - return True - except ImportError: - return False - - -class EchoClient(object): - """WebSocket echo client.""" - - def __init__(self, options): - self._options = options - self._socket = None - - self._logger = util.get_class_logger(self) - - def run(self): - """Run the client. - - Shake hands and then repeat sending message and receiving its echo. - """ - - self._socket = socket.socket() - self._socket.settimeout(self._options.socket_timeout) - try: - self._socket.connect((self._options.server_host, - self._options.server_port)) - if self._options.use_tls: - self._socket = _TLSSocket( - self._socket, - self._options.tls_module, - self._options.tls_version, - self._options.disable_tls_compression) - - version = self._options.protocol_version - - if (version == _PROTOCOL_VERSION_HYBI08 or - version == _PROTOCOL_VERSION_HYBI13): - self._handshake = ClientHandshakeProcessor( - self._socket, self._options) - elif version == _PROTOCOL_VERSION_HYBI00: - self._handshake = ClientHandshakeProcessorHybi00( - self._socket, self._options) - else: - raise ValueError( - 'Invalid --protocol-version flag: %r' % version) - - self._handshake.handshake() - - self._logger.info('Connection established') - - request = ClientRequest(self._socket) - - version_map = { - _PROTOCOL_VERSION_HYBI08: common.VERSION_HYBI08, - _PROTOCOL_VERSION_HYBI13: common.VERSION_HYBI13, - _PROTOCOL_VERSION_HYBI00: common.VERSION_HYBI00} - request.ws_version = version_map[version] - - if (version == _PROTOCOL_VERSION_HYBI08 or - version == _PROTOCOL_VERSION_HYBI13): - stream_option = StreamOptions() - stream_option.mask_send = True - stream_option.unmask_receive = False - - if self._options.deflate_frame is not False: - processor = self._options.deflate_frame - processor.setup_stream_options(stream_option) - - if self._options.use_permessage_deflate is not False: - framer = self._options.use_permessage_deflate - framer.setup_stream_options(stream_option) - - self._stream = Stream(request, stream_option) - elif version == _PROTOCOL_VERSION_HYBI00: - self._stream = StreamHixie75(request, True) - - for line in self._options.message.split(','): - self._stream.send_message(line) - if self._options.verbose: - print 'Send: %s' % line - try: - received = self._stream.receive_message() - - if self._options.verbose: - print 'Recv: %s' % received - except Exception, e: - if self._options.verbose: - print 'Error: %s' % e - raise - - self._do_closing_handshake() - finally: - self._socket.close() - - def _do_closing_handshake(self): - """Perform closing handshake using the specified closing frame.""" - - if self._options.message.split(',')[-1] == _GOODBYE_MESSAGE: - # requested server initiated closing handshake, so - # expecting closing handshake message from server. - self._logger.info('Wait for server-initiated closing handshake') - message = self._stream.receive_message() - if message is None: - print 'Recv close' - print 'Send ack' - self._logger.info( - 'Received closing handshake and sent ack') - return - print 'Send close' - self._stream.close_connection() - self._logger.info('Sent closing handshake') - print 'Recv ack' - self._logger.info('Received ack') - - -def main(): - sys.stdout = codecs.getwriter('utf-8')(sys.stdout) - - parser = OptionParser() - # We accept --command_line_flag style flags which is the same as Google - # gflags in addition to common --command-line-flag style flags. - parser.add_option('-s', '--server-host', '--server_host', - dest='server_host', type='string', - default='localhost', help='server host') - parser.add_option('-p', '--server-port', '--server_port', - dest='server_port', type='int', - default=_UNDEFINED_PORT, help='server port') - parser.add_option('-o', '--origin', dest='origin', type='string', - default=None, help='origin') - parser.add_option('-r', '--resource', dest='resource', type='string', - default='/echo', help='resource path') - parser.add_option('-m', '--message', dest='message', type='string', - help=('comma-separated messages to send. ' - '%s will force close the connection from server.' % - _GOODBYE_MESSAGE)) - parser.add_option('-q', '--quiet', dest='verbose', action='store_false', - default=True, help='suppress messages') - parser.add_option('-t', '--tls', dest='use_tls', action='store_true', - default=False, help='use TLS (wss://). By default, ' - 'it looks for ssl and pyOpenSSL module and uses found ' - 'one. Use --tls-module option to specify which module ' - 'to use') - parser.add_option('--tls-module', '--tls_module', dest='tls_module', - type='choice', - choices=[_TLS_BY_STANDARD_MODULE, _TLS_BY_PYOPENSSL], - help='Use ssl module if "%s" is specified. ' - 'Use pyOpenSSL module if "%s" is specified' % - (_TLS_BY_STANDARD_MODULE, _TLS_BY_PYOPENSSL)) - parser.add_option('--tls-version', '--tls_version', - dest='tls_version', - type='string', default=_TLS_VERSION_SSL23, - help='TLS/SSL version to use. One of \'' + - _TLS_VERSION_SSL23 + '\' (SSL version 2 or 3), \'' + - _TLS_VERSION_SSL3 + '\' (SSL version 3), \'' + - _TLS_VERSION_TLS1 + '\' (TLS version 1)') - parser.add_option('--disable-tls-compression', '--disable_tls_compression', - dest='disable_tls_compression', - action='store_true', default=False, - help='Disable TLS compression. Available only when ' - 'pyOpenSSL module is used.') - parser.add_option('-k', '--socket-timeout', '--socket_timeout', - dest='socket_timeout', type='int', default=_TIMEOUT_SEC, - help='Timeout(sec) for sockets') - parser.add_option('--draft75', dest='draft75', - action='store_true', default=False, - help='Obsolete option. Don\'t use this.') - parser.add_option('--protocol-version', '--protocol_version', - dest='protocol_version', - type='string', default=_PROTOCOL_VERSION_HYBI13, - help='WebSocket protocol version to use. One of \'' + - _PROTOCOL_VERSION_HYBI13 + '\', \'' + - _PROTOCOL_VERSION_HYBI08 + '\', \'' + - _PROTOCOL_VERSION_HYBI00 + '\'') - parser.add_option('--version-header', '--version_header', - dest='version_header', - type='int', default=-1, - help='Specify Sec-WebSocket-Version header value') - parser.add_option('--deflate-frame', '--deflate_frame', - dest='deflate_frame', - action='store_true', default=False, - help='Use the deflate-frame extension.') - parser.add_option('--use-permessage-deflate', '--use_permessage_deflate', - dest='use_permessage_deflate', - action='store_true', default=False, - help='Use the permessage-deflate extension.') - parser.add_option('--log-level', '--log_level', type='choice', - dest='log_level', default='warn', - choices=['debug', 'info', 'warn', 'error', 'critical'], - help='Log level.') - - (options, unused_args) = parser.parse_args() - - logging.basicConfig(level=logging.getLevelName(options.log_level.upper())) - - if options.draft75: - logging.critical('--draft75 option is obsolete.') - sys.exit(1) - - if options.protocol_version == _PROTOCOL_VERSION_HIXIE75: - logging.critical( - 'Value %s is obsolete for --protocol_version options' % - _PROTOCOL_VERSION_HIXIE75) - sys.exit(1) - - if options.use_tls: - if options.tls_module is None: - if _import_ssl(): - options.tls_module = _TLS_BY_STANDARD_MODULE - logging.debug('Using ssl module') - elif _import_pyopenssl(): - options.tls_module = _TLS_BY_PYOPENSSL - logging.debug('Using pyOpenSSL module') - else: - logging.critical( - 'TLS support requires ssl or pyOpenSSL module.') - sys.exit(1) - elif options.tls_module == _TLS_BY_STANDARD_MODULE: - if not _import_ssl(): - logging.critical('ssl module is not available') - sys.exit(1) - elif options.tls_module == _TLS_BY_PYOPENSSL: - if not _import_pyopenssl(): - logging.critical('pyOpenSSL module is not available') - sys.exit(1) - else: - logging.critical('Invalid --tls-module option: %r', - options.tls_module) - sys.exit(1) - - if (options.disable_tls_compression and - options.tls_module != _TLS_BY_PYOPENSSL): - logging.critical('You can disable TLS compression only when ' - 'pyOpenSSL module is used.') - sys.exit(1) - else: - if options.tls_module is not None: - logging.critical('Use --tls-module option only together with ' - '--use-tls option.') - sys.exit(1) - - if options.disable_tls_compression: - logging.critical('Use --disable-tls-compression only together ' - 'with --use-tls option.') - sys.exit(1) - - # Default port number depends on whether TLS is used. - if options.server_port == _UNDEFINED_PORT: - if options.use_tls: - options.server_port = common.DEFAULT_WEB_SOCKET_SECURE_PORT - else: - options.server_port = common.DEFAULT_WEB_SOCKET_PORT - - # optparse doesn't seem to handle non-ascii default values. - # Set default message here. - if not options.message: - options.message = u'Hello,\u65e5\u672c' # "Japan" in Japanese - - EchoClient(options).run() - - -if __name__ == '__main__': - main() - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/echo_noext_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/echo_noext_wsh.py deleted file mode 100644 index 1df515122..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/echo_noext_wsh.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright 2013, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -_GOODBYE_MESSAGE = u'Goodbye' - - -def web_socket_do_extra_handshake(request): - """Received Sec-WebSocket-Extensions header value is parsed into - request.ws_requested_extensions. pywebsocket creates extension - processors using it before do_extra_handshake call and never looks at it - after the call. - - To reject requested extensions, clear the processor list. - """ - - request.ws_extension_processors = [] - - -def web_socket_transfer_data(request): - """Echo. Same as echo_wsh.py.""" - - while True: - line = request.ws_stream.receive_message() - if line is None: - return - if isinstance(line, unicode): - request.ws_stream.send_message(line, binary=False) - if line == _GOODBYE_MESSAGE: - return - else: - request.ws_stream.send_message(line, binary=True) - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/echo_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/echo_wsh.py deleted file mode 100644 index 38646c32c..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/echo_wsh.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2011, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -_GOODBYE_MESSAGE = u'Goodbye' - - -def web_socket_do_extra_handshake(request): - # This example handler accepts any request. See origin_check_wsh.py for how - # to reject access from untrusted scripts based on origin value. - - pass # Always accept. - - -def web_socket_transfer_data(request): - while True: - line = request.ws_stream.receive_message() - if line is None: - return - if isinstance(line, unicode): - request.ws_stream.send_message(line, binary=False) - if line == _GOODBYE_MESSAGE: - return - else: - request.ws_stream.send_message(line, binary=True) - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.cgi b/testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.cgi deleted file mode 100755 index adddf237c..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.cgi +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/python - -# Copyright 2013, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -"""This CGI script generates text/event-stream type data stream for testing -the Server-Sent Events. - -It will only work correctly with HTTP servers that do not buffer the output of -CGI scripts. -""" - - -import sys -import time - -sys.stdout.write('Content-type: text/event-stream\r\n\r\n') - -id = 0 - -while True: - sys.stdout.write('data: Hello\r\nid: %d\r\n\r\n' % id) - sys.stdout.flush() - - id = id + 1 - - time.sleep(1) diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.html b/testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.html deleted file mode 100644 index 1598a8807..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/eventsource.html +++ /dev/null @@ -1,74 +0,0 @@ -<!-- -Copyright 2013, Google Inc. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---> - -<!-- -Simple example of the Server-Sent Events -http://dev.w3.org/html5/eventsource/ - -For comparison with the WebSocket Protocol & API. - -Run the pywebsocket with the --cgi_path parameter. ---> - -<html> -<head> -<title>Server-Sent Events Example</title> -<script> -var eventSource = null; - -function addToLog(data) { - logBox.value += data + '\n'; - logBox.scrollTop = 1000000; -} - -function init() { - logBox = document.getElementById('log'); - - eventSource = new EventSource('/eventsource.cgi'); - eventSource.onopen = function() { - addToLog('onopen (readyState = ' + eventSource.readyState + ')'); - } - eventSource.onmessage = function(event) { - addToLog(event.data); - } - eventSource.onerror = function(event) { - addToLog('onerror (readyState = ' + eventSource.readyState + ')'); - } -} -</script> -</head> -<body onload="init()"> -<textarea id="log" rows="10" cols="40" readonly></textarea> -<p style="font-size: small"> - Make sure that pywebsocket is run with --cgi_path parameter. -</p> -</body> -</html> diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/handler_map.txt b/testing/web-platform/tests/tools/pywebsocket/src/example/handler_map.txt deleted file mode 100644 index 21c4c09aa..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/handler_map.txt +++ /dev/null @@ -1,11 +0,0 @@ -# websocket handler map file, used by standalone.py -m option. -# A line starting with '#' is a comment line. -# Each line consists of 'alias_resource_path' and 'existing_resource_path' -# separated by spaces. -# Aliasing is processed from the top to the bottom of the line, and -# 'existing_resource_path' must exist before it is aliased. -# For example, -# / /echo -# means that a request to '/' will be handled by handlers for '/echo'. -/ /echo - diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/hsts_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/hsts_wsh.py deleted file mode 100644 index e86194692..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/hsts_wsh.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2013, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -def web_socket_do_extra_handshake(request): - request.extra_headers.append( - ('Strict-Transport-Security', 'max-age=86400')) - - -def web_socket_transfer_data(request): - request.ws_stream.send_message('Hello', binary=False) - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/internal_error_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/internal_error_wsh.py deleted file mode 100644 index fe581b54a..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/internal_error_wsh.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2012, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -from mod_pywebsocket import msgutil - - -def web_socket_do_extra_handshake(request): - pass - - -def web_socket_transfer_data(request): - raise msgutil.BadOperationException('Intentional') - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/origin_check_wsh.py b/testing/web-platform/tests/tools/pywebsocket/src/example/origin_check_wsh.py deleted file mode 100644 index e05767ab9..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/origin_check_wsh.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2011, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -# This example is derived from test/testdata/handlers/origin_check_wsh.py. - - -def web_socket_do_extra_handshake(request): - if request.ws_origin == 'http://example.com': - return - raise ValueError('Unacceptable origin: %r' % request.ws_origin) - - -def web_socket_transfer_data(request): - request.connection.write('origin_check_wsh.py is called for %s, %s' % - (request.ws_resource, request.ws_protocol)) - - -# vi:sts=4 sw=4 et diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/pywebsocket.conf b/testing/web-platform/tests/tools/pywebsocket/src/example/pywebsocket.conf deleted file mode 100644 index 335d130a5..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/pywebsocket.conf +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2011, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -# -# Sample configuration file for apache2 -# -LogLevel debug -<IfModule python_module> - PythonPath "sys.path+['/mod_pywebsocket']" - PythonOption mod_pywebsocket.handler_root /var/www - PythonOption mod_pywebsocket.handler_scan /var/www/ws - #PythonOption mod_pywebsocket.allow_draft75 On - <Location /ws> - PythonHeaderParserHandler mod_pywebsocket.headerparserhandler - </Location> -</IfModule> diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/special_headers.cgi b/testing/web-platform/tests/tools/pywebsocket/src/example/special_headers.cgi deleted file mode 100755 index ea5080f1f..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/special_headers.cgi +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python - -# Copyright 2014 Google Inc. All rights reserved. -# -# Use of this source code is governed by a BSD-style -# license that can be found in the COPYING file or at -# https://developers.google.com/open-source/licenses/bsd - -"""CGI script sample for testing effect of HTTP headers on the origin page. - -Note that CGI scripts don't work on the standalone pywebsocket running in TLS -mode. -""" - - -print """Content-type: text/html -Content-Security-Policy: connect-src self - -<html> -<head> -<title></title> -</head> -<body> -<script> -var socket = new WebSocket("ws://example.com"); -</script> -</body> -</html>""" diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/util.js b/testing/web-platform/tests/tools/pywebsocket/src/example/util.js deleted file mode 100644 index a1cad4975..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/util.js +++ /dev/null @@ -1,177 +0,0 @@ -// Copyright 2013, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Utilities for example applications (for both main and worker thread). - -var results = {}; - -function getTimeStamp() { - return Date.now(); -} - -function formatResultInKiB(size, timePerMessageInMs, stddevTimePerMessageInMs, - speed, printSize) { - if (printSize) { - return (size / 1024) + - '\t' + timePerMessageInMs.toFixed(3) + - (stddevTimePerMessageInMs == -1 ? - '' : - '\t' + stddevTimePerMessageInMs.toFixed(3)) + - '\t' + speed.toFixed(3); - } else { - return speed.toString(); - } -} - -function clearAverageData() { - results = {}; -} - -function reportAverageData(config) { - config.addToSummary( - 'Size[KiB]\tAverage time[ms]\tStddev time[ms]\tSpeed[KB/s]'); - for (var size in results) { - var averageTimePerMessageInMs = results[size].sum_t / results[size].n; - var speed = calculateSpeedInKB(size, averageTimePerMessageInMs); - // Calculate sample standard deviation - var stddevTimePerMessageInMs = Math.sqrt( - (results[size].sum_t2 / results[size].n - - averageTimePerMessageInMs * averageTimePerMessageInMs) * - results[size].n / - (results[size].n - 1)); - config.addToSummary(formatResultInKiB( - size, averageTimePerMessageInMs, stddevTimePerMessageInMs, speed, - true)); - } -} - -function calculateSpeedInKB(size, timeSpentInMs) { - return Math.round(size / timeSpentInMs * 1000) / 1000; -} - -function calculateAndLogResult(config, size, startTimeInMs, totalSize) { - var timeSpentInMs = getTimeStamp() - startTimeInMs; - var speed = calculateSpeedInKB(totalSize, timeSpentInMs); - var timePerMessageInMs = timeSpentInMs / (totalSize / size); - if (!results[size]) { - results[size] = {n: 0, sum_t: 0, sum_t2: 0}; - } - config.measureValue(timePerMessageInMs); - results[size].n ++; - results[size].sum_t += timePerMessageInMs; - results[size].sum_t2 += timePerMessageInMs * timePerMessageInMs; - config.addToLog(formatResultInKiB(size, timePerMessageInMs, -1, speed, - config.printSize)); -} - -function fillArrayBuffer(buffer, c) { - var i; - - var u32Content = c * 0x01010101; - - var u32Blocks = Math.floor(buffer.byteLength / 4); - var u32View = new Uint32Array(buffer, 0, u32Blocks); - // length attribute is slow on Chrome. Don't use it for loop condition. - for (i = 0; i < u32Blocks; ++i) { - u32View[i] = u32Content; - } - - // Fraction - var u8Blocks = buffer.byteLength - u32Blocks * 4; - var u8View = new Uint8Array(buffer, u32Blocks * 4, u8Blocks); - for (i = 0; i < u8Blocks; ++i) { - u8View[i] = c; - } -} - -function verifyArrayBuffer(buffer, expectedChar) { - var i; - - var expectedU32Value = expectedChar * 0x01010101; - - var u32Blocks = Math.floor(buffer.byteLength / 4); - var u32View = new Uint32Array(buffer, 0, u32Blocks); - for (i = 0; i < u32Blocks; ++i) { - if (u32View[i] != expectedU32Value) { - return false; - } - } - - var u8Blocks = buffer.byteLength - u32Blocks * 4; - var u8View = new Uint8Array(buffer, u32Blocks * 4, u8Blocks); - for (i = 0; i < u8Blocks; ++i) { - if (u8View[i] != expectedChar) { - return false; - } - } - - return true; -} - -function verifyBlob(config, blob, expectedChar, doneCallback) { - var reader = new FileReader(blob); - reader.onerror = function() { - config.addToLog('FileReader Error: ' + reader.error.message); - doneCallback(blob.size, false); - } - reader.onloadend = function() { - var result = verifyArrayBuffer(reader.result, expectedChar); - doneCallback(blob.size, result); - } - reader.readAsArrayBuffer(blob); -} - -function verifyAcknowledgement(config, message, size) { - if (typeof message != 'string') { - config.addToLog('Invalid ack type: ' + typeof message); - return false; - } - var parsedAck = parseInt(message); - if (isNaN(parsedAck)) { - config.addToLog('Invalid ack value: ' + message); - return false; - } - if (parsedAck != size) { - config.addToLog( - 'Expected ack for ' + size + 'B but received one for ' + parsedAck + - 'B'); - return false; - } - - return true; -} - -function cloneConfig(obj) { - var newObj = {}; - for (key in obj) { - newObj[key] = obj[key]; - } - return newObj; -} diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/util_main.js b/testing/web-platform/tests/tools/pywebsocket/src/example/util_main.js deleted file mode 100644 index b03d1c2bd..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/util_main.js +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2014 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the COPYING file or at -// https://developers.google.com/open-source/licenses/bsd - -// Utilities for example applications (for the main thread only). - -var logBox = null; -var queuedLog = ''; - -var summaryBox = null; - -function queueLog(log) { - queuedLog += log + '\n'; -} - -function addToLog(log) { - logBox.value += queuedLog; - queuedLog = ''; - logBox.value += log + '\n'; - logBox.scrollTop = 1000000; -} - -function addToSummary(log) { - summaryBox.value += log + '\n'; - summaryBox.scrollTop = 1000000; -} - -// value: execution time in milliseconds. -// config.measureValue is intended to be used in Performance Tests. -// Do nothing here in non-PerformanceTest. -function measureValue(value) { -} - -function getIntFromInput(id) { - return parseInt(document.getElementById(id).value); -} - -function getStringFromRadioBox(name) { - var list = document.getElementById('benchmark_form')[name]; - for (var i = 0; i < list.length; ++i) - if (list.item(i).checked) - return list.item(i).value; - return undefined; -} -function getBoolFromCheckBox(id) { - return document.getElementById(id).checked; -} - -function getIntArrayFromInput(id) { - var strArray = document.getElementById(id).value.split(','); - return strArray.map(function(str) { return parseInt(str, 10); }); -} - -function onMessage(message) { - if (message.data.type === 'addToLog') - addToLog(message.data.data); - else if (message.data.type === 'addToSummary') - addToSummary(message.data.data); - else if (message.data.type === 'measureValue') - measureValue(message.data.data); -} diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/util_worker.js b/testing/web-platform/tests/tools/pywebsocket/src/example/util_worker.js deleted file mode 100644 index b64f7829d..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/util_worker.js +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2014 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the COPYING file or at -// https://developers.google.com/open-source/licenses/bsd - -// Utilities for example applications (for the worker threads only). - -function workerAddToLog(text) { - postMessage({type: 'addToLog', data: text}); -} - -function workerAddToSummary(text) { - postMessage({type: 'addToSummary', data: text}); -} - -function workerMeasureValue(value) { - postMessage({type: 'measureValue', data: value}); -} diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_benchmark.html b/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_benchmark.html deleted file mode 100644 index 186229775..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_benchmark.html +++ /dev/null @@ -1,222 +0,0 @@ -<!-- -Copyright 2013, Google Inc. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---> - -<html> -<head> -<title>XMLHttpRequest benchmark</title> -<script src="util_main.js"></script> -<script src="util.js"></script> -<script src="xhr_benchmark.js"></script> -<script> -var addressBox = null; - -function getConfig() { - return { - prefixUrl: addressBox.value, - printSize: getBoolFromCheckBox('printsize'), - numXHRs: getIntFromInput('numXHRs'), - async: getBoolFromCheckBox('async'), - // Initial size of messages. - numIterations: getIntFromInput('numiterations'), - numWarmUpIterations: getIntFromInput('numwarmupiterations'), - startSize: getIntFromInput('startsize'), - // Stops benchmark when the size of message exceeds this threshold. - stopThreshold: getIntFromInput('stopthreshold'), - // If the size of each message is small, send/receive multiple messages - // until the sum of sizes reaches this threshold. - // minTotal: getIntFromInput('mintotal'), - // minTotal is not yet implemented on XHR benchmark - multipliers: getIntArrayFromInput('multipliers'), - verifyData: getBoolFromCheckBox('verifydata') - }; -} - -var worker = new Worker('xhr_benchmark.js'); -worker.onmessage = onMessage; - -function onSendBenchmark() { - var config = getConfig(); - config.dataType = getStringFromRadioBox('datatyperadio'); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'sendBenchmark', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - sendBenchmark(config); - } -} - -function onReceiveBenchmark() { - var config = getConfig(); - config.dataType = getStringFromRadioBox('datatyperadio'); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'receiveBenchmark', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - receiveBenchmark(config); - } -} - -function onBatchBenchmark() { - var config = getConfig(); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'batchBenchmark', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - batchBenchmark(config); - } -} - -function onStop() { - var config = getConfig(); - - if (getBoolFromCheckBox('worker')) { - worker.postMessage({type: 'stop', config: config}); - } else { - config.addToLog = addToLog; - config.addToSummary = addToSummary; - config.measureValue = measureValue; - stop(config); - } -} - -function init() { - addressBox = document.getElementById('address'); - logBox = document.getElementById('log'); - - summaryBox = document.getElementById('summary'); - - // Special address of pywebsocket for XHR benchmark. - addressBox.value = '/073be001e10950692ccbf3a2ad21c245'; - - addToLog(window.navigator.userAgent.toLowerCase()); - addToSummary(window.navigator.userAgent.toLowerCase()); -} -</script> -</head> -<body onload="init()"> - -<form id="benchmark_form"> - url prefix <input type="text" id="address" size="40"> - <input type="button" value="send" onclick="onSendBenchmark()"> - <input type="button" value="receive" onclick="onReceiveBenchmark()"> - <input type="button" value="batch" onclick="onBatchBenchmark()"> - <input type="button" value="stop" onclick="onStop()"> - - <br/> - - <input type="checkbox" id="printsize" checked> - <label for="printsize">Print size and time per message</label> - <input type="checkbox" id="verifydata" checked> - <label for="verifydata">Verify data</label> - <input type="checkbox" id="worker"> - <label for="worker">Run on worker</label> - <input type="checkbox" id="async" checked> - <label for="async">Async</label><br> - (Receive && Non-Worker && Sync is not supported by spec) - - <br/> - - Parameters: - - <br/> - - <table> - <tr> - <td>Num XHRs</td> - <td><input type="text" id="numXHRs" value="1"></td> - </tr> - <tr> - <td>Number of iterations</td> - <td><input type="text" id="numiterations" value="1"></td> - </tr> - <tr> - <td>Number of warm-up iterations</td> - <td><input type="text" id="numwarmupiterations" value="0"></td> - </tr> - <tr> - <td>Start size</td> - <td><input type="text" id="startsize" value="10240"></td> - </tr> - <tr> - <td>Stop threshold</td> - <td><input type="text" id="stopthreshold" value="102400000"></td> - </tr> - <tr> - <td>Minimum total</td> - <td><input type="text" id="mintotal" value="102400000"></td> - </tr> - <tr> - <td>Multipliers</td> - <td><input type="text" id="multipliers" value="5, 2"></td> - </tr> - </table> - - Set data type - <input type="radio" - name="datatyperadio" - id="datatyperadiotext" - value="text" - checked><label for="datatyperadiotext">text</label> - <input type="radio" - name="datatyperadio" - id="datatyperadioblob" - value="blob" - ><label for="datatyperadioblob">blob</label> - <input type="radio" - name="datatyperadio" - id="datatyperadioarraybuffer" - value="arraybuffer" - ><label for="datatyperadioarraybuffer">arraybuffer</label> -</form> - -<div id="log_div"> - <textarea - id="log" rows="20" style="width: 100%" readonly></textarea> -</div> -<div id="summary_div"> - Summary - <textarea - id="summary" rows="20" style="width: 100%" readonly></textarea> -</div> - -Note: Effect of RTT and time spent for ArrayBuffer creation in receive benchmarks are not eliminated. - -</body> -</html> diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_benchmark.js b/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_benchmark.js deleted file mode 100644 index 233c7cb38..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_benchmark.js +++ /dev/null @@ -1,389 +0,0 @@ -// Copyright 2014 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the COPYING file or at -// https://developers.google.com/open-source/licenses/bsd - -var isWorker = typeof importScripts !== "undefined"; - -if (isWorker) { - // Running on a worker - importScripts('util.js', 'util_worker.js'); -} - -// Namespace for holding globals. -var benchmark = {}; -benchmark.startTimeInMs = 0; - -var xhrs = []; - -var timerID = null; - -function destroyAllXHRs() { - for (var i = 0; i < xhrs.length; ++i) { - xhrs[i].onreadystatechange = null; - // Abort XHRs if they are not yet DONE state. - // Calling abort() here (i.e. in onreadystatechange handler) - // causes "NetworkError" messages in DevTools in sync mode, - // even if it is after transition to DONE state. - if (xhrs[i].readyState != XMLHttpRequest.DONE) - xhrs[i].abort(); - } - xhrs = []; - // gc() might be needed for Chrome/Blob -} - -function repeatString(str, count) { - var data = ''; - var expChunk = str; - var remain = count; - while (true) { - if (remain % 2) { - data += expChunk; - remain = (remain - 1) / 2; - } else { - remain /= 2; - } - - if (remain == 0) - break; - - expChunk = expChunk + expChunk; - } - return data; -} - -function sendBenchmarkStep(size, config) { - timerID = null; - - benchmark.startTimeInMs = null; - var totalSize = 0; - var totalReplied = 0; - - var onReadyStateChangeHandler = function () { - if (this.readyState != this.DONE) { - return; - } - - if (this.status != 200) { - config.addToLog('Failed (status=' + this.status + ')'); - destroyAllXHRs(); - return; - } - - if (config.verifyData && - !verifyAcknowledgement(config, this.response, size)) { - destroyAllXHRs(); - return; - } - - totalReplied += size; - - if (totalReplied < totalSize) { - return; - } - - if (benchmark.startTimeInMs == null) { - config.addToLog('startTimeInMs not set'); - destroyAllXHRs(); - return; - } - - calculateAndLogResult(config, size, benchmark.startTimeInMs, totalSize); - - destroyAllXHRs(); - - runNextTask(config); - }; - - for (var i = 0; i < config.numXHRs; ++i) { - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = onReadyStateChangeHandler; - xhrs.push(xhr); - } - - var dataArray = []; - - for (var i = 0; i < xhrs.length; ++i) { - var data = null; - if (config.dataType == 'arraybuffer' || - config.dataType == 'blob') { - data = new ArrayBuffer(size); - - fillArrayBuffer(data, 0x61); - - if (config.dataType == 'blob') { - data = new Blob([data]); - } - } else { - data = repeatString('a', size); - } - - dataArray.push(data); - } - - - benchmark.startTimeInMs = getTimeStamp(); - totalSize = size * xhrs.length; - - for (var i = 0; i < xhrs.length; ++i) { - var data = dataArray[i]; - var xhr = xhrs[i]; - xhr.open('POST', config.prefixUrl + '_send', config.async); - xhr.send(data); - } -} - -function receiveBenchmarkStep(size, config) { - timerID = null; - - benchmark.startTimeInMs = null; - var totalSize = 0; - var totalReplied = 0; - - var checkResultAndContinue = function (bytesReceived, verificationResult) { - if (!verificationResult) { - config.addToLog('Response verification failed'); - destroyAllXHRs(); - return; - } - - totalReplied += bytesReceived; - - if (totalReplied < totalSize) { - return; - } - - if (benchmark.startTimeInMs == null) { - config.addToLog('startTimeInMs not set'); - destroyAllXHRs(); - return; - } - - calculateAndLogResult(config, size, benchmark.startTimeInMs, totalSize); - - destroyAllXHRs(); - - runNextTask(config); - } - - var onReadyStateChangeHandler = function () { - if (this.readyState != this.DONE) { - return; - } - - if (this.status != 200) { - config.addToLog('Failed (status=' + this.status + ')'); - destroyAllXHRs(); - return; - } - - var bytesReceived = -1; - if (this.responseType == 'arraybuffer') { - bytesReceived = this.response.byteLength; - } else if (this.responseType == 'blob') { - bytesReceived = this.response.size; - } else { - bytesReceived = this.response.length; - } - if (bytesReceived != size) { - config.addToLog('Expected ' + size + - 'B but received ' + bytesReceived + 'B'); - destroyAllXHRs(); - return; - } - - if (this.responseType == 'arraybuffer') { - checkResultAndContinue(bytesReceived, - !config.verifyData || verifyArrayBuffer(this.response, 0x61)); - } else if (this.responseType == 'blob') { - if (config.verifyData) - verifyBlob(config, this.response, 0x61, checkResultAndContinue); - else - checkResultAndContinue(bytesReceived, true); - } else { - checkResultAndContinue( - bytesReceived, - !config.verifyData || - this.response == repeatString('a', this.response.length)); - } - }; - - for (var i = 0; i < config.numXHRs; ++i) { - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = onReadyStateChangeHandler; - xhrs.push(xhr); - } - - benchmark.startTimeInMs = getTimeStamp(); - totalSize = size * xhrs.length; - - for (var i = 0; i < xhrs.length; ++i) { - var xhr = xhrs[i]; - xhr.open('POST', config.prefixUrl + '_receive', config.async); - xhr.responseType = config.dataType; - xhr.send(size + ' none'); - } -} - - -function getConfigString(config) { - return '(' + config.dataType + - ', verifyData=' + config.verifyData + - ', ' + (isWorker ? 'Worker' : 'Main') + - ', ' + (config.async ? 'Async' : 'Sync') + - ', numXHRs=' + config.numXHRs + - ', numIterations=' + config.numIterations + - ', numWarmUpIterations=' + config.numWarmUpIterations + - ')'; -} - -function startBenchmark(config) { - clearTimeout(timerID); - destroyAllXHRs(); - - runNextTask(config); -} - -// TODO(hiroshige): the following code is the same as benchmark.html -// and some of them should be merged into e.g. util.js - -var tasks = []; - -function runNextTask(config) { - var task = tasks.shift(); - if (task == undefined) { - config.addToLog('Finished'); - destroyAllXHRs(); - return; - } - timerID = setTimeout(task, 0); -} - -function buildLegendString(config) { - var legend = '' - if (config.printSize) - legend = 'Message size in KiB, Time/message in ms, '; - legend += 'Speed in kB/s'; - return legend; -} - -function addTasks(config, stepFunc) { - for (var i = 0; - i < config.numWarmUpIterations + config.numIterations; ++i) { - // Ignore the first |config.numWarmUpIterations| iterations. - if (i == config.numWarmUpIterations) - addResultClearingTask(config); - - var multiplierIndex = 0; - for (var size = config.startSize; - size <= config.stopThreshold; - ++multiplierIndex) { - var task = stepFunc.bind( - null, - size, - config); - tasks.push(task); - size *= config.multipliers[ - multiplierIndex % config.multipliers.length]; - } - } -} - -function addResultReportingTask(config, title) { - tasks.push(function(){ - timerID = null; - config.addToSummary(title); - reportAverageData(config); - clearAverageData(); - runNextTask(config); - }); -} - -function addResultClearingTask(config) { - tasks.push(function(){ - timerID = null; - clearAverageData(); - runNextTask(config); - }); -} - -// -------------------------------- - -function sendBenchmark(config) { - config.addToLog('Send benchmark'); - config.addToLog(buildLegendString(config)); - - tasks = []; - clearAverageData(); - addTasks(config, sendBenchmarkStep); - addResultReportingTask(config, 'Send Benchmark ' + getConfigString(config)); - startBenchmark(config); -} - -function receiveBenchmark(config) { - config.addToLog('Receive benchmark'); - config.addToLog(buildLegendString(config)); - - tasks = []; - clearAverageData(); - addTasks(config, receiveBenchmarkStep); - addResultReportingTask(config, - 'Receive Benchmark ' + getConfigString(config)); - startBenchmark(config); -} - -function batchBenchmark(originalConfig) { - originalConfig.addToLog('Batch benchmark'); - - tasks = []; - clearAverageData(); - - var dataTypes = ['text', 'blob', 'arraybuffer']; - var stepFuncs = [sendBenchmarkStep, receiveBenchmarkStep]; - var names = ['Send', 'Receive']; - var async = [true, false]; - for (var i = 0; i < stepFuncs.length; ++i) { - for (var j = 0; j < dataTypes.length; ++j) { - for (var k = 0; k < async.length; ++k) { - var config = cloneConfig(originalConfig); - config.dataType = dataTypes[j]; - config.async = async[k]; - - // Receive && Non-Worker && Sync is not supported by the spec - if (stepFuncs[i] === receiveBenchmarkStep && !isWorker && - !config.async) - continue; - - addTasks(config, stepFuncs[i]); - addResultReportingTask(config, - names[i] + ' benchmark ' + getConfigString(config)); - } - } - } - - startBenchmark(config); -} - - -function stop(config) { - destroyAllXHRs(); - clearTimeout(timerID); - timerID = null; - config.addToLog('Stopped'); -} - -onmessage = function (message) { - var config = message.data.config; - config.addToLog = workerAddToLog; - config.addToSummary = workerAddToSummary; - config.measureValue = workerMeasureValue; - if (message.data.type === 'sendBenchmark') - sendBenchmark(config); - else if (message.data.type === 'receiveBenchmark') - receiveBenchmark(config); - else if (message.data.type === 'batchBenchmark') - batchBenchmark(config); - else if (message.data.type === 'stop') - stop(config); -}; diff --git a/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_event_logger.html b/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_event_logger.html deleted file mode 100644 index 6983553b8..000000000 --- a/testing/web-platform/tests/tools/pywebsocket/src/example/xhr_event_logger.html +++ /dev/null @@ -1,110 +0,0 @@ -<!-- -Copyright 2014 Google Inc. All rights reserved. - -Use of this source code is governed by a BSD-style -license that can be found in the COPYING file or at -https://developers.google.com/open-source/licenses/bsd ---> - -<html> -<head> -<title>XHR event logger</title> -<script src="util_main.js"></script> -<script> -var events = []; - -function run() { - events = []; - - function pushToLog(type) { - if (events.length != 0 && type === events[events.length - 1].type) { - events[events.length - 1].count += 1; - } else { - events.push({type: type, count: 1}); - } - } - - var xhr = new XMLHttpRequest(); - - function getProgressEventDump(e) { - return '(' + e.lengthComputable + ', ' + e.loaded + ', ' + e.total + ')'; - } - - var dumpProgressEvent = getBoolFromCheckBox('dumpprogressevent'); - - function log(e) { - var type = e.type; - if (type === 'readystatechange') { - type += e.target.readyState; - } - if (dumpProgressEvent && (e instanceof ProgressEvent)) { - type += getProgressEventDump(e); - } - pushToLog(type); - }; - - function logUpload(e) { - var type = e.type; - if (dumpProgressEvent && (e instanceof ProgressEvent)) { - type += getProgressEventDump(e); - } - pushToLog('upload' + type); - } - - if (getBoolFromCheckBox('upload')) { - var upload = xhr.upload; - upload.onloadstart = logUpload; - upload.onprogress = logUpload; - upload.onabort = logUpload; - upload.onerror = logUpload; - upload.onload = logUpload; - upload.ontimeout = logUpload; - upload.onloadend = logUpload; - } - - xhr.onreadystatechange = log; - xhr.onloadstart = log; - xhr.onprogress = log; - xhr.onabort = log; - xhr.onerror = log; - xhr.onload = log; - xhr.ontimeout = log; - xhr.onloadend = log; - - xhr.open('POST', '/073be001e10950692ccbf3a2ad21c245_receive', - getBoolFromCheckBox('async')); - var size = getIntFromInput('size'); - var chunkedMode = 'none'; - if (getBoolFromCheckBox('chunkedresponse')) { - chunkedMode = 'chunked'; - } - xhr.send(size + ' ' + chunkedMode); -} - -function print() { - var result = ''; - for (var i = 0; i < events.length; ++i) { - var event = events[i]; - result += event.type + ' * ' + event.count + '\n'; - } - document.getElementById('log').value = result; -} -</script> - -<body> - <textarea id="log" rows="10" cols="40" readonly></textarea> - <br/> - Size: <input type="text" id="size" value="65536"><br/> - <input type="checkbox" id="chunkedresponse"> - <label for="chunkedresponse">Use Chunked T-E for response</label><br/> - <input type="checkbox" id="upload"> - <label for="upload">Upload progress</label><br/> - <input type="checkbox" id="dumpprogressevent"> - <label for="dumpprogressevent"> - Dump lengthComputable/loaded/total</label><br/> - <input type="checkbox" id="async" checked> - <label for="async">Async</label><br/> - <input type="button" onclick="run()" value="Run XHR"> - <input type="button" onclick="print()" value="Print log"> -</body> -</html> |