def test_varargs(req, *args): var_info = {'len': len(args), 'uri_rule': req.uri_rule} var_info.update(req.groups) buff = get_header("Variable args test") + \ (get_crumbnav(req), "<h2>Variables</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, html(val)) for key, val in var_info.items()) + \ ("</table>", "<h2>Browser Headers</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, val) for key, val in req.headers.items()) + \ ("</table>", "<h2>Request Variables </h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, val) for key, val in get_variables(req)) + \ ("</table>",) + \ get_footer() response = Response() for line in buff: response.write(line + '\n') return response
def test_dynamic(req, variable=None): var_info = { 'type': type(variable), 'value': variable, 'uri_rule': req.uri_rule } title = "Variable" if variable is not None else "Static" buff = get_header(title + " test") + \ (get_crumbnav(req), "<h2>Variable</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, html(val)) for key, val in var_info.items()) + \ ("</table>", "<h2>Browser Headers</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, val) for key, val in req.headers.items()) + \ ("</table>", "<h2>Request Variables </h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, val) for key, val in get_variables(req)) + \ ("</table>",) + \ get_footer() response = Response() for line in buff: response.write(line + '\n') return response
def test_upload(req): var_info = OrderedDict(( ('form_keys', req.form.keys()), ('form_value_names', ', '.join(tuple(req.form[key].name for key in req.form.keys()))), ('form_value_types', ', '.join(tuple(req.form[key].type for key in req.form.keys()))), ('form_value_fnames', ', '.join( tuple(str(req.form[key].filename) for key in req.form.keys()))), ('form_value_lenghts', ', '.join(tuple(str(req.form[key].length) for key in req.form.keys()))), ('form_value_files', ', '.join(tuple(str(req.form[key].file) for key in req.form.keys()))), ('form_value_lists', ', '.join( tuple('Yes' if req.form[key].list else 'No' for key in req.form.keys()))), )) files = [] for key in req.form.keys(): if req.form[key].filename: files.append("<h2>%s</h2>" % req.form[key].filename) files.append("<i>%s</i>" % req.form[key].type) if req.form[key].type.startswith('text/'): files.append("<pre>%s</pre>" % html(req.form.getvalue(key).decode('utf-8'))) else: files.append("<pre>%s</pre>" % encodebytes(req.form.getvalue(key)).decode()) os.remove("./upload/%s" % (req.form[key].filename)) buff = get_header('HTTP file upload test') + \ (get_crumbnav(req), "<h2>Upload Form</h2>", '<form method="post" enctype="multipart/form-data">', '<input type="file" name="file_0"><br/>', '<input type="file" name="file_1"><br/>', '<input type="file" name="file_2"><br/>', '<input type="submit" name="btn" value="Upload">' '</form>', "<h2>Uploaded File</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, html(val)) for key, val in var_info.items()) + \ ("</table>",) + \ tuple(files) + \ get_footer() response = Response() for line in buff: response.write(line + '\n') return response
def not_found(req): buff = ( "<html>", "<head>", '<meta http-equiv="content-type" content="text/html; charset=utf-8"/>', "<title>404 - Page Not Found</title>", '<link rel="stylesheet" href="/style.css">', "</head>", "<body>", "<h1>404 - Page Not Found</h1>", get_crumbnav(req), "<p>Your reqeuest <code>%s</code> was not found.</p>" % req.uri, ) + get_footer() response = Response(status_code=state.HTTP_NOT_FOUND) for line in buff: response.write(line + '\n') return response
def root(req): buff = get_header("Index") + ( get_crumbnav(req), "<ul>", '<li><a href="%s">/</a> - This Page</li>' % req.construct_url('/'), '<li><a href="/test/static">/test/static</a> - Testing Static Page' '</li>', '<li><a href="/test/42">/test/<variable:int></a>' ' - Testing regular:int Page</li>', '<li><a href="/test/3.14">/test/<variable:float></a>' ' - Testing regular:float Page</li>', '<li><a href="/test/word">/test/<variable:word></a>' ' - Testing regular:word Page</li>', '<li><a href="/test/[email protected]">/test/<variable:user></a>' ' - Testing regular:user Page</li>', '<li><a href="/test/[grr]">/test/<variable:re:.*></a>' ' - Testing regular:re Page</li>', '<li><a href="/test/one/too/three">' '/test/<variable0>/<variable1>/<variable2></a>' ' - Testing variable args</li>', '<li><a href="/test/headers">/test/headers</a> - Testing Headers' '</li>', '<li><a href="/login">/login</a> - Create login session</li>', '<li><a href="/logout">/logout</a> - Destroy login session</li>', '<li><a href="/test/form">/test/form</a>' ' - Testing http form (only if you have login cookie / session)</li>', '<li><a href="/test/upload">/test/upload</a> - ' 'Testing file upload (only if you have login cookie / session)</li>', '<li><a href="/debug-info">/debug-info</a>' ' - Debug Page (only if poor_Debug is set)</li>', '<li><a href="/no-page">/no-page</a> - No Exist Page</li>', '<li><a href="/internal-server-error">/internal-server-error</a>' ' - Inernal Server Error</li>', "</ul>", ) + get_footer() response = Response() for line in buff: response.write(line + '\n') return response
def internal_server_error(req, **kwargs): """ More debug 500 Internal Server Error server handler. It was be called automatically when no handlers are not defined in dispatch_table.errors. If poor_Debug variable is to On, Tracaback will be generated. """ assert kwargs is not None exc_type, exc_value, exc_traceback = exc_info() traceback = format_exception(exc_type, exc_value, exc_traceback) traceback = ''.join(traceback) log.error(traceback) traceback = traceback.split('\n') res = Response(status_code=HTTP_INTERNAL_SERVER_ERROR) res.write("<!DOCTYPE html>\n" "<html>\n" " <head>\n" " <title>500 - Internal Server Error</title>\n" ' <meta http-equiv="content-type" ' 'content="text/html; charset=utf-8"/>\n' " <style>\n" " body {width: 80%; margin: auto; padding-top: 30px;}\n" " h1 {text-align: center; color: #707070;}\n" " pre .line1 {background: #e0e0e0}\n" " </style>\n" " </head>\n" " <body>\n" " <h1>500 - Internal Server Error</h1>\n") if req.debug: res.write(" <h2>Response detail</h2>\n" " remote host: <b><code>{req.remote_host}</code></b><br/>\n" " remote addr: <b><code>{req.remote_addr}</code></b><br/>\n" " method: <b><code>{req.method}</code></b><br/>\n" " uri: <b><code>{req.uri}</code></b><br/>\n" " uri_rule: <b><code>{req.uri_rule}</code></b><br/>\n" " uri_handler: <b><code>{uri_handler}</code></b><br/>\n" "".format(req=req, uri_handler=html_escape(str(req.uri_handler)))) res.write(" <h2>Exception Traceback</h2>\n" " <pre>\n") # Traceback for i, line in enumerate(traceback): traceback_line = html_escape(line) res.write('<span class="line%s">%s</span>\n' % (i % 2, traceback_line)) res.write( " </pre>\n" " <hr>\n" " <small><i>%s / Poor WSGI for Python ,webmaster: %s</i></small>" "\n" % (req.server_software, req.server_admin)) else: res.write(" <hr>\n" " <small><i>webmaster: %s </i></small>\n" % req.server_admin) # endif res.write(" </body>\n" "</html>") return res
def test_form(req): # get_var_info = {'len': len(args)} var_info = OrderedDict(( ('form_keys', req.form.keys()), ('form_values', ', '.join( tuple(str(req.form.getvalue(key)) for key in req.form.keys()))), ('form_getfirst', '%s,%s' % (req.form.getfirst('pname'), req.form.getfirst('px'))), ('form_getlist', '%s,%s' % (list(req.form.getlist('pname')), list(req.form.getlist('px')))), ('', ''), ('args_keys', req.args.keys()), ('args_values', ', '.join(tuple(str(req.args[key]) for key in req.args.keys()))), ('args_getfirst', '%s,%s' % (req.args.getfirst('gname'), req.args.getfirst('gx'))), ('args_getlist', '%s,%s' % (list(req.args.getlist('gname')), list(req.args.getlist('gx')))), )) buff = get_header("HTTP Form args test") + \ (get_crumbnav(req), "<h2>Get Form</h2>", '<form method="get">', '<input type="text" name="gname" value="Ondřej"><br/>', '<input type="text" name="gsurname" value="Tůma"><br/>', '<input type="text" name="gx" value="1">' '<input type="text" name="gx" value="2">' '<input type="text" name="gx" value="3"><br/>', '<input type="submit" name="btn" value="Send">' '</form>', "<h2>Post Form</h2>", '<form method="post">', '<input type="text" name="pname" value="Ondřej"><br/>', '<input type="text" name="psurname" value="Tůma"><br/>', '<input type="text" name="px" value="8">' '<input type="text" name="px" value="7">' '<input type="text" name="px" value="6"><br/>', '<input type="submit" name="btn" value="Send">' '</form>', "<h2>Variables</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, html(val)) for key, val in var_info.items()) + \ ("</table>", "<h2>Browser Headers</h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, val) for key, val in req.headers.items()) + \ ("</table>", "<h2>Request Variables </h2>", "<table>") + \ tuple("<tr><td>%s:</td><td>%s</td></tr>" % (key, val) for key, val in get_variables(req)) + \ ("</table>",) + \ get_footer() response = Response() for line in buff: response.write(line + '\n') return response