<commit_before>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import string, time, dircache, os
from cgi import escape

def getErrorPage(errorcode, msg = ""):
    if errorcode == 400:
        return { "statuscode" : "400",
                 "data"       : u"<html>\n<title>400 Bad Request</title>\n<body style='background-color: black; color: white;'>\n<h2>400 Bad Request</h2>\n<p>" + msg + "</p></body>\n</html>\n\n",
                 "type"       : "text/html" }
    elif errorcode == 404:
        return { "statuscode" : "404",
                 "data"       : u"<html>\n<title>404 Not Found</title>\n<body style='background-color: black; color: white;'>\n<h2>404 Not Found</h2>\n<p>" + msg + u"</p></body>\n</html>\n\n",
                 "type"       : "text/html" }
    elif errorcode == 500:
        return { "statuscode" : "500",
                 "data"       : u"<html>\n<title>500 Internal Server Error</title>\n<body style='background-color: black; color: white;'>\n<h2>500 Internal Server Error</h2>\n<p>" + msg + u"</p></body>\n</html>\n\n",
                 "type"       : "text/html" }
    elif errorcode == 501:
        return { "statuscode" : "501",
                 "data"       : u"<html>\n<title>501 Not Implemented</title>\n<body style='background-color: black; color: white;'>\n<h2>501 Not Implemented</h2>\n<p>" + msg + u"</p></body>\n</html>\n\n",
                 "type"       : "text/html" }

def sanitizeFilename(filename):
    output = ""
    for char in filename:
        if char >= "0" and char <= "9": output += char
        elif char >= "a" and char <= "z": output += char
        elif char >= "A" and char <= "Z": output += char
        elif char == "-" or char == "_" or char == ".": output += char
    return output

def sanitizePath(uri): #needs work
    outputpath = []
    while uri[0] == "/": #remove leading slashes
        uri = uri[1:]
        if len(uri) == 0: break
    
    splitpath = string.split(uri, "/")
    for directory in splitpath:
        if directory == ".":
            pass
        elif directory == "..":
            if len(outputpath) > 0: outputpath.pop()
        else:
            outputpath.append(directory)
    outputpath = string.join(outputpath, "/")
    return outputpath

def websiteListFilesPage(directory):
    files = dircache.listdir(homedirectory + directory)
    data = u"<html>\n<title>" + directory + u"</title>\n<body style='background-color: black; color: white;'>\n<h2>" + directory + u"</h2>\n<p>Files</p><ul>"
    
    
    for entry in files:
        data += u"<li><a href=\"" + directory + entry + u"\">" + entry + u"</a></li>\n"
    data += u"</ul></body>\n</html>\n\n"
    return {
                 "statuscode" : "200",
                 "data"       : data,
                 "type"       : "text/html" }

def websiteHandlerDefault(request):
    try:
        filename = sanitizePath(request["raw-uri"])
        if os.path.isdir(filename):
            if filename[-1:] != "/": filename += "/"
            if os.path.isfile(filename + indexfilename):
                filename += indexfilename
            else:
                return websiteListFilesPage(filename)
         
        filetype = workoutMimeType(filename)
        sourcefile = open(homedirectory + filename, "rb", 0)
        data = sourcefile.read()
        sourcefile.close()
           
        resource = {
            "type" : filetype,
            "statuscode" : "200",
            "data" : data
        }
    except IOError:
        resource = getErrorPage(404)
    
    return resource


def workoutMimeType(filename):
    fileextension = string.rsplit(filename,".",1)[-1]
    if extensionToMimeType.has_key(fileextension):
        return extensionToMimeType[fileextension]
    else:
        return "application/octet-stream"

extensionToMimeType = {
    "png"  : "image/png",
    "gif"  : "image/gif",
    "jpg"  : "image/jpeg",
    "jpeg" : "image/jpeg",
    "txt"  : "text/plain",
    "htm"  : "text/html",
    "html" : "text/html",
    "css"  : "text/css",
    "cool" : "text/cool"
}	

indexfilename = "index.html"
homedirectory = "./"
URLHandlers = [
    ["/"                       , websiteHandlerDefault]
]
<commit_msg>Returns file resources for HTTPServer. Plain and simple.<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import string, time, dircache, os
from cgi import escape

import MimeTypes
import ErrorPages

def sanitizeFilename(filename):
    output = ""
    for char in filename:
        if char >= "0" and char <= "9": output += char
        elif char >= "a" and char <= "z": output += char
        elif char >= "A" and char <= "Z": output += char
        elif char == "-" or char == "_" or char == ".": output += char
    return output

def sanitizePath(uri): #needs work
    outputpath = []
    while uri[0] == "/": #remove leading slashes
        uri = uri[1:]
        if len(uri) == 0: break
    
    splitpath = string.split(uri, "/")
    for directory in splitpath:
        if directory == ".":
            pass
        elif directory == "..":
            if len(outputpath) > 0: outputpath.pop()
        else:
            outputpath.append(directory)
    outputpath = string.join(outputpath, "/")
    return outputpath

def websiteListFilesPage(directory):
    files = dircache.listdir(homedirectory + directory)
    data = u"<html>\n<title>" + directory + u"</title>\n<body style='background-color: black; color: white;'>\n<h2>" + directory + u"</h2>\n<p>Files</p><ul>"
    
    
    for entry in files:
        data += u"<li><a href=\"" + directory + entry + u"\">" + entry + u"</a></li>\n"
    data += u"</ul></body>\n</html>\n\n"
    
    return {
        "statuscode" : "200",
        "data"       : data,
        "type"       : "text/html"
    }

def handler(request):
    print "websiteMinimal.handler"
    try:
        filename = sanitizePath(request["raw-uri"])
        if os.path.isdir(homedirectory + filename):
            if filename[-1:] != "/": filename += "/"
            if os.path.isfile(homedirectory + filename + indexfilename):
                filename += indexfilename
            else:
                yield websiteListFilesPage(filename)
                return
         
        filetype = MimeTypes.workoutMimeType(filename)
                
        sourcefile = open(homedirectory + filename, "rb", 0)
        
        data = sourcefile.read(1024)
        data = (data, sourcefile.read(1024))
        
        resource = {
            "type" : filetype,
            "statuscode" : "200",
            "data" : data[0],
            "incomplete" : len(data[1]) != 0,
        }
        yield resource

        while len(data[1]) > 0:
            data = (data[1], sourcefile.read(1024))
            resource["data"] = data[0]
            resource["incomplete"] = (len(data[1]) != 0)
            yield resource
        
        sourcefile.close()
           
        
    except IOError:
        resource = ErrorPages.getErrorPage(404)
        yield resource
        
    return


indexfilename = "index.html"
homedirectory = "./"

{'fraction_non_alphanumeric': 0.16143688768990486, 'fraction_numerical': 0.016896209001845804, 'mean_word_length': 2.0257731958762886, 'pattern_counts': {'":': 16, '<': 81, '<?xml version=': 0, '>': 84, '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': '5982788', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2159, 'n_words': 497}