annotate tests/testlib/badserverext.py @ 48946:642e31cb55f0

py3: use class X: instead of class X(object): The inheritance from object is implied in Python 3. So this should be equivalent. This change was generated via an automated search and replace. So there may have been some accidental changes. Differential Revision: https://phab.mercurial-scm.org/D12352
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 21 Feb 2022 13:08:28 -0700
parents 6000f5b25c9b
children 425ca3428d03
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
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
30 close-after-recv-patterns
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
31 If defined, the `close-after-recv-bytes` values only start counting after the
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
32 `read` operation that encountered the defined patterns.
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
33 (The value is a list, multiple values can use used to close a series of requests
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
34 request)
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
35
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
36 close-after-send-bytes
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
37 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
38 (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
39 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
40
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
41 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
42 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
43 (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
44 request)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
45 """
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
46
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
47
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
48 import re
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
49 import socket
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
50
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
51 from mercurial import (
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
52 pycompat,
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
53 registrar,
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
54 )
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 from mercurial.hgweb import server
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
57
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
58 configtable = {}
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
59 configitem = registrar.configitem(configtable)
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-accept',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
64 default=False,
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
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-recv-bytes',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
69 default=b'0',
33192
c538fca0d511 configitems: register the 'badserver.closeafterrecvbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33191
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',
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
73 b'close-after-recv-patterns',
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
74 default=b'',
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
75 )
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
76 configitem(
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
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-after-send-bytes',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
79 default=b'0',
33193
cbb50fd830ea configitems: register the 'badserver.closeaftersendbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33192
diff changeset
80 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
81 configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
82 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
83 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
84 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
85 )
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 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
87 b'badserver',
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
88 b'close-before-accept',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
89 default=False,
33194
c077eac329e2 configitems: register the 'badserver.closebeforeaccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33193
diff changeset
90 )
33191
8065b4ab0ed7 configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32021
diff changeset
91
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
92
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
93 class ConditionTracker:
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
94 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
95 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
96 close_after_recv_bytes,
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
97 close_after_recv_patterns,
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 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
99 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
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 self._all_close_after_recv_bytes = close_after_recv_bytes
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
102 self._all_close_after_recv_patterns = close_after_recv_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
103 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
104 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
105
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
106 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
107 self.remaining_recv_bytes = None
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
108 self.recv_patterns = None
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
109 self.recv_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
110 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
111 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
112 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
113 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
114
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
115 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
116 """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
117 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
118 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
119 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
120 else:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
121 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
122 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
123
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
124 self.recv_data = b''
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
125 if self._all_close_after_recv_patterns:
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
126 self.recv_pattern = self._all_close_after_recv_patterns.pop(0)
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
127 else:
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
128 self.recv_pattern = None
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
129
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
130 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
131 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
132 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
133 else:
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
134 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
135 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
136
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
137 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
138 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
139 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
140 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
141 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
142
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
143 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
144 """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
145 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
146 return True
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
147 if self.recv_pattern is not None:
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
148 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
149 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
150 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
151 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
152 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
153 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
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 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
156 """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
157
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 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
159 """
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 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
161 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
162
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
163 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
164 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
165 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
166
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
167 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
168 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
169 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
170 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
171 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
172 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
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:
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
175 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
176
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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186
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
187 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
188
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
189 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
190 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
191 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
192 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
193 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
194 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
195
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
196 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
197 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
198 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
199 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
200
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
201 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
202
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
203 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
204 """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
205
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
206 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
207 """
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
208 remaining = self.remaining_recv_bytes
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
209 pattern = self.recv_pattern
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
210
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
211 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
212 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
213 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
214
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
215 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
216 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
217
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
218 if pattern is None and remaining:
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 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
220 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
221 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
222 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
223
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
224 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
225
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
226 if pattern is None and remaining:
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
227 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
228 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
229
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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
242 if pattern is not None:
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
243 self.recv_data += result
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
244 if pattern.search(self.recv_data):
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
245 # start counting bytes starting with the next read
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
246 self.recv_pattern = None
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
247
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
248 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
249 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
250 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
251
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
252 # 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
253 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
254
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
255 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
256
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
257
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
258 # We can't adjust __class__ on a socket instance. So we define a proxy type.
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
259 class 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
260 __slots__ = ('_orig', '_logfp', '_cond')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
261
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
262 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
263 object.__setattr__(self, '_orig', obj)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
264 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
265 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
266
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
267 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
268 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
269 return object.__getattribute__(self, name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
270
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
271 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
272
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
273 def __delattr__(self, name):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
274 delattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
275
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
276 def __setattr__(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
277 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
278
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
279 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
280 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
281
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
282 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
283 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
284 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
285
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
286 def makefile(self, mode, bufsize):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
287 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
288
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
289 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
290 cond = object.__getattribute__(self, '_cond')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
291
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
292 return fileobjectproxy(f, logfp, cond)
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
293
41466
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
294 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
295 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
296 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
297
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
298 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
299 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
300
4d5aae86c9bd tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41464
diff changeset
301
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
302 # We can't adjust __class__ on socket._fileobject, so define a proxy.
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
303 class fileobjectproxy:
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
304 __slots__ = ('_orig', '_logfp', '_cond')
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
305
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
306 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
307 object.__setattr__(self, '_orig', obj)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
308 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
309 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
310
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
311 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
312 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
313 '_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
314 '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
315 '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
316 '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
317 '_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
318 '_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
319 ):
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
320 return object.__getattribute__(self, name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
321
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
322 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
323
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
324 def __delattr__(self, name):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
325 delattr(object.__getattribute__(self, '_orig'), name)
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
326
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
327 def __setattr__(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
328 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
329
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
330 def _writelog(self, msg):
41463
ba7298160357 tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39428
diff changeset
331 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
332
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
333 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
334 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
335 object.__getattribute__(self, '_logfp').flush()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
336
41464
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
337 def _close(self):
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
338 # 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
339 # object wrapper.
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
340 if pycompat.ispy3:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
341 orig = object.__getattribute__(self, '_orig')
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
342
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
343 if hasattr(orig, 'raw'):
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
344 orig.raw._sock.shutdown(socket.SHUT_RDWR)
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
345 else:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
346 self.close()
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
347 else:
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
348 self._sock.shutdown(socket.SHUT_RDWR)
d343d9ac173e tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41463
diff changeset
349
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
350 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
351 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
352 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
353
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
354 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
355 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
356 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
357
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
358 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
359 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
360 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
361
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
362 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
363 self._close()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
364
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
365
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
366 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
367 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
368 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
369 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
370
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
371
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
372 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
373 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 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
375 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
376 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
377 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
378 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
379 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
380 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
381
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
382
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
383 def extsetup(ui):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
384 # 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
385 # 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
386 class badserver(server.MercurialHTTPServer):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
387 def __init__(self, ui, *args, **kwargs):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
388 self._ui = ui
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
389 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
390
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
391 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
392 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
393 )
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
394 all_recv_bytes = process_bytes_config(all_recv_bytes)
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
395 all_recv_pattern = self._ui.config(
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
396 b'badserver', b'close-after-recv-patterns'
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
397 )
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
398 all_recv_pattern = process_pattern_config(all_recv_pattern)
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
399 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
400 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
401 )
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
402 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
403 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
404 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
405 )
9dc00c5617ea test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48617
diff changeset
406 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
407 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
408 all_recv_bytes,
48637
5154f2025d8a test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48618
diff changeset
409 all_recv_pattern,
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
410 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
411 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
412 )
37498
aacfca6f9767 wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents: 36395
diff changeset
413
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
414 # 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
415 class badrequesthandler(self.RequestHandlerClass, object):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
416 def send_header(self, name, value):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
417 # Make headers deterministic to facilitate testing.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
418 if name.lower() == 'date':
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
419 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
420 elif name.lower() == 'server':
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
421 value = 'badhttpserver'
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
422
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
423 return super(badrequesthandler, self).send_header(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
424 name, value
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
425 )
32001
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 self.RequestHandlerClass = badrequesthandler
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
428
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
429 # Called to accept() a pending socket.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
430 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
431 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
432 self.socket.close()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
433
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
434 # 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
435 self.__shutdown_request = True
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
436
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
437 # 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
438 raise socket.error('close before accept')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
439
48606
ee1235afda4b test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48605
diff changeset
440 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
441 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
442 request.close()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
443 raise socket.error('close after accept')
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
444
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
445 return super(badserver, self).get_request()
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
446
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
447 # 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
448 # 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
449 # is a hgweb.server._httprequesthandler.
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
450 def process_request(self, socket, address):
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
451 # 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
452 self._cond.start_next_request()
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
453
48610
caa6694dac45 test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48609
diff changeset
454 if self._cond.might_close():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
455 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
456 socket, self.errorlog, condition_tracked=self._cond
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41466
diff changeset
457 )
32001
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
458
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
459 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
460
c85f19c66e8d tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
461 server.MercurialHTTPServer = badserver