status.py (1299B)
1 from wptserve.utils import isomorphic_encode 2 3 def main(request, response): 4 response.headers.set(b"Access-Control-Allow-Origin", request.headers.get(b"origin")) 5 response.headers.set(b"Access-Control-Expose-Headers", b"X-Request-Method") 6 7 if request.method == u'OPTIONS': 8 response.headers.set(b"Access-Control-Allow-Methods", b"GET, CHICKEN, HEAD, POST, PUT") 9 10 if b'headers' in request.GET: 11 response.headers.set(b"Access-Control-Allow-Headers", request.GET.first(b'headers')) 12 13 response.headers.set(b"X-Request-Method", isomorphic_encode(request.method)) 14 15 response.headers.set(b"X-A-C-Request-Method", request.headers.get(b"Access-Control-Request-Method", b"")) 16 17 18 #This should reasonably work for most response codes. 19 try: 20 code = int(request.GET.first(b"code", 200)) 21 except ValueError: 22 code = 200 23 24 text = request.GET.first(b"text", b"OMG") 25 26 if request.method == u"OPTIONS" and b"preflight" in request.GET: 27 try: 28 code = int(request.GET.first(b'preflight')) 29 except KeyError: 30 pass 31 32 status = code, text 33 34 if b"type" in request.GET: 35 response.headers.set(b"Content-Type", request.GET.first(b'type')) 36 37 body = request.GET.first(b'content', b"") 38 39 return status, [], body