<commit_before>#!/usr/bin/env python
import socket, os

def parse_request(req):
    """Takes an HTTP request in the form of a string and parses it, returning the URI,
    if the request is valid"""
    pass

def reqOK():
    """Returns HTTP '200 OK' response"""
    pass

def reqERR():
    """Returns HTTP error response. Makes use of the module httplib.responses()"""
    pass

def getResource(uri):
    """Takes URI as argument; returns resource requested as a body and headers indicating the type of content in the body"""
    body = ""
    if os.path.exists(uri):  # <--- If the resource exists, return a body and Headers appropriate to the request.
        if os.path.isfile(uri):  # <--- If the resource is a file... 
            with open(uri, 'rb') as f:  # <--- Open it as binary data
                while True:
                    readBytes = f.read(1024)
                    body += readBytes
                    if readBytes == "":
                        break
    return body



    # else:  # <--- If the resource does not exist, return an Error message... (as the body of the response?)

<commit_msg>Update getResource() method: returns a tuple containing the headers and body of the response. Handles files<commit_after>#!/usr/bin/env python
import socket, os

def htmlize(conent):
    return "<html>{content}</html>".format(content=content)

def parse_request(req):
    """Takes an HTTP request in the form of a string and parses it, returning the URI,
    if the request is valid"""
    pass

def reqOK():
    """Returns HTTP '200 OK' response"""
    pass

def reqERR():
    """Returns HTTP error response. Makes use of the module httplib.responses()"""
    pass

def getResource(uri):
    """Takes URI as argument; returns a tuple containing the content_type, the content_length, and the resource requested as the body, in that order"""
    body = ""                                       #
    content_type = ""                               # <--- Initialize the variables to hold the body and headers. For clarity.
    content_length = 0                              #
    type_dict = {'txt':'text', 'html':'text', 'py':'text', 'jpg':'image', 'png':'image'}
    if os.path.exists(uri):                         # <--- If the resource exists, return a body and Headers appropriate to the request.
        if os.path.isfile(uri):                     # <--- If the resource is a file:
            content_length = os.path.getsize(uri)   # <--- Determine its size...
            subtype =  uri.split('.')[-1]           # <--- Grab the extension...
            content_type = "%s/%s" % (type_dict[subtype], subtype)  # <--- Determine content type from file extension, and finally...
            with open(uri, 'rb') as f:              # <--- Open the file as binary data.
                while True:
                    readBytes = f.read(1024)
                    body += readBytes               # <--- Writes contents of file to a string.
                    if readBytes == "":
                        break
    #   if os.path.isdir(uri):   # <--- If the resource is a directory...
    # I don't know how to do this yet. If the resource requested is a directory, then we want to set the body equal to
    # an HTML listing of the contents of that directory.

    #   else:  # <--- If the resource does not exist, return an Error message... (as the body of the response?)
    #   I don't know whether we want to raise an error AS the body returned, or whether we want this conditional to 
    #   call the reqERR() funciton. Will come back to this.

    return (content_type, content_length, body)


{'fraction_non_alphanumeric': 0.10914617297596022, 'fraction_numerical': 0.004421110804089527, 'mean_word_length': 2.0242272347535506, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '55196', 'n_tokens_mistral': 985, 'n_tokens_neox': 939, 'n_words': 412}