<commit_before>#!/usr/bin/env python3
"""Sundara jāla
   For a beautiful web.

   SundaraRequestHandler.py
   Implements the functionality for the `sundara runserver` command.
   A tiny little HTTP server, not suitable for use in production
   environments.

   This is an http.server Request Handler.
"""

import os
from http.server import BaseHTTPRequestHandler

class SundaraRequestHandler(BaseHTTPRequestHandler):

    def getLocalFile(file):
        """Translate the HTTP path to a local path.
           Using os.sep makes sure everything works on Windows as well
           as *nix.
        """
        file = file.replace('/', os.sep)
        # FIXME: Replace hardcoded 'www' with the `generate` path.
        return os.curdir + os.sep + 'www' + file

    def HTTPStatusFromFile(file):
        """Return an HTTP status message, one of '200 OK', '404 File
           Not Found', or '403 Forbidden', depending on whether a file
           exists and can be opened for reading.
        """
        file = getLocalFile(file)

        if os.path.isdir(file):
            file = file + os.sep + "index.html"

        try:
            f = open(file, "r+")
            f.close()
        except IOError:
            return "404 File Not Found"
        except PermissionError:
            return "403 Forbidden"
        return "200 OK"

    def getRequestedFile(self):
        """Parse the HTTP request and return the path to the file the
           client is looking for. If the request cannot be parsed, write
           out '400 Bad Request' to the HTTP stream.

           Requests look like

                OPTIONS *

            or

                OPTIONS /

            or
            
                GET /path HTTP/1.1

            etc.
        """
        try:
            readfile = self.rfile.read()
            file = str(readfile).split(' ')[1]
        except IndexError:
            self.wfile.write("400 Bad Request")
            file = False
        return file

    def do_OPTIONS(self):
        file = self.getRequestedFile()
        if not file:
            return

        self.wfile.write("%s\r\n" % HTTPStatusFromFile(file))
        self.wfile.write("Allow: HEAD,GET,OPTIONS\r\n")
        self.wfile.write("\r\n")

    def do_HEAD(self):
        file = self.getRequestedFile()
        if not file:
            return

        self.wfile.write("%s\r\n" % HTTPStatusFromFile(file))
        self.wfile.write("\r\n")

    def do_GET(self):
        file = self.getRequestedFile()
        if not file:
            return

        print(self.rfile.read())

        status = HTTPStatusFromFile(file)
        self.wfile.write("%s\r\n" % status)
        self.wfile.write("\r\n")

        if status == "200 OK":
            hfile = open(getLocalFile(file), 'r+')
            self.wfile.write(hfile.read())
            self.wfile.write("\r\n")

        self.wfile.close()<commit_msg>Getting closer to a working server.<commit_after>#!/usr/bin/env python3
"""Sundara jāla
   For a beautiful web.

   SundaraRequestHandler.py
   Implements the functionality for the `sundara runserver` command.
   A tiny little HTTP server, not suitable for use in production
   environments.

   This is an http.server Request Handler.
"""

import os
from http.server import BaseHTTPRequestHandler

class SundaraRequestHandler(BaseHTTPRequestHandler):

    def getLocalFile(self):
        """Translate the HTTP path to a local path.
           Using os.sep makes sure everything works on Windows as well
           as *nix.
        """
        file = self.path
        file = file.replace('/', os.sep)
        # FIXME: Replace hardcoded 'www' with the `generate` path.
        return os.curdir + os.sep + 'www' + file

    def HTTPStatusFromFile(self):
        """Return an HTTP status message, one of '200 OK', '404 File
           Not Found', or '403 Forbidden', depending on whether a file
           exists and can be opened for reading.
        """
        file = self.getLocalFile()

        if os.path.isdir(file):
            file = file + os.sep + "index.html"

        try:
            f = open(file, "r+")
            f.close()
        except IOError:
            return "404 File Not Found"
        except PermissionError:
            return "403 Forbidden"
        return "200 OK"

    def do_OPTIONS(self):
        self.wfile.write("%s %s\r\n" % (self.request_version,
                    self.HTTPStatusFromFile()))
        self.wfile.write("Allow: HEAD,GET,OPTIONS\r\n")
        self.wfile.write("\r\n")

    def do_HEAD(self):
        self.wfile.write("%s %s\r\n" % (self.request_version,
                    self.HTTPStatusFromFile()))
        self.wfile.write("\r\n")

    def do_GET(self):
        status = self.HTTPStatusFromFile()
        self.wfile.write("%s %s\r\n" % (self.request_version, status))
        self.wfile.write("\r\n")

        if status == "200 OK":
            hfile = open(self.getLocalFile(), 'r+')
            self.wfile.write(hfile.read())
            self.wfile.write("\r\n")
{'fraction_non_alphanumeric': 0.1088950937375349, 'fraction_numerical': 0.010570402871958515, 'mean_word_length': 1.8888248847926268, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '6578047', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1440, 'n_words': 426}