Home  >  Article  >  Backend Development  >  Detailed explanation of HeaderDict of Bottle source code

Detailed explanation of HeaderDict of Bottle source code

零下一度
零下一度Original
2017-07-24 09:20:031487browse

All framework request responses are based on one principle http request --> wsgi server --> wsgi interface (actually the function implemented by a custom implementation in the framework is encapsulated at the bottom) --> response You can refer to the explanation of the wsgi interface in Liao Xuefeng's tutorial

class HeaderDict(dict):''' A dictionary with case insensitive (titled) keys.        You may add a list of strings to send multible headers with the same name.'''def __setitem__(self, key, value):return dict.__setitem__(self,key.title(), value) #注意这里使用title函数,它能将每个单词的开头大写def __getitem__(self, key):return dict.__getitem__(self,key.title())def __delitem__(self, key):return dict.__delitem__(self,key.title())def __contains__(self, key):return dict.__contains__(self,key.title())def items(self):""" Returns a list of (key, value) tuples """for key, values in dict.items(self):if not isinstance(values, list):
                values = [values]for value in values:yield (key, str(value))                def add(self, key, value):""" Adds a new header without deleting old ones """if isinstance(value, list):for v in value:self.add(key, v) #注意这里使用了递归elif key in self:if isinstance(self[key], list):self[key].append(value)else:self[key] = [self[key], value]else:          self[key] = [value]

HeaderDict encapsulates the dict and capitalizes the first letter of the word in the dictionary key. And turn value into an iterable object, and turn value into a list object, that is, value=[value]. The WSGI standard defines that a string type should be converted into a list type, which will give it a better representation. The server does not need to output all at once, but can use yield to control the output to avoid outputting too much at one time. All in all, this class that encapsulates dict implements two functions:

  1. Convert value to list, optimize the data representation form

  2. The first letter of the word in key is capitalized

def abort(code=500, text='Unknown Error: Appliction stopped.'):""" Aborts execution and causes a HTTP error. """raise HTTPError(code, text)def redirect(url, code=307):""" Aborts execution and causes a 307 redirect """response.status = code
    response.header['Location'] = urlraise BreakTheBottle("")def send_file(filename, root, guessmime = True, mimetype = 'text/plain'):""" Aborts execution and sends a static files as response. """root = os.path.abspath(root) + '/'filename = os.path.normpath(filename).strip('/')
    filename = os.path.join(root, filename)#判断文件是否可获得if not filename.startswith(root): #主目录下的文件不可以下载abort(401, "Access denied.")if not os.path.exists(filename) or not os.path.isfile(filename):
        abort(404, "File does not exist.")if not os.access(filename, os.R_OK):
        abort(401, "You do not have permission to access this file.")# 获得文件类型if guessmime:
        guess = mimetypes.guess_type(filename)[0]if guess:
            response.content_type = guesselif mimetype:
            response.content_type = mimetypeelif mimetype:
        response.content_type = mimetype#设置Content_typestats = os.stat(filename)# TODO: HTTP_IF_MODIFIED_SINCE -> 304 (Thu, 02 Jul 2009 23:16:31 CEST)if 'Content-Length' not in response.header:
        response.header['Content-Length'] = stats.st_sizeif 'Last-Modified' not in response.header:
        ts = time.gmtime(stats.st_mtime)
        ts = time.strftime("%a, %d %b %Y %H:%M:%S +0000", ts)
        response.header['Last-Modified'] = tsraise BreakTheBottle(open(filename, 'r'))

The three functions above implement internal server errors, redirection, and file download respectively. This function of file download implements the judgment of file type, setting of Content_type, judgment of file permissions, obtaining file status, etc. This function is still very simple and can be customized.

The above is the detailed content of Detailed explanation of HeaderDict of Bottle source code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn