mirror of
				https://github.com/django/daphne.git
				synced 2025-10-31 15:57:25 +03:00 
			
		
		
		
	Respond with a code when closing a connection
Regards django/channels#414
This commit is contained in:
		
							parent
							
								
									3c8c21b352
								
							
						
					
					
						commit
						c6e4ea25d1
					
				|  | @ -265,7 +265,6 @@ class WebRequest(http.Request): | ||||||
|         }) |         }) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| class HTTPProtocol(http.HTTPChannel): | class HTTPProtocol(http.HTTPChannel): | ||||||
| 
 | 
 | ||||||
|     requestFactory = WebRequest |     requestFactory = WebRequest | ||||||
|  | @ -323,11 +322,13 @@ class HTTPFactory(http.HTTPFactory): | ||||||
|                 protocol.serverSend(message["bytes"], True) |                 protocol.serverSend(message["bytes"], True) | ||||||
|             if message.get("text", None): |             if message.get("text", None): | ||||||
|                 protocol.serverSend(message["text"], False) |                 protocol.serverSend(message["text"], False) | ||||||
|             if message.get("close", False): | 
 | ||||||
|  |             closing_code = message.get("close", False) | ||||||
|  |             if closing_code: | ||||||
|                 if protocol.state == protocol.STATE_CONNECTING: |                 if protocol.state == protocol.STATE_CONNECTING: | ||||||
|                     protocol.serverReject() |                     protocol.serverReject() | ||||||
|                 else: |                 else: | ||||||
|                     protocol.serverClose() |                     protocol.serverClose(code=closing_code) | ||||||
|         else: |         else: | ||||||
|             raise ValueError("Cannot dispatch message on channel %r" % channel) |             raise ValueError("Cannot dispatch message on channel %r" % channel) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -94,7 +94,7 @@ class WebSocketProtocol(WebSocketServerProtocol): | ||||||
|             # so drop the connection. |             # so drop the connection. | ||||||
|             self.muted = True |             self.muted = True | ||||||
|             logger.warn("WebSocket force closed for %s due to connect backpressure", self.reply_channel) |             logger.warn("WebSocket force closed for %s due to connect backpressure", self.reply_channel) | ||||||
|             # Send code 1013 "try again later" with close. |             # Send code 503 "Service Unavailable" with close. | ||||||
|             raise ConnectionDeny(code=503, reason="Connection queue at capacity") |             raise ConnectionDeny(code=503, reason="Connection queue at capacity") | ||||||
|         else: |         else: | ||||||
|             self.factory.log_action("websocket", "connecting", { |             self.factory.log_action("websocket", "connecting", { | ||||||
|  | @ -187,11 +187,12 @@ class WebSocketProtocol(WebSocketServerProtocol): | ||||||
|         else: |         else: | ||||||
|             self.sendMessage(content.encode("utf8"), binary) |             self.sendMessage(content.encode("utf8"), binary) | ||||||
| 
 | 
 | ||||||
|     def serverClose(self): |     def serverClose(self, code=True): | ||||||
|         """ |         """ | ||||||
|         Server-side channel message to close the socket |         Server-side channel message to close the socket | ||||||
|         """ |         """ | ||||||
|         self.sendClose() |         code = 1000 if code is True else code | ||||||
|  |         self.sendClose(code=code) | ||||||
| 
 | 
 | ||||||
|     def onClose(self, wasClean, code, reason): |     def onClose(self, wasClean, code, reason): | ||||||
|         self.cleanup() |         self.cleanup() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user