1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
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
36
37
38
39
40
41 defaults = {}.fromkeys(valid, [])
42 params = web.input(**defaults)
43
44 return dict((k,v) for k,v in params.items() if k in valid and v)
45
46
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
57
59 """
60 Return the current URI path
61 @return: full current URI path
62 """
63 return web.http.url(web.ctx.path)
64
65
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
74
75
76
77
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
86
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
112
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
123 """
124 Set response code to ok
125 """
126 _status(httplib.OK)
127
128
130 """
131 Set response code to created
132 """
133 _status(httplib.CREATED)
134
135
137 """
138 Set response code to no content
139 """
140 _status(httplib.NO_CONTENT)
141
142
144 """
145 Set response code to accepted
146 """
147 _status(httplib.ACCEPTED)
148
149
151 """
152 Set the response code to bad request
153 """
154 _status(httplib.BAD_REQUEST)
155
156
158 """
159 Set response code to unauthorized
160 """
161 _status(httplib.UNAUTHORIZED)
162
163
165 """
166 Set response code to not found
167 """
168 _status(httplib.NOT_FOUND)
169
170
172 """
173 Set response code to method not allowed
174 """
175 _status(httplib.METHOD_NOT_ALLOWED)
176
177
179 """
180 Set response code to not acceptable
181 """
182 _status(httplib.NOT_ACCEPTABLE)
183
184
186 """
187 Set response code to conflict
188 """
189 _status(httplib.CONFLICT)
190
192 """
193 Set the resonse code to internal server error
194 """
195 _status(httplib.INTERNAL_SERVER_ERROR)
196