<commit_before>package bsw

import (
	"crypto/tls"
	"net/http"
	"net/url"
	"regexp"
)

func Headers(ip string) ([]Result, error) {
	results := make([]Result, 0, 2)
	host, _ := hostnameFromHttpLocationHeader(ip, "http")
	if host != "" {
		results = append(results, Result{Source: "Headers", IP: ip, Hostname: host})
	}
	host, _ = hostnameFromHttpLocationHeader(ip, "https")
	if host != "" {
		results = append(results, Result{Source: "Headers", IP: ip, Hostname: host})
	}
	return results, nil
}

func hostnameFromHttpLocationHeader(ip string, protocol string) (string, error) {
	req, err := http.NewRequest("GET", protocol+"://"+ip, nil)
	if err != nil {
		return "", err
	}
	tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
	res, err := tr.RoundTrip(req)
	if err != nil {
		return "", err
	}
	location := res.Header["Location"]
	if location != nil {
		u, err := url.Parse(location[0])
		if err != nil {
			return "", err
		}
		host := u.Host
		if m, _ := regexp.Match("[a-zA-Z]+", []byte(host)); m == true {
			return host, nil
		}
		return "", nil
	}
	return "", nil
}
<commit_msg>Refactors thanks to cespare<commit_after>package bsw

import (
	"crypto/tls"
	"net/http"
	"net/url"
	"regexp"
)

func Headers(ip string) ([]Result, error) {
	results := []Result{}
	for _, proto := range []string{"http", "https"} {
		if host := hostnameFromHttpLocationHeader(ip, proto); host != "" {
			results = append(results, Result{Source: "Headers", IP: ip, Hostname: host})
		}
	}
	return results, nil
}

func hostnameFromHttpLocationHeader(ip string, protocol string) string {
	req, err := http.NewRequest("GET", protocol+"://"+ip, nil)
	if err != nil {
		return ""
	}
	tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
	res, err := tr.RoundTrip(req)
	if err != nil {
		return ""
	}
	location := res.Header["Location"]
	if location != nil {
		u, err := url.Parse(location[0])
		if err != nil {
			return ""
		}
		host := u.Host
		if m, _ := regexp.Match("[a-zA-Z]+", []byte(host)); m == true {
			return host
		}
		return ""
	}
	return ""
}

{'fraction_non_alphanumeric': 0.1824712643678161, 'fraction_numerical': 0.0019157088122605363, 'mean_word_length': 3.9502369668246446, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6089359', 'n_tokens_mistral': 795, 'n_tokens_neox': 760, 'n_words': 190}