<commit_before>#!/usr/bin/env python3

import subprocess, sys
from http.server import BaseHTTPRequestHandler, HTTPServer

kiosk_script, server_port, current_page_file, \
saved_page_file, temp_page_file = sys.argv[1:6]
try:
    server_auth = sys.argv[6]
except IndexError:
    server_auth = ""

def restartChromium():
    subprocess.call(kiosk_script + " chromium", shell=True)

def readFile(fileName):
    file = open(fileName, 'r');
    data = file.read()
    file.close()
    return data

def overwriteFile(data, filename):
    file = open(filename, 'w');
    file.write(data);
    file.close();

def getPage():
    return readFile(current_page_file)

def setTempPage(url):
    overwriteFile(url, temp_page_file)

def setPage(url):
    overwriteFile(url, saved_page_file)

class KioskHandler(BaseHTTPRequestHandler):
    def handle(self):
        '''Overridden from parent to perform authorization'''
        self.raw_requestline = self.rfile.readline()
        if not self.parse_request():
            return
        if server_auth != self.headers.get('kiosk-auth', ''):
            self.send_error(401)
            return
        mname = 'do_' + self.command
        if not hasattr(self, mname):
            self.send_error(405)
            return
        method = getattr(self, mname)
        method()

    def sendPlainText(self, message):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write(bytes(message.strip(), 'utf-8'))

    def do_GET(self):
        self.sendPlainText(getPage())

    def do_POST(self):
        newpage = self.headers.get('kiosk-page', '')
        if len(newpage) > 0:
            setTempPage(newpage)
        restartChromium()
        self.sendPlainText(getPage())

    def do_PUT(self):
        newpage = self.headers.get('kiosk-page', '')
        if len(newpage) > 0:
            setPage(newpage)
            restartChromium()
            self.sendPlainText(getPage())
            return
        self.send_error(400)

httpd = HTTPServer(('localhost', int(server_port)), KioskHandler)
httpd.serve_forever()<commit_msg>Fixed API being unavailable outside of localhost<commit_after>#!/usr/bin/env python3

import subprocess, sys
from http.server import BaseHTTPRequestHandler, HTTPServer

kiosk_script, server_port, current_page_file, \
saved_page_file, temp_page_file = sys.argv[1:6]
try:
    server_auth = sys.argv[6]
except IndexError:
    server_auth = ""

def restartChromium():
    subprocess.call(kiosk_script + " chromium", shell=True)

def readFile(fileName):
    file = open(fileName, 'r');
    data = file.read()
    file.close()
    return data

def overwriteFile(data, filename):
    file = open(filename, 'w');
    file.write(data);
    file.close();

def getPage():
    return readFile(current_page_file)

def setTempPage(url):
    overwriteFile(url, temp_page_file)

def setPage(url):
    overwriteFile(url, saved_page_file)

class KioskHandler(BaseHTTPRequestHandler):
    def handle(self):
        '''Overridden from parent to perform authorization'''
        self.raw_requestline = self.rfile.readline()
        if not self.parse_request():
            return
        if server_auth != self.headers.get('kiosk-auth', ''):
            self.send_error(401)
            return
        mname = 'do_' + self.command
        if not hasattr(self, mname):
            self.send_error(405)
            return
        method = getattr(self, mname)
        method()

    def sendPlainText(self, message):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write(bytes(message.strip(), 'utf-8'))

    def do_GET(self):
        self.sendPlainText(getPage())

    def do_POST(self):
        newpage = self.headers.get('kiosk-page', '')
        if len(newpage) > 0:
            setTempPage(newpage)
        restartChromium()
        self.sendPlainText(getPage())

    def do_PUT(self):
        newpage = self.headers.get('kiosk-page', '')
        if len(newpage) > 0:
            setPage(newpage)
            restartChromium()
            self.sendPlainText(getPage())
            return
        self.send_error(400)

httpd = HTTPServer(('', int(server_port)), KioskHandler)
httpd.serve_forever()
{'fraction_non_alphanumeric': 0.12191717077710564, 'fraction_numerical': 0.008841321544904607, 'mean_word_length': 2.8145519077196095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2710059', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1376, 'n_words': 265}