def __init__(self, base_uri, method, query_string, output_hdrs): self.base_uri = base_uri self.method = method self._output_hdrs = output_hdrs self.output_body = None self.body_done = None self.test_uri = None self.req_hdrs = None self.format = None self.test_id = None self.descend = None self.save = None self.parse_qs(method, query_string) self.start = time.time() self.timeout = nbhttp.schedule(max_runtime, self.timeoutError) if self.save and save_dir and self.test_id: self.save_test() elif self.test_id: self.load_saved_test() elif self.test_uri: self.run_test() else: self.show_default()
def __init__(self, test_id, test_uri, req_hdrs, base_uri, format, output_hdrs, output_body, descend=False, save=False): self.output_body = output_body self.start = time.time() timeout = nbhttp.schedule(max_runtime, self.timeoutError) if save and save_dir and test_id: try: os.utime( os.path.join(save_dir, test_id), ( nbhttp.now(), nbhttp.now() + (save_days * 24 * 60 * 60) ) ) location = "?id=%s" % test_id if descend: location = "%s&descend=True" % location output_hdrs("303 See Other", [ ("Location", location) ]) output_body("Redirecting...") except (OSError, IOError): output_hdrs("500 Internal Server Error", [ ("Content-Type", "text/html; charset=%s" % charset), ]) # TODO: better error message (through formatter?) output_body(error_template % "Sorry, I couldn't save that.") elif test_id: try: test_id = os.path.basename(test_id) fd = gzip.open(os.path.join(save_dir, test_id)) mtime = os.fstat(fd.fileno()).st_mtime except (OSError, IOError, zlib.error): output_hdrs("404 Not Found", [ ("Content-Type", "text/html; charset=%s" % charset), ("Cache-Control", "max-age=600, must-revalidate") ]) # TODO: better error page (through formatter?) self.output_body(error_template % "I'm sorry, I can't find that saved response." ) timeout.delete() return is_saved = mtime > nbhttp.now() try: ired = pickle.load(fd) except (pickle.PickleError, EOFError): output_hdrs("500 Internal Server Error", [ ("Content-Type", "text/html; charset=%s" % charset), ("Cache-Control", "max-age=600, must-revalidate") ]) # TODO: better error page (through formatter?) self.output_body(error_template % "I'm sorry, I had a problem reading that response." ) timeout.delete() return finally: fd.close() formatter = find_formatter(format, 'html', descend)( base_uri, ired.uri, ired.orig_req_hdrs, lang, self.output, allow_save=(not is_saved), is_saved=True, test_id=test_id ) output_hdrs("200 OK", [ ("Content-Type", "%s; charset=%s" % ( formatter.media_type, charset)), ("Cache-Control", "max-age=3600, must-revalidate") ]) formatter.start_output() formatter.finish_output(ired) elif test_uri: if save_dir and os.path.exists(save_dir): try: fd, path = tempfile.mkstemp(prefix='', dir=save_dir) test_id = os.path.split(path)[1] except (OSError, IOError): # Don't try to store it. test_id = None else: test_id = None formatter = find_formatter(format, 'html', descend)( base_uri, test_uri, req_hdrs, lang, self.output, allow_save=test_id, is_saved=False, test_id=test_id, descend=descend ) output_hdrs("200 OK", [ ("Content-Type", "%s; charset=%s" % ( formatter.media_type, charset)), ("Cache-Control", "max-age=60, must-revalidate") ]) formatter.start_output() ired = droid.InspectingResourceExpertDroid( test_uri, req_hdrs=req_hdrs, status_cb=formatter.status, body_procs=[formatter.feed], descend=descend ) formatter.finish_output(ired) if test_id: try: tmp_file = gzip.open(path, 'w') pickle.dump(ired, tmp_file) tmp_file.close() except (IOError, zlib.error, pickle.PickleError): pass # we don't cry if we can't store it. else: # no test_uri formatter = html.BaseHtmlFormatter( base_uri, test_uri, req_hdrs, lang, self.output) output_hdrs("200 OK", [ ("Content-Type", "%s; charset=%s" % ( formatter.media_type, charset) ), ("Cache-Control", "max-age=300") ]) formatter.start_output() formatter.finish_output(None) timeout.delete()