asgiref 3.11.0__py3-none-any.whl → 3.11.1__py3-none-any.whl

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.
asgiref/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "3.11.0"
1
+ __version__ = "3.11.1"
asgiref/sync.py CHANGED
@@ -432,9 +432,11 @@ class SyncToAsync(Generic[_P, _R]):
432
432
  or iscoroutinefunction(getattr(func, "__call__", func))
433
433
  ):
434
434
  raise TypeError("sync_to_async can only be applied to sync functions.")
435
+
436
+ functools.update_wrapper(self, func)
435
437
  self.func = func
436
438
  self.context = context
437
- functools.update_wrapper(self, func)
439
+
438
440
  self._thread_sensitive = thread_sensitive
439
441
  markcoroutinefunction(self)
440
442
  if thread_sensitive and executor is not None:
asgiref/wsgi.py CHANGED
@@ -1,4 +1,5 @@
1
1
  import sys
2
+ from collections import defaultdict
2
3
  from tempfile import SpooledTemporaryFile
3
4
 
4
5
  from asgiref.sync import AsyncToSync, sync_to_async
@@ -9,8 +10,9 @@ class WsgiToAsgi:
9
10
  Wraps a WSGI application to make it into an ASGI application.
10
11
  """
11
12
 
12
- def __init__(self, wsgi_application):
13
+ def __init__(self, wsgi_application, duplicate_header_limit=100):
13
14
  self.wsgi_application = wsgi_application
15
+ self.duplicate_header_limit = duplicate_header_limit
14
16
 
15
17
  async def __call__(self, scope, receive, send):
16
18
  """
@@ -18,7 +20,9 @@ class WsgiToAsgi:
18
20
  We return a new WsgiToAsgiInstance here with the WSGI app
19
21
  and the scope, ready to respond when it is __call__ed.
20
22
  """
21
- await WsgiToAsgiInstance(self.wsgi_application)(scope, receive, send)
23
+ await WsgiToAsgiInstance(self.wsgi_application, self.duplicate_header_limit)(
24
+ scope, receive, send
25
+ )
22
26
 
23
27
 
24
28
  class WsgiToAsgiInstance:
@@ -26,8 +30,9 @@ class WsgiToAsgiInstance:
26
30
  Per-socket instance of a wrapped WSGI application
27
31
  """
28
32
 
29
- def __init__(self, wsgi_application):
33
+ def __init__(self, wsgi_application, duplicate_header_limit=100):
30
34
  self.wsgi_application = wsgi_application
35
+ self.duplicate_header_limit = duplicate_header_limit
31
36
  self.response_started = False
32
37
  self.response_content_length = None
33
38
 
@@ -84,6 +89,7 @@ class WsgiToAsgiInstance:
84
89
  environ["REMOTE_ADDR"] = scope["client"][0]
85
90
 
86
91
  # Go through headers and make them into environ entries
92
+ _headers = defaultdict(list)
87
93
  for name, value in self.scope.get("headers", []):
88
94
  name = name.decode("latin1")
89
95
  if name == "content-length":
@@ -94,9 +100,17 @@ class WsgiToAsgiInstance:
94
100
  corrected_name = "HTTP_%s" % name.upper().replace("-", "_")
95
101
  # HTTPbis say only ASCII chars are allowed in headers, but we latin1 just in case
96
102
  value = value.decode("latin1")
97
- if corrected_name in environ:
98
- value = environ[corrected_name] + "," + value
99
- environ[corrected_name] = value
103
+ if (
104
+ self.duplicate_header_limit
105
+ and len(_headers[corrected_name]) >= self.duplicate_header_limit
106
+ ):
107
+ raise ValueError(
108
+ f"Too many duplicate headers: {corrected_name} exceeds limit of"
109
+ f"{self.duplicate_header_limit}"
110
+ )
111
+ _headers[corrected_name].append(value)
112
+ for name, values in _headers.items():
113
+ environ[name] = ",".join(values)
100
114
  return environ
101
115
 
102
116
  def start_response(self, status, response_headers, exc_info=None):
@@ -138,7 +152,24 @@ class WsgiToAsgiInstance:
138
152
  this so that the start_response callable is called in the same thread.
