<commit_before>import glob
import os
import base64
import datetime

from flask import jsonify, make_response, request, g, render_template

from server import app
from .database import connect_db
from .utils import valid_auth_token, bam_test, get_tempdir, get_chunk_filename, gzip_test, merge_chunks, \
    remove_from_uploads, check_status, update_files_table

INVALID_AUTH_TOKEN_MSG = 'Error: Invalid transfer code'


@app.before_request
def before_request():
    # connect to the database before processing a request
    g.db = connect_db()


@app.teardown_request
def teardown_request(exception):
    # close the database connection regardless of request outcome
    db = getattr(g, 'db', None)
    if db is not None:
        db.close()


@app.route("/", methods=['GET'])
def home():
    return render_template('home.html')


@app.route("/get-auth-token", methods=['GET'])
def get_auth_token():
    # check if request has a recognized access code in header
    if 'x-access-code' in request.headers and request.headers['x-access-code'] in app.config['ACCESS_CODES']:
        auth_token = base64.urlsafe_b64encode(os.urandom(12))
        current_date = datetime.datetime.today()
        expiry_date = current_date + datetime.timedelta(days=1)  # Code expires in 24 hours
        g.db.execute('insert into access (site_access_code,auth_token,date_created,date_expired) '
                     'values (?,?,?,?)',
                     (request.headers['x-access-code'],
                     auth_token,
                     current_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
                     expiry_date.strftime("%Y-%m-%dT%H:%M:%SZ")))
        g.db.commit()
        return make_response(
            jsonify({'Access_code': auth_token, 'Expiry_date': expiry_date.strftime("%Y-%m-%dT%H:%M:%SZ")}), 200)
    else:
        return make_response(jsonify({'Error': 'unauthorized'}), 401)


@app.route("/authorize", methods=['POST'])
def authorize():
    auth_token = request.form.get('authToken', type=str)
    if valid_auth_token(auth_token):
        return make_response(jsonify({'message': 'Success: Valid transfer code'}), 200)
    else:
        return make_response(jsonify({'message': INVALID_AUTH_TOKEN_MSG}), 403)

<commit_msg>Access code header encoding fix<commit_after>import glob
import os
import base64
import datetime

from flask import jsonify, make_response, request, g, render_template

from server import app
from .database import connect_db
from .utils import valid_auth_token, bam_test, get_tempdir, get_chunk_filename, gzip_test, merge_chunks, \
    remove_from_uploads, check_status, update_files_table

INVALID_AUTH_TOKEN_MSG = 'Error: Invalid transfer code'


@app.before_request
def before_request():
    # connect to the database before processing a request
    g.db = connect_db()


@app.teardown_request
def teardown_request(exception):
    # close the database connection regardless of request outcome
    db = getattr(g, 'db', None)
    if db is not None:
        db.close()


@app.route("/", methods=['GET'])
def home():
    return render_template('home.html')


@app.route("/get-auth-token", methods=['GET'])
def get_auth_token():
    # get access code from header (convert to str to fix weird encoding issue on production)
    access_code = str(request.headers['x-access-code'])
    # check if request has a recognized access code in header
    if 'x-access-code' in request.headers and access_code in app.config['ACCESS_CODES']:
        auth_token = base64.urlsafe_b64encode(os.urandom(12))
        current_date = datetime.datetime.today()
        expiry_date = current_date + datetime.timedelta(days=1)  # Code expires in 24 hours
        g.db.execute('insert into access (site_access_code,auth_token,date_created,date_expired) '
                     'values (?,?,?,?)',
                     (access_code,
                     auth_token,
                     current_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
                     expiry_date.strftime("%Y-%m-%dT%H:%M:%SZ")))
        g.db.commit()
        return make_response(
            jsonify({'Access_code': auth_token, 'Expiry_date': expiry_date.strftime("%Y-%m-%dT%H:%M:%SZ")}), 200)
    else:
        return make_response(jsonify({'Error': 'unauthorized'}), 401)


@app.route("/authorize", methods=['POST'])
def authorize():
    auth_token = request.form.get('authToken', type=str)
    if valid_auth_token(auth_token):
        return make_response(jsonify({'message': 'Success: Valid transfer code'}), 200)
    else:
        return make_response(jsonify({'message': INVALID_AUTH_TOKEN_MSG}), 403)


{'fraction_non_alphanumeric': 0.12601004586154183, 'fraction_numerical': 0.010045861541821358, 'mean_word_length': 3.6030150753768844, 'pattern_counts': {'":': 0, '<': 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': '714539', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1396, 'n_words': 337}