OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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/UDP/ server used for testing Chrome. | 6 """This is a simple HTTP/FTP/SYNC/TCP/UDP/ server used for testing Chrome. |
7 | 7 |
8 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. |
9 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 |
10 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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 import hashlib | 21 import hashlib |
22 import json | 22 import json |
23 import logging | 23 import logging |
24 import minica | 24 import minica |
25 import os | 25 import os |
26 import random | 26 import random |
27 import re | 27 import re |
28 import select | 28 import select |
29 import socket | 29 import socket |
30 import SocketServer | 30 import SocketServer |
| 31 import struct |
31 import sys | 32 import sys |
32 import threading | 33 import threading |
33 import time | 34 import time |
34 import urllib | 35 import urllib |
35 import urlparse | 36 import urlparse |
36 import zlib | 37 import zlib |
37 | 38 |
38 import echo_message | 39 import echo_message |
39 import pyftpdlib.ftpserver | 40 import pyftpdlib.ftpserver |
40 import testserver_base | 41 import testserver_base |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 self.SlowServerHandler, | 467 self.SlowServerHandler, |
467 self.ChunkedServerHandler, | 468 self.ChunkedServerHandler, |
468 self.ContentTypeHandler, | 469 self.ContentTypeHandler, |
469 self.NoContentHandler, | 470 self.NoContentHandler, |
470 self.ServerRedirectHandler, | 471 self.ServerRedirectHandler, |
471 self.ClientRedirectHandler, | 472 self.ClientRedirectHandler, |
472 self.MultipartHandler, | 473 self.MultipartHandler, |
473 self.MultipartSlowHandler, | 474 self.MultipartSlowHandler, |
474 self.GetSSLSessionCacheHandler, | 475 self.GetSSLSessionCacheHandler, |
475 self.CloseSocketHandler, | 476 self.CloseSocketHandler, |
| 477 self.RangeResetHandler, |
476 self.DefaultResponseHandler] | 478 self.DefaultResponseHandler] |
477 post_handlers = [ | 479 post_handlers = [ |
478 self.EchoTitleHandler, | 480 self.EchoTitleHandler, |
479 self.EchoHandler, | 481 self.EchoHandler, |
480 self.DeviceManagementHandler, | 482 self.DeviceManagementHandler, |
481 self.PostOnlyFileHandler] + get_handlers | 483 self.PostOnlyFileHandler] + get_handlers |
482 put_handlers = [ | 484 put_handlers = [ |
483 self.EchoTitleHandler, | 485 self.EchoTitleHandler, |
484 self.EchoHandler] + get_handlers | 486 self.EchoHandler] + get_handlers |
485 head_handlers = [ | 487 head_handlers = [ |
(...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1615 | 1617 |
1616 def CloseSocketHandler(self): | 1618 def CloseSocketHandler(self): |
1617 """Closes the socket without sending anything.""" | 1619 """Closes the socket without sending anything.""" |
1618 | 1620 |
1619 if not self._ShouldHandleRequest('/close-socket'): | 1621 if not self._ShouldHandleRequest('/close-socket'): |
1620 return False | 1622 return False |
1621 | 1623 |
1622 self.wfile.close() | 1624 self.wfile.close() |
1623 return True | 1625 return True |
1624 | 1626 |
| 1627 def RangeResetHandler(self): |
| 1628 """Send data broken up by connection resets every N (default 4K) bytes. |
| 1629 Support range requests. If the data requested doesn't straddle a reset |
| 1630 boundary, it will all be sent. Used for testing resuming downloads.""" |
| 1631 |
| 1632 if not self._ShouldHandleRequest('/rangereset'): |
| 1633 return False |
| 1634 |
| 1635 _, _, url_path, _, query, _ = urlparse.urlparse(self.path) |
| 1636 |
| 1637 # Defaults |
| 1638 size = 8000 |
| 1639 # Note that the rst is sent just before sending the rst_boundary byte. |
| 1640 rst_boundary = 4000 |
| 1641 respond_to_range = True |
| 1642 hold_for_signal = False |
| 1643 |
| 1644 # Parse the query |
| 1645 qdict = urlparse.parse_qs(query, True) |
| 1646 if 'size' in qdict: |
| 1647 size = int(qdict['size']) |
| 1648 if 'rst_boundary' in qdict: |
| 1649 rst_boundary = int(qdict['rst_boundary']) |
| 1650 if 'bounce_range' in qdict: |
| 1651 respond_to_range = False |
| 1652 if 'hold' in qdict: |
| 1653 hold_for_signal = True |
| 1654 |
| 1655 first_byte = 0 |
| 1656 last_byte = size - 1 |
| 1657 |
| 1658 # Does that define what we want to return, or do we need to apply |
| 1659 # a range? |
| 1660 range_response = False |
| 1661 range_header = self.headers.getheader('range') |
| 1662 if range_header and respond_to_range: |
| 1663 mo = re.match("bytes=(\d*)-(\d*)", range_header) |
| 1664 if mo.group(1): |
| 1665 first_byte = int(mo.group(1)) |
| 1666 if mo.group(2): |
| 1667 last_byte = int(mo.group(2)) |
| 1668 if last_byte > size - 1: |
| 1669 last_byte = size - 1 |
| 1670 range_response = True |
| 1671 if last_byte < first_byte: |
| 1672 return False |
| 1673 |
| 1674 # Set socket send buf high enough that we don't need to worry |
| 1675 # about asynchronous closes when sending RSTs. |
| 1676 self.connection.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, |
| 1677 16284) |
| 1678 |
| 1679 if range_response: |
| 1680 self.send_response(206) |
| 1681 self.send_header('Content-Range', |
| 1682 'bytes %d-%d/%d' % (first_byte, last_byte, size)) |
| 1683 else: |
| 1684 self.send_response(200) |
| 1685 self.send_header('Content-Type', 'application/octet-stream') |
| 1686 self.send_header('Content-Length', last_byte - first_byte + 1) |
| 1687 self.end_headers() |
| 1688 |
| 1689 if hold_for_signal: |
| 1690 # TODO(rdsmith/phajdan.jr): http://crbug.com/169519: Without writing |
| 1691 # a single byte, the self.server.handle_request() below hangs |
| 1692 # without processing new incoming requests. |
| 1693 self.wfile.write('X') |
| 1694 first_byte = first_byte + 1 |
| 1695 # handle requests until one of them clears this flag. |
| 1696 self.server.waitForDownload = True |
| 1697 while self.server.waitForDownload: |
| 1698 self.server.handle_request() |
| 1699 |
| 1700 possible_rst = ((first_byte / rst_boundary) + 1) * rst_boundary |
| 1701 if possible_rst >= last_byte: |
| 1702 # No RST has been requested in this range, so we don't need to |
| 1703 # do anything fancy; just write the data and let the python |
| 1704 # infrastructure close the connection. |
| 1705 self.wfile.write('X' * (last_byte - first_byte + 1)) |
| 1706 self.wfile.flush() |
| 1707 return True |
| 1708 |
| 1709 # We're resetting the connection part way in; go to the RST |
| 1710 # boundary and then send an RST. |
| 1711 # WINDOWS WARNING: On windows, if the amount of data sent before the |
| 1712 # reset is > 4096, only 4096 bytes will make it across before the RST |
| 1713 # despite the flush. This is hypothesized to be due to an underlying |
| 1714 # asynchronous sending implementation, which the 0 second linger |
| 1715 # forcibly terminates. The amount of data pre-RST should be kept below |
| 1716 # 4096 for this reason. |
| 1717 self.wfile.write('X' * (possible_rst - first_byte)) |
| 1718 self.wfile.flush() |
| 1719 l_onoff = 1 # Linger is active. |
| 1720 l_linger = 0 # Seconds to linger for. |
| 1721 self.connection.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
| 1722 struct.pack('ii', l_onoff, l_linger)) |
| 1723 |
| 1724 # Close all duplicates of the underlying socket to force the RST. |
| 1725 self.wfile.close() |
| 1726 self.rfile.close() |
| 1727 self.connection.close() |
| 1728 |
| 1729 return True |
| 1730 |
1625 def DefaultResponseHandler(self): | 1731 def DefaultResponseHandler(self): |
1626 """This is the catch-all response handler for requests that aren't handled | 1732 """This is the catch-all response handler for requests that aren't handled |
1627 by one of the special handlers above. | 1733 by one of the special handlers above. |
1628 Note that we specify the content-length as without it the https connection | 1734 Note that we specify the content-length as without it the https connection |
1629 is not closed properly (and the browser keeps expecting data).""" | 1735 is not closed properly (and the browser keeps expecting data).""" |
1630 | 1736 |
1631 contents = "Default response given for path: " + self.path | 1737 contents = "Default response given for path: " + self.path |
1632 self.send_response(200) | 1738 self.send_response(200) |
1633 self.send_header('Content-Type', 'text/html') | 1739 self.send_header('Content-Type', 'text/html') |
1634 self.send_header('Content-Length', len(contents)) | 1740 self.send_header('Content-Length', len(contents)) |
(...skipping 808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2443 'load multipe keys into the server. If the ' | 2549 'load multipe keys into the server. If the ' |
2444 'server has multiple keys, it will rotate ' | 2550 'server has multiple keys, it will rotate ' |
2445 'through them in at each request a ' | 2551 'through them in at each request a ' |
2446 'round-robin fashion. The server will ' | 2552 'round-robin fashion. The server will ' |
2447 'generate a random key if none is specified ' | 2553 'generate a random key if none is specified ' |
2448 'on the command line.') | 2554 'on the command line.') |
2449 | 2555 |
2450 | 2556 |
2451 if __name__ == '__main__': | 2557 if __name__ == '__main__': |
2452 sys.exit(ServerRunner().main()) | 2558 sys.exit(ServerRunner().main()) |
OLD | NEW |