139
153
  """
140
154
  # Translate the scope and incoming request body into a WSGI environ
141
- environ = self.build_environ(self.scope, body)
155
+ try:
156
+ environ = self.build_environ(self.scope, body)
157
+ except ValueError:
158
+ # Return 400 Bad Request if header limit exceeded
159
+ self.sync_send(
160
+ {
161
+ "type": "http.response.start",
162
+ "status": 400,
163
+ "headers": [(b"content-type", b"text/plain")],
164
+ }
165
+ )
166
+ self.sync_send(
167
+ {
168
+ "type": "http.response.body",
169
+ "body": b"Bad Request: Too many duplicate headers",
170
+ }
171
+ )
172
+ return
142
173
  # Run the WSGI app
143
174
  bytes_sent = 0
144
175
  for output in self.wsgi_application(environ, self.start_response):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: asgiref
3
- Version: 3.11.0
3
+ Version: 3.11.1
4
4
  Summary: ASGI specs, helper code, and adapters
5
5
  Home-page: https://github.com/django/asgiref/
6
6
  Author: Django Software Foundation
@@ -1,16 +1,16 @@
1
- asgiref/__init__.py,sha256=cpptu6yAKjujkKCECVRXYkw3SmUpyBiTvJvB3Mmcu5s,23
1
+ asgiref/__init__.py,sha256=UuKR3QjWk9sJtn2huDFsPdSgtx1CgiEl2B4BUtVRd58,23
2
2
  asgiref/compatibility.py,sha256=DhY1SOpOvOw0Y1lSEjCqg-znRUQKecG3LTaV48MZi68,1606
3
3
  asgiref/current_thread_executor.py,sha256=42CU1VODLTk-_PYise-cP1XgyAvI5Djc8f97owFzdrs,4157
4
4
  asgiref/local.py,sha256=ZZeWWIXptVU4GbNApMMWQ-skuglvodcQA5WpzJDMxh4,4912
5
5
  asgiref/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  asgiref/server.py,sha256=3A68169Nuh2sTY_2O5JzRd_opKObWvvrEFcrXssq3kA,6311
7
- asgiref/sync.py,sha256=kYfWtf4CI438zIw85kX9YQx0y64B1N6AM0PcRGf01M4,22927
7
+ asgiref/sync.py,sha256=KHjSkYRZKIJ1cmBvxcloGrcnhvAuEfXCWCZAoxN8grg,22929
8
8
  asgiref/testing.py,sha256=U5wcs_-ZYTO5SIGfl80EqRAGv_T8BHrAhvAKRuuztT4,4421
9
9
  asgiref/timeout.py,sha256=LtGL-xQpG8JHprdsEUCMErJ0kNWj4qwWZhEHJ3iKu4s,3627
10
10
  asgiref/typing.py,sha256=Zi72AZlOyF1C7N14LLZnpAdfUH4ljoBqFdQo_bBKMq0,6290
11
- asgiref/wsgi.py,sha256=J8OAgirfsYHZmxxqIGfFiZ43uq1qKKv2xGMkRISNIo4,6742
12
- asgiref-3.11.0.dist-info/licenses/LICENSE,sha256=uEZBXRtRTpwd_xSiLeuQbXlLxUbKYSn5UKGM0JHipmk,1552
13
- asgiref-3.11.0.dist-info/METADATA,sha256=gV4IrytPfzCXgnDhv_rM5GBoUUd_Tz-QvqZSFKEmVGs,9287
14
- asgiref-3.11.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
- asgiref-3.11.0.dist-info/top_level.txt,sha256=bokQjCzwwERhdBiPdvYEZa4cHxT4NCeAffQNUqJ8ssg,8
16
- asgiref-3.11.0.dist-info/RECORD,,
11
+ asgiref/wsgi.py,sha256=OSxanm5Qf-VfrOkAx62mYW0mOnh8pwaTz1099HbJzM0,7941
12
+ asgiref-3.11.1.dist-info/licenses/LICENSE,sha256=uEZBXRtRTpwd_xSiLeuQbXlLxUbKYSn5UKGM0JHipmk,1552
13
+ asgiref-3.11.1.dist-info/METADATA,sha256=aLePcJP6N7HpneCbacTmO_4Gc4b0PRSo2fWPwsWLeYQ,9287
14
+ asgiref-3.11.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
15
+ asgiref-3.11.1.dist-info/top_level.txt,sha256=bokQjCzwwERhdBiPdvYEZa4cHxT4NCeAffQNUqJ8ssg,8
16
+ asgiref-3.11.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.9.0)
2
+ Generator: setuptools (80.10.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5