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