paul@440 | 1 | #!/usr/bin/env python |
paul@440 | 2 | |
paul@440 | 3 | """ |
paul@440 | 4 | Web interface utilities. |
paul@440 | 5 | |
paul@440 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@440 | 7 | |
paul@440 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@440 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@440 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@440 | 11 | version. |
paul@440 | 12 | |
paul@440 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@440 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@440 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@440 | 16 | details. |
paul@440 | 17 | |
paul@440 | 18 | You should have received a copy of the GNU General Public License along with |
paul@440 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@440 | 20 | """ |
paul@440 | 21 | |
paul@440 | 22 | import cgi, os, sys |
paul@440 | 23 | |
paul@440 | 24 | getenv = os.environ.get |
paul@440 | 25 | setenv = os.environ.__setitem__ |
paul@440 | 26 | |
paul@440 | 27 | class CGIEnvironment: |
paul@440 | 28 | |
paul@440 | 29 | "A CGI-compatible environment." |
paul@440 | 30 | |
paul@440 | 31 | def __init__(self, charset=None): |
paul@440 | 32 | self.charset = charset |
paul@440 | 33 | self.args = None |
paul@440 | 34 | self.method = None |
paul@440 | 35 | self.path = None |
paul@440 | 36 | self.path_info = None |
paul@440 | 37 | self.user = None |
paul@440 | 38 | |
paul@440 | 39 | def get_args(self): |
paul@440 | 40 | if self.args is None: |
paul@440 | 41 | if self.get_method() != "POST": |
paul@440 | 42 | setenv("QUERY_STRING", "") |
paul@440 | 43 | args = cgi.parse(keep_blank_values=True) |
paul@440 | 44 | |
paul@440 | 45 | if not self.charset: |
paul@440 | 46 | self.args = args |
paul@440 | 47 | else: |
paul@440 | 48 | self.args = {} |
paul@440 | 49 | for key, values in args.items(): |
paul@440 | 50 | self.args[key] = [unicode(value, self.charset) for value in values] |
paul@440 | 51 | |
paul@440 | 52 | return self.args |
paul@440 | 53 | |
paul@440 | 54 | def get_method(self): |
paul@440 | 55 | if self.method is None: |
paul@440 | 56 | self.method = getenv("REQUEST_METHOD") or "GET" |
paul@440 | 57 | return self.method |
paul@440 | 58 | |
paul@440 | 59 | def get_path(self): |
paul@440 | 60 | if self.path is None: |
paul@440 | 61 | self.path = getenv("SCRIPT_NAME") or "" |
paul@440 | 62 | return self.path |
paul@440 | 63 | |
paul@440 | 64 | def get_path_info(self): |
paul@440 | 65 | if self.path_info is None: |
paul@440 | 66 | self.path_info = getenv("PATH_INFO") or "" |
paul@440 | 67 | return self.path_info |
paul@440 | 68 | |
paul@440 | 69 | def get_user(self): |
paul@440 | 70 | if self.user is None: |
paul@440 | 71 | self.user = getenv("REMOTE_USER") or "" |
paul@440 | 72 | return self.user |
paul@440 | 73 | |
paul@440 | 74 | def get_output(self): |
paul@440 | 75 | return sys.stdout |
paul@440 | 76 | |
paul@440 | 77 | def get_url(self): |
paul@440 | 78 | path = self.get_path() |
paul@440 | 79 | path_info = self.get_path_info() |
paul@440 | 80 | return "%s%s" % (path.rstrip("/"), path_info) |
paul@440 | 81 | |
paul@440 | 82 | def new_url(self, path_info): |
paul@440 | 83 | path = self.get_path() |
paul@440 | 84 | return "%s/%s" % (path.rstrip("/"), path_info.lstrip("/")) |
paul@440 | 85 | |
paul@440 | 86 | # vim: tabstop=4 expandtab shiftwidth=4 |