Wednesday, January 23, 2019

Re: Channels 2.0 close code and http response status

I can't get it to load mysql on my centos system ?

On Wed, Jan 23, 2019, 9:46 AM <guillaume@sparkhorizon.com wrote:
Hey I read the spec of websocket handshake and I believe that in abrupt close connection, the server can send either a 403 or 404. I wish there were a way to send a 404. :(

On Sunday, June 24, 2018 at 7:21:10 PM UTC+2, Andrew Godwin wrote:
I'm not quite sure - WebSockets do have browser differences. You could also try adding a delay between the accept and the close to see if that changes how they handle it.

Andrew

On Sun, 24 Jun 2018, 06:02 Kirill Tyushchakov, <kirth...@gmail.com> wrote:
Hi, Andrew! Thanks a lot for your response.
I've changed my consumer's connect method like this:
def connect(self):
    user
= self.scope.get('user')
   
super(NotificationsConsumer, self).connect()
   
if user.is_anonymous:
       
self.close(code=4003)


I've tested it in Chrome, Firefox, Safari and IE 11 and here is the result that I've got:
Chrome - 1006
IE 11 - 1005
Firefox - 4003
Safari - 1006

It doesn't return 403 HTTP status code. Seems it works fine, but I think I need to test it more.
So the expected close code appears only in Firefox. Should I consider it as the browser issue?


Hi!

Close code 1006 means that the connection was closed abruptly by the other end (basically, the socket just got chopped off without an actual close frame being sent).

This is what happens when you close during the handshake, which is what closing during connect() does - at that point, the connection is still HTTP before the upgrade has finished, and so Channels sends a HTTP 403 response code down instead. Looks like your browser interprets this as code 1006 for a websocket.

There's no way to change the HTTP response code sent when a handshake is terminated at the moment - it's hardcoded (as you can see in Daphne's serverReject method, here: https://github.com/django/daphne/blob/master/daphne/ws_protocol.py#L200).

Given that no browser I know of will actually tell you the HTTP response code in this case, I don't think there's much value in letting it be changed - you're always going to see an aborted WebSocket connection code instead. If you want to provide more detail to the user, you can instead accept the socket, letting the handshake finish, and then close it immediately with a custom WebSocket close code (you can do all of that inside connect, I think).

Andrew

On Thu, Jun 21, 2018 at 2:52 PM Kirill Tyushchakov <kirth...@gmail.com> wrote:
Hello everyone!
I'm new in Django Channels. I'm using Channels 2.0 and I have few questions about it.
I'm using JsonWebsocketConsumer class for my consumer and I wrote my definition of connect method like this: 
def connect(self):
    user
= self.scope.get('user')
   
if user.is_anonymous:
       
super(NotificationsConsumer, self).connect()
   
else:
       
self.close(code=4003)

On client side I'm using native JS Websocket. 
But when I try to connect to this socket as unauthorzied user I get code 1006 and HTTP response status code 403.

My questions is:
1) How can I send custom close code to client?
2) Can I send another HTTP response status code? In my case 401 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/446d3be6-438b-4e59-ad4f-7841895f4fcb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b758b6f7-91f0-43b8-8ea8-087c137598c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e12469fa-02d5-45ff-a8c1-bab77124f570%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGNKn_Cc__MUhyTsC6TCAfy%2BSZoQJg5JHAgQn-68jFPhk3KJLg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment