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 server used for testing Chrome. | 6 """This is a simple HTTP/FTP/SYNC/TCP ECHO/UDP ECHO/ server used for testing |
| 7 Chrome. |
7 | 8 |
8 It supports several test URLs, as specified by the handlers in TestPageHandler. | 9 It supports several test URLs, as specified by the handlers in TestPageHandler. |
9 By default, it listens on an ephemeral port and sends the port number back to | 10 By default, it listens on an ephemeral port and sends the port number back to |
10 the originating process over a pipe. The originating process can specify an | 11 the originating process over a pipe. The originating process can specify an |
11 explicit port if necessary. | 12 explicit port if necessary. |
12 It can use https if you specify the flag --https=CERT where CERT is the path | 13 It can use https if you specify the flag --https=CERT where CERT is the path |
13 to a pem file containing the certificate and private key that should be used. | 14 to a pem file containing the certificate and private key that should be used. |
14 """ | 15 """ |
15 | 16 |
16 import asyncore | 17 import asyncore |
(...skipping 28 matching lines...) Expand all Loading... |
45 except ImportError: | 46 except ImportError: |
46 import md5 | 47 import md5 |
47 _new_md5 = md5.new | 48 _new_md5 = md5.new |
48 | 49 |
49 if sys.platform == 'win32': | 50 if sys.platform == 'win32': |
50 import msvcrt | 51 import msvcrt |
51 | 52 |
52 SERVER_HTTP = 0 | 53 SERVER_HTTP = 0 |
53 SERVER_FTP = 1 | 54 SERVER_FTP = 1 |
54 SERVER_SYNC = 2 | 55 SERVER_SYNC = 2 |
| 56 SERVER_TCP_ECHO = 3 |
| 57 SERVER_UDP_ECHO = 4 |
55 | 58 |
56 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . | 59 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . |
57 debug_output = sys.stderr | 60 debug_output = sys.stderr |
58 def debug(str): | 61 def debug(str): |
59 debug_output.write(str + "\n") | 62 debug_output.write(str + "\n") |
60 debug_output.flush() | 63 debug_output.flush() |
61 | 64 |
62 class StoppableHTTPServer(BaseHTTPServer.HTTPServer): | 65 class StoppableHTTPServer(BaseHTTPServer.HTTPServer): |
63 """This is a specialization of of BaseHTTPServer to allow it | 66 """This is a specialization of of BaseHTTPServer to allow it |
64 to be exited cleanly (by setting its "stop" member to True).""" | 67 to be exited cleanly (by setting its "stop" member to True).""" |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 205 |
203 for fd in write_fds: | 206 for fd in write_fds: |
204 HandleXmppSocket(fd, self._xmpp_socket_map, | 207 HandleXmppSocket(fd, self._xmpp_socket_map, |
205 asyncore.dispatcher.handle_write_event) | 208 asyncore.dispatcher.handle_write_event) |
206 | 209 |
207 for fd in exceptional_fds: | 210 for fd in exceptional_fds: |
208 HandleXmppSocket(fd, self._xmpp_socket_map, | 211 HandleXmppSocket(fd, self._xmpp_socket_map, |
209 asyncore.dispatcher.handle_expt_event) | 212 asyncore.dispatcher.handle_expt_event) |
210 | 213 |
211 | 214 |
| 215 class TCPEchoServer(SocketServer.TCPServer): |
| 216 """A TCP echo server that echoes back what it has received.""" |
| 217 |
| 218 def server_bind(self): |
| 219 """Override server_bind to store the server name.""" |
| 220 SocketServer.TCPServer.server_bind(self) |
| 221 host, port = self.socket.getsockname()[:2] |
| 222 self.server_name = socket.getfqdn(host) |
| 223 self.server_port = port |
| 224 |
| 225 def serve_forever(self): |
| 226 self.stop = False |
| 227 self.nonce_time = None |
| 228 while not self.stop: |
| 229 self.handle_request() |
| 230 self.socket.close() |
| 231 |
| 232 |
| 233 class UDPEchoServer(SocketServer.UDPServer): |
| 234 """A UDP echo server that echoes back what it has received.""" |
| 235 |
| 236 def server_bind(self): |
| 237 """Override server_bind to store the server name.""" |
| 238 SocketServer.UDPServer.server_bind(self) |
| 239 host, port = self.socket.getsockname()[:2] |
| 240 self.server_name = socket.getfqdn(host) |
| 241 self.server_port = port |
| 242 |
| 243 def serve_forever(self): |
| 244 self.stop = False |
| 245 self.nonce_time = None |
| 246 while not self.stop: |
| 247 self.handle_request() |
| 248 self.socket.close() |
| 249 |
| 250 |
212 class BasePageHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 251 class BasePageHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
213 | 252 |
214 def __init__(self, request, client_address, socket_server, | 253 def __init__(self, request, client_address, socket_server, |
215 connect_handlers, get_handlers, post_handlers, put_handlers): | 254 connect_handlers, get_handlers, post_handlers, put_handlers): |
216 self._connect_handlers = connect_handlers | 255 self._connect_handlers = connect_handlers |
217 self._get_handlers = get_handlers | 256 self._get_handlers = get_handlers |
218 self._post_handlers = post_handlers | 257 self._post_handlers = post_handlers |
219 self._put_handlers = put_handlers | 258 self._put_handlers = put_handlers |
220 BaseHTTPServer.BaseHTTPRequestHandler.__init__( | 259 BaseHTTPServer.BaseHTTPRequestHandler.__init__( |
221 self, request, client_address, socket_server) | 260 self, request, client_address, socket_server) |
(...skipping 1217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1439 # Create the default path to our data dir, relative to the exe dir. | 1478 # Create the default path to our data dir, relative to the exe dir. |
1440 my_data_dir = os.path.dirname(sys.argv[0]) | 1479 my_data_dir = os.path.dirname(sys.argv[0]) |
1441 my_data_dir = os.path.join(my_data_dir, "..", "..", "..", "..", | 1480 my_data_dir = os.path.join(my_data_dir, "..", "..", "..", "..", |
1442 "test", "data") | 1481 "test", "data") |
1443 | 1482 |
1444 #TODO(ibrar): Must use Find* funtion defined in google\tools | 1483 #TODO(ibrar): Must use Find* funtion defined in google\tools |
1445 #i.e my_data_dir = FindUpward(my_data_dir, "test", "data") | 1484 #i.e my_data_dir = FindUpward(my_data_dir, "test", "data") |
1446 | 1485 |
1447 return my_data_dir | 1486 return my_data_dir |
1448 | 1487 |
| 1488 |
| 1489 class TCPEchoHandler(SocketServer.BaseRequestHandler): |
| 1490 """The RequestHandler class for TCP echo server. |
| 1491 |
| 1492 It is instantiated once per connection to the server, and overrides the |
| 1493 handle() method to implement communication to the client. |
| 1494 """ |
| 1495 |
| 1496 def handle(self): |
| 1497 data = self.request.recv(65536) |
| 1498 if not data: |
| 1499 return |
| 1500 self.request.send(data) |
| 1501 |
| 1502 |
| 1503 class UDPEchoHandler(SocketServer.BaseRequestHandler): |
| 1504 """The RequestHandler class for UDP echo server. |
| 1505 |
| 1506 It is instantiated once per connection to the server, and overrides the |
| 1507 handle() method to implement communication to the client. |
| 1508 """ |
| 1509 |
| 1510 def handle(self): |
| 1511 data = self.request[0].strip() |
| 1512 socket = self.request[1] |
| 1513 socket.sendto(data, self.client_address) |
| 1514 |
| 1515 |
1449 class FileMultiplexer: | 1516 class FileMultiplexer: |
1450 def __init__(self, fd1, fd2) : | 1517 def __init__(self, fd1, fd2) : |
1451 self.__fd1 = fd1 | 1518 self.__fd1 = fd1 |
1452 self.__fd2 = fd2 | 1519 self.__fd2 = fd2 |
1453 | 1520 |
1454 def __del__(self) : | 1521 def __del__(self) : |
1455 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: | 1522 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: |
1456 self.__fd1.close() | 1523 self.__fd1.close() |
1457 if self.__fd2 != sys.stdout and self.__fd2 != sys.stderr: | 1524 if self.__fd2 != sys.stdout and self.__fd2 != sys.stderr: |
1458 self.__fd2.close() | 1525 self.__fd2.close() |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1502 server_data['port'] = server.server_port | 1569 server_data['port'] = server.server_port |
1503 server._device_management_handler = None | 1570 server._device_management_handler = None |
1504 server.policy_keys = options.policy_keys | 1571 server.policy_keys = options.policy_keys |
1505 server.policy_user = options.policy_user | 1572 server.policy_user = options.policy_user |
1506 elif options.server_type == SERVER_SYNC: | 1573 elif options.server_type == SERVER_SYNC: |
1507 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) | 1574 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) |
1508 print 'Sync HTTP server started on port %d...' % server.server_port | 1575 print 'Sync HTTP server started on port %d...' % server.server_port |
1509 print 'Sync XMPP server started on port %d...' % server.xmpp_port | 1576 print 'Sync XMPP server started on port %d...' % server.xmpp_port |
1510 server_data['port'] = server.server_port | 1577 server_data['port'] = server.server_port |
1511 server_data['xmpp_port'] = server.xmpp_port | 1578 server_data['xmpp_port'] = server.xmpp_port |
| 1579 elif options.server_type == SERVER_TCP_ECHO: |
| 1580 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler) |
| 1581 print 'Echo TCP server started on port %d...' % server.server_port |
| 1582 server_data['port'] = server.server_port |
| 1583 elif options.server_type == SERVER_UDP_ECHO: |
| 1584 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler) |
| 1585 print 'Echo UDP server started on port %d...' % server.server_port |
| 1586 server_data['port'] = server.server_port |
1512 # means FTP Server | 1587 # means FTP Server |
1513 else: | 1588 else: |
1514 my_data_dir = MakeDataDir() | 1589 my_data_dir = MakeDataDir() |
1515 | 1590 |
1516 # Instantiate a dummy authorizer for managing 'virtual' users | 1591 # Instantiate a dummy authorizer for managing 'virtual' users |
1517 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() | 1592 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() |
1518 | 1593 |
1519 # Define a new user having full r/w permissions and a read-only | 1594 # Define a new user having full r/w permissions and a read-only |
1520 # anonymous user | 1595 # anonymous user |
1521 authorizer.add_user('chrome', 'chrome', my_data_dir, perm='elradfmw') | 1596 authorizer.add_user('chrome', 'chrome', my_data_dir, perm='elradfmw') |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1564 if __name__ == '__main__': | 1639 if __name__ == '__main__': |
1565 option_parser = optparse.OptionParser() | 1640 option_parser = optparse.OptionParser() |
1566 option_parser.add_option("-f", '--ftp', action='store_const', | 1641 option_parser.add_option("-f", '--ftp', action='store_const', |
1567 const=SERVER_FTP, default=SERVER_HTTP, | 1642 const=SERVER_FTP, default=SERVER_HTTP, |
1568 dest='server_type', | 1643 dest='server_type', |
1569 help='start up an FTP server.') | 1644 help='start up an FTP server.') |
1570 option_parser.add_option('', '--sync', action='store_const', | 1645 option_parser.add_option('', '--sync', action='store_const', |
1571 const=SERVER_SYNC, default=SERVER_HTTP, | 1646 const=SERVER_SYNC, default=SERVER_HTTP, |
1572 dest='server_type', | 1647 dest='server_type', |
1573 help='start up a sync server.') | 1648 help='start up a sync server.') |
| 1649 option_parser.add_option('', '--tcp-echo', action='store_const', |
| 1650 const=SERVER_TCP_ECHO, default=SERVER_HTTP, |
| 1651 dest='server_type', |
| 1652 help='start up a tcp echo server.') |
| 1653 option_parser.add_option('', '--udp-echo', action='store_const', |
| 1654 const=SERVER_UDP_ECHO, default=SERVER_HTTP, |
| 1655 dest='server_type', |
| 1656 help='start up a udp echo server.') |
1574 option_parser.add_option('', '--log-to-console', action='store_const', | 1657 option_parser.add_option('', '--log-to-console', action='store_const', |
1575 const=True, default=False, | 1658 const=True, default=False, |
1576 dest='log_to_console', | 1659 dest='log_to_console', |
1577 help='Enables or disables sys.stdout logging to ' | 1660 help='Enables or disables sys.stdout logging to ' |
1578 'the console.') | 1661 'the console.') |
1579 option_parser.add_option('', '--port', default='0', type='int', | 1662 option_parser.add_option('', '--port', default='0', type='int', |
1580 help='Port used by the server. If unspecified, the ' | 1663 help='Port used by the server. If unspecified, the ' |
1581 'server will listen on an ephemeral port.') | 1664 'server will listen on an ephemeral port.') |
1582 option_parser.add_option('', '--data-dir', dest='data_dir', | 1665 option_parser.add_option('', '--data-dir', dest='data_dir', |
1583 help='Directory from which to read the files.') | 1666 help='Directory from which to read the files.') |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1617 'random key if none is specified on the command ' | 1700 'random key if none is specified on the command ' |
1618 'line.') | 1701 'line.') |
1619 option_parser.add_option('', '--policy-user', default='user@example.com', | 1702 option_parser.add_option('', '--policy-user', default='user@example.com', |
1620 dest='policy_user', | 1703 dest='policy_user', |
1621 help='Specify the user name the server should ' | 1704 help='Specify the user name the server should ' |
1622 'report back to the client as the user owning the ' | 1705 'report back to the client as the user owning the ' |
1623 'token used for making the policy request.') | 1706 'token used for making the policy request.') |
1624 options, args = option_parser.parse_args() | 1707 options, args = option_parser.parse_args() |
1625 | 1708 |
1626 sys.exit(main(options, args)) | 1709 sys.exit(main(options, args)) |
OLD | NEW |