yhttp 9.5.0__tar.gz → 9.6.0__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.
Files changed (63) hide show
  1. {yhttp-9.5.0/yhttp.egg-info → yhttp-9.6.0}/PKG-INFO +1 -1
  2. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_static.py +116 -0
  3. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/static.py +89 -19
  4. yhttp-9.6.0/yhttp/core/version.py +1 -0
  5. {yhttp-9.5.0 → yhttp-9.6.0/yhttp.egg-info}/PKG-INFO +1 -1
  6. yhttp-9.5.0/yhttp/core/version.py +0 -1
  7. {yhttp-9.5.0 → yhttp-9.6.0}/LICENSE +0 -0
  8. {yhttp-9.5.0 → yhttp-9.6.0}/README.md +0 -0
  9. {yhttp-9.5.0 → yhttp-9.6.0}/setup.cfg +0 -0
  10. {yhttp-9.5.0 → yhttp-9.6.0}/setup.py +0 -0
  11. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_application.py +0 -0
  12. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_applicationcli.py +0 -0
  13. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_builtincli_serve.py +0 -0
  14. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_builtincli_version.py +0 -0
  15. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_builtincli_yhttp_version.py +0 -0
  16. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_cookie.py +0 -0
  17. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_cookieset.py +0 -0
  18. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_extension.py +0 -0
  19. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_form.py +0 -0
  20. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_form_json.py +0 -0
  21. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_form_multipart.py +0 -0
  22. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_form_urlencoded.py +0 -0
  23. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_fswatcher.py +0 -0
  24. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_guard.py +0 -0
  25. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_guard_body.py +0 -0
  26. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_guard_contenttypes.py +0 -0
  27. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_guard_query.py +0 -0
  28. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_headerset.py +0 -0
  29. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_jsonencoding.py +0 -0
  30. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_locale.py +0 -0
  31. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_logging.py +0 -0
  32. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_middleware.py +0 -0
  33. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_multidict.py +0 -0
  34. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_multipart.py +0 -0
  35. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_querystring.py +0 -0
  36. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_request.py +0 -0
  37. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_responseheaders.py +0 -0
  38. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_rewrite.py +0 -0
  39. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_rewritecli.py +0 -0
  40. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_routing.py +0 -0
  41. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_statuses.py +0 -0
  42. {yhttp-9.5.0 → yhttp-9.6.0}/tests/test_statushandler.py +0 -0
  43. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/__init__.py +0 -0
  44. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/application.py +0 -0
  45. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/cli.py +0 -0
  46. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/contenttypes.py +0 -0
  47. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/cookieset.py +0 -0
  48. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/fswatcher.py +0 -0
  49. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/guard.py +0 -0
  50. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/headerset.py +0 -0
  51. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/logging.py +0 -0
  52. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/main.py +0 -0
  53. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/multidict.py +0 -0
  54. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/multipart.py +0 -0
  55. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/request.py +0 -0
  56. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/response.py +0 -0
  57. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/rewrite.py +0 -0
  58. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp/core/statuses.py +0 -0
  59. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp.egg-info/SOURCES.txt +0 -0
  60. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp.egg-info/dependency_links.txt +0 -0
  61. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp.egg-info/entry_points.txt +0 -0
  62. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp.egg-info/requires.txt +0 -0
  63. {yhttp-9.5.0 → yhttp-9.6.0}/yhttp.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: yhttp
3
- Version: 9.5.0
3
+ Version: 9.6.0
4
4
  Summary: A very micro http framework.
5
5
  Home-page: http://github.com/yhttp/yhttp
6
6
  Author: Vahid Mardani
@@ -1,6 +1,11 @@
1
1
  from os import path
2
+ from types import SimpleNamespace
2
3
 
3
4
  from bddrest import status, response, when
5
+ import pytest
6
+
7
+ from yhttp.core.headerset import HeaderSet
8
+ from yhttp.core.static import file as staticfile
4
9
 
5
10
 
6
11
  def test_staticfile(app, httpreq, tmpdir):
@@ -17,6 +22,117 @@ def test_staticfile(app, httpreq, tmpdir):
17
22
  assert response == 'foo'
18
23
 
19
24
 
