1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
class RequestHeaders(object):
'''A custom dictionary impementation for headers which ignores the case
of requests, since different HTTP libraries seem to mangle them.
'''
def __init__(self, dict_):
if isinstance(dict_, RequestHeaders):
self._dict = dict_
else:
self._dict = dict((k.lower(), v) for k, v in dict_.iteritems())
def get(self, key, default=None):
return self._dict.get(key.lower(), default)
def __repr__(self):
return repr(self._dict)
def __str__(self):
return repr(self._dict)
class Request(object):
'''Request data.
'''
def __init__(self, path, host, headers, arguments={}):
self.path = path.lstrip('/')
assert not '/' in host, 'Host "%s" should not contain a slash' % host
self.host = host
self.headers = RequestHeaders(headers)
self.arguments = arguments
@staticmethod
def ForTest(path, host=None, headers=None, arguments=None):
return Request(path,
host or 'developer.chrome.com',
headers or {},
arguments or {})
def __repr__(self):
return 'Request(path=%s, host=%s, headers=%s)' % (
self.path, self.host, self.headers)
def __str__(self):
return repr(self)
class _ContentBuilder(object):
'''Builds the response content.
'''
def __init__(self):
self._buf = []
def Append(self, content):
if isinstance(content, unicode):
content = content.encode('utf-8', 'replace')
self._buf.append(content)
def ToString(self):
self._Collapse()
return self._buf[0]
def __str__(self):
return self.ToString()
def __len__(self):
return len(self.ToString())
def _Collapse(self):
self._buf = [''.join(self._buf)]
class Response(object):
'''The response from Get().
'''
def __init__(self, content=None, headers=None, status=None):
self.content = _ContentBuilder()
if content is not None:
self.content.Append(content)
self.headers = {}
if headers is not None:
self.headers.update(headers)
self.status = status
@staticmethod
def Ok(content, headers=None):
'''Returns an OK (200) response.
'''
return Response(content=content, headers=headers, status=200)
@staticmethod
def Redirect(url, permanent=False):
'''Returns a redirect (301 or 302) response.
'''
status = 301 if permanent else 302
return Response(headers={'Location': url}, status=status)
@staticmethod
def BadRequest(content, headers=None):
'''Returns a bad request (400) response.
'''
return Response(content=content, headers=headers, status=400)
@staticmethod
def Unauthorized(content, method, realm, headers={}):
'''Returns an unauthorized (401) response.
'''
new_headers = headers.copy()
new_headers.update({
'WWW-Authentication': '%s realm="%s"' % (method, realm)})
return Response(content=content, headers=headers, status=401)
@staticmethod
def Forbidden(content, headers=None):
'''Returns an forbidden (403) response.
'''
return Response(content=content, headers=headers, status=403)
@staticmethod
def NotFound(content, headers=None):
'''Returns a not found (404) response.
'''
return Response(content=content, headers=headers, status=404)
@staticmethod
def NotModified(content, headers=None):
return Response(content=content, headers=headers, status=304)
@staticmethod
def InternalError(content, headers=None):
'''Returns an internal error (500) response.
'''
return Response(content=content, headers=headers, status=500)
@staticmethod
def ThrottledError(content, headers=None):
'''Returns an HTTP throttle error (429) response.
'''
return Response(content=content, headers=headers, status=429)
def Append(self, content):
'''Appends |content| to the response content.
'''
self.content.append(content)
def AddHeader(self, key, value):
'''Adds a header to the response.
'''
self.headers[key] = value
def AddHeaders(self, headers):
'''Adds several headers to the response.
'''
self.headers.update(headers)
def SetStatus(self, status):
self.status = status
def GetRedirect(self):
if self.headers.get('Location') is None:
return (None, None)
return (self.headers.get('Location'), self.status == 301)
def IsNotFound(self):
return self.status == 404
def __eq__(self, other):
return (isinstance(other, self.__class__) and
str(other.content) == str(self.content) and
other.headers == self.headers and
other.status == self.status)
def __ne__(self, other):
return not (self == other)
def __repr__(self):
return 'Response(content=%s bytes, status=%s, headers=%s)' % (
len(self.content), self.status, self.headers)
def __str__(self):
return repr(self)
class Servlet(object):
def __init__(self, request):
self._request = request
def Get(self):
'''Returns a Response.
'''
raise NotImplemented()
|