Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: chrome/common/extensions/docs/server2/servlet.py

Issue 14273035: Docserver: refactor the Handler/ServerInstance relationship into a servlet (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: done Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 class Request(object):
6 '''Request data.
7 '''
8 def __init__(self, path, headers):
9 self.path = path
10 self.headers = headers
11
12 class _ContentBuilder(object):
13 '''Builds the response content.
14 '''
15 def __init__(self):
16 self._buf = []
17
18 def Append(self, content):
19 if isinstance(content, unicode):
20 content = content.encode('utf-8', 'replace')
21 self._buf.append(content)
22
23 def ToString(self):
24 self._Collapse()
25 return self._buf[0]
26
27 def __str__(self):
28 return self.ToString()
29
30 def __len__(self):
31 return len(self.ToString())
32
33 def _Collapse(self):
34 self._buf = [''.join(self._buf)]
35
36 class Response(object):
37 '''The response from Get().
38 '''
39 def __init__(self, content=None, headers=None, status=None):
40 self.content = _ContentBuilder()
41 if content is not None:
42 self.content.Append(content)
43 self.headers = {}
44 if headers is not None:
45 self.headers.update(headers)
46 self.status = status
47
48 @staticmethod
49 def Ok(content, headers=None):
50 '''Returns an OK (200) response.
51 '''
52 return Response(content=content, headers=headers, status=200)
53
54 @staticmethod
55 def Redirect(url, permanent=False):
56 '''Returns a redirect (301 or 302) response.
57 '''
58 if not url.startswith('/'):
59 url = '/%s' % url
60 status = 301 if permanent else 302
61 return Response(headers={'Location': url}, status=status)
62
63 @staticmethod
64 def NotFound(content, headers=None):
65 '''Returns a not found (404) response.
66 '''
67 return Response(content=content, headers=headers, status=404)
68
69 @staticmethod
70 def InternalError(content, headers=None):
71 '''Returns an internal error (500) response.
72 '''
73 return Response(content=content, headers=headers, status=500)
74
75 def Append(self, content):
76 '''Appends |content| to the response content.
77 '''
78 self.content.append(content)
79
80 def AddHeader(self, key, value):
81 '''Adds a header to the response.
82 '''
83 self.headers[key] = value
84
85 def AddHeaders(self, headers):
86 '''Adds several headers to the response.
87 '''
88 self.headers.update(headers)
89
90 def SetStatus(self, status):
91 self.status = status
92
93 def __repr__(self):
94 return '{content: %s bytes, status: %s, headers: %s entries}' % (
95 len(self.content), self.status, len(self.headers.keys()))
96
97 class Servlet(object):
98 def __init__(self, request):
99 self._request = request
100
101 def Get(self):
102 '''Returns a Response.
103 '''
104 raise NotImplemented()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698