| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 |
| 6 class RequestHeaders(object): |
| 7 '''A custom dictionary impementation for headers which ignores the case |
| 8 of requests, since different HTTP libraries seem to mangle them. |
| 9 ''' |
| 10 def __init__(self, dict_): |
| 11 if isinstance(dict_, RequestHeaders): |
| 12 self._dict = dict_ |
| 13 else: |
| 14 self._dict = dict((k.lower(), v) for k, v in dict_.iteritems()) |
| 15 |
| 16 def get(self, key, default=None): |
| 17 return self._dict.get(key.lower(), default) |
| 18 |
| 19 def __repr__(self): |
| 20 return repr(self._dict) |
| 21 |
| 22 def __str__(self): |
| 23 return repr(self._dict) |
| 24 |
| 25 |
| 5 class Request(object): | 26 class Request(object): |
| 6 '''Request data. | 27 '''Request data. |
| 7 ''' | 28 ''' |
| 8 def __init__(self, path, host, headers): | 29 def __init__(self, path, host, headers): |
| 9 self.path = path.lstrip('/') | 30 self.path = path.lstrip('/') |
| 10 self.host = host.rstrip('/') | 31 self.host = host.rstrip('/') |
| 11 self.headers = headers | 32 self.headers = RequestHeaders(headers) |
| 12 | 33 |
| 13 @staticmethod | 34 @staticmethod |
| 14 def ForTest(path, host='http://developer.chrome.com', headers=None): | 35 def ForTest(path, host='http://developer.chrome.com', headers=None): |
| 15 return Request(path, host, headers or {}) | 36 return Request(path, host, headers or {}) |
| 16 | 37 |
| 17 def __repr__(self): | 38 def __repr__(self): |
| 18 return 'Request(path=%s, host=%s, headers=%s)' % ( | 39 return 'Request(path=%s, host=%s, headers=%s)' % ( |
| 19 self.path, self.host, self.headers) | 40 self.path, self.host, self.headers) |
| 20 | 41 |
| 21 def __str__(self): | 42 def __str__(self): |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 status = 301 if permanent else 302 | 91 status = 301 if permanent else 302 |
| 71 return Response(headers={'Location': url}, status=status) | 92 return Response(headers={'Location': url}, status=status) |
| 72 | 93 |
| 73 @staticmethod | 94 @staticmethod |
| 74 def NotFound(content, headers=None): | 95 def NotFound(content, headers=None): |
| 75 '''Returns a not found (404) response. | 96 '''Returns a not found (404) response. |
| 76 ''' | 97 ''' |
| 77 return Response(content=content, headers=headers, status=404) | 98 return Response(content=content, headers=headers, status=404) |
| 78 | 99 |
| 79 @staticmethod | 100 @staticmethod |
| 101 def NotModified(content, headers=None): |
| 102 return Response(content=content, headers=headers, status=304) |
| 103 |
| 104 @staticmethod |
| 80 def InternalError(content, headers=None): | 105 def InternalError(content, headers=None): |
| 81 '''Returns an internal error (500) response. | 106 '''Returns an internal error (500) response. |
| 82 ''' | 107 ''' |
| 83 return Response(content=content, headers=headers, status=500) | 108 return Response(content=content, headers=headers, status=500) |
| 84 | 109 |
| 85 def Append(self, content): | 110 def Append(self, content): |
| 86 '''Appends |content| to the response content. | 111 '''Appends |content| to the response content. |
| 87 ''' | 112 ''' |
| 88 self.content.append(content) | 113 self.content.append(content) |
| 89 | 114 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 return repr(self) | 150 return repr(self) |
| 126 | 151 |
| 127 class Servlet(object): | 152 class Servlet(object): |
| 128 def __init__(self, request): | 153 def __init__(self, request): |
| 129 self._request = request | 154 self._request = request |
| 130 | 155 |
| 131 def Get(self): | 156 def Get(self): |
| 132 '''Returns a Response. | 157 '''Returns a Response. |
| 133 ''' | 158 ''' |
| 134 raise NotImplemented() | 159 raise NotImplemented() |
| OLD | NEW |