Beispiel #1
0
    def create_from_exception(cls,
                              log_type,
                              url,
                              exception,
                              start,
                              classifier=None,
                              channel=None,
                              ticketer=None):
        org = (classifier or channel or ticketer).org

        data = bytearray()
        prefixes = dump.PrefixSettings(cls.REQUEST_DELIM, cls.RESPONSE_DELIM)
        dump._dump_request_data(exception.request, prefixes, data)

        data = data.decode("utf-8")
        request_lines = data.split(cls.REQUEST_DELIM)
        request = "".join(request_lines)

        return cls.objects.create(
            org=org,
            log_type=log_type,
            url=url,
            request=request,
            response="",
            is_error=True,
            created_on=timezone.now(),
            request_time=(timezone.now() - start).total_seconds() * 1000,
            channel=channel,
            classifier=classifier,
            ticketer=ticketer,
        )
Beispiel #2
0
    def create_from_exception(cls, log_type, url, exception, start, classifier=None, channel=None):
        if classifier is not None:
            org = classifier.org

        if channel is not None:
            org = channel.org

        data = bytearray()
        prefixes = dump.PrefixSettings(cls.REQUEST_DELIM, cls.RESPONSE_DELIM)
        dump._dump_request_data(exception.request, prefixes, data)

        data = data.decode("utf-8")
        request_lines = data.split(cls.REQUEST_DELIM)
        request = "".join(request_lines)

        return HTTPLog.objects.create(
            channel=channel,
            classifier=classifier,
            log_type=HTTPLog.WHATSAPP_TEMPLATES_SYNCED,
            url=url,
            request=request,
            response="",
            is_error=True,
            created_on=timezone.now(),
            request_time=(timezone.now() - start).total_seconds() * 1000,
            org=org,
        )
def main(host_info: dict, timeout: int = 3) -> dict:
    """
    Get raw HTTP response as decoded bytes (headers + response body)
    :param host_info: host information
    :param timeout: host timeout
    :return: dictionary with status and data
    """
    output = {"http_response": "", "status": "Success"}

    try:
        proto = "https" if host_info.get("port") in [443, 8443] else "http"
        url = f"{proto}://{host_info.get('ip')}:{host_info.get('port')}"
        response = get(url, verify=False, timeout=timeout)
        response_dump = bytearray()
        dump._dump_response_data(response,
                                 prefixes=dump.PrefixSettings(b"", b""),
                                 bytearr=response_dump)
    except (TimeoutError, ConnectTimeout):
        output.update({"status": "Timeout error was caught"})
    except ConnectionError as connection_err:
        output.update(
            {"status": f"Connection error was caught: {str(connection_err)}"})
    except ContentDecodingError as content_err:
        output.update({
            "status":
            f"Content decoding error was caight: {str(content_err)}"
        })
    except Exception as unexp_err:
        output.update(
            {"status": f"Unexpected error was caught: {str(unexp_err)}"})
    else:
        output.update({"http_response": response_dump.decode("utf-8")})
    return output
Beispiel #4
0
    def test_dump_non_string_request_data(self):
        """Build up the request data into a bytearray."""
        self.configure_request(url='http://example.com/',
                               method='POST',
                               body=1)

        array = bytearray()
        prefixes = dump.PrefixSettings('request:', 'response:')
        dump._dump_request_data(
            request=self.request,
            prefixes=prefixes,
            bytearr=array,
            proxy_info={},
        )
        assert b'request:POST / HTTP/1.1\r\n' in array
        assert b'request:Host: example.com\r\n' in array
        assert b'<< Request body is not a string-like type >>\r\n' in array
Beispiel #5
0
    def test_dump_request_data(self):
        """Build up the request data into a bytearray."""
        self.configure_request(
            url='http://example.com/',
            method='GET',
        )

        array = bytearray()
        prefixes = dump.PrefixSettings('request:', 'response:')
        dump._dump_request_data(
            request=self.request,
            prefixes=prefixes,
            bytearr=array,
            proxy_info={},
        )

        assert b'request:GET / HTTP/1.1\r\n' in array
        assert b'request:Host: example.com\r\n' in array
Beispiel #6
0
    def test_dump_request_data_with_proxy_info(self):
        """Build up the request data into a bytearray."""
        self.configure_request(
            url='http://example.com/',
            method='GET',
        )

        array = bytearray()
        prefixes = dump.PrefixSettings('request:', 'response:')
        dump._dump_request_data(
            request=self.request,
            prefixes=prefixes,
            bytearr=array,
            proxy_info={
                'request_path': b'fake-request-path',
                'method': b'CONNECT',
            },
        )

        assert b'request:CONNECT fake-request-path HTTP/1.1\r\n' in array
        assert b'request:Host: example.com\r\n' in array
Beispiel #7
0
    def test_dump_response_data(self):
        """Build up the response data into a bytearray."""
        self.configure_response(
            url='https://example.com/redirected',
            content=b'foobarbogus',
            reason=b'OK',
        )
        self.configure_httpresponse(
            headers={'Content-Type': 'application/json'},
            reason=b'OK',
            status=201,
        )

        array = bytearray()
        prefixes = dump.PrefixSettings('request:', 'response:')
        dump._dump_response_data(
            response=self.response,
            prefixes=prefixes,
            bytearr=array,
        )

        assert b'response:HTTP/1.1 201 OK\r\n' in array
        assert b'response:Content-Type: application/json\r\n' in array