Beispiel #1
0
def request(method, uuid, body=None):
    log.debug("Sending request method=%r, ticket=%r, body=%r",
              method, uuid, body)
    con = uhttp.UnixHTTPConnection(DAEMON_SOCK)
    with closing(con):
        try:
            con.request(method, "/tickets/%s" % uuid, body=body)
            res = con.getresponse()
        except (httplib.HTTPException, EnvironmentError) as e:
            raise se.ImageTicketsError("Error communicating with "
                                       "ovirt-imageio-daemon: "
                                       "{error}".format(error=e))

        if res.status >= 300:
            try:
                content_length = int(res.getheader("content-length",
                                                   default=""))
            except ValueError as e:
                error_info = {"explanation": "Invalid content-length",
                              "detail": str(e)}
                raise se.ImageDaemonError(res.status, res.reason, error_info)

            try:
                res_data = res.read(content_length)
            except EnvironmentError as e:
                error_info = {"explanation": "Error reading response",
                              "detail": str(e)}
                raise se.ImageDaemonError(res.status, res.reason, error_info)

            try:
                error_info = json.loads(res_data)
            except ValueError as e:
                error_info = {"explanation": "Invalid JSON", "detail": str(e)}
            raise se.ImageDaemonError(res.status, res.reason, error_info)
Beispiel #2
0
def _read_content(response):
    try:
        content_length = int(response.getheader("content-length", default=""))
    except ValueError as e:
        error_info = {
            "explanation": "Invalid content-length",
            "detail": str(e)
        }
        raise se.ImageDaemonError(response.status, response.reason, error_info)

    if content_length == 0:
        return {}

    try:
        res_data = response.read(content_length)
    except EnvironmentError as e:
        error_info = {
            "explanation": "Error reading response",
            "detail": str(e)
        }
        raise se.ImageDaemonError(response.status, response.reason, error_info)

    try:
        return json.loads(res_data.decode("utf8"))
    except ValueError as e:
        error_info = {"explanation": "Invalid JSON", "detail": str(e)}
        raise se.ImageDaemonError(response.status, response.reason, error_info)
Beispiel #3
0
def get_ticket(ticket_id):
    response, content = _request("GET", ticket_id)
    try:
        return json.loads(content)
    except ValueError as e:
        error_info = {"explanation": "Invalid JSON", "detail": str(e)}
        raise se.ImageDaemonError(response.status, response.reason, error_info)
Beispiel #4
0
def _read_content(response):
    # We must consume the entire response, otherwise we cannot use keep-alive
    # connections. HTTPResponse.read() is doing the right thing, handling
    # request content length or chunked encoding.
    try:
        res_data = response.read()
    except EnvironmentError as e:
        error_info = {"explanation": "Error reading response",
                      "detail": str(e)}
        raise se.ImageDaemonError(response.status, response.reason, error_info)

    # This can be a "200 OK" with "Content-Length: 0", or "204 No Content"
    # without Content-Length header.
    # See https://tools.ietf.org/html/rfc7230#section-3.3.2
    if not res_data:
        return {}

    try:
        return json.loads(res_data.decode("utf8"))
    except ValueError as e:
        error_info = {"explanation": "Invalid JSON", "detail": str(e)}
        raise se.ImageDaemonError(response.status, response.reason, error_info)
Beispiel #5
0
def _read_content(response):
    # We must consume the entire response, otherwise we cannot use keep-alive
    # connections. HTTPResponse.read() is doing the right thing, handling
    # request content length or chunked encoding.
    try:
        # This can also be a "200 OK" with "Content-Length: 0",
        # or "204 No Content" without Content-Length header.
        # See https://tools.ietf.org/html/rfc7230#section-3.3.2
        return response.read()
    except EnvironmentError as e:
        error_info = {
            "explanation": "Error reading response",
            "detail": str(e)
        }
        raise se.ImageDaemonError(response.status, response.reason, error_info)
Beispiel #6
0
def request(method, uuid, body=None):
    log.debug("Sending request method=%r, ticket=%r, body=%r", method, uuid,
              body)
    con = uhttp.UnixHTTPConnection(DAEMON_SOCK)
    with closing(con):
        try:
            con.request(method, "/tickets/%s" % uuid, body=body)
            res = con.getresponse()
        except (http_client.HTTPException, EnvironmentError) as e:
            raise se.ImageTicketsError("Error communicating with "
                                       "ovirt-imageio-daemon: "
                                       "{error}".format(error=e))

        if res.status >= 300:
            content = _read_content(res)
            raise se.ImageDaemonError(res.status, res.reason, content)
Beispiel #7
0
def _request(method, uuid, body=None):
    log.debug("Sending request method=%r, ticket=%r, body=%r", method, uuid,
              body)
    if body is not None:
        body = body.encode("utf8")
    con = UnixHTTPConnection(DAEMON_SOCK)
    with closing(con):
        try:
            con.request(method, "/tickets/%s" % uuid, body=body)
            res = con.getresponse()
        except (http_client.HTTPException, EnvironmentError) as e:
            raise se.ImageTicketsError("Error communicating with "
                                       "ovirt-imageio-daemon: "
                                       "{error}".format(error=e))

        content = _read_content(res)
        if res.status >= 300:
            try:
                error = content.decode("utf8")
            except (UnicodeDecodeError, LookupError):
                error = repr(content)
            raise se.ImageDaemonError(res.status, res.reason, error)
        return res, content