from general import *
import requests


class DetailedError:

    def __init__(self, path):
        self.log = list()
        self.url_401 = set()
        self.url_404 = set()
        self.url_500 = set()
        self.url_503 = set()
        self.listUrl = file_to_set(path + "/crawled.txt")

        self.boot()

    def boot(self):
        header = {
            "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            'Accept-Language': "en-US,en;q=0.5"
        }
        for url in self.listUrl:
            try:
                re = requests.get(url, timeout=5, headers=header)
            except:
                continue
            code = re.status_code
            if code == 401:
                self.url_401.add(url)
            elif code == 404:
                self.url_404.add(url)
            elif code == 500:
                self.url_500.add(url)
            elif code == 503:
                self.url_503.add(url)

        if len(self.url_401) is not 0:
            self.log.append("url with code 401 : " + str(len(self.url_401)))
        if len(self.url_404) is not 0:
            self.log.append("url with code 404 : " + str(len(self.url_404)))
        if len(self.url_500) is not 0:
            self.log.append("url with code 500 : " + str(len(self.url_500)))
        if len(self.url_503) is not 0:
            self.log.append("url with code 503 : " + str(len(self.url_503)))

        sum = len(self.url_401) + len(self.url_404) + len(self.url_500) +len(self.url_503)
        self.log.append("total : " + str(sum))
        if sum == 0:
            self.log.append("not vulnerability - error page not found")

    def return_output(self):
        output = self.log
        return output
