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 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))
def parse_request(self): if not BaseHTTPRequestHandler.parse_request(self): return False # grab the loginuid from `/proc` as soon as possible creds = self.peer_creds if creds is not None: self.loginuid = self._get_loginuid(creds['pid']) # after basic parsing also use urlparse to retrieve individual # elements of a request. url = urlparse(self.path) # Yes, override path with the path part only self.path = url.path self.path_chain = self._parse_path(url) # Create dict out of query self.query = parse_qs(url.query) # keep the rest into the 'url' element in case someone needs it self.url = url return True
def __init__(self, srvurl, config): url = urlparse(srvurl) serverclass, address = self._get_serverclass(url) if sd is not None: address = self._get_systemd_socket(address) self.httpd = serverclass(address, self.handler, config)