def _parse_path(self, url): path_chain = [] for segment in url.path.split('/'): # unquote URL path encoding segment = unquote(segment) path_chain.append(segment) return tuple(path_chain)
def check_socket(self): url = urlparse(self.socket_url) timeout = 0.5 if url.scheme == 'http+unix': address = unquote(url.netloc) s = socket.socket(socket.AF_UNIX) s.settimeout(timeout) try: s.connect(address) except OSError as e: self.fail("Server socket unavailable: {}".format(e)) finally: s.close() else: host, port = url.netloc.rsplit(":", 1) try: socket.create_connection((host, int(port)), timeout=timeout).close() except OSError as e: self.fail("Server socket unavailable: {}".format(e))
def _get_serverclass(self, url): if url.scheme == 'http+unix': # Unix socket address = unquote(url.netloc) if not address: raise ValueError('Empty address {}'.format(url)) logger.info('Serving on Unix socket %s', address) serverclass = ForkingUnixHTTPServer elif url.scheme == 'http': host, port = url.netloc.split(":") address = (host, int(port)) logger.info('Serving on %s (HTTP)', url.netloc) serverclass = ForkingHTTPServer elif url.scheme == 'https': host, port = url.netloc.split(":") address = (host, int(port)) logger.info('Serving on %s (HTTPS)', url.netloc) serverclass = ForkingTLSServer else: raise ValueError('Unknown URL Scheme: %s' % url.scheme) return serverclass, address
def error_message(args, exc): out = StringIO() parts = urlparse(args.server) if args.debug: traceback.print_exc(file=out) out.write('\n') out.write("ERROR: Custodia command '{args.sub} {args.name}' failed.\n") if args.verbose: out.write("Custodia server '{args.server}'.\n") if isinstance(exc, requests.exceptions.HTTPError): errcode = E_HTTP_ERROR out.write("{exc.__class__.__name__}: {exc}\n") elif isinstance(exc, requests.exceptions.ConnectionError): errcode = E_CONNECTION_ERROR if parts.scheme == 'http+unix': out.write("Failed to connect to Unix socket '{unix_path}':\n") else: out.write("Failed to connect to '{parts.netloc}' " "({parts.scheme}):\n") # ConnectionError always contains an inner exception out.write(" {exc.args[0]}\n") elif isinstance(exc, JSONDecodeError): errcode = E_JSON_ERROR out.write("Server returned invalid JSON response:\n") out.write(" {exc}\n") else: errcode = E_OTHER out.write("{exc.__class__.__name__}: {exc}\n") msg = out.getvalue() if not msg.endswith('\n'): msg += '\n' return errcode, msg.format(args=args, exc=exc, parts=parts, unix_path=unquote(parts.netloc))