<commit_before>from flask import Blueprint, request

from skier import pgp
from skier.keyinfo import KeyInfo

legacypks = Blueprint("pks", __name__)

@legacypks.route("/add", methods=["POST"])
def pksadd():
    keytext = request.form["keytext"]
    result = pgp.add_pgp_key(keytext)
    if result[0]:
        return "", 200, {"X-HKP-Results-Count": "1"}
    else:
        return "Key add failed", 400, {"X-HKP-Results-Count": "1"}

@legacypks.route("/lookup", methods=["GET"])
def pksgetkey():
    if 'op' in request.args and request.args.get("op") == "get":
        # Lookup the key
        keyid = request.args.get("search")
        if keyid is None or not keyid.startswith("0x"):
            return "Invalid key data", 401
        else:
            key = pgp.get_pgp_armor_key(keyid)
            if key: return key, 200, {"Cache-Control": "no-cache", "Pragma": "no-cache"}
            else: return "", 404
    elif 'op' in request.args and request.args.get("op") == "index":
        return pkssearch(request.args)
    else:
        return "Invalid request", 400

def format_pks(keys):
    # First, add header.
    # This comes in the format of "info:1:{number-of-keys}"
    data = ""
    data += "info:1:{}\n".format(keys.total)
    # Then, add the strings for each key.
    for key in keys.query.all():
        # Load keyinfo.
        newkey = KeyInfo.pgp_dump(key.armored)
        s1, s2 = newkey.to_pks()
        data += s1 + '\n' + s2 + '\n'
    return data

def pkssearch(rargs):
    keys = pgp.search_through_keys(rargs.get("search"))
    if not keys.total:
        return "No keys found", 404
    else:
        return format_pks(keys), 200, {"X-HKP-Results-Count": keys.total}<commit_msg>Add custom Content-Type headers to PKS, to prevent them from being compressed.<commit_after>from flask import Blueprint, request

from skier import pgp
from skier.keyinfo import KeyInfo

legacypks = Blueprint("pks", __name__)

@legacypks.route("/add", methods=["POST"])
def pksadd():
    keytext = request.form["keytext"]
    result = pgp.add_pgp_key(keytext)
    if result[0]:
        return "", 200, {"X-HKP-Results-Count": "1"}
    else:
        return "Key add failed", 400, {"X-HKP-Results-Count": "1"}

@legacypks.route("/lookup", methods=["GET"])
def pksgetkey():
    if 'op' in request.args and request.args.get("op") == "get":
        # Lookup the key
        keyid = request.args.get("search")
        if keyid is None or not keyid.startswith("0x"):
            return "Invalid key data", 401
        else:
            key = pgp.get_pgp_armor_key(keyid)
            if key: return key, 200, {"Cache-Control": "no-cache", "Pragma": "no-cache", "Content-Type": "application/x-pgp-key"}
            else: return "", 404
    elif 'op' in request.args and request.args.get("op") == "index":
        return pkssearch(request.args)
    else:
        return "Invalid request", 400

def format_pks(keys):
    # First, add header.
    # This comes in the format of "info:1:{number-of-keys}"
    data = ""
    data += "info:1:{}\n".format(keys.total)
    # Then, add the strings for each key.
    for key in keys.query.all():
        # Load keyinfo.
        newkey = KeyInfo.pgp_dump(key.armored)
        s1, s2 = newkey.to_pks()
        data += s1 + '\n' + s2 + '\n'
    return data

def pkssearch(rargs):
    keys = pgp.search_through_keys(rargs.get("search"))
    if not keys.total:
        return "No keys found", 404
    else:
        return format_pks(keys), 200, {"X-HKP-Results-Count": keys.total, "Content-Type": "application/x-pgp-search"}
{'fraction_non_alphanumeric': 0.15200225606316978, 'fraction_numerical': 0.019176536943034405, 'mean_word_length': 2.757415254237288, 'pattern_counts': {'":': 16, '<': 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': '5723547', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1202, 'n_words': 320}