# Path: hurricane/utils.py
# class HttpResponse(object):
#     def __init__(self, status_code, content_type=None, body=''):
#         self.status_code = status_code
#         self.content_type = content_type or 'text/plain'
#         self.body = body
# 
#     def as_bytes(self):
#         response = 'HTTP/1.1 %s\r\n' % self.status_code
#         response += 'Content-Length: %s\r\n' % len(self.body)
#         response += 'Content-Type: %s\r\n\r\n' % self.content_type
#         response += self.body
#         return response


import mimetypes
import os
from hurricane.utils import HttpResponse


def get_media_view(base_path, url_part):
    def media(request):
        path = os.path.join(base_path, request.path[len(url_part):].lstrip('/'))    
        try:
            f = open(path).read()
        except (OSError, IOError):
            path = os.path.join(path, 'index.html')
            try:
                f = open(path).read()
            except (OSError, IOError):
                request.write(HttpResponse(404).as_bytes())
                request.finish()
                return
        (content_type, encoding) = mimetypes.guess_type(path)
        length = len(f)

# Path: hurricane/utils.py
# class HttpResponse(object):
#     def __init__(self, status_code, content_type=None, body=''):
#         self.status_code = status_code
#         self.content_type = content_type or 'text/plain'
#         self.body = body
# 
#     def as_bytes(self):
#         response = 'HTTP/1.1 %s\r\n' % self.status_code
#         response += 'Content-Length: %s\r\n' % len(self.body)
#         response += 'Content-Type: %s\r\n\r\n' % self.content_type
#         response += self.body
#         return response


# Path: hurricane/handlers/comet/views.py
import mimetypes
import os
from hurricane.utils import HttpResponse



def get_media_view(base_path, url_part):
    def media(request):
        path = os.path.join(base_path, request.path[len(url_part):].lstrip('/'))    
        try:
            f = open(path).read()
        except (OSError, IOError):
            path = os.path.join(path, 'index.html')
            try:
                f = open(path).read()
            except (OSError, IOError):
                request.write(HttpResponse(404).as_bytes())
                request.finish()
                return
        (content_type, encoding) = mimetypes.guess_type(path)
        length = len(f)

        request.write(HttpResponse(200, content_type, f).as_bytes())