def index(request, path_info): parital_response = False path_info = path_info.replace('%28', '(').replace('%29', ')').replace('%20', ' ') response = BaseResponse() # define a file extansion base, ext = os.path.splitext(path_info) # Get the file extansion mime_type = MIME_TYPES.get(ext) if not mime_type: raise Exception("unknown doc, or something like that :-P: %s" % ext ) static_file_path = os.path.join(STATIC_ROOT, path_info) # Check if this path exists if not os.path.exists(static_file_path): error_msg = "<h1>Error 404</h1> No such file STATIC_ROOT/%s" % path_info return Error404(error_msg) # configure response static_file = open(static_file_path, 'rb') # Open file # Here we try to handle Range parameter content_offset = 0 content_end = 0 request_range = request.headers.get('Range') if request_range: range_bytes = request_range.replace('bytes=', '') range_bytes = range_bytes.split('-') if len(range_bytes) > 2: raise Exception('Wrong http Range parameter "%s"' % request_range) content_offset = toInt(range_bytes[0]) content_end = toInt(range_bytes[1]) parital_response = True static_content = static_file.read() if content_end <= 0 or content_end >= len(static_content): content_end = len(static_content) - 1 response.body = static_content[content_offset: content_end + 1] response.charset = 'utf-8' if parital_response: response.status = 206 response.headerlist = [('Content-type', mime_type), ('Content-Range', 'bytes %i-%i/%i' % (content_offset, content_end, len(static_content)))] else: response.headerlist = [('Content-type', mime_type)] # This seems to be clear, return this response object return response
def index(request, path_info, path): partial_response = False path_info = path_info.replace('%28', '(')\ .replace('%29', ')')\ .replace('%20', ' ') response = BaseResponse() # define a file extansion base, ext = os.path.splitext(path_info) # Get the file extansion mime_type = MIME_TYPES.get(ext, 'text/plain') if not mime_type: raise Exception("unknown doc, or something like that :-P: %s" % ext) static_file_path = os.path.join(path, path_info) # Check if this path exists if not os.path.exists(static_file_path): error_msg = "<h1>Error 404</h1> No such file STATIC_ROOT/%s"\ % path_info log.debug('not found: %s' % static_file_path) return Error404(error_msg) # configure response static_file = open(static_file_path, 'rb') # Open file # Here we try to handle Range parameter content_offset = 0 content_end = 0 request_range = request.headers.get('Range') if request_range: range_bytes = request_range.replace('bytes=', '') range_bytes = range_bytes.split('-') if len(range_bytes) > 2: raise Exception('Wrong http Range parameter "%s"' % request_range) content_offset = toInt(range_bytes[0]) content_end = toInt(range_bytes[1]) partial_response = True static_content = static_file.read() if content_end <= 0 or content_end >= len(static_content): content_end = len(static_content) - 1 response.body = static_content[content_offset:content_end + 1] response.charset = 'utf-8' response.headerlist = [] response.headerlist.append(('Content-type', mime_type)) if ext in ['.jpg', '.gif', '.png', '.tiff', '.mp3', '.ogg', '.ttf']: response.headerlist.append(('Access-Control-Allow-Origin', '*')) if partial_response: response.status = 206 response.headerlist.append( ('Content-Range', 'bytes %i-%i/%i' % (content_offset, content_end, len(static_content)))) st = os.stat(static_file_path) response.headerlist.append(('Last-modified', formatdate(st.st_mtime))) response.headerlist.append(('Cache-control', 'max-age=626560472')) response.conditional_response = True response.md5_etag() return response
def index(request, path_info, path, auth=False): if auth and auth_check: state = auth_check(request) if type(state) != bool: return state partial_response = False path_info = path_info.replace('%28', '(')\ .replace('%29', ')')\ .replace('%20', ' ') response = BaseResponse() # define a file extansion base, ext = os.path.splitext(path_info) # Get the file extansion mime_type = MIME_TYPES.get(ext, 'text/plain') if not mime_type: raise Exception("unknown doc, or something like that :-P: %s" % ext) static_file_path = os.path.join(path, path_info) # Check if this path exists if not os.path.exists(static_file_path): error_msg = "<h1>Error 404</h1> No such file STATIC_ROOT/%s"\ % path_info logging.debug('not found: %s' % static_file_path) return Error404(error_msg) # configure response static_file = open(static_file_path, 'rb') # Open file # Here we try to handle Range parameter content_offset = 0 content_end = 0 request_range = request.headers.get('Range') if request_range: range_bytes = request_range.replace('bytes=', '') range_bytes = range_bytes.split('-') if len(range_bytes) > 2: raise Exception('Wrong http Range parameter "%s"' % request_range) content_offset = toInt(range_bytes[0]) content_end = toInt(range_bytes[1]) partial_response = True static_content = static_file.read() if content_end <= 0 or content_end >= len(static_content): content_end = len(static_content) - 1 response.body = static_content[content_offset: content_end + 1] response.charset = 'utf-8' response.headerlist = [] response.headerlist.append(('Content-type', mime_type)) if partial_response: response.status = 206 response.headerlist.append(('Content-Range', 'bytes %i-%i/%i' % (content_offset, content_end, len(static_content)))) st = os.stat(static_file_path) response.headerlist.append(('Last-modified', formatdate(st.st_mtime))) response.headerlist.append(('Cache-control', 'max-age=626560472')) # This seems to be clear, return this response object return response
def index(request, path_info, path, auth=None): partial_response = False response = BaseResponse() # define a file extansion base, ext = os.path.splitext(path_info) # Get the file extansion mime_type = MIME_TYPES.get(ext, 'text/plain') if not mime_type: raise Exception("unknown doc, or something like that :-P: %s" % ext) static_file_path = os.path.join(path, path_info) # Check if this path exists if not os.path.exists(static_file_path): error_msg = "<h1>Error 404</h1> No such file STATIC_ROOT/%s"\ % path_info log.debug('not found: %s' % static_file_path) return Error404(error_msg) # configure response static_file = open(static_file_path, 'rb') # Open file # Here we try to handle Range parameter content_offset = 0 content_end = 0 request_range = request.headers.get('Range') if request_range: range_bytes = request_range.replace('bytes=', '') range_bytes = range_bytes.split('-') if len(range_bytes) > 2: raise Exception('Wrong http Range parameter "%s"' % request_range) content_offset = toInt(range_bytes[0]) content_end = toInt(range_bytes[1]) partial_response = True static_content = static_file.read() if content_end <= 0 or content_end >= len(static_content): content_end = len(static_content) - 1 response.body = static_content[content_offset: content_end + 1] response.charset = 'utf-8' response.headerlist = [] response.headerlist.append(('Content-type', mime_type)) if ext in ['.jpg', '.gif', '.png', '.tiff', '.mp3', '.ogg', '.ttf']: response.headerlist.append(('Access-Control-Allow-Origin', '*')) if partial_response: response.status = 206 response.headerlist.append( ('Content-Range', 'bytes %i-%i/%i' % (content_offset, content_end, len(static_content)))) st = os.stat(static_file_path) response.headerlist.append(('Last-modified', formatdate(st.st_mtime))) response.headerlist.append(('Cache-control', 'max-age=626560472')) response.conditional_response = True response.md5_etag() return response
def assets(request, r_type=None, r_file=None): map = { 'css': 'text/css', 'js': 'application/javascript', 'fonts': 'application/octet-stream', 'img': 'image/*', } fname = os.path.join(APP_DIR,'/'.join(['assets', r_type, r_file])) if r_type not in map: return HTTPNotFound(fname) if r_file is None: return HTTPNotFound(fname) try: response = BaseResponse() f = open(fname,'rb').read() response.headerlist=[('Content-Type', map[r_type])] response.body = f return response except: return exc.HTTPNotFound(fname)
def assets(request, r_type=None, r_file=None): map = { 'css': 'text/css', 'js': 'application/javascript', 'fonts': 'application/octet-stream', 'img': 'image/*', } fname = '/'.join(['sd/assets', r_type, r_file]) if r_type not in map: return HTTPNotFound(fname) if r_file is None: return HTTPNotFound(fname) try: response = BaseResponse() f = open(fname).read() response.headerlist=[('Content-Type', map[r_type])] response.body = f return response except: return exc.HTTPNotFound(fname)
def favicon(request): response = BaseResponse() response.headerlist=[('Content-Type', 'image/x-icon')] f = open(os.path.join(APP_DIR,'favicon.ico'),'rb').read() response.body = f return response
def favicon(request): response = BaseResponse() response.headerlist=[('Content-Type', 'image/x-icon')] f = open('sd/favicon.ico').read() response.body = f return response