author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Thu, 03 Feb 2022 18:14:11 +0100 | |
changeset 48716 | f1eb77dceb36 |
parent 48637 | 5154f2025d8a |
child 48875 | 6000f5b25c9b |
permissions | -rw-r--r-- |
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 |
from __future__ import absolute_import |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 |
|
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
|
49 |
import re |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 |
import socket |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
52 |
from mercurial import ( |
41464
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
53 |
pycompat, |
33191
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
54 |
registrar, |
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
55 |
) |
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
56 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
57 |
from mercurial.hgweb import server |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 |
|
33191
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
59 |
configtable = {} |
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
60 |
configitem = registrar.configitem(configtable) |
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
61 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
62 |
configitem( |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
63 |
b'badserver', |
48606
ee1235afda4b
test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48605
diff
changeset
|
64 |
b'close-after-accept', |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
65 |
default=False, |
33191
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
66 |
) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
67 |
configitem( |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
68 |
b'badserver', |
48606
ee1235afda4b
test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48605
diff
changeset
|
69 |
b'close-after-recv-bytes', |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
70 |
default=b'0', |
33192
c538fca0d511
configitems: register the 'badserver.closeafterrecvbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33191
diff
changeset
|
71 |
) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
72 |
configitem( |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
) |
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 |
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
|
78 |
b'badserver', |
48606
ee1235afda4b
test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48605
diff
changeset
|
79 |
b'close-after-send-bytes', |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
80 |
default=b'0', |
33193
cbb50fd830ea
configitems: register the 'badserver.closeaftersendbytes' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33192
diff
changeset
|
81 |
) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
82 |
configitem( |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
) |
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 |
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
|
88 |
b'badserver', |
48606
ee1235afda4b
test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48605
diff
changeset
|
89 |
b'close-before-accept', |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43076
diff
changeset
|
90 |
default=False, |
33194
c077eac329e2
configitems: register the 'badserver.closebeforeaccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33193
diff
changeset
|
91 |
) |
33191
8065b4ab0ed7
configitems: register the 'badserver.closeafteraccept' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32021
diff
changeset
|
92 |
|
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 |
|
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
): |
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
|
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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
116 |
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
|
117 |
"""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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
else: |
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.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
|
123 |
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
|
124 |
|
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
|
125 |
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
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
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
|
130 |
|
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 |
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
|
132 |
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
|
133 |
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
|
134 |
else: |
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.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
|
136 |
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
|
137 |
|
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
144 |
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
|
145 |
"""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
|
146 |
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
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
|
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
|
156 |
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
|
157 |
"""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
|
158 |
|
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 |
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
|
160 |
""" |
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 |
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
|
162 |
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
|
163 |
|
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 |
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
|
165 |
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
|
166 |
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
|
167 |
|
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
|
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
|
175 |
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
|
176 |
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
|
177 |
|
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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
|
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
|
188 |
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
|
189 |
|
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
|
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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
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
|
201 |
|
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 |
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
|
203 |
|
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
|
204 |
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
|
205 |
"""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
|
206 |
|
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 |
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
|
208 |
""" |
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
|
209 |
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
|
210 |
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
|
211 |
|
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 |
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
|
213 |
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
|
214 |
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
|
215 |
|
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
|
216 |
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
|
217 |
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
|
218 |
|
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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
|
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
|
225 |
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
|
226 |
|
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
|
227 |
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
|
228 |
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
|
229 |
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
|
230 |
|
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
|
231 |
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
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
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
|
242 |
|
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
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
# 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
|
247 |
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
|
248 |
|
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
|
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 |
# 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
|
254 |
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
|
255 |
|
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 |
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
|
257 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
258 |
|
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
259 |
# 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
|
260 |
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
|
261 |
__slots__ = ('_orig', '_logfp', '_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 |
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
|
264 |
object.__setattr__(self, '_orig', obj) |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
265 |
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
|
266 |
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
|
267 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
268 |
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
|
269 |
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
|
270 |
return object.__getattribute__(self, name) |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
271 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
272 |
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
|
273 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
274 |
def __delattr__(self, name): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
275 |
delattr(object.__getattribute__(self, '_orig'), name) |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
276 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
277 |
def __setattr__(self, name, value): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
278 |
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
|
279 |
|
41466
4d5aae86c9bd
tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41464
diff
changeset
|
280 |
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
|
281 |
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
|
282 |
|
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(msg) |
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').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
|
285 |
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
|
286 |
|
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
287 |
def makefile(self, mode, bufsize): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
288 |
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
|
289 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
290 |
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
|
291 |
cond = object.__getattribute__(self, '_cond') |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
292 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
293 |
return fileobjectproxy(f, logfp, cond) |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
294 |
|
41466
4d5aae86c9bd
tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41464
diff
changeset
|
295 |
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
|
296 |
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
|
297 |
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
|
298 |
|
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
|
299 |
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
|
300 |
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
|
301 |
|
4d5aae86c9bd
tests: log sendall() operations and port test-http-bad-server.t
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41464
diff
changeset
|
302 |
|
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
303 |
# 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
|
304 |
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
|
305 |
__slots__ = ('_orig', '_logfp', '_cond') |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
306 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
307 |
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
|
308 |
object.__setattr__(self, '_orig', obj) |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
309 |
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
|
310 |
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
|
311 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
312 |
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
|
313 |
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
|
314 |
'_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
|
315 |
'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
|
316 |
'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
|
317 |
'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
|
318 |
'_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
|
319 |
'_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
|
320 |
): |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
321 |
return object.__getattribute__(self, name) |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
322 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
323 |
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
|
324 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
325 |
def __delattr__(self, name): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
326 |
delattr(object.__getattribute__(self, '_orig'), name) |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
327 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
328 |
def __setattr__(self, name, value): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
329 |
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
|
330 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
331 |
def _writelog(self, msg): |
41463
ba7298160357
tests: add b'' prefixes to badserverext.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39428
diff
changeset
|
332 |
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
|
333 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
334 |
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
|
335 |
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
|
336 |
object.__getattribute__(self, '_logfp').flush() |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
337 |
|
41464
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
338 |
def _close(self): |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
339 |
# 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
|
340 |
# object wrapper. |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
341 |
if pycompat.ispy3: |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
342 |
orig = object.__getattribute__(self, '_orig') |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
343 |
|
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
344 |
if hasattr(orig, 'raw'): |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
345 |
orig.raw._sock.shutdown(socket.SHUT_RDWR) |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
346 |
else: |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
347 |
self.close() |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
348 |
else: |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
349 |
self._sock.shutdown(socket.SHUT_RDWR) |
d343d9ac173e
tests: change how sockets are closed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41463
diff
changeset
|
350 |
|
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
351 |
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
|
352 |
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
|
353 |
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
|
354 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
|
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
|
363 |
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
|
364 |
self._close() |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
365 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
366 |
|
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
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
|
371 |
|
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
372 |
|
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
|
373 |
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
|
374 |
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
|
375 |
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
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
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
|
381 |
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
|
382 |
|
9dc00c5617ea
test-bad-http-server: introduce sock closing when writing a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48617
diff
changeset
|
383 |
|
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
384 |
def extsetup(ui): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
385 |
# 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
|
386 |
# 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
|
387 |
class badserver(server.MercurialHTTPServer): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
388 |
def __init__(self, ui, *args, **kwargs): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
389 |
self._ui = ui |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
390 |
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
|
391 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
392 |
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
|
393 |
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
|
394 |
) |
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
|
395 |
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
|
396 |
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
|
397 |
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
|
398 |
) |
5154f2025d8a
test-http-bad-server: introduce socket closing after reading a pattern
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48618
diff
changeset
|
399 |
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
|
400 |
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
|
401 |
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
|
402 |
) |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
) |
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 |
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
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
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
|
413 |
) |
37498
aacfca6f9767
wireproto: support for pullbundles
Joerg Sonnenberger <joerg@bec.de>
parents:
36395
diff
changeset
|
414 |
|
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
415 |
# 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
|
416 |
class badrequesthandler(self.RequestHandlerClass, object): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
417 |
def send_header(self, name, value): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
418 |
# Make headers deterministic to facilitate testing. |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
419 |
if name.lower() == 'date': |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
420 |
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
|
421 |
elif name.lower() == 'server': |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
422 |
value = 'badhttpserver' |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
423 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
424 |
return super(badrequesthandler, self).send_header( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
425 |
name, value |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
426 |
) |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
427 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
428 |
self.RequestHandlerClass = badrequesthandler |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
429 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
430 |
# Called to accept() a pending socket. |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
431 |
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
|
432 |
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
|
433 |
self.socket.close() |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
434 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
435 |
# 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
|
436 |
self.__shutdown_request = True |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
437 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
438 |
# 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
|
439 |
raise socket.error('close before accept') |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
440 |
|
48606
ee1235afda4b
test-http-bad-server: rename config to use `-`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48605
diff
changeset
|
441 |
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
|
442 |
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
|
443 |
request.close() |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
444 |
raise socket.error('close after accept') |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
445 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
446 |
return super(badserver, self).get_request() |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
447 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
448 |
# 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
|
449 |
# 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
|
450 |
# is a hgweb.server._httprequesthandler. |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
451 |
def process_request(self, socket, address): |
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
452 |
# 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
|
453 |
self._cond.start_next_request() |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
454 |
|
48610
caa6694dac45
test-http-bad-server: track close condition in an object
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48609
diff
changeset
|
455 |
if self._cond.might_close(): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
456 |
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
|
457 |
socket, self.errorlog, condition_tracked=self._cond |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41466
diff
changeset
|
458 |
) |
32001
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
459 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
460 |
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
|
461 |
|
c85f19c66e8d
tests: add tests for poorly behaving HTTP server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
462 |
server.MercurialHTTPServer = badserver |