annotate tests/testlib/badserverext.py @ 48618:9dc00c5617ea

test-bad-http-server: introduce sock closing when writing a pattern Instead of using a fixed number a bytes, a very unstable method. We introduce a new config that can define a regex. One this regex is detected, nothing after it will be written and the socket will be closed. Tests will be migrated to this new method in later changesets. Differential Revision: https://phab.mercurial-scm.org/D12049
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Sat, 22 Jan 2022 01:51:28 +0100
parents 9642dbe7bca1
children 5154f2025d8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
17 close-before-accept
32001
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
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
21 close-after-accept
32001
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
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
25 close-after-recv-bytes
32001
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.
48617
9642dbe7bca1 test-http-bad-server: document that the value are actually a list
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48616
diff changeset
27 (The value is a list, multiple values can use used to close a series of requests
9642dbe7bca1 test-http-bad-server: document that the value are actually a list
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48616
diff changeset
28 request)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
29
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
30 close-after-send-bytes
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
31 If defined, close the client socket after sending this many bytes.
48617
9642dbe7bca1 test-http-bad-server: document that the value are actually a list
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48616
diff changeset
32 (The value is a list, multiple values can use used to close a series of requests
9642dbe7bca1 test-http-bad-server: document that the value are actually a list
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48616
diff changeset
33 request)
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
34
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
35 close-after-send-patterns
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
36 If defined, close the client socket after the configured regexp is seen.
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
37 (The value is a list, multiple values can use used to close a series of requests
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
38 request)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
39 """
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
40
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
41 from __future__ import absolute_import
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
42
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
43 import re
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
44 import socket
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
45
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
46 from mercurial import (
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
47 pycompat,
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
48 registrar,
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
49 )
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
50
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
51 from mercurial.hgweb import server
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
52
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
53 configtable = {}
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
54 configitem = registrar.configitem(configtable)
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
55
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
56 configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
57 b'badserver',
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
58 b'close-after-accept',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
59 default=False,
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
60 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
61 configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
62 b'badserver',
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
63 b'close-after-recv-bytes',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
64 default=b'0',
33192
c538fca0d511 configitems: register the 'badserver.closeafterrecvbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33191
diff changeset
65 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
66 configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
67 b'badserver',
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
68 b'close-after-send-bytes',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
69 default=b'0',
33193
cbb50fd830ea configitems: register the 'badserver.closeaftersendbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33192
diff changeset
70 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
71 configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
72 b'badserver',
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
73 b'close-after-send-patterns',
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
74 default=b'',
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
75 )
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
76 configitem(
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
77 b'badserver',
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
78 b'close-before-accept',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
79 default=False,
33194
c077eac329e2 configitems: register the 'badserver.closebeforeaccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33193
diff changeset
80 )
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
81
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
82
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
83 class ConditionTracker(object):
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
84 def __init__(
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
85 self,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
86 close_after_recv_bytes,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
87 close_after_send_bytes,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
88 close_after_send_patterns,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
89 ):
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
90 self._all_close_after_recv_bytes = close_after_recv_bytes
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
91 self._all_close_after_send_bytes = close_after_send_bytes
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
92 self._all_close_after_send_patterns = close_after_send_patterns
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
93
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
94 self.target_recv_bytes = None
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
95 self.remaining_recv_bytes = None
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
96 self.target_send_bytes = None
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
97 self.remaining_send_bytes = None
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
98 self.send_pattern = None
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
99 self.send_data = b''
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
100
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
101 def start_next_request(self):
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
102 """move to the next set of close condition"""
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
103 if self._all_close_after_recv_bytes:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
104 self.target_recv_bytes = self._all_close_after_recv_bytes.pop(0)
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
105 self.remaining_recv_bytes = self.target_recv_bytes
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
106 else:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
107 self.target_recv_bytes = None
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
108 self.remaining_recv_bytes = None
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
109
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
110 if self._all_close_after_send_bytes:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
111 self.target_send_bytes = self._all_close_after_send_bytes.pop(0)
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
112 self.remaining_send_bytes = self.target_send_bytes
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
113 else:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
114 self.target_send_bytes = None
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
115 self.remaining_send_bytes = None
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
116
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
117 self.send_data = b''
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
118 if self._all_close_after_send_patterns:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
119 self.send_pattern = self._all_close_after_send_patterns.pop(0)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
120 else:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
121 self.send_pattern = None
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
122
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
123 def might_close(self):
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
124 """True, if any processing will be needed"""
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
125 if self.remaining_recv_bytes is not None:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
126 return True
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
127 if self.remaining_send_bytes is not None:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
128 return True
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
129 if self.send_pattern is not None:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
130 return True
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
131 return False
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
132
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
133 def forward_write(self, obj, method, data, *args, **kwargs):
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
134 """call an underlying write function until condition are met
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
135
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
136 When the condition are met the socket is closed
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
137 """
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
138 remaining = self.remaining_send_bytes
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
139 pattern = self.send_pattern
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
140
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
141 orig = object.__getattribute__(obj, '_orig')
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
142 bmethod = method.encode('ascii')
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
143 func = getattr(orig, method)
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
144
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
145 if pattern:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
146 self.send_data += data
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
147 pieces = pattern.split(self.send_data, maxsplit=1)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
148 if len(pieces) > 1:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
149 dropped = len(pieces[-1])
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
150 remaining = len(data) - dropped
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
151
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
152 if remaining:
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
153 remaining = max(0, remaining)
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
154
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
155 if not remaining:
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
156 newdata = data
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
157 else:
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
158 if remaining < len(data):
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
159 newdata = data[0:remaining]
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
160 else:
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
161 newdata = data
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
162 remaining -= len(newdata)
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
163 self.remaining_send_bytes = remaining
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
164
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
165 result = func(newdata, *args, **kwargs)
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
166
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
167 if remaining is None:
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
168 obj._writelog(b'%s(%d) -> %s' % (bmethod, len(data), data))
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
169 else:
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
170 msg = b'%s(%d from %d) -> (%d) %s'
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
171 msg %= (bmethod, len(newdata), len(data), remaining, newdata)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
172 obj._writelog(msg)
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
173
48616
98c502a2c462 test-http-bad-server: refactor the writing logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48615
diff changeset
174 if remaining is not None and remaining <= 0:
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
175 obj._writelog(b'write limit reached; closing socket')
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
176 object.__getattribute__(obj, '_cond_close')()
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
177 raise Exception('connection closed after sending N bytes')
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
178
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
179 return result
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
180
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
181 def forward_read(self, obj, method, size=-1):
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
182 """call an underlying read function until condition are met
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
183
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
184 When the condition are met the socket is closed
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
185 """
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
186 remaining = self.remaining_recv_bytes
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
187
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
188 orig = object.__getattribute__(obj, '_orig')
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
189 bmethod = method.encode('ascii')
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
190 func = getattr(orig, method)
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
191
48614
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
192 requested_size = size
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
193 actual_size = size
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
194
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
195 if remaining:
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
196 if size < 0:
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
197 actual_size = remaining
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
198 else:
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
199 actual_size = min(remaining, requested_size)
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
200
48614
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
201 result = func(actual_size)
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
202
48614
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
203 if remaining:
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
204 remaining -= len(result)
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
205 self.remaining_recv_bytes = remaining
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
206
48615
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
207 if requested_size == 65537:
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
208 requested_repr = b'~'
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
209 else:
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
210 requested_repr = b'%d' % requested_size
48614
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
211 if requested_size == actual_size:
48615
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
212 msg = b'%s(%s) -> (%d) %s'
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
213 msg %= (bmethod, requested_repr, len(result), result)
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
214 else:
48615
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
215 msg = b'%s(%d from %s) -> (%d) %s'
e38776a4c2cb test-http-bad-server: replace the default 65537 value in output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48614
diff changeset
216 msg %= (bmethod, actual_size, requested_repr, len(result), result)
48614
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
217 obj._writelog(msg)
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
218
48614
3efc8644dd00 test-http-bad-server: refactor the reading logic to avoid early return
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48613
diff changeset
219 if remaining is not None and remaining <= 0:
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
220 obj._writelog(b'read limit reached; closing socket')
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
221 obj._cond_close()
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
222
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
223 # This is the easiest way to abort the current request.
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
224 raise Exception('connection closed after receiving N bytes')
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
225
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
226 return result
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
227
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
228
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
229 # 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
230 class socketproxy(object):
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
231 __slots__ = ('_orig', '_logfp', '_cond')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
232
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
233 def __init__(self, obj, logfp, condition_tracked):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
234 object.__setattr__(self, '_orig', obj)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
235 object.__setattr__(self, '_logfp', logfp)
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
236 object.__setattr__(self, '_cond', condition_tracked)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
237
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
238 def __getattribute__(self, name):
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
239 if name in ('makefile', 'sendall', '_writelog', '_cond_close'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
240 return object.__getattribute__(self, name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
241
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
242 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
243
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
244 def __delattr__(self, name):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
245 delattr(object.__getattribute__(self, '_orig'), name)
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 def __setattr__(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
248 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
249
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
250 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
251 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
252
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
253 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
254 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
255 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
256
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
257 def makefile(self, mode, bufsize):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
258 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
259
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
260 logfp = object.__getattribute__(self, '_logfp')
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
261 cond = object.__getattribute__(self, '_cond')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
262
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
263 return fileobjectproxy(f, logfp, cond)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
264
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
265 def sendall(self, data, flags=0):
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
266 cond = object.__getattribute__(self, '_cond')
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
267 return cond.forward_write(self, 'sendall', data, flags)
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
268
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
269 def _cond_close(self):
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
270 object.__getattribute__(self, '_orig').shutdown(socket.SHUT_RDWR)
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
271
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
272
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
273 # 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
274 class fileobjectproxy(object):
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
275 __slots__ = ('_orig', '_logfp', '_cond')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
276
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
277 def __init__(self, obj, logfp, condition_tracked):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
278 object.__setattr__(self, '_orig', obj)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
279 object.__setattr__(self, '_logfp', logfp)
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
280 object.__setattr__(self, '_cond', condition_tracked)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
281
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
282 def __getattribute__(self, name):
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
283 if name in (
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
284 '_close',
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
285 'read',
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
286 'readline',
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
287 'write',
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
288 '_writelog',
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
289 '_cond_close',
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
290 ):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
291 return object.__getattribute__(self, name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
292
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
293 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
294
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
295 def __delattr__(self, name):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
296 delattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
297
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
298 def __setattr__(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
299 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
300
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
301 def _writelog(self, msg):
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
302 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
303
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
304 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
305 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
306 object.__getattribute__(self, '_logfp').flush()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
307
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
308 def _close(self):
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
309 # 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
310 # object wrapper.
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
311 if pycompat.ispy3:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
312 orig = object.__getattribute__(self, '_orig')
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
313
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
314 if hasattr(orig, 'raw'):
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
315 orig.raw._sock.shutdown(socket.SHUT_RDWR)
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
316 else:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
317 self.close()
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
318 else:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
319 self._sock.shutdown(socket.SHUT_RDWR)
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
320
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
321 def read(self, size=-1):
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
322 cond = object.__getattribute__(self, '_cond')
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
323 return cond.forward_read(self, 'read', size)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
324
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
325 def readline(self, size=-1):
48613
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
326 cond = object.__getattribute__(self, '_cond')
b060e305d79f test-http-bad-server: factor code dealing with "read" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48612
diff changeset
327 return cond.forward_read(self, 'readline', size)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
328
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
329 def write(self, data):
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
330 cond = object.__getattribute__(self, '_cond')
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
331 return cond.forward_write(self, 'write', data)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
332
48612
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
333 def _cond_close(self):
11e5cb170d36 test-http-bad-server: factor code dealing with "write" in the new object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48611
diff changeset
334 self._close()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
335
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
336
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
337 def process_bytes_config(value):
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
338 parts = value.split(b',')
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
339 integers = [int(v) for v in parts if v]
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
340 return [v if v else None for v in integers]
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
341
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
342
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
343 def process_pattern_config(value):
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
344 patterns = []
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
345 for p in value.split(b','):
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
346 if not p:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
347 p = None
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
348 else:
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
349 p = re.compile(p, re.DOTALL | re.MULTILINE)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
350 patterns.append(p)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
351 return patterns
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
352
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
353
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
354 def extsetup(ui):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
355 # 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
356 # 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
357 class badserver(server.MercurialHTTPServer):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
358 def __init__(self, ui, *args, **kwargs):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
359 self._ui = ui
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
360 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
361
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
362 all_recv_bytes = self._ui.config(
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
363 b'badserver', b'close-after-recv-bytes'
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
364 )
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
365 all_recv_bytes = process_bytes_config(all_recv_bytes)
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
366 all_send_bytes = self._ui.config(
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
367 b'badserver', b'close-after-send-bytes'
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
368 )
48618
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
369 all_send_bytes = process_bytes_config(all_send_bytes)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
370 all_send_patterns = self._ui.config(
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
371 b'badserver', b'close-after-send-patterns'
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
372 )
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
373 all_send_patterns = process_pattern_config(all_send_patterns)
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
374 self._cond = ConditionTracker(
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
375 all_recv_bytes,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
376 all_send_bytes,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
377 all_send_patterns,
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
378 )
37498
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
379
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
380 # 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
381 class badrequesthandler(self.RequestHandlerClass, object):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
382 def send_header(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
383 # Make headers deterministic to facilitate testing.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
384 if name.lower() == 'date':
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
385 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
386 elif name.lower() == 'server':
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
387 value = 'badhttpserver'
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
388
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
389 return super(badrequesthandler, self).send_header(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
390 name, value
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
391 )
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
392
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
393 self.RequestHandlerClass = badrequesthandler
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
394
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
395 # Called to accept() a pending socket.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
396 def get_request(self):
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
397 if self._ui.configbool(b'badserver', b'close-before-accept'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
398 self.socket.close()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
399
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
400 # 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
401 self.__shutdown_request = True
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
402
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
403 # 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
404 raise socket.error('close before accept')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
405
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
406 if self._ui.configbool(b'badserver', b'close-after-accept'):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
407 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
408 request.close()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
409 raise socket.error('close after accept')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
410
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
411 return super(badserver, self).get_request()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
412
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
413 # 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
414 # 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
415 # is a hgweb.server._httprequesthandler.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
416 def process_request(self, socket, address):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
417 # Wrap socket in a proxy if we need to count bytes.
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
418 self._cond.start_next_request()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
419
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
420 if self._cond.might_close():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
421 socket = socketproxy(
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
422 socket, self.errorlog, condition_tracked=self._cond
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
423 )
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
424
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
425 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
426
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
427 server.MercurialHTTPServer = badserver