Skip to content

Commit

Permalink
Squashed 'tools/third_party/pywebsocket3/' changes from d1958b2ca7..9…
Browse files Browse the repository at this point in the history
…db3d29db4

9db3d29db4 Bump version number to reflect functional changes (#25)
c6aea56f8b Stop wrapping os.popen3 (#24)
353b4aaf8a Give up expecting echo_client to be unicode safe (#23)
9ae2b3c90b Make tests work with python 2 on Windows (#20)
e894c1b2ad Update the URL in setup.py (#19)
f76e23c094 Fix incorrect use of "is" for integer comparison (#21)
3ab411d97f Fix version comparisons (#22)

git-subtree-dir: tools/third_party/pywebsocket3
git-subtree-split: 9db3d29db4f3e3efb1722c0210bc88500ab00611
  • Loading branch information
stephenmcgruer committed Mar 2, 2021
1 parent eff7017 commit 7cf2bef
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 34 deletions.
5 changes: 5 additions & 0 deletions example/cgi-bin/hi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python

print('Content-Type: text/plain')
print('')
print('Hi from hi.py')
10 changes: 8 additions & 2 deletions example/echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,14 @@ def _do_closing_handshake(self):


def main():
# Force Python 2 to use the locale encoding, even when the output is not a
# tty. This makes the behaviour the same as Python 3. The encoding won't
# necessarily support all unicode characters. This problem is particularly
# prevalent on Windows.
if six.PY2:
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
import locale
encoding = locale.getpreferredencoding()
sys.stdout = codecs.getwriter(encoding)(sys.stdout)

parser = argparse.ArgumentParser()
# We accept --command_line_flag style flags which is the same as Google
Expand Down Expand Up @@ -636,7 +642,7 @@ def main():
'--message',
dest='message',
type=six.text_type,
default=u'Hello,\u65e5\u672c',
default=u'Hello,<>',
help=('comma-separated messages to send. '
'%s will force close the connection from server.' %
_GOODBYE_MESSAGE))
Expand Down
4 changes: 1 addition & 3 deletions mod_pywebsocket/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ def _parse_args_and_config(args):
def _main(args=None):
"""You can call this function from your own program, but please note that
this function has some side-effects that might affect your program. For
example, util.wrap_popen3_for_win use in this method replaces implementation
of os.popen3.
example, it changes the current directory.
"""

options, args = _parse_args_and_config(args=args)
Expand All @@ -427,7 +426,6 @@ def _main(args=None):
# full path of third_party/cygwin/bin.
if 'CYGWIN_PATH' in os.environ:
cygwin_path = os.environ['CYGWIN_PATH']
util.wrap_popen3_for_win(cygwin_path)

def __check_script(scriptpath):
return util.get_script_interp(scriptpath, cygwin_path)
Expand Down
19 changes: 0 additions & 19 deletions mod_pywebsocket/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,6 @@ def get_script_interp(script_path, cygwin_path=None):
return None


def wrap_popen3_for_win(cygwin_path):
"""Wrap popen3 to support #!-script on Windows.
Args:
cygwin_path: path for cygwin binary if command path is needed to be
translated. None if no translation required.
"""
__orig_popen3 = os.popen3

def __wrap_popen3(cmd, mode='t', bufsize=-1):
cmdline = cmd.split(' ')
interp = get_script_interp(cmdline[0], cygwin_path)
if interp:
cmd = interp + ' ' + cmd
return __orig_popen3(cmd, mode, bufsize)

os.popen3 = __wrap_popen3


def hexify(s):
return ' '.join(['%02x' % x for x in six.iterbytes(s)])

Expand Down
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

# This is used since python_requires field is not recognized with
# pip version 9.0.0 and earlier
if sys.version < '2.7':
if sys.hexversion < 0x020700f0:
print('%s requires Python 2.7 or later.' % _PACKAGE_NAME, file=sys.stderr)
sys.exit(1)

Expand All @@ -66,9 +66,8 @@
packages=[_PACKAGE_NAME, _PACKAGE_NAME + '.handshake'],
python_requires='>=2.7',
install_requires=['six'],
#TODO(suzukikeita): Update this to new Github URL
url='http://code.google.com/p/pywebsocket/',
version='3.0.0',
url='https://github.com/GoogleChromeLabs/pywebsocket3',
version='3.0.1',
)

# vi:sts=4 sw=4 et
2 changes: 1 addition & 1 deletion test/client_for_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def handshake(self, socket):
self._options.server_port,
self._options.use_tls))

if self._options.version is 8:
if self._options.version == 8:
fields.append(_sec_origin_header(self._options.origin))
else:
fields.append(_origin_header(self._options.origin))
Expand Down
47 changes: 43 additions & 4 deletions test/test_endtoend.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"""

from __future__ import absolute_import
from six.moves import urllib
import locale
import logging
import os
import signal
Expand Down Expand Up @@ -153,8 +155,9 @@ def setUp(self):
# TODO(tyoshino): Use tearDown to kill the server.

def _run_python_command(self, commandline, stdout=None, stderr=None):
close_fds = True if sys.platform != 'win32' else None
return subprocess.Popen([sys.executable] + commandline,
close_fds=True,
close_fds=close_fds,
stdout=stdout,
stderr=stderr)

Expand Down Expand Up @@ -632,7 +635,14 @@ def setUp(self):

def _check_example_echo_client_result(self, expected, stdoutdata,
stderrdata):
actual = stdoutdata.decode("utf-8")
actual = stdoutdata.decode(locale.getpreferredencoding())

# In Python 3 on Windows we get "\r\n" terminators back from
# the subprocess and we need to replace them with "\n" to get
# a match. This is a bit of a hack, but avoids platform- and
# version- specific code.
actual = actual.replace('\r\n', '\n')

if actual != expected:
raise Exception('Unexpected result on example echo client: '
'%r (expected) vs %r (actual)' %
Expand All @@ -654,8 +664,8 @@ def test_example_echo_client(self):
# Expected output for the default messages.
default_expectation = (u'Send: Hello\n'
u'Recv: Hello\n'
u'Send: \u65e5\u672c\n'
u'Recv: \u65e5\u672c\n'
u'Send: <>\n'
u'Recv: <>\n'
u'Send close\n'
u'Recv ack\n')

Expand Down Expand Up @@ -693,6 +703,35 @@ def test_example_echo_client(self):
self._close_server(server)


class EndToEndTestWithCgi(EndToEndTestBase):
def setUp(self):
EndToEndTestBase.setUp(self)

def test_cgi(self):
"""Verifies that CGI scripts work."""

server = self._run_server(extra_args=['--cgi-paths', '/cgi-bin'])
time.sleep(_SERVER_WARMUP_IN_SEC)

url = 'http://localhost:%d/cgi-bin/hi.py' % self._options.server_port

# urlopen() in Python 2.7 doesn't support "with".
try:
f = urllib.request.urlopen(url)
except:
self._close_server(server)
raise

try:
self.assertEqual(f.getcode(), 200)
self.assertEqual(f.info().get('Content-Type'), 'text/plain')
body = f.read()
self.assertEqual(body.rstrip(b'\r\n'), b'Hi from hi.py')
finally:
f.close()
self._close_server(server)


if __name__ == '__main__':
unittest.main()

Expand Down
2 changes: 1 addition & 1 deletion test/test_http_header_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_parse_invalid_uri(self):

host, port, resource = http_header_util.parse_uri(
'ws://localhost:-1/ws')
if sys.version >= '3.6':
if sys.hexversion >= 0x030600f0:
self.assertEqual(None, resource)
else:
self.assertEqual('localhost', host)
Expand Down

0 comments on commit 7cf2bef

Please sign in to comment.