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