# Buggy code
def forward(self, method):
    path = self.path
    if "://" in path:
        path = "/" + path.split("://", 1)[1].split("/", 1)[1]
    proxy_url = "http://%s%s" % (self.proxy.forward_host, path)
    target_url = self.path
    if "://" not in target_url:
        target_url = "http://%s%s" % (self.proxy.forward_host, target_url)
    data = None
    if method in ["POST", "PUT", "PATCH"]:
        data_string = self.data_bytes
        try:
            if not isinstance(data_string, string_types):
                data_string = data_string.decode(DEFAULT_ENCODING)
            data = json.loads(data_string)
        except Exception as e:
            # unable to parse JSON, fallback to verbatim string/bytes
            data = data_string

    forward_headers = dict(self.headers)
    # update original "Host" header
    forward_headers["host"] = urlparse(target_url).netloc
    try:
        response = None
        modified_request = None
        # update listener (pre-invocation)
        if self.proxy.update_listener:
            listener_result = self.proxy.update_listener(
                method=method,
                path=path,
                data=data,
                headers=forward_headers,
                return_forward_info=True,
            )
            if isinstance(listener_result, Response):
                response = listener_result
            elif isinstance(listener_result, Request):
                modified_request = listener_result
            elif listener_result is not True:
                # get status code from response, or use Bad Gateway status code
                code = listener_result if isinstance(listener_result, int) else 503
                self.send_response(code)
                self.end_headers()
                return
        if response is None:
            if modified_request:
                response = self.method(
                    proxy_url,
                    data=modified_request.data,
                    headers=modified_request.headers,
                )
            else:
                response = self.method(
                    proxy_url, data=self.data_bytes, headers=forward_headers
                )
        # update listener (post-invocation)
        if self.proxy.update_listener:
            updated_response = self.proxy.update_listener(
                method=method,
                path=path,
                data=data,
                headers=self.headers,
                response=response,
            )
            if isinstance(updated_response, Response):
                response = updated_response
        # copy headers and return response
        self.send_response(response.status_code)
        for header_key, header_value in iteritems(response.headers):
            self.send_header(header_key, header_value)
        self.end_headers()
        self.wfile.write(bytes_(response.content))
    except Exception as e:
        if not self.proxy.quiet:
            LOGGER.exception("Error forwarding request: %s" % str(e))


# Traceback (ResponseParserError)
ERROR:localstack.mock.generic_proxy:Error forwarding request: Traceback (most recent call last):
File "/opt/code/localstack/localstack/mock/generic_proxy.py", line 88, in forward
data=data, headers=self.headers, return_forward_info=True)
File "/opt/code/localstack/localstack/mock/proxy/sns_listener.py", line 37, in update_sns
queue_url = aws_stack.get_sqs_queue_url(queue_name)
File "/opt/code/localstack/localstack/utils/aws/aws_stack.py", line 285, in get_sqs_queue_url
response = client.get_queue_url(QueueName=queue_name)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/client.py", line 531, in _make_api_call
operation_model, request_dict)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 141, in make_request
return self._send_request(request_dict, operation_model)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 168, in _send_request
request, operation_model, attempts)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 233, in _get_response
response_dict, operation_model.output_shape)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 210, in parse
parsed = self._do_error_parse(response, shape)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 438, in _do_error_parse
root = self._parse_xml_string_to_dom(xml_contents)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 390, in _parse_xml_string_to_dom
"invalid XML received:\n%s" % (e, xml_string))
ResponseParserError: Unable to parse response (syntax error: line 1, column 54), invalid XML received:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

# Fixed code
def forward(self, method):
    path = self.path
    if "://" in path:
        path = "/" + path.split("://", 1)[1].split("/", 1)[1]
    proxy_url = "http://%s%s" % (self.proxy.forward_host, path)
    target_url = self.path
    if "://" not in target_url:
        target_url = "http://%s%s" % (self.proxy.forward_host, target_url)
    data = None
    if method in ["POST", "PUT", "PATCH"]:
        data_string = self.data_bytes
        try:
            if not isinstance(data_string, string_types):
                data_string = data_string.decode(DEFAULT_ENCODING)
            data = json.loads(data_string)
        except Exception as e:
            # unable to parse JSON, fallback to verbatim string/bytes
            data = data_string

    forward_headers = CaseInsensitiveDict(self.headers)
    # update original "Host" header
    forward_headers["host"] = urlparse(target_url).netloc
    try:
        response = None
        modified_request = None
        # update listener (pre-invocation)
        if self.proxy.update_listener:
            listener_result = self.proxy.update_listener(
                method=method,
                path=path,
                data=data,
                headers=forward_headers,
                return_forward_info=True,
            )
            if isinstance(listener_result, Response):
                response = listener_result
            elif isinstance(listener_result, Request):
                modified_request = listener_result
                data = modified_request.data
                forward_headers = modified_request.headers
            elif listener_result is not True:
                # get status code from response, or use Bad Gateway status code
                code = listener_result if isinstance(listener_result, int) else 503
                self.send_response(code)
                self.end_headers()
                return
        if response is None:
            if modified_request:
                response = self.method(
                    proxy_url,
                    data=modified_request.data,
                    headers=modified_request.headers,
                )
            else:
                response = self.method(
                    proxy_url, data=self.data_bytes, headers=forward_headers
                )
        # update listener (post-invocation)
        if self.proxy.update_listener:
            updated_response = self.proxy.update_listener(
                method=method,
                path=path,
                data=data,
                headers=forward_headers,
                response=response,
            )
            if isinstance(updated_response, Response):
                response = updated_response
        # copy headers and return response
        self.send_response(response.status_code)
        for header_key, header_value in iteritems(response.headers):
            self.send_header(header_key, header_value)
        self.end_headers()
        self.wfile.write(bytes_(response.content))
    except Exception as e:
        if not self.proxy.quiet:
            LOGGER.exception("Error forwarding request: %s" % str(e))