tests/badserverext.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sun, 03 Mar 2019 18:22:49 -0800
changeset 41846 765f836a9484
parent 41466 4d5aae86c9bd
child 43076 2372284d9457
permissions -rw-r--r--
inno: move inno installer files to contrib/packaging/inno Let's isolate the inno installer files to their own directory so the separation between things is clearer. This required adjusting a few relative paths and references to the old directory. Differential Revision: https://phab.mercurial-scm.org/D6059
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# badserverext.py - Extension making servers behave badly
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
# no-check-code
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
"""Extension to make servers behave badly.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
This extension is useful for testing Mercurial behavior when various network
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
events occur.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
Various config options in the [badserver] section influence behavior:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
closebeforeaccept
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
   If true, close() the server socket when a new connection arrives before
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
   accept() is called. The server will then exit.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
closeafteraccept
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
   If true, the server will close() the client socket immediately after
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
   accept().
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
closeafterrecvbytes
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
   If defined, close the client socket after receiving this many bytes.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
closeaftersendbytes
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
   If defined, close the client socket after sending this many bytes.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
"""
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
from __future__ import absolute_import
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
import socket
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    36
from mercurial import(
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
    37
    pycompat,
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    38
    registrar,
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    39
)
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    40
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
from mercurial.hgweb import (
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
    server,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    45
configtable = {}
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    46
configitem = registrar.configitem(configtable)
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    47
36395
fb7897e53d49 py3: add b'' prefixes to config options in test/badserverext.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33194
diff changeset
    48
configitem(b'badserver', b'closeafteraccept',
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    49
    default=False,
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    50
)
36395
fb7897e53d49 py3: add b'' prefixes to config options in test/badserverext.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33194
diff changeset
    51
configitem(b'badserver', b'closeafterrecvbytes',
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
    52
    default=b'0',
33192
c538fca0d511 configitems: register the 'badserver.closeafterrecvbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33191
diff changeset
    53
)
36395
fb7897e53d49 py3: add b'' prefixes to config options in test/badserverext.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33194
diff changeset
    54
configitem(b'badserver', b'closeaftersendbytes',
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
    55
    default=b'0',
33193
cbb50fd830ea configitems: register the 'badserver.closeaftersendbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33192
diff changeset
    56
)
36395
fb7897e53d49 py3: add b'' prefixes to config options in test/badserverext.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33194
diff changeset
    57
configitem(b'badserver', b'closebeforeaccept',
33194
c077eac329e2 configitems: register the 'badserver.closebeforeaccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33193
diff changeset
    58
    default=False,
c077eac329e2 configitems: register the 'badserver.closebeforeaccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33193
diff changeset
    59
)
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
    60
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
# We can't adjust __class__ on a socket instance. So we define a proxy type.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
class socketproxy(object):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
    __slots__ = (
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
        '_orig',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
        '_logfp',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
        '_closeafterrecvbytes',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
        '_closeaftersendbytes',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
    )
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
    def __init__(self, obj, logfp, closeafterrecvbytes=0,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
                 closeaftersendbytes=0):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
        object.__setattr__(self, '_orig', obj)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
        object.__setattr__(self, '_logfp', logfp)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
        object.__setattr__(self, '_closeafterrecvbytes', closeafterrecvbytes)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
        object.__setattr__(self, '_closeaftersendbytes', closeaftersendbytes)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
    def __getattribute__(self, name):
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    78
        if name in ('makefile', 'sendall', '_writelog'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    79
            return object.__getattribute__(self, name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
        return getattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    83
    def __delattr__(self, name):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
        delattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    86
    def __setattr__(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
        setattr(object.__getattribute__(self, '_orig'), name, value)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    89
    def _writelog(self, msg):
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    90
        msg = msg.replace(b'\r', b'\\r').replace(b'\n', b'\\n')
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    91
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    92
        object.__getattribute__(self, '_logfp').write(msg)
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    93
        object.__getattribute__(self, '_logfp').write(b'\n')
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    94
        object.__getattribute__(self, '_logfp').flush()
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
    95
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    96
    def makefile(self, mode, bufsize):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
        f = object.__getattribute__(self, '_orig').makefile(mode, bufsize)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    99
        logfp = object.__getattribute__(self, '_logfp')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
        closeafterrecvbytes = object.__getattribute__(self,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
                                                      '_closeafterrecvbytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
        closeaftersendbytes = object.__getattribute__(self,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
                                                      '_closeaftersendbytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
        return fileobjectproxy(f, logfp,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   106
                               closeafterrecvbytes=closeafterrecvbytes,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   107
                               closeaftersendbytes=closeaftersendbytes)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   108
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   109
    def sendall(self, data, flags=0):
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   110
        remaining = object.__getattribute__(self, '_closeaftersendbytes')
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   111
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   112
        # No read limit. Call original function.
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   113
        if not remaining:
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   114
            result = object.__getattribute__(self, '_orig').sendall(data, flags)
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   115
            self._writelog(b'sendall(%d) -> %s' % (len(data), data))
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   116
            return result
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   117
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   118
        if len(data) > remaining:
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   119
            newdata = data[0:remaining]
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   120
        else:
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   121
            newdata = data
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   122
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   123
        remaining -= len(newdata)
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   124
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   125
        result = object.__getattribute__(self, '_orig').sendall(newdata, flags)
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   126
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   127
        self._writelog(b'sendall(%d from %d) -> (%d) %s' % (
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   128
            len(newdata), len(data), remaining, newdata))
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   129
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   130
        object.__setattr__(self, '_closeaftersendbytes', remaining)
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   131
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   132
        if remaining <= 0:
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   133
            self._writelog(b'write limit reached; closing socket')
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   134
            object.__getattribute__(self, '_orig').shutdown(socket.SHUT_RDWR)
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   135
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   136
            raise Exception('connection closed after sending N bytes')
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   137
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   138
        return result
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   139
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
   140
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   141
# We can't adjust __class__ on socket._fileobject, so define a proxy.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   142
class fileobjectproxy(object):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   143
    __slots__ = (
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   144
        '_orig',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   145
        '_logfp',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   146
        '_closeafterrecvbytes',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   147
        '_closeaftersendbytes',
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   148
    )
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   149
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   150
    def __init__(self, obj, logfp, closeafterrecvbytes=0,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   151
                 closeaftersendbytes=0):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   152
        object.__setattr__(self, '_orig', obj)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   153
        object.__setattr__(self, '_logfp', logfp)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   154
        object.__setattr__(self, '_closeafterrecvbytes', closeafterrecvbytes)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   155
        object.__setattr__(self, '_closeaftersendbytes', closeaftersendbytes)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   156
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   157
    def __getattribute__(self, name):
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   158
        if name in ('_close', 'read', 'readline', 'write', '_writelog'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   159
            return object.__getattribute__(self, name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   160
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   161
        return getattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   162
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   163
    def __delattr__(self, name):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   164
        delattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   165
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   166
    def __setattr__(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   167
        setattr(object.__getattribute__(self, '_orig'), name, value)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   168
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   169
    def _writelog(self, msg):
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   170
        msg = msg.replace(b'\r', b'\\r').replace(b'\n', b'\\n')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   171
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   172
        object.__getattribute__(self, '_logfp').write(msg)
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   173
        object.__getattribute__(self, '_logfp').write(b'\n')
32021
08e46fcb8637 badserverext: explicitly flush each log write
Matt Harbison <matt_harbison@yahoo.com>
parents: 32001
diff changeset
   174
        object.__getattribute__(self, '_logfp').flush()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   175
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   176
    def _close(self):
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   177
        # Python 3 uses an io.BufferedIO instance. Python 2 uses some file
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   178
        # object wrapper.
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   179
        if pycompat.ispy3:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   180
            orig = object.__getattribute__(self, '_orig')
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   181
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   182
            if hasattr(orig, 'raw'):
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   183
                orig.raw._sock.shutdown(socket.SHUT_RDWR)
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   184
            else:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   185
                self.close()
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   186
        else:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   187
            self._sock.shutdown(socket.SHUT_RDWR)
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   188
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   189
    def read(self, size=-1):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   190
        remaining = object.__getattribute__(self, '_closeafterrecvbytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   191
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   192
        # No read limit. Call original function.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   193
        if not remaining:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   194
            result = object.__getattribute__(self, '_orig').read(size)
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   195
            self._writelog(b'read(%d) -> (%d) (%s) %s' % (size,
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   196
                                                          len(result),
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   197
                                                          result))
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   198
            return result
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   199
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   200
        origsize = size
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   201
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   202
        if size < 0:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   203
            size = remaining
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   204
        else:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   205
            size = min(remaining, size)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   206
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   207
        result = object.__getattribute__(self, '_orig').read(size)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   208
        remaining -= len(result)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   209
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   210
        self._writelog(b'read(%d from %d) -> (%d) %s' % (
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   211
            size, origsize, len(result), result))
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   212
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   213
        object.__setattr__(self, '_closeafterrecvbytes', remaining)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   214
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   215
        if remaining <= 0:
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   216
            self._writelog(b'read limit reached, closing socket')
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   217
            self._close()
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   218
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   219
            # This is the easiest way to abort the current request.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   220
            raise Exception('connection closed after receiving N bytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   221
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   222
        return result
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   223
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   224
    def readline(self, size=-1):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   225
        remaining = object.__getattribute__(self, '_closeafterrecvbytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   226
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   227
        # No read limit. Call original function.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   228
        if not remaining:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   229
            result = object.__getattribute__(self, '_orig').readline(size)
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   230
            self._writelog(b'readline(%d) -> (%d) %s' % (
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   231
                size, len(result), result))
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   232
            return result
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   233
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   234
        origsize = size
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   235
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   236
        if size < 0:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   237
            size = remaining
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   238
        else:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   239
            size = min(remaining, size)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   240
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   241
        result = object.__getattribute__(self, '_orig').readline(size)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   242
        remaining -= len(result)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   243
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   244
        self._writelog(b'readline(%d from %d) -> (%d) %s' % (
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   245
            size, origsize, len(result), result))
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   246
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   247
        object.__setattr__(self, '_closeafterrecvbytes', remaining)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   248
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   249
        if remaining <= 0:
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   250
            self._writelog(b'read limit reached; closing socket')
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   251
            self._close()
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   252
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   253
            # This is the easiest way to abort the current request.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   254
            raise Exception('connection closed after receiving N bytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   255
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   256
        return result
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   257
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   258
    def write(self, data):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   259
        remaining = object.__getattribute__(self, '_closeaftersendbytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   260
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   261
        # No byte limit on this operation. Call original function.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   262
        if not remaining:
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   263
            self._writelog(b'write(%d) -> %s' % (len(data), data))
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   264
            result = object.__getattribute__(self, '_orig').write(data)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   265
            return result
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   266
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   267
        if len(data) > remaining:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   268
            newdata = data[0:remaining]
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   269
        else:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   270
            newdata = data
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   271
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   272
        remaining -= len(newdata)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   273
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   274
        self._writelog(b'write(%d from %d) -> (%d) %s' % (
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   275
            len(newdata), len(data), remaining, newdata))
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   276
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   277
        result = object.__getattribute__(self, '_orig').write(newdata)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   278
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   279
        object.__setattr__(self, '_closeaftersendbytes', remaining)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   280
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   281
        if remaining <= 0:
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   282
            self._writelog(b'write limit reached; closing socket')
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   283
            self._close()
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
   284
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   285
            raise Exception('connection closed after sending N bytes')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   286
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   287
        return result
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   288
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   289
def extsetup(ui):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   290
    # Change the base HTTP server class so various events can be performed.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   291
    # See SocketServer.BaseServer for how the specially named methods work.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   292
    class badserver(server.MercurialHTTPServer):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   293
        def __init__(self, ui, *args, **kwargs):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   294
            self._ui = ui
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   295
            super(badserver, self).__init__(ui, *args, **kwargs)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   296
39428
cbfab495dbcf py3: add missing b'' prefixes in tests/badserverext.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 37498
diff changeset
   297
            recvbytes = self._ui.config(b'badserver', b'closeafterrecvbytes')
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   298
            recvbytes = recvbytes.split(b',')
37498
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   299
            self.closeafterrecvbytes = [int(v) for v in recvbytes if v]
39428
cbfab495dbcf py3: add missing b'' prefixes in tests/badserverext.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 37498
diff changeset
   300
            sendbytes = self._ui.config(b'badserver', b'closeaftersendbytes')
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   301
            sendbytes = sendbytes.split(b',')
37498
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   302
            self.closeaftersendbytes = [int(v) for v in sendbytes if v]
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   303
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   304
            # Need to inherit object so super() works.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   305
            class badrequesthandler(self.RequestHandlerClass, object):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   306
                def send_header(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   307
                    # Make headers deterministic to facilitate testing.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   308
                    if name.lower() == 'date':
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   309
                        value = 'Fri, 14 Apr 2017 00:00:00 GMT'
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   310
                    elif name.lower() == 'server':
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   311
                        value = 'badhttpserver'
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   312
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   313
                    return super(badrequesthandler, self).send_header(name,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   314
                                                                      value)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   315
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   316
            self.RequestHandlerClass = badrequesthandler
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   317
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   318
        # Called to accept() a pending socket.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   319
        def get_request(self):
39428
cbfab495dbcf py3: add missing b'' prefixes in tests/badserverext.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 37498
diff changeset
   320
            if self._ui.configbool(b'badserver', b'closebeforeaccept'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   321
                self.socket.close()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   322
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   323
                # Tells the server to stop processing more requests.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   324
                self.__shutdown_request = True
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   325
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   326
                # Simulate failure to stop processing this request.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   327
                raise socket.error('close before accept')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   328
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
   329
            if self._ui.configbool(b'badserver', b'closeafteraccept'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   330
                request, client_address = super(badserver, self).get_request()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   331
                request.close()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   332
                raise socket.error('close after accept')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   333
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   334
            return super(badserver, self).get_request()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   335
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   336
        # Does heavy lifting of processing a request. Invokes
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   337
        # self.finish_request() which calls self.RequestHandlerClass() which
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   338
        # is a hgweb.server._httprequesthandler.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   339
        def process_request(self, socket, address):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   340
            # Wrap socket in a proxy if we need to count bytes.
37498
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   341
            if self.closeafterrecvbytes:
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   342
                closeafterrecvbytes = self.closeafterrecvbytes.pop(0)
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   343
            else:
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   344
                closeafterrecvbytes = 0
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   345
            if self.closeaftersendbytes:
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   346
                closeaftersendbytes = self.closeaftersendbytes.pop(0)
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   347
            else:
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
   348
                closeaftersendbytes = 0
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   349
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   350
            if closeafterrecvbytes or closeaftersendbytes:
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   351
                socket = socketproxy(socket, self.errorlog,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   352
                                     closeafterrecvbytes=closeafterrecvbytes,
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   353
                                     closeaftersendbytes=closeaftersendbytes)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   354
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   355
            return super(badserver, self).process_request(socket, address)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   356
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   357
    server.MercurialHTTPServer = badserver