def handle_cert_install(self, env): if env['pywb.proxy_req_uri'] in ('/', '/index.html', '/index.html'): available = (self.ca is not None) if self.proxy_cert_dl_view: return (self.proxy_cert_dl_view. render_response(available=available, pem_path=self.CERT_DL_PEM, p12_path=self.CERT_DL_P12)) elif env['pywb.proxy_req_uri'] == self.CERT_DL_PEM: if not self.ca: return None buff = '' with open(self.ca.ca_file, 'rb') as fh: buff = fh.read() content_type = 'application/x-x509-ca-cert' return WbResponse.text_response(buff, content_type=content_type) elif env['pywb.proxy_req_uri'] == self.CERT_DL_P12: if not self.ca: return None buff = self.ca.get_root_PKCS12() content_type = 'application/x-pkcs12' return WbResponse.text_response(buff, content_type=content_type)
def handle_cert_install(self, env): if env['pywb.proxy_req_uri'] in ('/', '/index.html', '/index.html'): available = (self.ca is not None) if self.proxy_cert_dl_view: return (self.proxy_cert_dl_view.render_response( available=available, pem_path=self.CERT_DL_PEM, p12_path=self.CERT_DL_P12)) elif env['pywb.proxy_req_uri'] == self.CERT_DL_PEM: if not self.ca: return None buff = '' with open(self.ca.ca_file) as fh: buff = fh.read() content_type = 'application/x-x509-ca-cert' return WbResponse.text_response(buff, content_type=content_type) elif env['pywb.proxy_req_uri'] == self.CERT_DL_P12: if not self.ca: return None buff = self.ca.get_root_PKCS12() content_type = 'application/x-pkcs12' return WbResponse.text_response(buff, content_type=content_type)
def render_home_page(self): # render the homepage! if self.home_view: return self.home_view.render_response(routes = self.routes) else: # default home page template text = '\n'.join(map(str, self.routes)) return WbResponse.text_response(text)
def render_home_page(self, env): # render the homepage! if self.home_view: return self.home_view.render_response(env=env, routes=self.routes) else: # default home page template text = '\n'.join(map(str, self.routes)) return WbResponse.text_response(text)
def handle_exception(self, env, exc, print_trace): error_view = None if hasattr(self.wb_router, "error_view"): error_view = self.wb_router.error_view if hasattr(exc, "status"): status = exc.status() else: status = "500 Internal Server Error" if hasattr(exc, "url"): err_url = exc.url else: err_url = None err_msg = exc.message if print_trace: import traceback err_details = traceback.format_exc(exc) print err_details else: logging.info(err_msg) err_details = None if error_view: if err_url and isinstance(err_url, str): err_url = err_url.decode("utf-8", "ignore") if err_msg and isinstance(err_msg, str): err_msg = err_msg.decode("utf-8", "ignore") return error_view.render_response( exc_type=type(exc).__name__, err_msg=err_msg, err_details=err_details, status=status, env=env, err_url=err_url, ) else: msg = status + " Error: " if err_msg: msg += err_msg msg = msg.encode("utf-8", "ignore") return WbResponse.text_response(msg, status=status)
def handle_exception(self, env, exc, print_trace): error_view = None if hasattr(self.wb_router, 'error_view'): error_view = self.wb_router.error_view if hasattr(exc, 'status'): status = exc.status() else: status = '400 Bad Request' if hasattr(exc, 'url'): err_url = exc.url else: err_url = None err_msg = exc.message if print_trace: import traceback err_details = traceback.format_exc(exc) print err_details else: logging.info(err_msg) err_details = None if error_view: if err_url and isinstance(err_url, str): err_url = err_url.decode('utf-8', 'ignore') if err_msg and isinstance(err_msg, str): err_msg = err_msg.decode('utf-8', 'ignore') return error_view.render_response(exc_type=type(exc).__name__, err_msg=err_msg, err_details=err_details, status=status, env=env, err_url=err_url) else: msg = status + ' Error: ' if err_msg: msg += err_msg msg = msg.encode('utf-8', 'ignore') return WbResponse.text_response(msg, status=status)
def make_pac_response(self, env): server_hostport = env['SERVER_NAME'] + ':' + env['SERVER_PORT'] buff = 'function FindProxyForURL (url, host) {\n' direct_cond =' if (shExpMatch(host, "{0}")) {{ return "DIRECT"; }}\n' for hostpath in self.hostpaths: parts = urlparse.urlsplit(hostpath).netloc.split(':') buff += direct_cond.format(parts[0]) buff += direct_cond.format(env['SERVER_NAME']) #buff += '\n return "PROXY {0}";\n}}\n'.format(self.hostpaths[0]) buff += '\n return "PROXY {0}";\n}}\n'.format(server_hostport) return WbResponse.text_response(buff, content_type = 'application/x-ns-proxy-autoconfig')
def handle_exception(self, env, exc, print_trace): error_view = None if hasattr(self.wb_router, 'error_view'): error_view = self.wb_router.error_view if hasattr(exc, 'status'): status = exc.status() else: status = '500 Internal Server Error' if hasattr(exc, 'url'): err_url = exc.url else: err_url = None err_msg = exc.message if print_trace: import traceback err_details = traceback.format_exc(exc) print err_details else: logging.info(err_msg) err_details = None if error_view: if err_url and isinstance(err_url, str): err_url = err_url.decode('utf-8', 'ignore') if err_msg and isinstance(err_msg, str): err_msg = err_msg.decode('utf-8', 'ignore') return error_view.render_response(exc_type=type(exc).__name__, err_msg=err_msg, err_details=err_details, status=status, env=env, err_url=err_url) else: msg = status + ' Error: ' if err_msg: msg += err_msg msg = msg.encode('utf-8', 'ignore') return WbResponse.text_response(msg, status=status)
def handle_exception(env, error_view, exc, print_trace): if hasattr(exc, 'status'): status = exc.status() else: status = '400 Bad Request' if print_trace: import traceback err_details = traceback.format_exc(exc) print err_details else: logging.info(str(exc)) err_details = None if error_view: import traceback return error_view.render_response(err_msg = str(exc), err_details = err_details, status = status) else: return WbResponse.text_response(status + ' Error: ' + str(exc), status = status)
def handle_magic_page(self, env): coll, ts = self.get_proxy_coll_ts(env) ip = self._get_ip(env) res = json.dumps({'ip': ip, 'coll': coll, 'ts': ts}) return WbResponse.text_response(res, content_type='application/json')
def handle_connect(self, env): sock = self.get_request_socket(env) if not sock: return WbResponse.text_response('HTTPS Proxy Not Supported', '405 HTTPS Proxy Not Supported') sock.send('HTTP/1.0 200 Connection Established\r\n') sock.send('Server: pywb proxy\r\n') sock.send('\r\n') hostname, port = env['REL_REQUEST_URI'].split(':') if not self.use_wildcard: _, certfile = self.ca.get_cert_for_host(hostname) else: certfile = self.ca.get_wildcard_cert(hostname) try: ssl_sock = ssl.wrap_socket(sock, server_side=True, certfile=certfile, #ciphers="ALL", suppress_ragged_eofs=False, ssl_version=ssl.PROTOCOL_SSLv23 ) env['pywb.proxy_ssl_sock'] = ssl_sock buffreader = BufferedReader(ssl_sock, block_size=self.BLOCK_SIZE) statusline = buffreader.readline().rstrip() except Exception as se: raise BadRequestException(se.message) statusparts = statusline.split(' ') if len(statusparts) < 3: raise BadRequestException('Invalid Proxy Request: ' + statusline) env['REQUEST_METHOD'] = statusparts[0] env['REL_REQUEST_URI'] = ('https://' + env['REL_REQUEST_URI'].replace(':443', '') + statusparts[1]) env['SERVER_PROTOCOL'] = statusparts[2].strip() env['pywb.proxy_scheme'] = 'https' env['pywb.proxy_host'] = hostname env['pywb.proxy_port'] = port env['pywb.proxy_req_uri'] = statusparts[1] queryparts = env['REL_REQUEST_URI'].split('?', 1) env['PATH_INFO'] = queryparts[0] env['QUERY_STRING'] = queryparts[1] if len(queryparts) > 1 else '' while True: line = buffreader.readline() if line: line = line.rstrip() if not line: break parts = line.split(':', 1) if len(parts) < 2: continue name = parts[0].strip() value = parts[1].strip() name = name.replace('-', '_').upper() if name not in ('CONTENT_LENGTH', 'CONTENT_TYPE'): name = 'HTTP_' + name env[name] = value remain = buffreader.rem_length() if remain > 0: remainder = buffreader.read(self.BLOCK_SIZE) env['wsgi.input'] = BufferedReader(ssl_sock, block_size=self.BLOCK_SIZE, starting_data=remainder)
def render_home_page(self, env): if self.home_view: return self.home_view.render_response(env=env, routes=self.routes) else: return WbResponse.text_response('No Home Page')
def render_search_page(self, wbrequest): if self.search_view: return self.search_view.render_response(wbrequest=wbrequest) else: return WbResponse.text_response("No Lookup Url Specified")
def __call__(self, wbrequest): return WbResponse.text_response(str(wbrequest))
def handle_connect(self, env): sock = self.get_request_socket(env) if not sock: return WbResponse.text_response('HTTPS Proxy Not Supported', '405 HTTPS Proxy Not Supported') sock.send('HTTP/1.0 200 Connection Established\r\n') sock.send('Server: pywb proxy\r\n') sock.send('\r\n') hostname, port = env['REL_REQUEST_URI'].split(':') if not self.use_wildcard: _, certfile = self.ca.get_cert_for_host(hostname) else: certfile = self.ca.get_wildcard_cert(hostname) try: ssl_sock = ssl.wrap_socket( sock, server_side=True, certfile=certfile, #ciphers="ALL", suppress_ragged_eofs=False, ssl_version=ssl.PROTOCOL_SSLv23) env['pywb.proxy_ssl_sock'] = ssl_sock buffreader = BufferedReader(ssl_sock, block_size=self.BLOCK_SIZE) statusline = buffreader.readline().rstrip() except Exception as se: raise BadRequestException(se.message) statusparts = statusline.split(' ') if len(statusparts) < 3: raise BadRequestException('Invalid Proxy Request: ' + statusline) env['REQUEST_METHOD'] = statusparts[0] env['REL_REQUEST_URI'] = ('https://' + env['REL_REQUEST_URI'].replace(':443', '') + statusparts[1]) env['SERVER_PROTOCOL'] = statusparts[2].strip() env['pywb.proxy_scheme'] = 'https' env['pywb.proxy_host'] = hostname env['pywb.proxy_port'] = port env['pywb.proxy_req_uri'] = statusparts[1] queryparts = env['REL_REQUEST_URI'].split('?', 1) env['PATH_INFO'] = queryparts[0] env['QUERY_STRING'] = queryparts[1] if len(queryparts) > 1 else '' while True: line = buffreader.readline() if line: line = line.rstrip() if not line: break parts = line.split(':', 1) if len(parts) < 2: continue name = parts[0].strip() value = parts[1].strip() name = name.replace('-', '_').upper() if not name in ('CONTENT_LENGTH', 'CONTENT_TYPE'): name = 'HTTP_' + name env[name] = value remain = buffreader.rem_length() if remain > 0: remainder = buffreader.read(self.BLOCK_SIZE) env['wsgi.input'] = BufferedReader(ssl_sock, block_size=self.BLOCK_SIZE, starting_data=remainder)