Re: How to handle OPTIONS request in Cherrypy?
Edvin Beqari <[email protected]> Sun, 16 Jun 2019 18:57:09 -0700 (PDT)
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_1594_546353683.1560736629690
Content-Type: multipart/alternative;
boundary="----=_Part_1595_1316074327.1560736629691"
------=_Part_1595_1316074327.1560736629691
Content-Type: text/plain; charset="UTF-8"
I fixed the problem by changing the cors method with the one shown below
which I found online. It would be nice if someone can explain what sort of
majic is going on here .i.e.: what is a pahe handler in this case or
whatever.
def cors_tool():
'''
Handle both simple and complex CORS requests
Add CORS headers to each response. If the request is a CORS preflight
request swap out the default handler with a simple, single-purpose handler
that verifies the request and provides a valid CORS response.
'''
req_head = cherrypy.request.headers
resp_head = cherrypy.response.headers
# Always set response headers necessary for 'simple' CORS.
resp_head['Access-Control-Allow-Origin'] = req_head.get('Origin', '*')
resp_head['Access-Control-Expose-Headers'] = 'GET, POST'
resp_head['Access-Control-Allow-Credentials'] = 'true'
# Non-simple CORS preflight request; short-circuit the normal handler.
if cherrypy.request.method == 'OPTIONS':
ac_method = req_head.get('Access-Control-Request-Method', None)
allowed_methods = ['GET', 'POST']
allowed_headers = [
'Content-Type',
'X-Auth-Token',
'X-Requested-With',
]
if ac_method and ac_method in allowed_methods:
resp_head['Access-Control-Allow-Methods'] = ', '.join(allowed_methods)
resp_head['Access-Control-Allow-Headers'] = ', '.join(allowed_headers)
resp_head['Connection'] = 'keep-alive'
resp_head['Access-Control-Max-Age'] = '3600'
# CORS requests should short-circuit the other tools.
cherrypy.response.body = ''.encode('utf8')
cherrypy.response.status = 200
cherrypy.serving.request.handler = None
# Needed to avoid the auth_tool check.
if cherrypy.request.config.get('tools.sessions.on', False):
cherrypy.session['token'] = True
return True
--
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-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/5a4a35ab-cc9b-44de-bfb3-5157cbea0302%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
------=_Part_1595_1316074327.1560736629691
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I fixed the problem by changing the cors method with the o=
ne shown below which I found online. It would be nice if someone can explai=
n what sort of majic is going on here .i.e.: what is a pahe handler in this=
case or whatever.=C2=A0<div><br></div><div><div style=3D"color: rgb(212, 2=
12, 212); background-color: rgb(30, 30, 30); font-family: Consolas, "C=
ourier New", monospace; font-size: 14px; line-height: 19px; white-spac=
e: pre;"><div><span style=3D"color: #569cd6;">def</span> <span style=3D"col=
or: #dcdcaa;">cors_tool</span>():</div><div> <span style=3D"color: #ce91=
78;">'''</span></div><div><span style=3D"color: #ce9178;"> H=
andle both simple and complex CORS requests</span></div><div><span style=3D=
"color: #ce9178;"> </span></div><div><span style=3D"color: #ce9178;"> =
Add CORS headers to each response. If the request is a CORS preflight</span=
></div><div><span style=3D"color: #ce9178;"> request swap out the defaul=
t handler with a simple, single-purpose handler</span></div><div><span styl=
e=3D"color: #ce9178;"> that verifies the request and provides a valid CO=
RS response.</span></div><div><span style=3D"color: #ce9178;"> ''=
;'</span></div><div> req_head =3D cherrypy.request.headers</div><div=
> resp_head =3D cherrypy.response.headers</div><br><div> <span style=
=3D"color: #6a9955;"># Always set response headers necessary for 'simpl=
e' CORS.</span></div><div> resp_head[<span style=3D"color: #ce9178;"=
>'Access-Control-Allow-Origin'</span>] =3D req_head.get(<span style=
=3D"color: #ce9178;">'Origin'</span>, <span style=3D"color: #ce9178=
;">'*'</span>)</div><div> resp_head[<span style=3D"color: #ce917=
8;">'Access-Control-Expose-Headers'</span>] =3D <span style=3D"colo=
r: #ce9178;">'GET, POST'</span></div><div> resp_head[<span style=
=3D"color: #ce9178;">'Access-Control-Allow-Credentials'</span>] =3D=
<span style=3D"color: #ce9178;">'true'</span></div><div> </div><=
div> <span style=3D"color: #6a9955;"># Non-simple CORS preflight request=
; short-circuit the normal handler.</span></div><div> <span style=3D"col=
or: #c586c0;">if</span> cherrypy.request.method =3D=3D <span style=3D"color=
: #ce9178;">'OPTIONS'</span>:</div><div> ac_method =3D req_h=
ead.get(<span style=3D"color: #ce9178;">'Access-Control-Request-Method&=
#39;</span>, <span style=3D"color: #569cd6;">None</span>)</div><div> </div=
><div> allowed_methods =3D [<span style=3D"color: #ce9178;">'GET=
'</span>, <span style=3D"color: #ce9178;">'POST'</span>]</div><=
div> allowed_headers =3D [</div><div> <span style=3D"c=
olor: #ce9178;">'Content-Type'</span>,</div><div> <sp=
an style=3D"color: #ce9178;">'X-Auth-Token'</span>,</div><div> =
<span style=3D"color: #ce9178;">'X-Requested-With'</span>=
,</div><div> ]</div><div> </div><div> <span style=3D"color:=
#c586c0;">if</span> ac_method <span style=3D"color: #569cd6;">and</span> a=
c_method <span style=3D"color: #569cd6;">in</span> allowed_methods:</div><d=
iv> resp_head[<span style=3D"color: #ce9178;">'Access-Contro=
l-Allow-Methods'</span>] =3D <span style=3D"color: #ce9178;">', =
9;</span>.join(allowed_methods)</div><div> resp_head[<span style=
=3D"color: #ce9178;">'Access-Control-Allow-Headers'</span>] =3D <sp=
an style=3D"color: #ce9178;">', '</span>.join(allowed_headers)</div=
><div> </div><div> resp_head[<span style=3D"color: #ce9178;">&#=
39;Connection'</span>] =3D <span style=3D"color: #ce9178;">'keep-al=
ive'</span></div><div> resp_head[<span style=3D"color: #ce91=
78;">'Access-Control-Max-Age'</span>] =3D <span style=3D"color: #ce=
9178;">'3600'</span></div><div> </div><div> <span style=3D=
"color: #6a9955;"># CORS requests should short-circuit the other tools.</sp=
an></div><div> cherrypy.response.body =3D <span style=3D"color: #ce9=
178;">''</span>.encode(<span style=3D"color: #ce9178;">'utf8=
9;</span>)</div><div> cherrypy.response.status =3D <span style=3D"co=
lor: #b5cea8;">200</span></div><div> cherrypy.serving.request.handle=
r =3D <span style=3D"color: #569cd6;">None</span></div><div> </div><div> =
<span style=3D"color: #6a9955;"># Needed to avoid the auth_tool chec=
k.</span></div><div> <span style=3D"color: #c586c0;">if</span> cherr=
ypy.request.config.get(<span style=3D"color: #ce9178;">'tools.sessions.=
on'</span>, <span style=3D"color: #569cd6;">False</span>):</div><div> =
cherrypy.session[<span style=3D"color: #ce9178;">'token'<=
/span>] =3D <span style=3D"color: #569cd6;">True</span></div><div> <=
span style=3D"color: #c586c0;">return</span> <span style=3D"color: #569cd6;=
">True</span></div></div></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com">cherrypy-users-/[email protected]</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs">https://groups.google.com/group/cherrypy-users</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/cherrypy-users/5a4a35ab-cc9b-44de-bfb3-5157cbea0302%40googlegrou=
ps.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/=
msgid/cherrypy-users/5a4a35ab-cc9b-44de-bfb3-5157cbea0302%40googlegroups.co=
m</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />
------=_Part_1595_1316074327.1560736629691--
------=_Part_1594_546353683.1560736629690--