25
+ def test_staticfile_byte_ranges(app, httpreq, tmpdir):
26
+ filename = path.join(tmpdir, 'range.txt')
27
+ with open(filename, 'wb') as f:
28
+ f.write(b'0123456789')
29
+
30
+ app.staticfile(r'/range\.txt', filename)
31
+
32
+ with httpreq('/range.txt', headers={'Range': 'bytes=2-5'}):
33
+ assert status == 206
34
+ assert response.headers['accept-ranges'] == 'bytes'
35
+ assert response.headers['content-range'] == 'bytes 2-5/10'
36
+ assert response.headers['content-length'] == '4'
37
+ assert response == '2345'
38
+
39
+ with httpreq('/range.txt', headers={'Range': 'bytes=-3'}):
40
+ assert status == 206
41
+ assert response.headers['content-range'] == 'bytes 7-9/10'
42
+ assert response == '789'
43
+
44
+ with httpreq('/range.txt', headers={'Range': 'bytes=20-'}):
45
+ assert status == 416
46
+ assert response.headers['content-range'] == 'bytes */10'
47
+
48
+ with httpreq('/range.txt', headers={'Range': 'bytes=-0'}):
49
+ assert status == 416
50
+ assert response.headers['content-range'] == 'bytes */10'
51
+
52
+ with httpreq('/range.txt', headers={'Range': 'invalid'}):
53
+ assert status == 200
54
+ assert response.headers['content-length'] == '10'
55
+ assert response == '0123456789'
56
+
57
+ with httpreq(
58
+ '/range.txt', headers={'Range': 'bytes=0-1,4-5'}
59
+ ):
60
+ assert status == 200
61
+ assert response == '0123456789'
62
+
63
+
64
+ @pytest.mark.parametrize('range_header', [
65
+ 'bytes=10',
66
+ 'bytes=-',
67
+ 'bytes=3-1',
68
+ 'bytes=abc-def',
69
+ 'bytes=--1',
70
+ ])
71
+ def test_malformed_or_unsupported_ranges_send_full_file(
72
+ app, httpreq, tmpdir, range_header,
73
+ ):
74
+ filename = path.join(tmpdir, 'range.txt')
75
+ with open(filename, 'wb') as f:
76
+ f.write(b'0123456789')
77
+ app.staticfile(r'/range\.txt', filename)
78
+
79
+ with httpreq('/range.txt', headers={'Range': range_header}):
80
+ assert status == 200
81
+ assert response == '0123456789'
82
+
83
+
84
+ def test_empty_static_file(app, httpreq, tmpdir):
85
+ filename = path.join(tmpdir, 'empty.txt')
86
+ open(filename, 'wb').close()
87
+ app.staticfile(r'/empty\.txt', filename)
88
+
89
+ with httpreq('/empty.txt'):
90
+ assert status == 200
91
+ assert response.headers['content-length'] == '0'
92
+ assert response == ''
93
+
94
+
95
+ def test_staticfile_short_read_after_size_lookup(tmpdir):
96
+ filename = path.join(tmpdir, 'short.txt')
97
+ with open(filename, 'wb') as f:
98
+ f.write(b'content')
99
+
100
+ handler = staticfile(filename)
101
+ request = SimpleNamespace(
102
+ verb='get', environ={},
103
+ response=SimpleNamespace(headers=HeaderSet()),
104
+ )
105
+ open(filename, 'wb').close()
106
+
107
+ assert list(handler(request)) == []
108
+
109
+
110
+ def test_staticdirectory_byte_ranges(app, httpreq, tmpdir):
111
+ filename = path.join(tmpdir, 'range.txt')
112
+ with open(filename, 'wb') as f:
113
+ f.write(b'0123456789')
114
+ app.staticdirectory('/static', tmpdir)
115
+
116
+ with httpreq('/static/range.txt', headers={'Range': 'bytes=3-'}):
117
+ assert status == 206
118
+ assert response.headers['content-range'] == 'bytes 3-9/10'
119
+ assert response == '3456789'
120
+
121
+
122
+ def test_range_request_with_if_range_sends_full_file(app, httpreq, tmpdir):
123
+ filename = path.join(tmpdir, 'range.txt')
124
+ with open(filename, 'wb') as f:
125
+ f.write(b'0123456789')
126
+ app.staticfile(r'/range\.txt', filename)
127
+
128
+ with httpreq('/range.txt', headers={
129
+ 'Range': 'bytes=2-5',
130
+ 'If-Range': '"unknown-validator"',
131
+ }):
132
+ assert status == 200
133
+ assert response == '0123456789'
134
+
135
+
20
136
  def test_staticdirectory(app, httpreq, mktmptree):
21
137
  temproot = mktmptree({
22
138
  'bar': {
@@ -8,21 +8,94 @@ from . import statuses
8
8
  CHUNKSIZE = 1024 * 10
9
9
 
10
10
 
11
+ def _byte_range(request, length):
12
+ """Return the selected byte interval, or None when Range is ignored."""
13
+ if request.verb != 'get' or 'HTTP_IF_RANGE' in request.environ:
14
+ return None
15
+
16
+ value = request.environ.get('HTTP_RANGE')
17
+ if not value or '=' not in value:
18
+ return None
19
+
20
+ unit, spec = value.split('=', 1)
21
+ if unit.strip().lower() != 'bytes' or ',' in spec:
22
+ # Multiple ranges are optional; ignore them and send the full entity.
23
+ return None
24
+
25
+ spec = spec.strip()
26
+ if '-' not in spec:
27
+ return None
28
+ first, last = (part.strip() for part in spec.split('-', 1))
29
+ if not first and not last:
30
+ return None
31
+
32
+ try:
33
+ if not first:
34
+ suffix_length = int(last)
35
+ if suffix_length < 0:
36
+ return None
37
+ if suffix_length == 0 or length == 0:
38
+ return False
39
+ return max(0, length - suffix_length), length - 1
40
+
41
+ start = int(first)
42
+ if last:
43
+ end = int(last)
44
+ if end < start:
45
+ return None
46
+ else:
47
+ end = length - 1
48
+ except ValueError:
49
+ return None
50
+
51
+ if start >= length:
52
+ return False
53
+ return start, min(end, length - 1)
54
+
55
+
56
+ def _stream_file(request, filename, length, contenttype):
57
+ response = request.response
58
+ response.contenttype = contenttype
59
+ response.headers['accept-ranges'] = 'bytes'
60
+
61
+ byte_range = _byte_range(request, length)
62
+ if byte_range is False:
63
+ response.status = '416 Range Not Satisfiable'
64
+ response.headers['content-range'] = f'bytes */{length}'
65
+ response.length = 0
66
+ yield b''
67
+ return
68
+
69
+ if byte_range is None:
70
+ response.length = length
71
+ start, end = 0, length - 1
72
+ else:
73
+ start, end = byte_range
74
+ response.status = '206 Partial Content'
75
+ response.headers['content-range'] = f'bytes {start}-{end}/{length}'
76
+ response.length = end - start + 1
77
+
78
+ if not response.length:
79
+ yield b''
80
+ return
81
+
82
+ with open(filename, 'rb') as f:
83
+ f.seek(start)
84
+ remaining = response.length
85
+ while remaining:
86
+ chunk = f.read(min(CHUNKSIZE, remaining))
87
+ if not chunk:
88
+ break
89
+ remaining -= len(chunk)
90
+ yield chunk
91
+
92
+
11
93
  def file(filename):
12
94
  length = path.getsize(filename)
13
95
  type_ = guess_type(path.split(filename)[1])[0]
14
96
 
15
97
  def get(request):
16
- response = request.response
17
- response.length = length
18
- response.contenttype = type_
19
- with open(filename, 'rb') as f:
20
- while True:
21
- chunk = f.read(CHUNKSIZE)
22
- if not chunk:
23
- break
24
-
25
- yield chunk
98
+ yield from _stream_file(request, filename, length, type_)
26
99
 
27
100
  return get
28
101
 
@@ -145,14 +218,11 @@ def directory(rootpath, default=False, autoindex=True, fallback=False):
145
218
  if not path.exists(target):
146
219
  raise statuses.notfound()
147
220
 
148
- response.length = path.getsize(target)
149
- response.contenttype = guess_type(path.split(target)[1])[0]
150
- with open(target, 'rb') as f:
151
- while True:
152
- chunk = f.read(CHUNKSIZE)
153
- if not chunk:
154
- break
155
-
156
- yield chunk
221
+ yield from _stream_file(
222
+ request,
223
+ target,
224
+ path.getsize(target),
225
+ guess_type(path.split(target)[1])[0],
226
+ )
157
227
 
158
228
  return get
@@ -0,0 +1 @@
1
+ __version__ = '9.6.0'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: yhttp
3
- Version: 9.5.0
3
+ Version: 9.6.0
4
4
  Summary: A very micro http framework.
5
5
  Home-page: http://github.com/yhttp/yhttp
6
6
  Author: Vahid Mardani
@@ -1 +0,0 @@
1
- __version__ = '9.5.0'
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
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
File without changes
File without changes
File without changes
File without changes