# -*- encoding: utf-8 -*- import urlparse from cStringIO import StringIO import time def html_esc(s): return s.replace('&','&').replace('<','<').replace('>','>') def do_response(req, body, code=200, headers={}): req.send_response(code) req.send_header('Content-Length', str(len(body))) if 'Content-Type' not in headers: req.send_header('Content-Type', 'text/html') for k, v in headers.items(): if k != 'Content-Length': req.send_header(k, v) req.end_headers() return StringIO(body) def do_redirect(req, target): return do_response(req, 'Go here'.format(target), code=302, headers={ 'Location': target }) def handle_request(req): url = urlparse.urlparse(req.path) # This handler returns one of several different documents, # depending on the query string. Many of the URLs involved contain # text encoded in Shift_JIS, and will not round-trip correctly if # misinterpreted as UTF-8. Comments indicate the Unicode equivalent. if url.query == '/': return do_redirect(req, '?/%83y%81[%83W') elif url.query == '/f': return do_response(req, '' 'framed' '' '' '' '') elif url.query == "/r": return do_response(req, '') elif url.query == "/re": return do_response(req, '' '' '') elif url.query == "/%83y%81[%83W": # ページ return do_response(req, '

PASS

') elif url.query == "/%98g": # 枠 return do_response(req, '

PASS

') elif url.query == "/%95s%96%D1%82%C8%98_%91%88": # 不毛な論争 return do_response(req, '

FRAME

') elif url.query == "/%8F%91": # 書 return do_response(req, 'window.onload=function(){' 'document.body.innerHTML="

PASS

";};', headers={'Content-Type': 'application/javascript'}) elif url.query == "/%8C%CC%8F%E1": # 故障 return do_response(req, 'internal server error', code=500) elif url.query == "/%89i%8Bv": # 永久 time.sleep(5) return do_response(req, '', code=204) else: return do_response(req, '404 Not Found' '

URL not found: {}

' .format(html_esc(req.path)), code=404)