paulb@110 | 1 | #!/usr/bin/env python |
paulb@110 | 2 | |
paulb@110 | 3 | """ |
paulb@110 | 4 | Environment helper functions. |
paulb@403 | 5 | |
paulb@663 | 6 | Copyright (C) 2004, 2005, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@110 | 21 | """ |
paulb@110 | 22 | |
paulb@724 | 23 | import WebStack.Helpers.Request |
paulb@724 | 24 | |
paulb@663 | 25 | cgi_names = ("CONTENT_LENGTH", "CONTENT_TYPE") |
paulb@663 | 26 | |
paulb@110 | 27 | def get_headers(env): |
paulb@110 | 28 | |
paulb@110 | 29 | """ |
paulb@110 | 30 | Get the headers from the given environment 'env', which should be a |
paulb@110 | 31 | dictionary-like object. |
paulb@110 | 32 | |
paulb@110 | 33 | Returns a dictionary-like object containing likely headers. |
paulb@110 | 34 | """ |
paulb@110 | 35 | |
paulb@724 | 36 | headers = WebStack.Helpers.Request.HeaderDict() |
paulb@724 | 37 | |
paulb@110 | 38 | for cgi_key, value in env.items(): |
paulb@110 | 39 | if cgi_key.startswith("HTTP_"): |
paulb@724 | 40 | header_name = cgi_key[len("HTTP_"):].replace("_", "-") |
paulb@110 | 41 | headers[header_name] = value |
paulb@663 | 42 | elif cgi_key in cgi_names: |
paulb@724 | 43 | header_name = cgi_key.replace("_", "-") |
paulb@663 | 44 | headers[header_name] = value |
paulb@110 | 45 | |
paulb@110 | 46 | return headers |
paulb@110 | 47 | |
paulb@110 | 48 | # vim: tabstop=4 expandtab shiftwidth=4 |