httpdis 0.6.32__tar.gz → 0.6.34__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {httpdis-0.6.32/httpdis.egg-info → httpdis-0.6.34}/PKG-INFO +34 -26
- {httpdis-0.6.32 → httpdis-0.6.34}/README.md +33 -21
- httpdis-0.6.34/RELEASE +1 -0
- httpdis-0.6.34/VERSION +1 -0
- httpdis-0.6.34/httpdis/content_disposition.py +52 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/httpdis.py +97 -35
- {httpdis-0.6.32 → httpdis-0.6.34/httpdis.egg-info}/PKG-INFO +34 -26
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis.egg-info/SOURCES.txt +3 -0
- httpdis-0.6.34/httpdis.egg-info/requires.txt +13 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/pyproject.toml +2 -1
- httpdis-0.6.34/requirements.txt +5 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/setup.yml +3 -3
- httpdis-0.6.34/tests/test_audit_regressions.py +170 -0
- httpdis-0.6.34/tests/test_http_contract_errors.py +80 -0
- httpdis-0.6.32/RELEASE +0 -1
- httpdis-0.6.32/VERSION +0 -1
- httpdis-0.6.32/httpdis.egg-info/requires.txt +0 -25
- httpdis-0.6.32/requirements.txt +0 -9
- {httpdis-0.6.32 → httpdis-0.6.34}/LICENSE +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/MANIFEST.in +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/__init__.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/auth_backend.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/auth_browser.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/auth_sqlite.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/authentication.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/config.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/ext/__init__.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis/ext/httpdis_json.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis.egg-info/dependency_links.txt +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/httpdis.egg-info/top_level.txt +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/setup.cfg +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/setup.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_auth_backend.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_auth_browser.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_auth_sqlite.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_authentication_provider.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_body_limits.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_context.py +0 -0
- {httpdis-0.6.32 → httpdis-0.6.34}/tests/test_httpdis.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: httpdis
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.34
|
|
4
4
|
Summary: httpdis
|
|
5
5
|
Home-page: https://github.com/decryptus/httpdis
|
|
6
6
|
Author: Adrien Delle Cave
|
|
@@ -20,12 +20,8 @@ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
|
20
20
|
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist: LEPL; python_version < "3.6"
|
|
24
23
|
Requires-Dist: python-magic
|
|
25
24
|
Requires-Dist: sonicprobe>=0.3.52
|
|
26
|
-
Requires-Dist: rfc6266; python_version < "3.4"
|
|
27
|
-
Requires-Dist: rfc6266-parser; python_version >= "3.4" and python_version < "3.6"
|
|
28
|
-
Requires-Dist: uc-rfc6266-parser; python_version >= "3.6"
|
|
29
25
|
Requires-Dist: six>=1.13.0
|
|
30
26
|
Requires-Dist: legacy-cgi; python_version >= "3.13"
|
|
31
27
|
Requires-Dist: legacycrypt; python_version >= "3.13"
|
|
@@ -96,6 +92,34 @@ Handlers receive a request with `get_path()`, `query_params()`, `payload_params(
|
|
|
96
92
|
lists, compiled regular expressions and `safe_init`, `at_start`, `at_stop` hooks.
|
|
97
93
|
Routes and options are process-global; use a separate process for independent apps.
|
|
98
94
|
|
|
95
|
+
## Request error contract
|
|
96
|
+
|
|
97
|
+
Since HTTPdis 0.6.33, the dispatcher distinguishes invalid request syntax, unsupported media,
|
|
98
|
+
missing routes and unsupported methods:
|
|
99
|
+
|
|
100
|
+
| Request | Response |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| Malformed JSON or invalid text encoding for a supported payload | 400, `Invalid request body` |
|
|
103
|
+
| Unsupported `Content-Type`, including disabled multipart | 415, `Unsupported Content-Type` |
|
|
104
|
+
| Path with no matching named or regex route | 404 |
|
|
105
|
+
| Recognized HTTP method with a matching path registered for other methods | 405 with `Allow` |
|
|
106
|
+
|
|
107
|
+
`Allow` lists the explicitly registered methods for the matching path in sorted
|
|
108
|
+
order. It does not invent HEAD or OPTIONS support. Method rejection does not read
|
|
109
|
+
the body or invoke a handler. Completely unsupported HTTP verbs remain the base
|
|
110
|
+
server's responsibility. These corrections change the previous 415 (malformed
|
|
111
|
+
payload), 501 (unsupported media) and 404 (known path, wrong method) responses;
|
|
112
|
+
clients should not depend on those old status mappings.
|
|
113
|
+
|
|
114
|
+
Payload-parser exceptions use a constant error message instead of reflecting
|
|
115
|
+
exception text or request body values. The JSON handler retains JSON errors.
|
|
116
|
+
This does not sanitize application-owned error messages or custom logging.
|
|
117
|
+
|
|
118
|
+
A registered route with `to_auth` enabled authenticates before body reading and
|
|
119
|
+
parsing. Routing and header/body-size checks may run before authentication;
|
|
120
|
+
unknown routes and wrong methods do not inherit another method's auth policy.
|
|
121
|
+
Applications still own operation-level authorization. Public routes are unchanged.
|
|
122
|
+
|
|
99
123
|
## Request body limits
|
|
100
124
|
|
|
101
125
|
Since HTTPdis 0.6.28, `register(..., max_body_size=...)` sets the body limit
|
|
@@ -156,27 +180,6 @@ serve large downloads through the reverse proxy.
|
|
|
156
180
|
The dependency loop with sonicprobe is retained because sonicprobe's historical
|
|
157
181
|
HTTP JSON module re-exports HTTPdis. A future major release should untangle it.
|
|
158
182
|
|
|
159
|
-
## Development and publication
|
|
160
|
-
|
|
161
|
-
```sh
|
|
162
|
-
python -m pip install -e . mock
|
|
163
|
-
python -m unittest discover -s tests -v
|
|
164
|
-
python -m pip install build twine
|
|
165
|
-
python -m build
|
|
166
|
-
python -m twine check --strict dist/*
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Update `VERSION`, `RELEASE` and `setup.yml` together. After tests and distribution
|
|
170
|
-
validation succeed, the master publication workflow creates `vX.Y.Z` and uploads
|
|
171
|
-
via Trusted Publishing (`decryptus/httpdis`, `pypi.yml`, environment `pypi`).
|
|
172
|
-
Already-tagged versions are not replaced by ordinary master commits.
|
|
173
|
-
|
|
174
|
-
License: GPL-3.0-or-later. Original authors and Wazo/Proformatique copyrights are
|
|
175
|
-
preserved in the source files.
|
|
176
|
-
|
|
177
|
-
See the [September 2026 code and architecture review](docs/REVIEW.md) (French).
|
|
178
|
-
|
|
179
|
-
|
|
180
183
|
### Independent embedded servers
|
|
181
184
|
|
|
182
185
|
Use `HttpServerContext` when multiple HTTPdis servers must coexist in one process.
|
|
@@ -505,3 +508,8 @@ OPTIONS behavior is unchanged: override it in the embedding browser handler.
|
|
|
505
508
|
Application scopes, ownership and action confirmation remain application policy.
|
|
506
509
|
This adapter supplies no HTML UI, proxy configuration, TOTP or SSO.
|
|
507
510
|
|
|
511
|
+
|
|
512
|
+
## Documentation
|
|
513
|
+
|
|
514
|
+
- **Users:** installation, configuration, operation and API usage in this README and the user guide.
|
|
515
|
+
- **Contributors:** [architecture, tests and development](https://github.com/decryptus/httpdis/blob/master/CONTRIBUTING.md).
|
|
@@ -49,6 +49,34 @@ Handlers receive a request with `get_path()`, `query_params()`, `payload_params(
|
|
|
49
49
|
lists, compiled regular expressions and `safe_init`, `at_start`, `at_stop` hooks.
|
|
50
50
|
Routes and options are process-global; use a separate process for independent apps.
|
|
51
51
|
|
|
52
|
+
## Request error contract
|
|
53
|
+
|
|
54
|
+
Since HTTPdis 0.6.33, the dispatcher distinguishes invalid request syntax, unsupported media,
|
|
55
|
+
missing routes and unsupported methods:
|
|
56
|
+
|
|
57
|
+
| Request | Response |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| Malformed JSON or invalid text encoding for a supported payload | 400, `Invalid request body` |
|
|
60
|
+
| Unsupported `Content-Type`, including disabled multipart | 415, `Unsupported Content-Type` |
|
|
61
|
+
| Path with no matching named or regex route | 404 |
|
|
62
|
+
| Recognized HTTP method with a matching path registered for other methods | 405 with `Allow` |
|
|
63
|
+
|
|
64
|
+
`Allow` lists the explicitly registered methods for the matching path in sorted
|
|
65
|
+
order. It does not invent HEAD or OPTIONS support. Method rejection does not read
|
|
66
|
+
the body or invoke a handler. Completely unsupported HTTP verbs remain the base
|
|
67
|
+
server's responsibility. These corrections change the previous 415 (malformed
|
|
68
|
+
payload), 501 (unsupported media) and 404 (known path, wrong method) responses;
|
|
69
|
+
clients should not depend on those old status mappings.
|
|
70
|
+
|
|
71
|
+
Payload-parser exceptions use a constant error message instead of reflecting
|
|
72
|
+
exception text or request body values. The JSON handler retains JSON errors.
|
|
73
|
+
This does not sanitize application-owned error messages or custom logging.
|
|
74
|
+
|
|
75
|
+
A registered route with `to_auth` enabled authenticates before body reading and
|
|
76
|
+
parsing. Routing and header/body-size checks may run before authentication;
|
|
77
|
+
unknown routes and wrong methods do not inherit another method's auth policy.
|
|
78
|
+
Applications still own operation-level authorization. Public routes are unchanged.
|
|
79
|
+
|
|
52
80
|
## Request body limits
|
|
53
81
|
|
|
54
82
|
Since HTTPdis 0.6.28, `register(..., max_body_size=...)` sets the body limit
|
|
@@ -109,27 +137,6 @@ serve large downloads through the reverse proxy.
|
|
|
109
137
|
The dependency loop with sonicprobe is retained because sonicprobe's historical
|
|
110
138
|
HTTP JSON module re-exports HTTPdis. A future major release should untangle it.
|
|
111
139
|
|
|
112
|
-
## Development and publication
|
|
113
|
-
|
|
114
|
-
```sh
|
|
115
|
-
python -m pip install -e . mock
|
|
116
|
-
python -m unittest discover -s tests -v
|
|
117
|
-
python -m pip install build twine
|
|
118
|
-
python -m build
|
|
119
|
-
python -m twine check --strict dist/*
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Update `VERSION`, `RELEASE` and `setup.yml` together. After tests and distribution
|
|
123
|
-
validation succeed, the master publication workflow creates `vX.Y.Z` and uploads
|
|
124
|
-
via Trusted Publishing (`decryptus/httpdis`, `pypi.yml`, environment `pypi`).
|
|
125
|
-
Already-tagged versions are not replaced by ordinary master commits.
|
|
126
|
-
|
|
127
|
-
License: GPL-3.0-or-later. Original authors and Wazo/Proformatique copyrights are
|
|
128
|
-
preserved in the source files.
|
|
129
|
-
|
|
130
|
-
See the [September 2026 code and architecture review](docs/REVIEW.md) (French).
|
|
131
|
-
|
|
132
|
-
|
|
133
140
|
### Independent embedded servers
|
|
134
141
|
|
|
135
142
|
Use `HttpServerContext` when multiple HTTPdis servers must coexist in one process.
|
|
@@ -458,3 +465,8 @@ OPTIONS behavior is unchanged: override it in the embedding browser handler.
|
|
|
458
465
|
Application scopes, ownership and action confirmation remain application policy.
|
|
459
466
|
This adapter supplies no HTML UI, proxy configuration, TOTP or SSO.
|
|
460
467
|
|
|
468
|
+
|
|
469
|
+
## Documentation
|
|
470
|
+
|
|
471
|
+
- **Users:** installation, configuration, operation and API usage in this README and the user guide.
|
|
472
|
+
- **Contributors:** [architecture, tests and development](https://github.com/decryptus/httpdis/blob/master/CONTRIBUTING.md).
|
httpdis-0.6.34/RELEASE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.6.34
|
httpdis-0.6.34/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.6.34
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Content-Disposition helpers used by static responses, without a web framework."""
|
|
3
|
+
from collections import namedtuple
|
|
4
|
+
from email.message import Message
|
|
5
|
+
from email.utils import collapse_rfc2231_value
|
|
6
|
+
import re
|
|
7
|
+
from six import PY2, text_type, ensure_text, ensure_binary
|
|
8
|
+
from six.moves.urllib.parse import quote
|
|
9
|
+
|
|
10
|
+
_TOKEN = re.compile(r"[!#$%&'*+.^_`|~0-9a-zA-Z-]+\Z")
|
|
11
|
+
_Disposition = namedtuple('ContentDisposition', 'disposition filename_unsafe')
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _text(value):
|
|
15
|
+
value = ensure_text(value)
|
|
16
|
+
if any(ord(char) < 32 or ord(char) == 127 for char in value):
|
|
17
|
+
raise ValueError('Control character in Content-Disposition')
|
|
18
|
+
return value
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def parse_headers(value):
|
|
22
|
+
value = _text(value)
|
|
23
|
+
disposition = value.split(';', 1)[0].strip().lower()
|
|
24
|
+
if not _TOKEN.match(disposition):
|
|
25
|
+
raise ValueError('Invalid Content-Disposition type')
|
|
26
|
+
message = Message()
|
|
27
|
+
message['Content-Disposition'] = value
|
|
28
|
+
filename = None
|
|
29
|
+
for name, parameter in message.get_params(header='content-disposition', unquote=True)[1:]:
|
|
30
|
+
if name.lower() != 'filename':
|
|
31
|
+
continue
|
|
32
|
+
if isinstance(parameter, tuple):
|
|
33
|
+
charset, language, raw_value = parameter
|
|
34
|
+
if PY2 and isinstance(raw_value, text_type):
|
|
35
|
+
raw_value = raw_value.encode('latin-1')
|
|
36
|
+
filename = collapse_rfc2231_value((charset, language, raw_value))
|
|
37
|
+
break
|
|
38
|
+
if filename is None:
|
|
39
|
+
filename = parameter
|
|
40
|
+
return _Disposition(disposition, filename)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def build_header(filename, disposition='attachment'):
|
|
44
|
+
filename, disposition = _text(filename), _text(disposition)
|
|
45
|
+
if not _TOKEN.match(disposition):
|
|
46
|
+
raise ValueError('Invalid Content-Disposition type')
|
|
47
|
+
fallback = ''.join(char if ord(char) < 128 else '_' for char in filename)
|
|
48
|
+
quoted = fallback.replace('\\', '\\\\').replace('"', '\\"')
|
|
49
|
+
result = '%s; filename="%s"' % (disposition, quoted)
|
|
50
|
+
if fallback != filename:
|
|
51
|
+
result += "; filename*=UTF-8''" + quote(ensure_binary(filename), safe='')
|
|
52
|
+
return result
|
|
@@ -71,10 +71,7 @@ from sonicprobe import helpers
|
|
|
71
71
|
from sonicprobe.libs import urisup
|
|
72
72
|
from sonicprobe.libs.threading_tcp_server import KillableThreadingHTTPServer
|
|
73
73
|
|
|
74
|
-
|
|
75
|
-
from rfc6266_parser import build_header, parse_headers
|
|
76
|
-
except ImportError:
|
|
77
|
-
from rfc6266 import build_header, parse_headers
|
|
74
|
+
from .content_disposition import build_header, parse_headers
|
|
78
75
|
|
|
79
76
|
from .config import (BUFFER_SIZE, # pylint: disable=unused-import
|
|
80
77
|
DEFAULT_CHARSET,
|
|
@@ -82,6 +79,9 @@ from .config import (BUFFER_SIZE, # pylint: disable=unused-import
|
|
|
82
79
|
|
|
83
80
|
|
|
84
81
|
LOG = logging.getLogger('httpdis') # pylint: disable-msg=C0103
|
|
82
|
+
_CONTENT_LENGTH = re.compile(r'[0-9]+\Z')
|
|
83
|
+
_BODYLESS_CODES = (204, 205, 304)
|
|
84
|
+
_NO_LENGTH_CODES = (204, 304)
|
|
85
85
|
|
|
86
86
|
_METHODS = ('HEAD',
|
|
87
87
|
'GET',
|
|
@@ -484,10 +484,8 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
484
484
|
|
|
485
485
|
code = response.get_code()
|
|
486
486
|
|
|
487
|
-
if
|
|
488
|
-
and self.command
|
|
489
|
-
and code >= 200 \
|
|
490
|
-
and code not in (204, 304):
|
|
487
|
+
if code >= 200 and code not in _BODYLESS_CODES \
|
|
488
|
+
and (response.is_send_body() or self.command == 'HEAD'):
|
|
491
489
|
data = ensure_binary(response.data or "")
|
|
492
490
|
clen = len(data)
|
|
493
491
|
else:
|
|
@@ -509,7 +507,8 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
509
507
|
self.send_header('Pragma', response.get_header('Pragma') or 'no-cache')
|
|
510
508
|
self.send_header('Connection', response.get_header('Connection') or 'close')
|
|
511
509
|
self.send_header('Content-Type', content_type)
|
|
512
|
-
|
|
510
|
+
if code >= 200 and code not in _NO_LENGTH_CODES:
|
|
511
|
+
self.send_header('Content-Length', str(clen))
|
|
513
512
|
|
|
514
513
|
for header, value in response.header_items():
|
|
515
514
|
if header.lower() not in tuple(h.lower() for h in _END_EXC_HEADERS):
|
|
@@ -517,7 +516,7 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
517
516
|
|
|
518
517
|
self.end_headers()
|
|
519
518
|
|
|
520
|
-
if clen:
|
|
519
|
+
if clen and response.is_send_body() and self.command != 'HEAD':
|
|
521
520
|
self.wfile.write(ensure_binary(data))
|
|
522
521
|
|
|
523
522
|
def _mk_error_explain_data(self, code, message, explain, charset):
|
|
@@ -868,6 +867,22 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
868
867
|
|
|
869
868
|
return repr(data)
|
|
870
869
|
|
|
870
|
+
def _missing_command(self, cmd):
|
|
871
|
+
"""Distinguish an unknown path from an unsupported registered method."""
|
|
872
|
+
context = self.get_context()
|
|
873
|
+
allowed = set()
|
|
874
|
+
for key in context.named_commands:
|
|
875
|
+
method, path = key.split(' ', 1)
|
|
876
|
+
if path == '/' + cmd:
|
|
877
|
+
allowed.add(method)
|
|
878
|
+
for key, command in context.regex_commands.items():
|
|
879
|
+
if command.name.match(cmd):
|
|
880
|
+
allowed.add(key.split(' ', 1)[0])
|
|
881
|
+
if allowed:
|
|
882
|
+
return self.req_error(405, 'Method not allowed',
|
|
883
|
+
headers={'Allow': ', '.join(sorted(allowed))})
|
|
884
|
+
return self.req_error(404)
|
|
885
|
+
|
|
871
886
|
def data_from_query(self, cmd):
|
|
872
887
|
"""
|
|
873
888
|
Callback for .execute_command() for DELETE/GET/HEAD requests
|
|
@@ -894,7 +909,7 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
894
909
|
|
|
895
910
|
try:
|
|
896
911
|
if not self._cmd:
|
|
897
|
-
raise self.
|
|
912
|
+
raise self._missing_command(cmd)
|
|
898
913
|
|
|
899
914
|
charset = self._cmd.charset or DEFAULT_CHARSET
|
|
900
915
|
|
|
@@ -919,6 +934,34 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
919
934
|
finally:
|
|
920
935
|
self._query_params = {}
|
|
921
936
|
|
|
937
|
+
def _validate_request_framing(self):
|
|
938
|
+
"""Reject ambiguous framing before authentication or application code."""
|
|
939
|
+
get_all = getattr(self.headers, 'get_all', None) or getattr(self.headers, 'getheaders', None)
|
|
940
|
+
if get_all is None:
|
|
941
|
+
get_all = lambda name: ([self.headers[name]] if name in self.headers else [])
|
|
942
|
+
lengths = get_all('Content-Length') or []
|
|
943
|
+
encodings = get_all('Transfer-Encoding') or []
|
|
944
|
+
self.close_connection = True
|
|
945
|
+
if encodings and lengths:
|
|
946
|
+
raise self.req_error(400, 'Conflicting request framing')
|
|
947
|
+
if len(encodings) > 1:
|
|
948
|
+
raise self.req_error(400, 'Multiple Transfer-Encoding headers')
|
|
949
|
+
if encodings and encodings[0].strip().lower() != 'identity':
|
|
950
|
+
raise self.req_error(400, 'Unsupported Transfer-Encoding')
|
|
951
|
+
values = []
|
|
952
|
+
for header in lengths:
|
|
953
|
+
for value in header.split(','):
|
|
954
|
+
value = value.strip(' \t')
|
|
955
|
+
if not _CONTENT_LENGTH.match(value):
|
|
956
|
+
raise self.req_error(400, 'Invalid Content-Length')
|
|
957
|
+
try:
|
|
958
|
+
values.append(int(value))
|
|
959
|
+
except ValueError:
|
|
960
|
+
raise self.req_error(400, 'Invalid Content-Length')
|
|
961
|
+
if values and any(value != values[0] for value in values):
|
|
962
|
+
raise self.req_error(400, 'Conflicting Content-Length headers')
|
|
963
|
+
return values[0] if values else 0
|
|
964
|
+
|
|
922
965
|
def data_from_payload(self, cmd):
|
|
923
966
|
"""
|
|
924
967
|
Callback for .execute_command() for PATCH/POST/PUT requests
|
|
@@ -945,20 +988,18 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
945
988
|
|
|
946
989
|
try:
|
|
947
990
|
if not self._cmd:
|
|
948
|
-
raise self.
|
|
991
|
+
raise self._missing_command(cmd)
|
|
949
992
|
|
|
950
993
|
charset = self._cmd.charset or DEFAULT_CHARSET
|
|
951
994
|
|
|
952
|
-
|
|
953
|
-
if tenc and tenc.lower() != 'identity':
|
|
954
|
-
raise self.req_error(501, "Not supported; Transfer-Encoding: %s" % tenc)
|
|
995
|
+
clen = self._validate_request_framing()
|
|
955
996
|
|
|
956
997
|
ctype = self.headers.get('Content-Type')
|
|
957
998
|
if ctype:
|
|
958
999
|
ctype = ctype.lower().split(';', 1)[0]
|
|
959
1000
|
if ctype == 'multipart/form-data':
|
|
960
1001
|
if not self._ALLOWED_MULTIPART_FORM:
|
|
961
|
-
raise self.req_error(
|
|
1002
|
+
raise self.req_error(415, "Unsupported Content-Type")
|
|
962
1003
|
multipart = True
|
|
963
1004
|
elif self._ALLOWED_CONTENT_TYPES:
|
|
964
1005
|
ct_found = False
|
|
@@ -967,15 +1008,7 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
967
1008
|
ct_found = True
|
|
968
1009
|
break
|
|
969
1010
|
if not ct_found:
|
|
970
|
-
raise self.req_error(
|
|
971
|
-
|
|
972
|
-
try:
|
|
973
|
-
clen = int(self.headers.get('Content-Length') or 0)
|
|
974
|
-
except (ValueError, TypeError):
|
|
975
|
-
raise self.req_error(411)
|
|
976
|
-
|
|
977
|
-
if clen < 0:
|
|
978
|
-
raise self.req_error(411)
|
|
1011
|
+
raise self.req_error(415, "Unsupported Content-Type")
|
|
979
1012
|
|
|
980
1013
|
max_body_size = self._cmd.max_body_size
|
|
981
1014
|
if max_body_size is None:
|
|
@@ -998,8 +1031,8 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
998
1031
|
self._payload_params = cgi.FieldStorage(environ = {'REQUEST_METHOD': 'POST'},
|
|
999
1032
|
fp = self._payload,
|
|
1000
1033
|
headers = self.headers)
|
|
1001
|
-
except Exception
|
|
1002
|
-
raise self.req_error(
|
|
1034
|
+
except Exception:
|
|
1035
|
+
raise self.req_error(400, "Invalid request body")
|
|
1003
1036
|
else:
|
|
1004
1037
|
try:
|
|
1005
1038
|
if ctype == 'application/x-www-form-urlencoded':
|
|
@@ -1007,8 +1040,8 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
1007
1040
|
encoding = charset))
|
|
1008
1041
|
else:
|
|
1009
1042
|
self._payload_params = self.parse_payload(payload, charset)
|
|
1010
|
-
except ValueError
|
|
1011
|
-
raise self.req_error(
|
|
1043
|
+
except ValueError:
|
|
1044
|
+
raise self.req_error(400, "Invalid request body")
|
|
1012
1045
|
|
|
1013
1046
|
res = self._cmd.handler(self)
|
|
1014
1047
|
|
|
@@ -1040,6 +1073,7 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
1040
1073
|
|
|
1041
1074
|
try:
|
|
1042
1075
|
try:
|
|
1076
|
+
self._validate_request_framing()
|
|
1043
1077
|
path = self._pathify() # pylint: disable-msg=W0612
|
|
1044
1078
|
cmd = path[1:]
|
|
1045
1079
|
res = execute(cmd)
|
|
@@ -1054,8 +1088,7 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
1054
1088
|
else:
|
|
1055
1089
|
if not isinstance(res, HttpResponse):
|
|
1056
1090
|
req = self.build_response()
|
|
1057
|
-
|
|
1058
|
-
req.add_data(res)
|
|
1091
|
+
req.add_data(res)
|
|
1059
1092
|
req.set_send_body(send_body)
|
|
1060
1093
|
else:
|
|
1061
1094
|
req = res
|
|
@@ -1084,6 +1117,11 @@ class HttpReqHandler(BaseHTTPRequestHandler):
|
|
|
1084
1117
|
|
|
1085
1118
|
def do_OPTIONS(self):
|
|
1086
1119
|
"OPTIONS method"
|
|
1120
|
+
try:
|
|
1121
|
+
self._validate_request_framing()
|
|
1122
|
+
except HttpReqError as error:
|
|
1123
|
+
error.report(self)
|
|
1124
|
+
return
|
|
1087
1125
|
req = self.build_response(code = 204)
|
|
1088
1126
|
req.add_header('Access-Control-Allow-Origin', "*")
|
|
1089
1127
|
req.add_header('Access-Control-Allow-Methods', "OPTIONS, POST")
|
|
@@ -1123,6 +1161,15 @@ class HttpServerContext(object):
|
|
|
1123
1161
|
self.killed = False
|
|
1124
1162
|
self._running = False
|
|
1125
1163
|
self._lifecycle_lock = threading.RLock()
|
|
1164
|
+
self._startup_cleanup = None
|
|
1165
|
+
|
|
1166
|
+
def _unique_commands(self):
|
|
1167
|
+
"""Yield each registration once, preserving distinct registrations."""
|
|
1168
|
+
seen = set()
|
|
1169
|
+
for name, cmd in iteritems(self.commands):
|
|
1170
|
+
if id(cmd) not in seen:
|
|
1171
|
+
seen.add(id(cmd))
|
|
1172
|
+
yield name, cmd
|
|
1126
1173
|
|
|
1127
1174
|
def register(self, handler,
|
|
1128
1175
|
op,
|
|
@@ -1236,6 +1283,7 @@ class HttpServerContext(object):
|
|
|
1236
1283
|
self.auth_provider = None
|
|
1237
1284
|
self.auth_challenge = None
|
|
1238
1285
|
self.killed = False
|
|
1286
|
+
self._startup_cleanup = None
|
|
1239
1287
|
|
|
1240
1288
|
self.options = DEFAULT_OPTIONS.copy()
|
|
1241
1289
|
if isinstance(options, dict):
|
|
@@ -1275,7 +1323,7 @@ class HttpServerContext(object):
|
|
|
1275
1323
|
self.auth = HttpAuthentication(self.options['auth_basic_file'],
|
|
1276
1324
|
realm = self.options['auth_basic']).parse_file()
|
|
1277
1325
|
|
|
1278
|
-
for name, cmd in
|
|
1326
|
+
for name, cmd in self._unique_commands():
|
|
1279
1327
|
if cmd.safe_init:
|
|
1280
1328
|
LOG.info("safe_init: %r", name)
|
|
1281
1329
|
cmd.safe_init(self.options)
|
|
@@ -1307,7 +1355,9 @@ class HttpServerContext(object):
|
|
|
1307
1355
|
server = self.server
|
|
1308
1356
|
# Application callbacks may wait for workers. Do not hold the lifecycle
|
|
1309
1357
|
# lock while they run; the serving thread must be able to finish.
|
|
1310
|
-
|
|
1358
|
+
commands = (self._unique_commands() if self._startup_cleanup is None
|
|
1359
|
+
else self._startup_cleanup)
|
|
1360
|
+
cleanups = [(name, cmd.at_stop) for name, cmd in commands
|
|
1311
1361
|
if cmd.at_stop]
|
|
1312
1362
|
if server:
|
|
1313
1363
|
cleanups.extend((('server.kill', server.kill),
|
|
@@ -1338,6 +1388,8 @@ class HttpServerContext(object):
|
|
|
1338
1388
|
raise RuntimeError('initialize the HTTP context before restarting')
|
|
1339
1389
|
self._running = True
|
|
1340
1390
|
try:
|
|
1391
|
+
self._startup_cleanup = [(name, cmd) for name, cmd in self._unique_commands()
|
|
1392
|
+
if cmd.safe_init]
|
|
1341
1393
|
with self._lifecycle_lock:
|
|
1342
1394
|
if self.killed:
|
|
1343
1395
|
return
|
|
@@ -1347,12 +1399,15 @@ class HttpServerContext(object):
|
|
|
1347
1399
|
self.bind_handler(http_req_handler), name='httpdis')
|
|
1348
1400
|
self.server = server
|
|
1349
1401
|
server.httpdis_context = self
|
|
1350
|
-
for name, cmd in
|
|
1402
|
+
for name, cmd in self._unique_commands():
|
|
1351
1403
|
if self.killed:
|
|
1352
1404
|
break
|
|
1353
1405
|
if cmd.at_start:
|
|
1406
|
+
if not any(active is cmd for _, active in self._startup_cleanup):
|
|
1407
|
+
self._startup_cleanup.append((name, cmd))
|
|
1354
1408
|
LOG.info("at_start: %r", name)
|
|
1355
1409
|
cmd.at_start(self.options if options is None else options)
|
|
1410
|
+
self._startup_cleanup = None
|
|
1356
1411
|
LOG.info("will now serve")
|
|
1357
1412
|
while not self.killed:
|
|
1358
1413
|
try:
|
|
@@ -1364,9 +1419,16 @@ class HttpServerContext(object):
|
|
|
1364
1419
|
LOG.debug("server close")
|
|
1365
1420
|
else:
|
|
1366
1421
|
raise
|
|
1422
|
+
except BaseException:
|
|
1423
|
+
original_error = sys.exc_info()
|
|
1424
|
+
try:
|
|
1425
|
+
self.stop()
|
|
1426
|
+
except BaseException:
|
|
1427
|
+
LOG.exception('HTTP cleanup failed after startup or serving failure')
|
|
1428
|
+
reraise(*original_error)
|
|
1367
1429
|
finally:
|
|
1368
1430
|
try:
|
|
1369
|
-
if server is not None:
|
|
1431
|
+
if server is not None and not self.killed:
|
|
1370
1432
|
try:
|
|
1371
1433
|
server.kill()
|
|
1372
1434
|
finally:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: httpdis
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.34
|
|
4
4
|
Summary: httpdis
|
|
5
5
|
Home-page: https://github.com/decryptus/httpdis
|
|
6
6
|
Author: Adrien Delle Cave
|
|
@@ -20,12 +20,8 @@ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
|
20
20
|
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist: LEPL; python_version < "3.6"
|
|
24
23
|
Requires-Dist: python-magic
|
|
25
24
|
Requires-Dist: sonicprobe>=0.3.52
|
|
26
|
-
Requires-Dist: rfc6266; python_version < "3.4"
|
|
27
|
-
Requires-Dist: rfc6266-parser; python_version >= "3.4" and python_version < "3.6"
|
|
28
|
-
Requires-Dist: uc-rfc6266-parser; python_version >= "3.6"
|
|
29
25
|
Requires-Dist: six>=1.13.0
|
|
30
26
|
Requires-Dist: legacy-cgi; python_version >= "3.13"
|
|
31
27
|
Requires-Dist: legacycrypt; python_version >= "3.13"
|
|
@@ -96,6 +92,34 @@ Handlers receive a request with `get_path()`, `query_params()`, `payload_params(
|
|
|
96
92
|
lists, compiled regular expressions and `safe_init`, `at_start`, `at_stop` hooks.
|
|
97
93
|
Routes and options are process-global; use a separate process for independent apps.
|
|
98
94
|
|
|
95
|
+
## Request error contract
|
|
96
|
+
|
|
97
|
+
Since HTTPdis 0.6.33, the dispatcher distinguishes invalid request syntax, unsupported media,
|
|
98
|
+
missing routes and unsupported methods:
|
|
99
|
+
|
|
100
|
+
| Request | Response |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| Malformed JSON or invalid text encoding for a supported payload | 400, `Invalid request body` |
|
|
103
|
+
| Unsupported `Content-Type`, including disabled multipart | 415, `Unsupported Content-Type` |
|
|
104
|
+
| Path with no matching named or regex route | 404 |
|
|
105
|
+
| Recognized HTTP method with a matching path registered for other methods | 405 with `Allow` |
|
|
106
|
+
|
|
107
|
+
`Allow` lists the explicitly registered methods for the matching path in sorted
|
|
108
|
+
order. It does not invent HEAD or OPTIONS support. Method rejection does not read
|
|
109
|
+
the body or invoke a handler. Completely unsupported HTTP verbs remain the base
|
|
110
|
+
server's responsibility. These corrections change the previous 415 (malformed
|
|
111
|
+
payload), 501 (unsupported media) and 404 (known path, wrong method) responses;
|
|
112
|
+
clients should not depend on those old status mappings.
|
|
113
|
+
|
|
114
|
+
Payload-parser exceptions use a constant error message instead of reflecting
|
|
115
|
+
exception text or request body values. The JSON handler retains JSON errors.
|
|
116
|
+
This does not sanitize application-owned error messages or custom logging.
|
|
117
|
+
|
|
118
|
+
A registered route with `to_auth` enabled authenticates before body reading and
|
|
119
|
+
parsing. Routing and header/body-size checks may run before authentication;
|
|
120
|
+
unknown routes and wrong methods do not inherit another method's auth policy.
|
|
121
|
+
Applications still own operation-level authorization. Public routes are unchanged.
|
|
122
|
+
|
|
99
123
|
## Request body limits
|
|
100
124
|
|
|
101
125
|
Since HTTPdis 0.6.28, `register(..., max_body_size=...)` sets the body limit
|
|
@@ -156,27 +180,6 @@ serve large downloads through the reverse proxy.
|
|
|
156
180
|
The dependency loop with sonicprobe is retained because sonicprobe's historical
|
|
157
181
|
HTTP JSON module re-exports HTTPdis. A future major release should untangle it.
|
|
158
182
|
|
|
159
|
-
## Development and publication
|
|
160
|
-
|
|
161
|
-
```sh
|
|
162
|
-
python -m pip install -e . mock
|
|
163
|
-
python -m unittest discover -s tests -v
|
|
164
|
-
python -m pip install build twine
|
|
165
|
-
python -m build
|
|
166
|
-
python -m twine check --strict dist/*
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Update `VERSION`, `RELEASE` and `setup.yml` together. After tests and distribution
|
|
170
|
-
validation succeed, the master publication workflow creates `vX.Y.Z` and uploads
|
|
171
|
-
via Trusted Publishing (`decryptus/httpdis`, `pypi.yml`, environment `pypi`).
|
|
172
|
-
Already-tagged versions are not replaced by ordinary master commits.
|
|
173
|
-
|
|
174
|
-
License: GPL-3.0-or-later. Original authors and Wazo/Proformatique copyrights are
|
|
175
|
-
preserved in the source files.
|
|
176
|
-
|
|
177
|
-
See the [September 2026 code and architecture review](docs/REVIEW.md) (French).
|
|
178
|
-
|
|
179
|
-
|
|
180
183
|
### Independent embedded servers
|
|
181
184
|
|
|
182
185
|
Use `HttpServerContext` when multiple HTTPdis servers must coexist in one process.
|
|
@@ -505,3 +508,8 @@ OPTIONS behavior is unchanged: override it in the embedding browser handler.
|
|
|
505
508
|
Application scopes, ownership and action confirmation remain application policy.
|
|
506
509
|
This adapter supplies no HTML UI, proxy configuration, TOTP or SSO.
|
|
507
510
|
|
|
511
|
+
|
|
512
|
+
## Documentation
|
|
513
|
+
|
|
514
|
+
- **Users:** installation, configuration, operation and API usage in this README and the user guide.
|
|
515
|
+
- **Contributors:** [architecture, tests and development](https://github.com/decryptus/httpdis/blob/master/CONTRIBUTING.md).
|
|
@@ -14,6 +14,7 @@ httpdis/auth_browser.py
|
|
|
14
14
|
httpdis/auth_sqlite.py
|
|
15
15
|
httpdis/authentication.py
|
|
16
16
|
httpdis/config.py
|
|
17
|
+
httpdis/content_disposition.py
|
|
17
18
|
httpdis/httpdis.py
|
|
18
19
|
httpdis.egg-info/PKG-INFO
|
|
19
20
|
httpdis.egg-info/SOURCES.txt
|
|
@@ -22,10 +23,12 @@ httpdis.egg-info/requires.txt
|
|
|
22
23
|
httpdis.egg-info/top_level.txt
|
|
23
24
|
httpdis/ext/__init__.py
|
|
24
25
|
httpdis/ext/httpdis_json.py
|
|
26
|
+
tests/test_audit_regressions.py
|
|
25
27
|
tests/test_auth_backend.py
|
|
26
28
|
tests/test_auth_browser.py
|
|
27
29
|
tests/test_auth_sqlite.py
|
|
28
30
|
tests/test_authentication_provider.py
|
|
29
31
|
tests/test_body_limits.py
|
|
30
32
|
tests/test_context.py
|
|
33
|
+
tests/test_http_contract_errors.py
|
|
31
34
|
tests/test_httpdis.py
|
|
@@ -3,7 +3,8 @@ requires = [
|
|
|
3
3
|
"setuptools>=40.8,<45; python_version < '3'",
|
|
4
4
|
"setuptools>=40.8,<51; python_version >= '3' and python_version < '3.6'",
|
|
5
5
|
"setuptools>=40.8,<60; python_version >= '3.6' and python_version < '3.8'",
|
|
6
|
-
"setuptools>=68,<81; python_version >= '3.8'",
|
|
6
|
+
"setuptools>=68,<81; python_version >= '3.8' and python_version < '3.10'",
|
|
7
|
+
"setuptools>=83; python_version >= '3.10'",
|
|
7
8
|
"wheel<0.35; python_version < '3.6'",
|
|
8
9
|
"wheel; python_version >= '3.6'",
|
|
9
10
|
"PyYAML>=3.10"
|
|
@@ -3,9 +3,9 @@ name: httpdis
|
|
|
3
3
|
description: httpdis
|
|
4
4
|
author: Adrien Delle Cave
|
|
5
5
|
author_email: pypi@doowan.net
|
|
6
|
-
copyright: '2022 Adrien Delle Cave'
|
|
7
|
-
release: '0.6.
|
|
8
|
-
version: '0.6.
|
|
6
|
+
copyright: '2022-2026 Adrien Delle Cave'
|
|
7
|
+
release: '0.6.34'
|
|
8
|
+
version: '0.6.34'
|
|
9
9
|
license: License GPL-3
|
|
10
10
|
url: https://github.com/decryptus/httpdis
|
|
11
11
|
python_requires:
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import socket
|
|
3
|
+
import threading
|
|
4
|
+
import unittest
|
|
5
|
+
from collections import OrderedDict
|
|
6
|
+
from six.moves.BaseHTTPServer import HTTPServer
|
|
7
|
+
from six.moves.socketserver import ThreadingMixIn
|
|
8
|
+
try:
|
|
9
|
+
from unittest import mock
|
|
10
|
+
except ImportError:
|
|
11
|
+
import mock
|
|
12
|
+
from httpdis import httpdis as h
|
|
13
|
+
from httpdis.ext import httpdis_json as j
|
|
14
|
+
from httpdis.content_disposition import build_header, parse_headers
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _Server(ThreadingMixIn, HTTPServer):
|
|
18
|
+
daemon_threads = True
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class FramingAuditTests(unittest.TestCase):
|
|
22
|
+
def setUp(self):
|
|
23
|
+
self.calls, self.auth = [], []
|
|
24
|
+
context = j.HttpServerContext()
|
|
25
|
+
context.register(lambda r: self.calls.append(r.payload_params()) or {'ok': True}, 'POST', name='echo', to_auth=True)
|
|
26
|
+
context.register(lambda r: 'hello', ['GET', 'HEAD'], name='text')
|
|
27
|
+
context.register(lambda r: h.HttpResponse(data=u'caf\u00e9'), ['GET', 'HEAD'], name='raw')
|
|
28
|
+
for code in (204, 205, 304):
|
|
29
|
+
context.register(lambda r, status=code: h.HttpResponse(data='hidden', code=status), 'GET', name=str(code))
|
|
30
|
+
context.init({})
|
|
31
|
+
base = context.bind_handler()
|
|
32
|
+
auth = self.auth
|
|
33
|
+
class Handler(base):
|
|
34
|
+
def authenticate(self, users=None):
|
|
35
|
+
auth.append(True)
|
|
36
|
+
self.server = _Server(('127.0.0.1', 0), Handler)
|
|
37
|
+
self.thread = threading.Thread(target=self.server.serve_forever)
|
|
38
|
+
self.thread.daemon = True
|
|
39
|
+
self.thread.start()
|
|
40
|
+
self.addCleanup(self.close)
|
|
41
|
+
|
|
42
|
+
def close(self):
|
|
43
|
+
self.server.shutdown(); self.server.server_close(); self.thread.join(2)
|
|
44
|
+
|
|
45
|
+
def request(self, headers, method='POST', path='echo', body=b'{}'):
|
|
46
|
+
raw = ('%s /%s HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\n%s\r\n\r\n' % (method, path, headers)).encode('ascii') + body
|
|
47
|
+
sock = socket.create_connection(self.server.server_address, timeout=2)
|
|
48
|
+
try:
|
|
49
|
+
sock.sendall(raw); sock.shutdown(socket.SHUT_WR)
|
|
50
|
+
parts = []
|
|
51
|
+
while True:
|
|
52
|
+
part = sock.recv(65536)
|
|
53
|
+
if not part: break
|
|
54
|
+
parts.append(part)
|
|
55
|
+
return b''.join(parts)
|
|
56
|
+
finally:
|
|
57
|
+
sock.close()
|
|
58
|
+
|
|
59
|
+
def test_ambiguous_and_invalid_lengths_fail_before_authentication(self):
|
|
60
|
+
for headers in ('Content-Length: 2\r\nContent-Length: 10',
|
|
61
|
+
'Content-Length: 2,10', 'Content-Length: +2', 'Content-Length: -1',
|
|
62
|
+
'Content-Length: 2x', 'Content-Length:', 'Content-Length: 2,',
|
|
63
|
+
'Transfer-Encoding: identity\r\nContent-Length: 2',
|
|
64
|
+
'Transfer-Encoding: chunked\r\nContent-Length: 2',
|
|
65
|
+
'Transfer-Encoding: identity\r\nTransfer-Encoding: chunked'):
|
|
66
|
+
response = self.request(headers)
|
|
67
|
+
self.assertIn(b' 400 ', response.split(b'\r\n')[0], headers)
|
|
68
|
+
self.assertEqual(self.calls, [])
|
|
69
|
+
self.assertEqual(self.auth, [])
|
|
70
|
+
|
|
71
|
+
def test_identical_lengths_are_accepted_and_unsupported_coding_rejected(self):
|
|
72
|
+
for headers in ('Content-Length: 2', 'Content-Length: 2\r\nContent-Length: 2', 'Content-Length: 2, 2'):
|
|
73
|
+
self.assertIn(b' 200 ', self.request(headers).split(b'\r\n')[0])
|
|
74
|
+
self.assertEqual(self.calls, [{}, {}, {}])
|
|
75
|
+
self.assertIn(b' 400 ', self.request('Transfer-Encoding: chunked').split(b'\r\n')[0])
|
|
76
|
+
self.assertEqual(len(self.auth), 3)
|
|
77
|
+
|
|
78
|
+
def test_get_and_options_reject_ambiguous_framing(self):
|
|
79
|
+
for method in ('GET', 'HEAD', 'OPTIONS'):
|
|
80
|
+
result = self.request('Content-Length: 2\r\nContent-Length: 3', method, 'text')
|
|
81
|
+
self.assertIn(b' 400 ', result.split(b'\r\n')[0])
|
|
82
|
+
|
|
83
|
+
def test_head_reports_get_length_without_body(self):
|
|
84
|
+
get = self.request('', 'GET', 'text', b'')
|
|
85
|
+
head = self.request('', 'HEAD', 'text', b'')
|
|
86
|
+
self.assertIn(b'Content-Length: 7\r\n', get)
|
|
87
|
+
self.assertIn(b'Content-Length: 7\r\n', head)
|
|
88
|
+
self.assertEqual(head.split(b'\r\n\r\n', 1)[1], b'')
|
|
89
|
+
raw = self.request('', 'HEAD', 'raw', b'')
|
|
90
|
+
self.assertIn(b'Content-Length: 5\r\n', raw)
|
|
91
|
+
self.assertEqual(raw.split(b'\r\n\r\n', 1)[1], b'')
|
|
92
|
+
|
|
93
|
+
def test_no_content_responses_do_not_send_forbidden_lengths_or_bodies(self):
|
|
94
|
+
for code in (204, 205, 304):
|
|
95
|
+
result = self.request('', 'GET', str(code), b'')
|
|
96
|
+
headers, body = result.split(b'\r\n\r\n', 1)
|
|
97
|
+
self.assertEqual(body, b'')
|
|
98
|
+
if code == 205:
|
|
99
|
+
self.assertIn(b'Content-Length: 0', headers)
|
|
100
|
+
else:
|
|
101
|
+
self.assertNotIn(b'Content-Length:', headers)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class LifecycleAuditTests(unittest.TestCase):
|
|
105
|
+
def test_server_creation_failure_cleans_initialized_resources(self):
|
|
106
|
+
ctx, events = h.HttpServerContext(), []
|
|
107
|
+
ctx.register(lambda r: None, ['GET', 'POST'], name='one',
|
|
108
|
+
safe_init=lambda o: events.append('init'), at_stop=lambda: events.append('stop'))
|
|
109
|
+
ctx.init({})
|
|
110
|
+
def fail(*args, **kwargs):
|
|
111
|
+
raise OSError('bind failed')
|
|
112
|
+
with self.assertRaises(OSError):
|
|
113
|
+
ctx.run(http_server_class=fail)
|
|
114
|
+
self.assertEqual(events, ['init', 'stop'])
|
|
115
|
+
self.assertFalse(ctx._running)
|
|
116
|
+
|
|
117
|
+
def test_each_registration_runs_once_even_when_callbacks_are_shared(self):
|
|
118
|
+
ctx, events = h.HttpServerContext(), []
|
|
119
|
+
init = lambda o: events.append('init')
|
|
120
|
+
start = lambda o: events.append('start')
|
|
121
|
+
stop = lambda: events.append('stop')
|
|
122
|
+
for name in ('first', 'second'):
|
|
123
|
+
ctx.register(lambda r: None, ['GET', 'POST'], name=name, safe_init=init, at_start=start, at_stop=stop)
|
|
124
|
+
ctx.init({})
|
|
125
|
+
server = mock.Mock()
|
|
126
|
+
server.serve_until_killed.side_effect = ctx.stop
|
|
127
|
+
ctx.run(http_server_class=lambda *a, **k: server)
|
|
128
|
+
ctx.stop()
|
|
129
|
+
self.assertEqual(events, ['init', 'init', 'start', 'start', 'stop', 'stop'])
|
|
130
|
+
server.server_close.assert_called_once_with()
|
|
131
|
+
|
|
132
|
+
def test_failed_start_cleans_only_entered_phases_and_preserves_original_error(self):
|
|
133
|
+
ctx, events = h.HttpServerContext(), []
|
|
134
|
+
original = ValueError('start failed')
|
|
135
|
+
def start_fail(options):
|
|
136
|
+
events.append('start-fail'); raise original
|
|
137
|
+
def cleanup_fail():
|
|
138
|
+
events.append('cleanup-fail'); raise RuntimeError('cleanup failed')
|
|
139
|
+
ctx.register(lambda r: None, ['GET', 'POST'], name='one', at_start=lambda o: events.append('start'), at_stop=cleanup_fail)
|
|
140
|
+
ctx.register(lambda r: None, 'GET', name='two', at_start=start_fail, at_stop=lambda: events.append('partial-cleanup'))
|
|
141
|
+
ctx.register(lambda r: None, 'GET', name='three', at_start=lambda o: events.append('not-started'), at_stop=lambda: events.append('not-cleaned'))
|
|
142
|
+
ctx.commands = OrderedDict((name, ctx.commands[name]) for name in ('GET /one', 'POST /one', 'GET /two', 'GET /three'))
|
|
143
|
+
ctx.init({})
|
|
144
|
+
server = mock.Mock()
|
|
145
|
+
with self.assertRaises(ValueError) as caught:
|
|
146
|
+
ctx.run(http_server_class=lambda *a, **k: server)
|
|
147
|
+
self.assertIs(caught.exception, original)
|
|
148
|
+
self.assertEqual(events, ['start', 'start-fail', 'cleanup-fail', 'partial-cleanup'])
|
|
149
|
+
ctx.stop()
|
|
150
|
+
self.assertEqual(events.count('partial-cleanup'), 1)
|
|
151
|
+
server.server_close.assert_called_once_with()
|
|
152
|
+
ctx.init({})
|
|
153
|
+
self.assertFalse(ctx.killed)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class DispositionAuditTests(unittest.TestCase):
|
|
157
|
+
def test_ascii_quotes_unicode_and_bare_attachment(self):
|
|
158
|
+
self.assertIsNone(parse_headers('attachment').filename_unsafe)
|
|
159
|
+
for name in ('plain.txt', 'a"b\\c.txt', u'caf\u00e9.txt'):
|
|
160
|
+
header = build_header(name)
|
|
161
|
+
self.assertEqual(parse_headers(header).disposition, 'attachment')
|
|
162
|
+
# RFC 5987 wins over the ASCII fallback when emitted.
|
|
163
|
+
self.assertEqual(parse_headers(header).filename_unsafe, name)
|
|
164
|
+
self.assertIn("filename*=UTF-8''caf%C3%A9.txt", build_header(u'caf\u00e9.txt'))
|
|
165
|
+
self.assertEqual(parse_headers("attachment; filename*=UTF-8''caf%C3%A9.txt").filename_unsafe, u'caf\u00e9.txt')
|
|
166
|
+
|
|
167
|
+
def test_control_characters_cannot_inject_headers(self):
|
|
168
|
+
for value in ('a\r\nInjected: yes', 'a\x00b'):
|
|
169
|
+
with self.assertRaises(ValueError): build_header(value)
|
|
170
|
+
with self.assertRaises(ValueError): parse_headers(value)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""HTTP errors must preserve routing semantics without reflecting request bodies."""
|
|
2
|
+
import io
|
|
3
|
+
import re
|
|
4
|
+
import unittest
|
|
5
|
+
from httpdis import httpdis as h
|
|
6
|
+
from httpdis.ext import httpdis_json as j
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
_METHODS = ('GET', 'POST')
|
|
10
|
+
_ITEM_ROUTE = re.compile(r'^items/(?P<item>[^/]+)$')
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ContractErrorsTests(unittest.TestCase):
|
|
14
|
+
def handler(self, method='POST', body=b'{"PRIVATE_BODY":', content_type='application/json'):
|
|
15
|
+
context = h.HttpServerContext()
|
|
16
|
+
context.init({}, use_sigterm_handler=False)
|
|
17
|
+
calls = []
|
|
18
|
+
context.register(lambda request: calls.append('called'), _METHODS, name='resource')
|
|
19
|
+
context.register(lambda request: calls.append('called'), 'GET', name=_ITEM_ROUTE)
|
|
20
|
+
class Handler(j.HttpReqHandler):
|
|
21
|
+
def __init__(self):
|
|
22
|
+
pass
|
|
23
|
+
handler = Handler()
|
|
24
|
+
handler._httpdis_context = context
|
|
25
|
+
handler.command = method
|
|
26
|
+
handler.headers = {'Content-Length': str(len(body)), 'Content-Type': content_type}
|
|
27
|
+
handler.rfile = io.BytesIO(body)
|
|
28
|
+
handler._cmd = None
|
|
29
|
+
handler._query_params = {}
|
|
30
|
+
return handler, calls
|
|
31
|
+
|
|
32
|
+
def test_malformed_json_and_utf8_are_400_without_body_reflection(self):
|
|
33
|
+
for body in (b'{"PRIVATE_BODY":', b'\xff'):
|
|
34
|
+
handler, calls = self.handler(body=body)
|
|
35
|
+
with self.assertRaises(h.HttpReqError) as raised:
|
|
36
|
+
handler.data_from_payload('resource')
|
|
37
|
+
self.assertEqual(raised.exception.code, 400)
|
|
38
|
+
self.assertEqual(raised.exception.text, 'Invalid request body')
|
|
39
|
+
self.assertEqual(calls, [])
|
|
40
|
+
|
|
41
|
+
def test_unsupported_content_type_is_415_without_reflection(self):
|
|
42
|
+
handler, calls = self.handler(content_type='application/PRIVATE_HEADER')
|
|
43
|
+
with self.assertRaises(h.HttpReqError) as raised:
|
|
44
|
+
handler.data_from_payload('resource')
|
|
45
|
+
self.assertEqual(raised.exception.code, 415)
|
|
46
|
+
self.assertEqual(raised.exception.text, 'Unsupported Content-Type')
|
|
47
|
+
self.assertEqual(handler.rfile.tell(), 0)
|
|
48
|
+
self.assertEqual(calls, [])
|
|
49
|
+
|
|
50
|
+
def test_named_and_regex_routes_report_allow_without_calling_handlers(self):
|
|
51
|
+
for path, allowed in (('resource', 'GET, POST'), ('items/123', 'GET')):
|
|
52
|
+
for method in ('DELETE', 'PATCH'):
|
|
53
|
+
handler, calls = self.handler(method=method)
|
|
54
|
+
dispatch = handler.data_from_query if method == 'DELETE' else handler.data_from_payload
|
|
55
|
+
with self.assertRaises(h.HttpReqError) as raised:
|
|
56
|
+
dispatch(path)
|
|
57
|
+
self.assertEqual(raised.exception.code, 405)
|
|
58
|
+
self.assertEqual(raised.exception.headers, {'Allow': allowed})
|
|
59
|
+
self.assertEqual(handler.rfile.tell(), 0)
|
|
60
|
+
self.assertEqual(calls, [])
|
|
61
|
+
|
|
62
|
+
def test_unknown_path_stays_404_without_allow(self):
|
|
63
|
+
handler, calls = self.handler()
|
|
64
|
+
with self.assertRaises(h.HttpReqError) as raised:
|
|
65
|
+
handler.data_from_payload('absent')
|
|
66
|
+
self.assertEqual(raised.exception.code, 404)
|
|
67
|
+
self.assertEqual(raised.exception.headers, {})
|
|
68
|
+
self.assertEqual(calls, [])
|
|
69
|
+
|
|
70
|
+
def test_authentication_precedes_body_read_and_parse(self):
|
|
71
|
+
handler, calls = self.handler()
|
|
72
|
+
handler._httpdis_context.named_commands['POST /resource'].to_auth = True
|
|
73
|
+
def denied(*args):
|
|
74
|
+
raise handler.req_error(401)
|
|
75
|
+
handler.authenticate = denied
|
|
76
|
+
with self.assertRaises(h.HttpReqError) as raised:
|
|
77
|
+
handler.data_from_payload('resource')
|
|
78
|
+
self.assertEqual(raised.exception.code, 401)
|
|
79
|
+
self.assertEqual(handler.rfile.tell(), 0)
|
|
80
|
+
self.assertEqual(calls, [])
|
httpdis-0.6.32/RELEASE
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.6.32
|
httpdis-0.6.32/VERSION
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.6.32
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
python-magic
|
|
2
|
-
sonicprobe>=0.3.52
|
|
3
|
-
six>=1.13.0
|
|
4
|
-
|
|
5
|
-
[:python_version < "3.4"]
|
|
6
|
-
rfc6266
|
|
7
|
-
|
|
8
|
-
[:python_version < "3.6"]
|
|
9
|
-
LEPL
|
|
10
|
-
|
|
11
|
-
[:python_version >= "3.13"]
|
|
12
|
-
legacy-cgi
|
|
13
|
-
legacycrypt
|
|
14
|
-
|
|
15
|
-
[:python_version >= "3.4" and python_version < "3.6"]
|
|
16
|
-
rfc6266-parser
|
|
17
|
-
|
|
18
|
-
[:python_version >= "3.6"]
|
|
19
|
-
uc-rfc6266-parser
|
|
20
|
-
|
|
21
|
-
[auth]
|
|
22
|
-
sonicprobe>=0.3.55
|
|
23
|
-
|
|
24
|
-
[auth:python_version >= "3.10"]
|
|
25
|
-
argon2-cffi<26,>=23.1
|
httpdis-0.6.32/requirements.txt
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
LEPL; python_version < '3.6'
|
|
2
|
-
python-magic
|
|
3
|
-
sonicprobe>=0.3.52
|
|
4
|
-
rfc6266 ; python_version < '3.4'
|
|
5
|
-
rfc6266-parser ; python_version >= '3.4' and python_version < '3.6'
|
|
6
|
-
uc-rfc6266-parser ; python_version >= '3.6'
|
|
7
|
-
six>=1.13.0
|
|
8
|
-
legacy-cgi; python_version >= '3.13'
|
|
9
|
-
legacycrypt; python_version >= '3.13'
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|