Package pulp :: Package server :: Package webservices :: Module http
[hide private]
[frames] | no frames]

Source Code for Module pulp.server.webservices.http

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright © 2010 Red Hat, Inc. 
  5  # 
  6  # This software is licensed to you under the GNU General Public License, 
  7  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  8  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  9  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 10  # along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 12  # 
 13  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 14  # granted to use or replicate Red Hat trademarks that are incorporated 
 15  # in this software or its documentation. 
 16   
 17  """ 
 18  HTTP utilities to help pulp web services with HTTP using the web.py framework 
 19  """ 
 20   
 21  import httplib 
 22  import os 
 23  import urllib 
 24   
 25  import web 
 26   
 27  # request methods ------------------------------------------------------------- 
 28       
29 -def query_parameters(valid):
30 """ 31 @type valid: list of str's 32 @param valid: list of expected query parameters 33 @return: dict of param: [value(s)] of uri query parameters 34 """ 35 # NOTE If a keyword argument of foo=[] is not passed into web.input, 36 # web.py will not record multiple parameters of 'foo' from the URI. 37 # So this line of code constructs those keyword arguments for 'valid' 38 # (ie expected) query parameters. 39 # This will return a list for every valid parameter, even if it's empty or 40 # only contains one element 41 defaults = {}.fromkeys(valid, []) 42 params = web.input(**defaults) 43 # scrub out invalid keys and empty lists from the parameters 44 return dict((k,v) for k,v in params.items() if k in valid and v)
45 46
47 -def http_authorization():
48 """ 49 Return the current http authorization credentials, if any 50 @return: str representing the http authorization credentials if found, 51 None otherwise 52 """ 53 credentials = web.ctx.environ.get('HTTP_AUTHORIZATION', None) 54 return credentials
55 56 # uri path functions ---------------------------------------------------------- 57
58 -def uri_path():
59 """ 60 Return the current URI path 61 @return: full current URI path 62 """ 63 return web.http.url(web.ctx.path)
64 65
66 -def extend_uri_path(suffix):
67 """ 68 Return the current URI path with the suffix appended to it 69 @type suffix: str 70 @param suffix: path fragment to be appended to the current path 71 @return: full path with the suffix appended 72 """ 73 # steps: 74 # cleanly concatenate the current path with the suffix 75 # add the application prefix 76 # all urls are paths, so need a trailing '/' 77 # make sure the path is properly encoded 78 prefix = uri_path() 79 suffix = urllib.pathname2url(suffix) 80 path = os.path.normpath(os.path.join(prefix, suffix)) 81 if not path.endswith('/'): 82 path += '/' 83 return path
84 85 # response functions ---------------------------------------------------------- 86
87 -def header(hdr, value, unique=True):
88 """ 89 Adds 'hdr: value' to the response. 90 This function has, in some regards, the opposite semantics of the web.header 91 function. If unique is True, the hdr will be overwritten if it already 92 exists in the response. Otherwise it will be appended. 93 @type hdr: str 94 @param hdr: valid http header key 95 @type value: str 96 @param value: valid value for corresponding header key 97 @type unique: bool 98 @param unique: whether only one instance of the header is in the response 99 """ 100 hdr = web.utf8(hdr) 101 value = web.utf8(value) 102 previous = [] 103 for h,v in web.ctx.headers: 104 if h.lower() == hdr.lower(): 105 previous.append((h,v)) 106 if unique: 107 for p in previous: 108 web.ctx.headers.remove(p) 109 web.ctx.headers.append((hdr, value))
110 111 # status functions ------------------------------------------------------------ 112
113 -def _status(code):
114 """ 115 Non-public function to set the web ctx status 116 @type code: int 117 @param code: http response code 118 """ 119 web.ctx.status = '%d %s' % (code, httplib.responses[code])
120 121
122 -def status_ok():
123 """ 124 Set response code to ok 125 """ 126 _status(httplib.OK)
127 128
129 -def status_created():
130 """ 131 Set response code to created 132 """ 133 _status(httplib.CREATED)
134 135
136 -def status_no_content():
137 """ 138 Set response code to no content 139 """ 140 _status(httplib.NO_CONTENT)
141 142
143 -def status_accepted():
144 """ 145 Set response code to accepted 146 """ 147 _status(httplib.ACCEPTED)
148 149
150 -def status_bad_request():
151 """ 152 Set the response code to bad request 153 """ 154 _status(httplib.BAD_REQUEST)
155 156
157 -def status_unauthorized():
158 """ 159 Set response code to unauthorized 160 """ 161 _status(httplib.UNAUTHORIZED)
162 163
164 -def status_not_found():
165 """ 166 Set response code to not found 167 """ 168 _status(httplib.NOT_FOUND)
169 170
171 -def status_method_not_allowed():
172 """ 173 Set response code to method not allowed 174 """ 175 _status(httplib.METHOD_NOT_ALLOWED)
176 177
178 -def status_not_acceptable():
179 """ 180 Set response code to not acceptable 181 """ 182 _status(httplib.NOT_ACCEPTABLE)
183 184
185 -def status_conflict():
186 """ 187 Set response code to conflict 188 """ 189 _status(httplib.CONFLICT)
190
191 -def status_internal_server_error():
192 """ 193 Set the resonse code to internal server error 194 """ 195 _status(httplib.INTERNAL_SERVER_ERROR)
196