def send_request(request)
      response_error = { 'uri' => '',
                         'duration' => '0',
                         'http_version' => '1.0',
                         'headers' => "Server: #{@banner}\n",
                         'body' => '' }

      # parse client request
      begin
        if request.to_s !~ %r{\A(CONNECT|GET|HEAD|DELETE|POST|PUT|OPTIONS) https?://}
          if request.to_s !~ /^Host: ([^\s]+)\r?\n/
            logger.warn('No host specified')
            raise SSRFProxy::HTTP::Error::InvalidClientRequest,
                  'No host specified'
          end
        end
        req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
        req.parse(StringIO.new(request))
      rescue => e
        logger.info('Received malformed client HTTP request.')
        error_msg = 'Error -- Invalid request: ' \
                    "Received malformed client HTTP request: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '502'
        response_error['message'] = 'Bad Gateway'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      end
      uri = req.request_uri

      # send request
      response = nil
      logger.info("Requesting URL: #{uri}")
      status_msg = "Request  -> #{req.request_method}"
      status_msg << " -> PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
      status_msg << " -> SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] -> URI[#{uri}]"
      print_status(status_msg)

      begin
        response = @ssrf.send_request(request.to_s)
      rescue SSRFProxy::HTTP::Error::InvalidClientRequest => e
        logger.info(e.message)
        error_msg = "Error -- Invalid request: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '502'
        response_error['message'] = 'Bad Gateway'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue SSRFProxy::HTTP::Error::InvalidResponse => e
        logger.info(e.message)
        error_msg = 'Response <- 503'
        error_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
        error_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
        error_msg << " -- Error: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '503'
        response_error['message'] = 'Service Unavailable'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue SSRFProxy::HTTP::Error::ConnectionFailed => e
        logger.info(e.message)
        error_msg = 'Response <- 503'
        error_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
        error_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
        error_msg << " -- Error: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '503'
        response_error['message'] = 'Service Unavailable'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue SSRFProxy::HTTP::Error::ConnectionTimeout => e
        logger.info(e.message)
        error_msg = 'Response <- 504'
        error_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
        error_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
        error_msg << " -- Error: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '504'
        response_error['message'] = 'Timeout'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue => e
        logger.warn(e.message)
        error_msg = "Error -- Unexpected error: #{e.backtrace.join("\n")}"
        print_error(error_msg)
        response_error['code'] = '502'
        response_error['message'] = 'Bad Gateway'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']} "
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      end

      # return response
      status_msg = "Response <- #{response['code']}"
      status_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
      status_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
      status_msg << " -- Title[#{response['title']}]" unless response['title'].eql?('')
      status_msg << " -- [#{response['body'].size} bytes]"
      print_good(status_msg)
      response
    end
def send_request(request)
      response_error = { 'uri' => '',
                         'duration' => '0',
                         'http_version' => '1.0',
                         'headers' => "Server: #{@banner}\n",
                         'body' => '' }

      # parse client request
      begin
        if request.to_s !~ %r{\A(CONNECT|GET|HEAD|DELETE|POST|PUT|OPTIONS) https?://}
          if request.to_s !~ /^Host: ([^\s]+)\r?\n/
            logger.warn('No host specified')
            raise SSRFProxy::HTTP::Error::InvalidClientRequest,
                  'No host specified'
          end
        end
        req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
        req.parse(StringIO.new(request))
      rescue => e
        logger.info('Received malformed client HTTP request.')
        error_msg = 'Error -- Invalid request: ' \
                    "Received malformed client HTTP request: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '502'
        response_error['message'] = 'Bad Gateway'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      end
      uri = req.request_uri

      # send request
      response = nil
      logger.info("Requesting URL: #{uri}")
      status_msg = "Request  -> #{req.request_method}"
      status_msg << " -> PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
      status_msg << " -> SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] -> URI[#{uri}]"
      print_status(status_msg)

      begin
        response = @ssrf.send_request(request.to_s)
      rescue SSRFProxy::HTTP::Error::InvalidClientRequest => e
        logger.info(e.message)
        error_msg = "Error -- Invalid request: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '502'
        response_error['message'] = 'Bad Gateway'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue SSRFProxy::HTTP::Error::InvalidResponse => e
        logger.info(e.message)
        error_msg = 'Response <- 503'
        error_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
        error_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
        error_msg << " -- Error: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '503'
        response_error['message'] = 'Service Unavailable'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue SSRFProxy::HTTP::Error::ConnectionFailed => e
        logger.info(e.message)
        error_msg = 'Response <- 503'
        error_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
        error_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
        error_msg << " -- Error: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '503'
        response_error['message'] = 'Service Unavailable'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue SSRFProxy::HTTP::Error::ConnectionTimeout => e
        logger.info(e.message)
        error_msg = 'Response <- 504'
        error_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
        error_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
        error_msg << " -- Error: #{e.message}"
        print_error(error_msg)
        response_error['code'] = '504'
        response_error['message'] = 'Timeout'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']}"
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      rescue => e
        logger.warn(e.message)
        error_msg = "Error -- Unexpected error: #{e.backtrace.join("\n")}"
        print_error(error_msg)
        response_error['code'] = '502'
        response_error['message'] = 'Bad Gateway'
        response_error['status_line'] = "HTTP/#{response_error['http_version']}"
        response_error['status_line'] << " #{response_error['code']} "
        response_error['status_line'] << " #{response_error['message']}"
        return response_error
      end

      # return response
      status_msg = "Response <- #{response['code']}"
      status_msg << " <- PROXY[#{@ssrf.proxy.host}:#{@ssrf.proxy.port}]" unless @ssrf.proxy.nil?
      status_msg << " <- SSRF[#{@ssrf.url.host}:#{@ssrf.url.port}] <- URI[#{uri}]"
      status_msg << " -- Title[#{response['title']}]" unless response['title'].eql?('')
      status_msg << " -- [#{response['body'].size} bytes]"
      print_good(status_msg)
      response
    end
Send client HTTP request

 @param [String] request client HTTP request

 @return [Hash] HTTP response