from scapy.layers import http

_MINHASH_INDEX = 4

def get_payload(pkt):
    """
    Fetch the everything from layer 3 and above (network layer +)

    :param pkt:
    :return:
    """
    for i in range(2):
        pkt = pkt.payload
    try:
    	return pkt.encode('utf8')
    except Exception as e:
    	return str(pkt)#.encode('utf8')


def calculate_hash(session_obj, new_session):
    """
    Function to calculate/update the hash.

    :param session_obj:
    :param new_session:
    :return:
    """
    for packet in new_session:
        session_obj[_MINHASH_INDEX].update(get_payload(packet))
    return session_obj

def entropy_domain_names(new_session):
    entropy = 0
    for packet in new_session:
        if packet.haslayer(http.HTTPRequest):
            http_layer = packet.getlayer(http.HTTPRequest)
            if http_layer.fields["Method"] == "POST":
                url = http_layer.fields["Host"] + http_layer.fields["Path"]
                proc = subprocess.Popen(" {0} | ent".format(url),shell=True,stdout=subprocess.STDOUT)
                output = proc.stdout.read()
                output = float(output[10:18])
                print output
                entropy +=  output
    return entropy
                

