Esempio n. 1
0
File: qt.py Progetto: yssoe/splash
def reply2har(reply, content=None):
    """
    Serialize QNetworkReply to HAR.
    If ``content`` (a bytes object) is not None, 'content' field is filled.
    This function doesn't read reply to get the content because
    QNetworkReply content can be read only once.
    """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": reply.url().toString()
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if content_type is not None:
        res["content"]["mimeType"] = six.text_type(content_type)

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if content_length is not None:
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if status is not None:
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if status_text is not None:
        try:
            res["statusText"] = bytes(status_text, 'latin1').decode('latin1')
        except TypeError:
            res["statusText"] = bytes(status_text).decode('latin1')

    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if redirect_url is not None:
        res["redirectURL"] = six.text_type(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if content is not None:
        res["content"]["size"] = len(content)
        res["content"]["text"] = base64.b64encode(content).decode('latin1')
        res["content"]["encoding"] = 'base64'

    return res
Esempio n. 2
0
 def callback(reply):
     if reply.error():
         reason = REQUEST_ERRORS_SHORT.get(reply.error(), '?')
         self._return(cmd_id, None, reason)
     else:
         source = bytes(reply.readAll())
         self.tab.autoload(source)
         self._return(cmd_id, True)
Esempio n. 3
0
 def callback(reply):
     if reply.error():
         reason = REQUEST_ERRORS_SHORT.get(reply.error(), '?')
         self._return(cmd_id, None, reason)
     else:
         source = bytes(reply.readAll())
         self.tab.autoload(source)
         self._return(cmd_id, True)
Esempio n. 4
0
 def callback(reply):
     if reply.error():
         reason = REQUEST_ERRORS_SHORT.get(reply.error(), '?')
         cmd.return_result(None, reason)
     else:
         source = bytes(reply.readAll()).decode('utf-8')
         self.tab.autoload(source)
         cmd.return_result(True)
Esempio n. 5
0
File: qt.py Progetto: greshem/splash
def reply2har(reply, include_content=False, binary_content=False):
    """ Serialize QNetworkReply to HAR. """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": unicode(reply.url().toString())
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if not content_type.isNull():
        res["content"]["mimeType"] = unicode(content_type.toString())

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if not content_length.isNull():
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length.toInt()[0]

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if not status.isNull():
        status, ok = status.toInt()
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if not status_text.isNull():
        res["statusText"] = bytes(status_text.toByteArray()).decode('latin1')
    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if not redirect_url.isNull():
        res["redirectURL"] = unicode(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if include_content:
        data = bytes(reply.readAll())
        if binary_content:
            res["content"]["encoding"] = "binary"
            res["content"]["text"] = data
            res["content"]["size"] = len(data)
        else:
            res["content"]["encoding"] = "base64"
            res["content"]["text"] = base64.b64encode(data)
            res["content"]["size"] = len(data)

    return res
Esempio n. 6
0
File: qt.py Progetto: greshem/splash
def reply2har(reply, include_content=False, binary_content=False):
    """ Serialize QNetworkReply to HAR. """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": unicode(reply.url().toString())
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if not content_type.isNull():
        res["content"]["mimeType"] = unicode(content_type.toString())

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if not content_length.isNull():
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length.toInt()[0]

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if not status.isNull():
        status, ok = status.toInt()
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if not status_text.isNull():
        res["statusText"] = bytes(status_text.toByteArray()).decode('latin1')
    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if not redirect_url.isNull():
        res["redirectURL"] = unicode(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if include_content:
        data = bytes(reply.readAll())
        if binary_content:
            res["content"]["encoding"] = "binary"
            res["content"]["text"] = data
            res["content"]["size"] = len(data)
        else:
            res["content"]["encoding"] = "base64"
            res["content"]["text"] = base64.b64encode(data)
            res["content"]["size"] = len(data)

    return res
Esempio n. 7
0
def reply2har(reply, content=None):
    """
    Serialize QNetworkReply to HAR.
    If ``content`` (a bytes object) is not None, 'content' field is filled.
    This function doesn't read reply to get the content because
    QNetworkReply content can be read only once.
    """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": reply.url().toString()
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if content_type is not None:
        res["content"]["mimeType"] = str(content_type)

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if content_length is not None:
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if status is not None:
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if status_text is not None:
        if not isinstance(status_text, str):
            status_text = qt_to_bytes(status_text).decode('latin1')
        res['statusText'] = status_text
    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if redirect_url is not None:
        res["redirectURL"] = str(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if content is not None:
        res["content"]["size"] = len(content)
        res["content"]["text"] = base64.b64encode(content).decode('latin1')
        res["content"]["encoding"] = 'base64'
                
    return res
Esempio n. 8
0
def reply2har(reply, include_content=False):
    """ Serialize QNetworkReply to HAR. """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": reply.url().toString()
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if content_type is not None:
        res["content"]["mimeType"] = six.text_type(content_type)

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if content_length is not None:
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if status is not None:
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if status_text is not None:
        try:
            res["statusText"] = bytes(status_text, 'latin1').decode('latin1')
        except TypeError:
            res["statusText"] = bytes(status_text).decode('latin1')

    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if redirect_url is not None:
        res["redirectURL"] = six.text_type(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if include_content:
        content = getattr(reply, 'content', None)
        if content is not None:
            res["content"]["size"] = content.size()
            bytes_array = bytes(content)

            if not is_binary_string(bytes_array):
                res["content"]["text"] = bytes_array.decode('utf8', 'replace')
            else:
                res["content"]["text"] = base64.b64encode(bytes_array).decode('utf8')
                res["content"]["encoding"] = 'base64'
                
    return res
Esempio n. 9
0
def reply2har(reply, include_content=False):
    """ Serialize QNetworkReply to HAR. """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": reply.url().toString()
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if content_type is not None:
        res["content"]["mimeType"] = six.text_type(content_type)

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if content_length is not None:
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if status is not None:
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if status_text is not None:
        try:
            res["statusText"] = bytes(status_text, 'latin1').decode('latin1')
        except TypeError:
            res["statusText"] = bytes(status_text).decode('latin1')

    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if redirect_url is not None:
        res["redirectURL"] = six.text_type(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if include_content:
        content = getattr(reply, 'content', None)
        if content is not None:
            res["content"]["size"] = content.size()
            bytes_array = bytes(content)

            if not is_binary_string(bytes_array):
                res["content"]["text"] = bytes_array.decode('utf8', 'replace')
            else:
                res["content"]["text"] = base64.b64encode(bytes_array).decode(
                    'utf8')
                res["content"]["encoding"] = 'base64'

    return res