def http_get(self): n = len(Path(self.mount_path)) path = Path(self.path)[n:] path = '%s%s' % (self.local_path, path) # 404 Not Found if not isfile(path): return set_response(self.soup_message, 404) # 304 Not Modified mtime = getmtime(path) mtime = datetime.utcfromtimestamp(mtime) mtime = mtime.replace(microsecond=0) mtime = fixed_offset(0).localize(mtime) since = self.get_header('If-Modified-Since') if since and since >= mtime: return set_response(self.soup_message, 304) # 200 Ok # FIXME Check we set the encoding for text files mimetype = get_mimetype(basename(path)) data = open(path).read() self.soup_message.set_status(200) self.soup_message.set_response(mimetype, data) self.set_header('Last-Modified', mtime)
def handle_request(self, soup_message, path): # (1) If path is null => 400 Bad Request if path is None: log_warning('Unexpected HTTP path (null)', domain='itools.web') return set_response(soup_message, 400) # (2) Attach to the soup message and path context = self() context.soup_message = soup_message context.path = path # (3) Get the method that will handle the request method_name = soup_message.get_method() method = getattr(context, 'http_%s' % method_name.lower(), None) # 501 Not Implemented if method is None: log_warning('Unexpected "%s" HTTP method' % method_name, domain='itools.web') return set_response(soup_message, 501) # (4) Go set_context(context) try: method() except StandardError: log_error('Internal error', domain='itools.web') return set_response(soup_message, 500) finally: set_context(None)