| 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 class Request(object): | 5 class Request(object): |
| 6 '''Request data. | 6 '''Request data. |
| 7 ''' | 7 ''' |
| 8 def __init__(self, path, headers): | 8 def __init__(self, path, host, headers): |
| 9 self.path = path | 9 self.path = path.lstrip('/') |
| 10 self.host = host.rstrip('/') |
| 10 self.headers = headers | 11 self.headers = headers |
| 11 | 12 |
| 13 @staticmethod |
| 14 def ForTest(path, url='http://localhost', headers=None): |
| 15 return Request(path, url, headers or {}) |
| 16 |
| 17 def __repr__(self): |
| 18 return 'Request(path=%s, host=%s, headers=%s entries)' % ( |
| 19 self.path, self.host, len(self.headers.keys())) |
| 20 |
| 21 def __str__(self): |
| 22 return repr(self) |
| 23 |
| 12 class _ContentBuilder(object): | 24 class _ContentBuilder(object): |
| 13 '''Builds the response content. | 25 '''Builds the response content. |
| 14 ''' | 26 ''' |
| 15 def __init__(self): | 27 def __init__(self): |
| 16 self._buf = [] | 28 self._buf = [] |
| 17 | 29 |
| 18 def Append(self, content): | 30 def Append(self, content): |
| 19 if isinstance(content, unicode): | 31 if isinstance(content, unicode): |
| 20 content = content.encode('utf-8', 'replace') | 32 content = content.encode('utf-8', 'replace') |
| 21 self._buf.append(content) | 33 self._buf.append(content) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 48 @staticmethod | 60 @staticmethod |
| 49 def Ok(content, headers=None): | 61 def Ok(content, headers=None): |
| 50 '''Returns an OK (200) response. | 62 '''Returns an OK (200) response. |
| 51 ''' | 63 ''' |
| 52 return Response(content=content, headers=headers, status=200) | 64 return Response(content=content, headers=headers, status=200) |
| 53 | 65 |
| 54 @staticmethod | 66 @staticmethod |
| 55 def Redirect(url, permanent=False): | 67 def Redirect(url, permanent=False): |
| 56 '''Returns a redirect (301 or 302) response. | 68 '''Returns a redirect (301 or 302) response. |
| 57 ''' | 69 ''' |
| 58 if not url.startswith('/'): | |
| 59 url = '/%s' % url | |
| 60 status = 301 if permanent else 302 | 70 status = 301 if permanent else 302 |
| 61 return Response(headers={'Location': url}, status=status) | 71 return Response(headers={'Location': url}, status=status) |
| 62 | 72 |
| 63 @staticmethod | 73 @staticmethod |
| 64 def NotFound(content, headers=None): | 74 def NotFound(content, headers=None): |
| 65 '''Returns a not found (404) response. | 75 '''Returns a not found (404) response. |
| 66 ''' | 76 ''' |
| 67 return Response(content=content, headers=headers, status=404) | 77 return Response(content=content, headers=headers, status=404) |
| 68 | 78 |
| 69 @staticmethod | 79 @staticmethod |
| (...skipping 14 matching lines...) Expand all Loading... |
| 84 | 94 |
| 85 def AddHeaders(self, headers): | 95 def AddHeaders(self, headers): |
| 86 '''Adds several headers to the response. | 96 '''Adds several headers to the response. |
| 87 ''' | 97 ''' |
| 88 self.headers.update(headers) | 98 self.headers.update(headers) |
| 89 | 99 |
| 90 def SetStatus(self, status): | 100 def SetStatus(self, status): |
| 91 self.status = status | 101 self.status = status |
| 92 | 102 |
| 93 def __repr__(self): | 103 def __repr__(self): |
| 94 return '{content: %s bytes, status: %s, headers: %s entries}' % ( | 104 return 'Response(content=%s bytes, status=%s, headers=%s entries)' % ( |
| 95 len(self.content), self.status, len(self.headers.keys())) | 105 len(self.content), self.status, len(self.headers.keys())) |
| 96 | 106 |
| 107 def __str__(self): |
| 108 return repr(self) |
| 109 |
| 97 class Servlet(object): | 110 class Servlet(object): |
| 98 def __init__(self, request): | 111 def __init__(self, request): |
| 99 self._request = request | 112 self._request = request |
| 100 | 113 |
| 101 def Get(self): | 114 def Get(self): |
| 102 '''Returns a Response. | 115 '''Returns a Response. |
| 103 ''' | 116 ''' |
| 104 raise NotImplemented() | 117 raise NotImplemented() |
| OLD | NEW |