HTTPListRedirects

List the redirects for a given URL.

python · Common Scripts

Source

import os

import demistomock as demisto
import requests
import urllib3
from CommonServerPython import *

urllib3.disable_warnings()


def get_response(url: str, use_head: str, verify_ssl: bool) -> requests.Response:
    if use_head == "true":
        response = requests.head(url, allow_redirects=True, verify=verify_ssl)
    else:
        response = requests.get(url, allow_redirects=True, verify=verify_ssl)
    return response


def create_command_result(history_urls: List[Dict[str, Union[str, int]]]):
    ec = {"URL(val.Data == obj.Data)": [{"Data": history_url["Data"]} for history_url in history_urls]}
    return {
        "ContentsFormat": formats["json"],
        "Type": entryTypes["note"],
        "Contents": history_urls,
        "ReadableContentsFormat": formats["markdown"],
        "HumanReadable": tableToMarkdown("URLs", history_urls, ["Data", "Status"]),
        "EntryContext": ec,
    }


def get_response_history(response: requests.Response):
    urls = []
    if response.history:
        for resp in response.history:
            urls.append({"Data": resp.url, "Status": resp.status_code})
    urls.append({"Data": response.url, "Status": response.status_code})
    return urls


def delete_environment_variables(use_system_proxy: str):
    if use_system_proxy == "false":
        env_variables = ["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"]
        for env_var in env_variables:
            if env_var in os.environ:
                del os.environ[env_var]


def main():
    url = demisto.args().get("url")
    try:
        delete_environment_variables(use_system_proxy=demisto.args().get("use_system_proxy").lower())
        url = f"http://{url}" if not url.lower().startswith("http") else url

        response = get_response(
            url=url,
            use_head=demisto.args()["useHead"],
            verify_ssl=demisto.args().get("trust_any_certificate", "true").lower() != "true",
        )
        history_urls = get_response_history(response=response)
        demisto.results(create_command_result(history_urls=history_urls))

    except Exception as e:
        return_error(f"Failed to execute script. Error:\n{e!s}")


if __name__ in ("__builtin__", "builtins", "__main__"):
    main()

README

List the redirects for a given URL

Script Data


Name Description
Script Type python2
Tags Utility
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
url The URL to follow
useHead Should we use HEAD instead of GET. Safer but might not be the same response.
use_system_proxy Use system proxy settings
trust_any_certificate Trust any certificate (not secure)

Outputs


Path Description Type
URL.Data The URL redirects from the given URL Unknown