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 1465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1514 | 1515 |
1515 | 1516 |
1516 class TCPEchoHandler(SocketServer.BaseRequestHandler): | 1517 class TCPEchoHandler(SocketServer.BaseRequestHandler): |
1517 """The RequestHandler class for TCP echo server. | 1518 """The RequestHandler class for TCP echo server. |
1518 | 1519 |
1519 It is instantiated once per connection to the server, and overrides the | 1520 It is instantiated once per connection to the server, and overrides the |
1520 handle() method to implement communication to the client. | 1521 handle() method to implement communication to the client. |
1521 """ | 1522 """ |
1522 | 1523 |
1523 def handle(self): | 1524 def handle(self): |
1524 data = self.request.recv(65536) | 1525 """Handles the request from the client and constructs a response.""" |
1525 if not data: | 1526 |
| 1527 data = self.request.recv(65536).strip() |
| 1528 # Verify the "echo request" message received from the client. Send back |
| 1529 # "echo response" message if "echo request" message is valid. |
| 1530 try: |
| 1531 return_data = echo_message.GetEchoResponseData(data) |
| 1532 if not return_data: |
1526 return | 1533 return |
1527 self.request.send(data) | 1534 except ValueError: |
| 1535 return |
| 1536 |
| 1537 self.request.send(return_data) |
1528 | 1538 |
1529 | 1539 |
1530 class UDPEchoHandler(SocketServer.BaseRequestHandler): | 1540 class UDPEchoHandler(SocketServer.BaseRequestHandler): |
1531 """The RequestHandler class for UDP echo server. | 1541 """The RequestHandler class for UDP echo server. |
1532 | 1542 |
1533 It is instantiated once per connection to the server, and overrides the | 1543 It is instantiated once per connection to the server, and overrides the |
1534 handle() method to implement communication to the client. | 1544 handle() method to implement communication to the client. |
1535 """ | 1545 """ |
1536 | 1546 |
1537 def handle(self): | 1547 def handle(self): |
| 1548 """Handles the request from the client and constructs a response.""" |
| 1549 |
1538 data = self.request[0].strip() | 1550 data = self.request[0].strip() |
1539 socket = self.request[1] | 1551 socket = self.request[1] |
1540 socket.sendto(data, self.client_address) | 1552 # Verify the "echo request" message received from the client. Send back |
| 1553 # "echo response" message if "echo request" message is valid. |
| 1554 try: |
| 1555 return_data = echo_message.GetEchoResponseData(data) |
| 1556 if not return_data: |
| 1557 return |
| 1558 except ValueError: |
| 1559 return |
| 1560 socket.sendto(return_data, self.client_address) |
1541 | 1561 |
1542 | 1562 |
1543 class FileMultiplexer: | 1563 class FileMultiplexer: |
1544 def __init__(self, fd1, fd2) : | 1564 def __init__(self, fd1, fd2) : |
1545 self.__fd1 = fd1 | 1565 self.__fd1 = fd1 |
1546 self.__fd2 = fd2 | 1566 self.__fd2 = fd2 |
1547 | 1567 |
1548 def __del__(self) : | 1568 def __del__(self) : |
1549 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: | 1569 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: |
1550 self.__fd1.close() | 1570 self.__fd1.close() |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1597 server._device_management_handler = None | 1617 server._device_management_handler = None |
1598 server.policy_keys = options.policy_keys | 1618 server.policy_keys = options.policy_keys |
1599 server.policy_user = options.policy_user | 1619 server.policy_user = options.policy_user |
1600 elif options.server_type == SERVER_SYNC: | 1620 elif options.server_type == SERVER_SYNC: |
1601 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) | 1621 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) |
1602 print 'Sync HTTP server started on port %d...' % server.server_port | 1622 print 'Sync HTTP server started on port %d...' % server.server_port |
1603 print 'Sync XMPP server started on port %d...' % server.xmpp_port | 1623 print 'Sync XMPP server started on port %d...' % server.xmpp_port |
1604 server_data['port'] = server.server_port | 1624 server_data['port'] = server.server_port |
1605 server_data['xmpp_port'] = server.xmpp_port | 1625 server_data['xmpp_port'] = server.xmpp_port |
1606 elif options.server_type == SERVER_TCP_ECHO: | 1626 elif options.server_type == SERVER_TCP_ECHO: |
| 1627 # Used for generating the key (randomly) that encodes the "echo request" |
| 1628 # message. |
| 1629 random.seed() |
1607 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler) | 1630 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler) |
1608 print 'Echo TCP server started on port %d...' % server.server_port | 1631 print 'Echo TCP server started on port %d...' % server.server_port |
1609 server_data['port'] = server.server_port | 1632 server_data['port'] = server.server_port |
1610 elif options.server_type == SERVER_UDP_ECHO: | 1633 elif options.server_type == SERVER_UDP_ECHO: |
| 1634 # Used for generating the key (randomly) that encodes the "echo request" |
| 1635 # message. |
| 1636 random.seed() |
1611 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler) | 1637 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler) |
1612 print 'Echo UDP server started on port %d...' % server.server_port | 1638 print 'Echo UDP server started on port %d...' % server.server_port |
1613 server_data['port'] = server.server_port | 1639 server_data['port'] = server.server_port |
1614 # means FTP Server | 1640 # means FTP Server |
1615 else: | 1641 else: |
1616 my_data_dir = MakeDataDir() | 1642 my_data_dir = MakeDataDir() |
1617 | 1643 |
1618 # Instantiate a dummy authorizer for managing 'virtual' users | 1644 # Instantiate a dummy authorizer for managing 'virtual' users |
1619 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() | 1645 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() |
1620 | 1646 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1727 'random key if none is specified on the command ' | 1753 'random key if none is specified on the command ' |
1728 'line.') | 1754 'line.') |
1729 option_parser.add_option('', '--policy-user', default='user@example.com', | 1755 option_parser.add_option('', '--policy-user', default='user@example.com', |
1730 dest='policy_user', | 1756 dest='policy_user', |
1731 help='Specify the user name the server should ' | 1757 help='Specify the user name the server should ' |
1732 'report back to the client as the user owning the ' | 1758 'report back to the client as the user owning the ' |
1733 'token used for making the policy request.') | 1759 'token used for making the policy request.') |
1734 options, args = option_parser.parse_args() | 1760 options, args = option_parser.parse_args() |
1735 | 1761 |
1736 sys.exit(main(options, args)) | 1762 sys.exit(main(options, args)) |
OLD | NEW |