#!/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)


def Main():
    """Here will be the main functionality of the server. We will build sockets, establish connections,
    listen for requests and return responses"""
    host, port = '127.0.0.1', 5000
    s = socket.socket()  # <--- May need to provide parameters... Unsure wheather the provided parameters are defaulted to.
    s.bind((host, port))
    s.listen(1)          # <--- May need to change this parameter. Takes values 0 thru 5.
    print "Server Started\nListening..."

    while True:          # <--- Not yet sure of the significance of this loop. Does it keep the server open and listening for new requests?
        conn, addr = s.accept()
        print "Client connected from ip: <%s>" % str(addr)
        # # # Here we might need to implement a thread to do our bidding while the server listens for requests # # #
        
        request = str(conn.recv(1024))
        # # # From here we will need to handle the request using the above defined functions! # # #
        #
        # # # First, we will parse the request to determine if it is valid. If it is, we will # # #
        #     extract the URI from the request and return it. If it is not valid, we will call
        #     upon our reqERR() function to raise the appropriate error message.
        #
        # # # Then, we will pass the URI to our getResource() function, which will return the # # #
        #     requested resource as a body, along with Headers that properly identify the 
        #     contents of the resource (e.g., text/html or image/gif.)
        #
        # # # Finally, the data returned from getResource() will be packaged with an HTTP     # # #
        #     "200 OK" response which we will send back through the socket


@@ -42,52 +42,514 @@
 def 
-htmlize(
+get_size(dirname):%0A    %22%22%22Gets the total size of a given directory%22%22%22%0A    size = 0%0A    for item in os.listdir(dirname):%0A        if os.path.isfile(item):%0A            size += os.path.getsize(item)%0A        elif os.path.isdir(item):%0A            size += get_size(item)%0A    return size%0A%0Adef htmlize(drct):%0A    %22%22%22Wraps 
 con
+t
 ent
-):%0A    return %22%3Chtml%3E%7Bcontent%7D
+ in html tags%22%22%22%0A    lst = %5B%5D%0A    for item in os.listdir(drct):%0A        lst.append(%22%3Cli%3E%25s%3C/li%3E%22%25item)%0A        content = (%22%5Cn%22).join(lst)%0A    return %22%3Chtml%3E%5Cn%3Cbody%3E%5Cn%7Bcontent%7D%5Cn%3C/body%3E%5Cn
 %3C/ht
@@ -2250,20 +2250,22 @@
 eak%0A    
-#
+ 
    
+el
 if os.pa
@@ -2322,187 +2322,300 @@
 tory
-...
+:
 %0A    
-# 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%0A    # an HTML listing of the contents of that directory.
+        content_type = 'text/directory'%0A            content_length = get_size(uri)   # %3C--- Gets the size of the directory--the sum of the size of all files and subdirectories.  Maybe the content_length for a directory should just be the length of the content actually displayed...idk.%0A
 %0A%0A  

Add 2 helper functions: get_size()--returns size of entire directory--and htmlize()--returns a the contents of a given directory wrapped in html tags.