Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """This is a simple HTTP/FTP/SYNC/TCP ECHO/UDP ECHO/ server used for testing | 6 """This is a simple HTTP/FTP/SYNC/TCP/UDP/ server used for testing Chrome. |
| 7 Chrome. | |
| 8 | 7 |
| 9 It supports several test URLs, as specified by the handlers in TestPageHandler. | 8 It supports several test URLs, as specified by the handlers in TestPageHandler. |
| 10 By default, it listens on an ephemeral port and sends the port number back to | 9 By default, it listens on an ephemeral port and sends the port number back to |
| 11 the originating process over a pipe. The originating process can specify an | 10 the originating process over a pipe. The originating process can specify an |
| 12 explicit port if necessary. | 11 explicit port if necessary. |
| 13 It can use https if you specify the flag --https=CERT where CERT is the path | 12 It can use https if you specify the flag --https=CERT where CERT is the path |
| 14 to a pem file containing the certificate and private key that should be used. | 13 to a pem file containing the certificate and private key that should be used. |
| 15 """ | 14 """ |
| 16 | 15 |
| 17 import asyncore | 16 import asyncore |
| 18 import base64 | 17 import base64 |
| 19 import BaseHTTPServer | 18 import BaseHTTPServer |
| 20 import cgi | 19 import cgi |
| 21 import errno | 20 import errno |
| 22 import optparse | 21 import optparse |
| 23 import os | 22 import os |
| 23 import random | |
| 24 import re | 24 import re |
| 25 import select | 25 import select |
| 26 import simplejson | 26 import simplejson |
| 27 import SocketServer | 27 import SocketServer |
| 28 import socket | 28 import socket |
| 29 import sys | 29 import sys |
| 30 import struct | 30 import struct |
| 31 import time | 31 import time |
| 32 import urlparse | 32 import urlparse |
| 33 import warnings | 33 import warnings |
| 34 import zlib | 34 import zlib |
| 35 | 35 |
| 36 # Ignore deprecation warnings, they make our output more cluttered. | 36 # Ignore deprecation warnings, they make our output more cluttered. |
| 37 warnings.filterwarnings("ignore", category=DeprecationWarning) | 37 warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 38 | 38 |
| 39 import echo_message | |
| 39 import pyftpdlib.ftpserver | 40 import pyftpdlib.ftpserver |
| 40 import tlslite | 41 import tlslite |
| 41 import tlslite.api | 42 import tlslite.api |
| 42 | 43 |
| 43 try: | 44 try: |
| 44 import hashlib | 45 import hashlib |
| 45 _new_md5 = hashlib.md5 | 46 _new_md5 = hashlib.md5 |
| 46 except ImportError: | 47 except ImportError: |
| 47 import md5 | 48 import md5 |
| 48 _new_md5 = md5.new | 49 _new_md5 = md5.new |
| (...skipping 1451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1500 | 1501 |
| 1501 | 1502 |
| 1502 class TCPEchoHandler(SocketServer.BaseRequestHandler): | 1503 class TCPEchoHandler(SocketServer.BaseRequestHandler): |
| 1503 """The RequestHandler class for TCP echo server. | 1504 """The RequestHandler class for TCP echo server. |
| 1504 | 1505 |
| 1505 It is instantiated once per connection to the server, and overrides the | 1506 It is instantiated once per connection to the server, and overrides the |
| 1506 handle() method to implement communication to the client. | 1507 handle() method to implement communication to the client. |
| 1507 """ | 1508 """ |
| 1508 | 1509 |
| 1509 def handle(self): | 1510 def handle(self): |
| 1510 data = self.request.recv(65536) | 1511 """Handles the request from the client and responds back with a response.""" |
|
jar (doing other things)
2011/08/15 19:33:50
nit: Avoid two forms of respond (responds + respon
ramant (doing other things)
2011/08/16 00:00:03
Done.
| |
| 1511 if not data: | 1512 |
| 1513 data = self.request.recv(65536).strip() | |
| 1514 # Verify the "echo request" message received from the client. Send back | |
| 1515 # "echo response" message if "echo request" message is valid. | |
| 1516 try: | |
| 1517 return_data = echo_message.GetEchoResponseData(data) | |
| 1518 if not return_data: | |
| 1512 return | 1519 return |
| 1513 self.request.send(data) | 1520 except ValueError: |
| 1521 return | |
| 1522 | |
| 1523 self.request.send(return_data) | |
| 1514 | 1524 |
| 1515 | 1525 |
| 1516 class UDPEchoHandler(SocketServer.BaseRequestHandler): | 1526 class UDPEchoHandler(SocketServer.BaseRequestHandler): |
| 1517 """The RequestHandler class for UDP echo server. | 1527 """The RequestHandler class for UDP echo server. |
| 1518 | 1528 |
| 1519 It is instantiated once per connection to the server, and overrides the | 1529 It is instantiated once per connection to the server, and overrides the |
| 1520 handle() method to implement communication to the client. | 1530 handle() method to implement communication to the client. |
| 1521 """ | 1531 """ |
| 1522 | 1532 |
| 1523 def handle(self): | 1533 def handle(self): |
| 1534 """Handles the request from the client and responds back with a response.""" | |
| 1535 | |
| 1524 data = self.request[0].strip() | 1536 data = self.request[0].strip() |
| 1525 socket = self.request[1] | 1537 socket = self.request[1] |
| 1526 socket.sendto(data, self.client_address) | 1538 # Verify the "echo request" message received from the client. Send back |
| 1539 # "echo response" message if "echo request" message is valid. | |
| 1540 try: | |
| 1541 return_data = echo_message.GetEchoResponseData(data) | |
| 1542 if not return_data: | |
| 1543 return | |
| 1544 except ValueError: | |
| 1545 return | |
| 1546 socket.sendto(return_data, self.client_address) | |
| 1527 | 1547 |
| 1528 | 1548 |
| 1529 class FileMultiplexer: | 1549 class FileMultiplexer: |
| 1530 def __init__(self, fd1, fd2) : | 1550 def __init__(self, fd1, fd2) : |
| 1531 self.__fd1 = fd1 | 1551 self.__fd1 = fd1 |
| 1532 self.__fd2 = fd2 | 1552 self.__fd2 = fd2 |
| 1533 | 1553 |
| 1534 def __del__(self) : | 1554 def __del__(self) : |
| 1535 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: | 1555 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: |
| 1536 self.__fd1.close() | 1556 self.__fd1.close() |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1583 server._device_management_handler = None | 1603 server._device_management_handler = None |
| 1584 server.policy_keys = options.policy_keys | 1604 server.policy_keys = options.policy_keys |
| 1585 server.policy_user = options.policy_user | 1605 server.policy_user = options.policy_user |
| 1586 elif options.server_type == SERVER_SYNC: | 1606 elif options.server_type == SERVER_SYNC: |
| 1587 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) | 1607 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) |
| 1588 print 'Sync HTTP server started on port %d...' % server.server_port | 1608 print 'Sync HTTP server started on port %d...' % server.server_port |
| 1589 print 'Sync XMPP server started on port %d...' % server.xmpp_port | 1609 print 'Sync XMPP server started on port %d...' % server.xmpp_port |
| 1590 server_data['port'] = server.server_port | 1610 server_data['port'] = server.server_port |
| 1591 server_data['xmpp_port'] = server.xmpp_port | 1611 server_data['xmpp_port'] = server.xmpp_port |
| 1592 elif options.server_type == SERVER_TCP_ECHO: | 1612 elif options.server_type == SERVER_TCP_ECHO: |
| 1613 # Used for generating the key (randomly) that encodes the "echo request" | |
| 1614 # message. | |
| 1615 random.seed() | |
| 1593 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler) | 1616 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler) |
| 1594 print 'Echo TCP server started on port %d...' % server.server_port | 1617 print 'Echo TCP server started on port %d...' % server.server_port |
| 1595 server_data['port'] = server.server_port | 1618 server_data['port'] = server.server_port |
| 1596 elif options.server_type == SERVER_UDP_ECHO: | 1619 elif options.server_type == SERVER_UDP_ECHO: |
| 1620 # Used for generating the key (randomly) that encodes the "echo request" | |
| 1621 # message. | |
| 1622 random.seed() | |
| 1597 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler) | 1623 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler) |
| 1598 print 'Echo UDP server started on port %d...' % server.server_port | 1624 print 'Echo UDP server started on port %d...' % server.server_port |
| 1599 server_data['port'] = server.server_port | 1625 server_data['port'] = server.server_port |
| 1600 # means FTP Server | 1626 # means FTP Server |
| 1601 else: | 1627 else: |
| 1602 my_data_dir = MakeDataDir() | 1628 my_data_dir = MakeDataDir() |
| 1603 | 1629 |
| 1604 # Instantiate a dummy authorizer for managing 'virtual' users | 1630 # Instantiate a dummy authorizer for managing 'virtual' users |
| 1605 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() | 1631 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() |
| 1606 | 1632 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1713 'random key if none is specified on the command ' | 1739 'random key if none is specified on the command ' |
| 1714 'line.') | 1740 'line.') |
| 1715 option_parser.add_option('', '--policy-user', default='user@example.com', | 1741 option_parser.add_option('', '--policy-user', default='user@example.com', |
| 1716 dest='policy_user', | 1742 dest='policy_user', |
| 1717 help='Specify the user name the server should ' | 1743 help='Specify the user name the server should ' |
| 1718 'report back to the client as the user owning the ' | 1744 'report back to the client as the user owning the ' |
| 1719 'token used for making the policy request.') | 1745 'token used for making the policy request.') |
| 1720 options, args = option_parser.parse_args() | 1746 options, args = option_parser.parse_args() |
| 1721 | 1747 |
| 1722 sys.exit(main(options, args)) | 1748 sys.exit(main(options, args)) |
| OLD | NEW |