Skip to content

Commit 1897bf7

Browse files
Dmitry Berezovskycarltongibson
authored andcommitted
Added ability to configure websocket message size when running as django dev server
Fixes #580
1 parent 0cd79c2 commit 1897bf7

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

daphne/management/commands/runserver.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ def add_arguments(self, parser):
7373
"seconds (default: 5)"
7474
),
7575
)
76+
parser.add_argument(
77+
"--websocket-max-message-size",
78+
type=int,
79+
help="Maximum size, in bytes, of an incoming WebSocket message. "
80+
"0 disables the limit (not recommended; allows unauthenticated "
81+
"memory exhaustion).",
82+
default=None,
83+
)
84+
parser.add_argument(
85+
"--websocket-max-frame-size",
86+
type=int,
87+
help="Maximum size, in bytes, of a single incoming WebSocket frame. "
88+
"0 disables the limit (not recommended; allows unauthenticated "
89+
"memory exhaustion).",
90+
default=None,
91+
)
7692
if apps.is_installed("django.contrib.staticfiles"):
7793
parser.add_argument(
7894
"--nostatic",
@@ -95,6 +111,17 @@ def handle(self, *args, **options):
95111
raise CommandError(
96112
"You have not set ASGI_APPLICATION, which is needed to run the server."
97113
)
114+
self.websocket_max_message_size = options.get("websocket_max_message_size")
115+
if self.websocket_max_message_size is None:
116+
self.websocket_max_message_size = getattr(
117+
settings, "DAPHNE_WEBSOCKET_MAX_MESSAGE_SIZE", 1024 * 1024
118+
)
119+
self.websocket_max_frame_size = options.get("websocket_max_frame_size")
120+
if self.websocket_max_frame_size is None:
121+
self.websocket_max_frame_size = getattr(
122+
settings, "DAPHNE_WEBSOCKET_MAX_FRAME_SIZE", 1024 * 1024
123+
)
124+
98125
# Dispatch upward
99126
super().handle(*args, **options)
100127

@@ -145,6 +172,8 @@ def inner_run(self, *args, **options):
145172
http_timeout=self.http_timeout,
146173
root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "",
147174
websocket_handshake_timeout=self.websocket_handshake_timeout,
175+
websocket_max_message_size=self.websocket_max_message_size,
176+
websocket_max_frame_size=self.websocket_max_frame_size,
148177
).run()
149178
logger.debug("Daphne exited")
150179
except KeyboardInterrupt:

0 commit comments

Comments
 (0)