How to force 204 response for captive portal detection?

361 views
Skip to first unread message

Alexander Krause

unread,
Sep 2, 2015, 2:44:56 AM9/2/15
to cherrypy-users
Hi all!

Does anybody know how to force cherrypy 3.8.0 to generate an empty 204 response? This seems to be required by some captive-portal detection methods like in Android 4.

I tried different things:
#gives 303 See Other
if '/generate_204' in uri:
    cherrypy.response.status = '204'
  raise cherrypy.HTTPRedirect('/')

#complains about error code < 300
if '/generate_204' in uri:
  raise cherrypy.HTTPError(204)


Thanks a lot for any hints!

Sylvain Hellegouarch

unread,
Sep 2, 2015, 3:23:23 AM9/2/15
to cherryp...@googlegroups.com
Hi Alexander,

I think you have to make a decision, either you wish to redirect (303) or respond with an empty content (204). So it's either: 

@cherrypy.expose
def handler(self):
     cherrypy.response.status = '204'

     # not mandatory, just a hint for the client. 
     cherrypy.response.headers['Content-Location'] = '/'



OR


@cherrypy.expose
def handler(self):
     raise cherrypy.HTTPRedirect('/')

--

Alexander Krause

unread,
Sep 2, 2015, 3:57:24 AM9/2/15
to cherrypy-users
An empty 204 would be perfect. Unfortunately I'm doing this in a custom cherrypy-tool:

def global_redirect(urls=None):
if urls:
uri=cherrypy.url()
print(uri)
if '/generate_204' in uri:
cherrypy.response.status = '204'
cherrypy.response.headers['Content-Location'] = '/'
cherrypy.tools.global_redirect = cherrypy.Tool(
'before_handler',
global_redirect,
priority=20
)


Do you know how I need to exit from here? Normally I've to raise an Exception:
raise cherrypy.HTTPRedirect(...)

Alexander Krause

unread,
Sep 2, 2015, 5:00:04 AM9/2/15
to cherrypy-users
Code hier eingeben...



Am Mittwoch, 2. September 2015 08:44:56 UTC+2 schrieb Alexander Krause:

Alexander Krause

unread,
Sep 2, 2015, 5:00:43 AM9/2/15
to cherrypy-users
I guess the simpliest way is to define a custom exception:
class Error204(cherrypy.HTTPError):
def __init__(self, message=None):
self.status = 204
self._message = message
cherrypy.CherryPyException.__init__(self, 204, message)



Am Mittwoch, 2. September 2015 08:44:56 UTC+2 schrieb Alexander Krause:

Sylvain Hellegouarch

unread,
Sep 2, 2015, 9:21:30 AM9/2/15
to cherryp...@googlegroups.com
Or, perhaps, in that case a dedicated dispatcher instead of a tool?



--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.
To post to this group, send email to cherryp...@googlegroups.com.
Visit this group at http://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages