<commit_before>import sys

from flask import Flask, render_template, make_response, request, Response


app = Flask(__name__)
DEFAULT_POLICY = "default-src 'none"


def request_handler(template, params):
    """
    Method tha generates response based on given template and set of parameters
    """
    if 'header' not in params.keys():
        params['header'] = None
    print(params)
    response = make_response(render_template(template, params=params))
    if params['header']:
        response.headers['Content-Security-Policy'] = params['policy']
    return response


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/base-uri')
def base_uri():
    """
    Test URI:

    using meta:
    http://127.0.0.1:8000/base-uri?policy=base-uri%20%27self%27
    http://127.0.0.1:8000/base-uri?allow=true&policy=base-uri%20http://example.com

    using header:
    http://127.0.0.1:8000/base-uri?header=true&policy=base-uri%20%27self%27
    http://127.0.0.1:8000/base-uri?header=true&allow=true&policy=base-uri%20http://example.com

    """
    params = {}
    params['meta'] = request.args.get('meta')
    params['allow'] = request.args.get('allow')
    params['header'] = request.args.get('header')
    params['policy'] = request.args.get('policy')

    return request_handler('base-uri.html', params)


@app.route('/child-src')
def child_src():
    # TODO: add frame redirect
    """
    Test URI:
    frame

    Allow:
    http://127.0.0.1:8000/child-src?meta=true&frame=true&allow=true&policy=child-src 'self';
    http://127.0.0.1:8000/child-src?frame=true&allow=true&header=true&policy=child-src 'self';
    Block:
    http://127.0.0.1:8000/child-src?meta=true&frame=true&policy=child-src 'none';
    http://127.0.0.1:8000/child-src?header=true&frame=true&header=true&policy=child-src 'none';

    worker:

    Allow:
    http://127.0.0.1:8000/child-src?worker=true&allow=true&policy=child-src 'self';
    http://127.0.0.1:8000/child-src?worker=true&allow=true&header=true&policy=child-src 'self';
    Block:
    http://127.0.0.1:8000/child-src?worker=true&policy=child-src 'none';
    http://127.0.0.1:8000/child-src?worker=true&header=true&policy=child-src 'none';

    shared:

    Allow:
    http://127.0.0.1:8000/child-src?shared=true&allow=true&policy=child-src 'self';
    http://127.0.0.1:8000/child-src?shared=true&allow=true&header=true&policy=child-src 'self';
    Block:
    http://127.0.0.1:8000/child-src?shared=true&policy=child-src 'none';
    http://127.0.0.1:8000/child-src?shared=true&header=true&policy=child-src 'none';

    """
    params = {}
    params['meta'] = request.args.get('meta')
    params['allow'] = request.args.get('allow')
    params['header'] = request.args.get('header')
    params['policy'] = request.args.get('policy')
    params['frame'] = request.args.get('frame')
    params['worker'] = request.args.get('worker')
    params['shared'] = request.args.get('shared')

    return request_handler('child-src.html', params)


@app.route('/connect-src')
def connect_src():
    """
    Test URI:

    Beacon:

    Allow:
    http://127.0.0.1:8000/connect-src?beacon=true&allow=true&policy=connect-src http://127.0.0.1:8000
    http://127.0.0.1:8000/connect-src?beacon=true&allow=true&header=true&policy=connect-src http://127.0.0.1:8000
    Block:
    http://127.0.0.1:8000/connect-src?beacon=true&policy=connect-src http://localhost:8000;
    http://127.0.0.1:8000/connect-src?beacon=true&header=true&policy=connect-src http://localhost:8008;

    XHR

    Allow:
    http://127.0.0.1:8000/connect-src?allow=true&xhr=true&policy=connect-src http://127.0.0.1:8000
    http://127.0.0.1:8000/connect-src?allow=true&xhr=true&header=true&policy=connect-src http://127.0.0.1:8000
    Block:
    http://127.0.0.1:8000/connect-src?xhr=true&policy=connect-src http://localhost:8000;
    http://127.0.0.1:8000/connect-src?xhr=true&header=true&policy=connect-src http://localhost:8000;
    """
    meta = {}
    allow = request.args.get('allow')
    header = request.args.get('header')
    policy = request.args.get('policy')
    beacon = request.args.get('beacon')
    event = request.args.get('event')
    websocket = request.args.get('websocket')
    xhr = request.args.get('xhr')

    if policy:
        meta['policy'] = policy

    if header:
        response = make_response(render_template('connect-src.html', meta=None,
                                                 allow=allow, xhr=xhr,
                                                 beacon=beacon, event=event,
                                                 websocket=websocket))
    else:
        response = make_response(render_template('connect-src.html', meta=meta,
                                                 allow=allow, xhr=xhr,
                                                 beacon=beacon, event=event,
                                                 websocket=websocket))
    if header:
        response.headers['Content-Security-Policy'] = meta['policy']
    return response


@app.route('/form-action')
def form_action():
    meta = {}
    allow = request.args.get('allow')
    header = request.args.get('header')
    policy = request.args.get('policy')
    method = request.args.get('method')

    if policy:
        meta['policy'] = policy
<commit_msg>adds logging into access.log for requests to server<commit_after>import sys
import logging

from flask import Flask, render_template, make_response, request, Response


DEFAULT_POLICY = "default-src 'none'"
LOGGER_FILE_NAME = 'access.log'

app = Flask(__name__)

logger = logging.getLogger('werkzeug')
handler = logging.FileHandler(LOGGER_FILE_NAME)
logger.addHandler(handler)

# Also add the handler to Flask's logger for cases
# where Werkzeug isn't used as the underlying WSGI server.
app.logger.addHandler(handler)


def request_handler(template, params):
    """
    Method tha generates response based on given template and set of parameters
    """
    if 'header' not in params.keys():
        params['header'] = None
    print(params)
    response = make_response(render_template(template, params=params))
    if params['header']:
        response.headers['Content-Security-Policy'] = params['policy']
    return response


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/base-uri')
def base_uri():
    """
    Test URI:

    using meta:
    http://127.0.0.1:8000/base-uri?policy=base-uri%20%27self%27
    http://127.0.0.1:8000/base-uri?allow=true&policy=base-uri%20http://example.com

    using header:
    http://127.0.0.1:8000/base-uri?header=true&policy=base-uri%20%27self%27
    http://127.0.0.1:8000/base-uri?header=true&allow=true&policy=base-uri%20http://example.com

    """
    params = {}
    params['meta'] = request.args.get('meta')
    params['allow'] = request.args.get('allow')
    params['header'] = request.args.get('header')
    params['policy'] = request.args.get('policy')

    return request_handler('base-uri.html', params)


@app.route('/child-src')
def child_src():
    # TODO: add frame redirect
    """
    Test URI:
    frame

    Allow:
    http://127.0.0.1:8000/child-src?meta=true&frame=true&allow=true&policy=child-src 'self';
    http://127.0.0.1:8000/child-src?frame=true&allow=true&header=true&policy=child-src 'self';
    Block:
    http://127.0.0.1:8000/child-src?meta=true&frame=true&policy=child-src 'none';
    http://127.0.0.1:8000/child-src?header=true&frame=true&header=true&policy=child-src 'none';

    worker:

    Allow:
    http://127.0.0.1:8000/child-src?worker=true&allow=true&policy=child-src 'self';
    http://127.0.0.1:8000/child-src?worker=true&allow=true&header=true&policy=child-src 'self';
    Block:
    http://127.0.0.1:8000/child-src?worker=true&policy=child-src 'none';
    http://127.0.0.1:8000/child-src?worker=true&header=true&policy=child-src 'none';

    shared:

    Allow:
    http://127.0.0.1:8000/child-src?shared=true&allow=true&policy=child-src 'self';
    http://127.0.0.1:8000/child-src?shared=true&allow=true&header=true&policy=child-src 'self';
    Block:
    http://127.0.0.1:8000/child-src?shared=true&policy=child-src 'none';
    http://127.0.0.1:8000/child-src?shared=true&header=true&policy=child-src 'none';

    """
    params = {}
    params['meta'] = request.args.get('meta')
    params['allow'] = request.args.get('allow')
    params['header'] = request.args.get('header')
    params['policy'] = request.args.get('policy')
    params['frame'] = request.args.get('frame')
    params['worker'] = request.args.get('worker')
    params['shared'] = request.args.get('shared')

    return request_handler('child-src.html', params)


@app.route('/connect-src')
def connect_src():
    """
    Test URI:

    Beacon:

    Allow:
    http://127.0.0.1:8000/connect-src?beacon=true&allow=true&meta=true&policy=connect-src http://127.0.0.1:8000
    http://127.0.0.1:8000/connect-src?beacon=true&allow=true&header=true&policy=connect-src http://127.0.0.1:8000
    Block:
    http://127.0.0.1:8000/connect-src?beacon=true&meta=true&policy=connect-src http://localhost:8000;
    http://127.0.0.1:8000/connect-src?beacon=true&header=true&policy=connect-src http://localhost:8008;

    XHR

    Allow:
    http://127.0.0.1:8000/connect-src?allow=true&xhr=true&policy=connect-src http://127.0.0.1:8000
    http://127.0.0.1:8000/connect-src?allow=true&xhr=true&header=true&policy=connect-src http://127.0.0.1:8000
    Block:
    http://127.0.0.1:8000/connect-src?xhr=true&policy=connect-src http://localhost:8000;
    http://127.0.0.1:8000/connect-src?xhr=true&header=true&policy=connect-src http://localhost:8000;
    """
    params = {}
    params['meta'] = request.args.get('meta')
    params['allow'] = request.args.get('allow')
    params['header'] = request.args.get('header')
    params['policy'] = request.args.get('policy')
    params['beacon'] = request.args.get('beacon')
    params['event'] = request.args.get('event')
    params['websocket'] = request.args.get('websocket')
    params['xhr'] = request.args.get('xhr')

    return request_handler('connect-src.html', params)
    # meta = {}
    # allow = request.args.get('allow')
    # header = request.args.get('header')
    # policy = request.args.get('policy')
    # beacon = request.args.get('beacon')
    # event = request.args.get('event')
    # websocket = request.args.get('websocket')
    # xhr = request.args.get('xhr')

    # if policy:
    #     meta['policy'] = policy

    # if header:
    #     response = make_response(render_template('connect-src.html', meta=None,
    #                                              allow=allow, xhr=xhr,
    #                                              beacon=beacon, event=event,
    #                                              websocket=websocket))
    # else:
    #     response = make_response(render_template('connect-src.html', meta=meta,
    #                                              allow=allow, xhr=xhr,
    #                                              beacon=beacon, event=event,
    #                                              websocket=websocket))
    # if header:
    #     response.headers['Content-Security-Policy'] = meta['policy']
    # return response


@app.route('/form-action')
def form_action():
    meta = {}
    allow = request.args.get('allow')
    header = request.args.get('header')
    policy = request.args.get('policy')
    method = request.args.get('method')

    if policy:
        meta['policy'] = policy

{'fraction_non_alphanumeric': 0.17588071154516915, 'fraction_numerical': 0.05441227764213463, 'mean_word_length': 3.052650176678445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 56, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2047461', 'n_tokens_mistral': 4183, 'n_tokens_neox': 3799, 'n_words': 505}